mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
Setting default account
This commit is contained in:
parent
3d35110c67
commit
c4674c06a4
@ -19,6 +19,10 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
|||||||
self.sid = sid
|
self.sid = sid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var instance: Instance {
|
||||||
|
Defaults[.instances].first { $0.id == instanceID }!
|
||||||
|
}
|
||||||
|
|
||||||
var anonymizedSID: String {
|
var anonymizedSID: String {
|
||||||
guard sid.count > 3 else {
|
guard sid.count > 3 else {
|
||||||
return ""
|
return ""
|
||||||
@ -35,42 +39,42 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
|||||||
func hash(into hasher: inout Hasher) {
|
func hash(into hasher: inout Hasher) {
|
||||||
hasher.combine(sid)
|
hasher.combine(sid)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
struct AccountsBridge: Defaults.Bridge {
|
struct AccountsBridge: Defaults.Bridge {
|
||||||
typealias Value = Account
|
typealias Value = Account
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
|
|
||||||
func serialize(_ value: Value?) -> Serializable? {
|
func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value = value else {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
"id": value.id.uuidString,
|
||||||
|
"instanceID": value.instanceID.uuidString,
|
||||||
|
"name": value.name ?? "",
|
||||||
|
"url": value.url,
|
||||||
|
"sid": value.sid
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
func deserialize(_ object: Serializable?) -> Value? {
|
||||||
"id": value.id.uuidString,
|
guard
|
||||||
"instanceID": value.instanceID.uuidString,
|
let object = object,
|
||||||
"name": value.name ?? "",
|
let id = object["id"],
|
||||||
"url": value.url,
|
let instanceID = object["instanceID"],
|
||||||
"sid": value.sid
|
let url = object["url"],
|
||||||
]
|
let sid = object["sid"]
|
||||||
}
|
else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> Value? {
|
let uuid = UUID(uuidString: id)
|
||||||
guard
|
let instanceUUID = UUID(uuidString: instanceID)!
|
||||||
let object = object,
|
let name = object["name"] ?? ""
|
||||||
let id = object["id"],
|
|
||||||
let instanceID = object["instanceID"],
|
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
|
||||||
let url = object["url"],
|
|
||||||
let sid = object["sid"]
|
|
||||||
else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let uuid = UUID(uuidString: id)
|
|
||||||
let instanceUUID = UUID(uuidString: instanceID)!
|
|
||||||
let name = object["name"] ?? ""
|
|
||||||
|
|
||||||
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,13 @@ import Defaults
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
final class InstancesModel: ObservableObject {
|
final class InstancesModel: ObservableObject {
|
||||||
var defaultAccount: Instance.Account! {
|
@Published var defaultAccount: Instance.Account?
|
||||||
Defaults[.accounts].first
|
|
||||||
|
init() {
|
||||||
|
if let id = Defaults[.defaultAccountID] {
|
||||||
|
let uuid = UUID(uuidString: id)
|
||||||
|
defaultAccount = Defaults[.accounts].first { $0.id == uuid }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func find(_ id: Instance.ID?) -> Instance? {
|
func find(_ id: Instance.ID?) -> Instance? {
|
||||||
@ -26,8 +31,10 @@ final class InstancesModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func remove(_ instance: Instance) {
|
func remove(_ instance: Instance) {
|
||||||
|
let accounts = accounts(instance.id)
|
||||||
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
||||||
Defaults[.instances].remove(at: index)
|
Defaults[.instances].remove(at: index)
|
||||||
|
accounts.forEach { removeAccount($0) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,4 +50,13 @@ final class InstancesModel: ObservableObject {
|
|||||||
Defaults[.accounts].remove(at: accountIndex)
|
Defaults[.accounts].remove(at: accountIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetDefaultAccount() {
|
||||||
|
setDefaultAccount(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setDefaultAccount(_ account: Instance.Account?) {
|
||||||
|
Defaults[.defaultAccountID] = account?.id.uuidString
|
||||||
|
defaultAccount = account
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ extension Defaults.Keys {
|
|||||||
|
|
||||||
static let instances = Key<[Instance]>("instances", default: [])
|
static let instances = Key<[Instance]>("instances", default: [])
|
||||||
static let accounts = Key<[Instance.Account]>("accounts", default: [])
|
static let accounts = Key<[Instance.Account]>("accounts", default: [])
|
||||||
|
static let defaultAccountID = Key<String?>("defaultAccountID")
|
||||||
|
|
||||||
static let selectedPlaylistID = Key<String?>("selectedPlaylistID")
|
static let selectedPlaylistID = Key<String?>("selectedPlaylistID")
|
||||||
static let showingAddToPlaylist = Key<Bool>("showingAddToPlaylist", default: false)
|
static let showingAddToPlaylist = Key<Bool>("showingAddToPlaylist", default: false)
|
||||||
|
@ -40,10 +40,8 @@ struct PearvidiousApp: App {
|
|||||||
search.api = api
|
search.api = api
|
||||||
subscriptions.api = api
|
subscriptions.api = api
|
||||||
|
|
||||||
guard api.account.isNil, instances.defaultAccount != nil else {
|
if let account = instances.defaultAccount {
|
||||||
return
|
api.setAccount(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
api.setAccount(instances.defaultAccount)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
import Defaults
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct AccountSettingsView: View {
|
struct AccountSettingsView: View {
|
||||||
let instance: Instance
|
|
||||||
let account: Instance.Account
|
let account: Instance.Account
|
||||||
@Binding var selectedAccount: Instance.Account?
|
@Binding var selectedAccount: Instance.Account?
|
||||||
|
|
||||||
@ -11,11 +11,22 @@ struct AccountSettingsView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
HStack {
|
HStack {
|
||||||
Text(account.description)
|
HStack(spacing: 2) {
|
||||||
|
Text(account.description)
|
||||||
|
if instances.defaultAccount == account {
|
||||||
|
Text("— default")
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
HStack {
|
HStack {
|
||||||
|
if instances.defaultAccount != account {
|
||||||
|
Button("Make default", action: makeDefault)
|
||||||
|
} else {
|
||||||
|
Button("Reset default", action: resetDefault)
|
||||||
|
}
|
||||||
Button("Remove", role: .destructive) {
|
Button("Remove", role: .destructive) {
|
||||||
presentingRemovalConfirmationDialog = true
|
presentingRemovalConfirmationDialog = true
|
||||||
}
|
}
|
||||||
@ -34,4 +45,12 @@ struct AccountSettingsView: View {
|
|||||||
.opacity(account == selectedAccount ? 1 : 0)
|
.opacity(account == selectedAccount ? 1 : 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func makeDefault() {
|
||||||
|
instances.setDefaultAccount(account)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func resetDefault() {
|
||||||
|
instances.resetDefaultAccount()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,24 @@ struct InstanceDetailsSettingsView: View {
|
|||||||
List {
|
List {
|
||||||
Section(header: Text("Accounts")) {
|
Section(header: Text("Accounts")) {
|
||||||
ForEach(instances.accounts(instanceID)) { account in
|
ForEach(instances.accounts(instanceID)) { account in
|
||||||
Text(account.description)
|
HStack(spacing: 2) {
|
||||||
|
Text(account.description)
|
||||||
|
if instances.defaultAccount == account {
|
||||||
|
Text("— default")
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
.swipeActions(edge: .leading, allowsFullSwipe: true) {
|
||||||
Button("Remove", role: .destructive) {
|
if instances.defaultAccount != account {
|
||||||
instances.removeAccount(account)
|
Button("Make Default", action: { makeDefault(account) })
|
||||||
accountsChanged.toggle()
|
} else {
|
||||||
|
Button("Reset Default", action: resetDefaultAccount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||||
|
Button("Remove", role: .destructive, action: { removeAccount(account) })
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
.redrawOn(change: accountsChanged)
|
.redrawOn(change: accountsChanged)
|
||||||
@ -42,4 +52,19 @@ struct InstanceDetailsSettingsView: View {
|
|||||||
AccountFormView(instance: instance)
|
AccountFormView(instance: instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func makeDefault(_ account: Instance.Account) {
|
||||||
|
instances.setDefaultAccount(account)
|
||||||
|
accountsChanged.toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func resetDefaultAccount() {
|
||||||
|
instances.resetDefaultAccount()
|
||||||
|
accountsChanged.toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func removeAccount(_ account: Instance.Account) {
|
||||||
|
instances.removeAccount(account)
|
||||||
|
accountsChanged.toggle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ struct InstancesSettingsView: View {
|
|||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
Group {
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
Section(header: instancesHeader) {
|
Section(header: instancesHeader, footer: instancesFooter) {
|
||||||
ForEach(instances) { instance in
|
ForEach(instances) { instance in
|
||||||
Button(action: {
|
Button(action: {
|
||||||
self.selectedInstanceID = instance.id
|
self.selectedInstanceID = instance.id
|
||||||
@ -81,7 +81,7 @@ struct InstancesSettingsView: View {
|
|||||||
.foregroundColor(.secondary)
|
.foregroundColor(.secondary)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let instance = selectedInstance {
|
if !selectedInstance.isNil {
|
||||||
if accounts.isEmpty {
|
if accounts.isEmpty {
|
||||||
Text("You have no accounts for this instance")
|
Text("You have no accounts for this instance")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
@ -90,7 +90,7 @@ struct InstancesSettingsView: View {
|
|||||||
Text("Accounts")
|
Text("Accounts")
|
||||||
List(selection: $selectedAccount) {
|
List(selection: $selectedAccount) {
|
||||||
ForEach(accounts) { account in
|
ForEach(accounts) { account in
|
||||||
AccountSettingsView(instance: instance, account: account,
|
AccountSettingsView(account: account,
|
||||||
selectedAccount: $selectedAccount)
|
selectedAccount: $selectedAccount)
|
||||||
.tag(account)
|
.tag(account)
|
||||||
}
|
}
|
||||||
@ -131,6 +131,9 @@ struct InstancesSettingsView: View {
|
|||||||
Button("Add Instance...") {
|
Button("Add Instance...") {
|
||||||
presentingInstanceForm = true
|
presentingInstanceForm = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defaultAccountSection
|
||||||
|
.padding(.top, 10)
|
||||||
}
|
}
|
||||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||||
|
|
||||||
@ -154,6 +157,24 @@ struct InstancesSettingsView: View {
|
|||||||
Text("Instances").background(instanceDetailsNavigationLink)
|
Text("Instances").background(instanceDetailsNavigationLink)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var instancesFooter: some View {
|
||||||
|
Group {
|
||||||
|
if let account = instancesModel.defaultAccount {
|
||||||
|
HStack(spacing: 2) {
|
||||||
|
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
|
||||||
|
.truncationMode(.middle)
|
||||||
|
.lineLimit(1)
|
||||||
|
|
||||||
|
Button("Reset", action: resetDefaultAccount)
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Text("You have no default account set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var instanceDetailsNavigationLink: some View {
|
var instanceDetailsNavigationLink: some View {
|
||||||
NavigationLink(
|
NavigationLink(
|
||||||
isActive: $presentingInstanceDetails,
|
isActive: $presentingInstanceDetails,
|
||||||
@ -162,7 +183,30 @@ struct InstancesSettingsView: View {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSelectedInstanceToFormInstance() {
|
private var defaultAccountSection: some View {
|
||||||
|
Group {
|
||||||
|
if let account = instancesModel.defaultAccount {
|
||||||
|
HStack(spacing: 2) {
|
||||||
|
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
|
||||||
|
.truncationMode(.middle)
|
||||||
|
.lineLimit(1)
|
||||||
|
Button("Reset", action: resetDefaultAccount)
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Text("You have no default account set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func resetDefaultAccount() {
|
||||||
|
instancesModel.resetDefaultAccount()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setSelectedInstanceToFormInstance() {
|
||||||
if let id = savedFormInstanceID {
|
if let id = savedFormInstanceID {
|
||||||
selectedInstanceID = id
|
selectedInstanceID = id
|
||||||
savedFormInstanceID = nil
|
savedFormInstanceID = nil
|
||||||
@ -172,6 +216,10 @@ struct InstancesSettingsView: View {
|
|||||||
|
|
||||||
struct InstancesSettingsView_Previews: PreviewProvider {
|
struct InstancesSettingsView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
InstancesSettingsView()
|
VStack {
|
||||||
|
InstancesSettingsView()
|
||||||
|
}
|
||||||
|
.frame(width: 400, height: 270)
|
||||||
|
.environmentObject(InstancesModel())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,28 +29,27 @@ struct SettingsView: View {
|
|||||||
.tag(Tabs.playback)
|
.tag(Tabs.playback)
|
||||||
}
|
}
|
||||||
.padding(20)
|
.padding(20)
|
||||||
.frame(width: 400, height: 270)
|
.frame(width: 400, height: 310)
|
||||||
#else
|
#else
|
||||||
NavigationView {
|
NavigationView {
|
||||||
List {
|
List {
|
||||||
InstancesSettingsView()
|
InstancesSettingsView()
|
||||||
PlaybackSettingsView()
|
PlaybackSettingsView()
|
||||||
}
|
}
|
||||||
|
.navigationTitle("Settings")
|
||||||
|
.toolbar {
|
||||||
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
|
Button("Done") {
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
#if !os(tvOS)
|
||||||
|
.keyboardShortcut(.cancelAction)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
.listStyle(.insetGrouped)
|
.listStyle(.insetGrouped)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
.navigationTitle("Settings")
|
|
||||||
.toolbar {
|
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
|
||||||
Button("Done") {
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
#if !os(tvOS)
|
|
||||||
.keyboardShortcut(.cancelAction)
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user