Setting default account

This commit is contained in:
Arkadiusz Fal 2021-09-27 00:03:33 +02:00
parent 3d35110c67
commit c4674c06a4
8 changed files with 171 additions and 61 deletions

View File

@ -19,6 +19,10 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
self.sid = sid
}
var instance: Instance {
Defaults[.instances].first { $0.id == instanceID }!
}
var anonymizedSID: String {
guard sid.count > 3 else {
return ""
@ -35,7 +39,6 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
func hash(into hasher: inout Hasher) {
hasher.combine(sid)
}
}
struct AccountsBridge: Defaults.Bridge {
typealias Value = Account
@ -73,6 +76,7 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
}
}
}
static var bridge = InstancesBridge()

View File

@ -2,8 +2,13 @@ import Defaults
import Foundation
final class InstancesModel: ObservableObject {
var defaultAccount: Instance.Account! {
Defaults[.accounts].first
@Published var defaultAccount: Instance.Account?
init() {
if let id = Defaults[.defaultAccountID] {
let uuid = UUID(uuidString: id)
defaultAccount = Defaults[.accounts].first { $0.id == uuid }
}
}
func find(_ id: Instance.ID?) -> Instance? {
@ -26,8 +31,10 @@ final class InstancesModel: ObservableObject {
}
func remove(_ instance: Instance) {
let accounts = accounts(instance.id)
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
Defaults[.instances].remove(at: index)
accounts.forEach { removeAccount($0) }
}
}
@ -43,4 +50,13 @@ final class InstancesModel: ObservableObject {
Defaults[.accounts].remove(at: accountIndex)
}
}
func resetDefaultAccount() {
setDefaultAccount(nil)
}
func setDefaultAccount(_ account: Instance.Account?) {
Defaults[.defaultAccountID] = account?.id.uuidString
defaultAccount = account
}
}

View File

@ -7,6 +7,7 @@ extension Defaults.Keys {
static let instances = Key<[Instance]>("instances", default: [])
static let accounts = Key<[Instance.Account]>("accounts", default: [])
static let defaultAccountID = Key<String?>("defaultAccountID")
static let selectedPlaylistID = Key<String?>("selectedPlaylistID")
static let showingAddToPlaylist = Key<Bool>("showingAddToPlaylist", default: false)

View File

@ -40,10 +40,8 @@ struct PearvidiousApp: App {
search.api = api
subscriptions.api = api
guard api.account.isNil, instances.defaultAccount != nil else {
return
if let account = instances.defaultAccount {
api.setAccount(account)
}
api.setAccount(instances.defaultAccount)
}
}

View File

@ -1,7 +1,7 @@
import Defaults
import SwiftUI
struct AccountSettingsView: View {
let instance: Instance
let account: Instance.Account
@Binding var selectedAccount: Instance.Account?
@ -11,11 +11,22 @@ struct AccountSettingsView: View {
var body: some View {
HStack {
HStack(spacing: 2) {
Text(account.description)
if instances.defaultAccount == account {
Text("— default")
.foregroundColor(.secondary)
}
}
Spacer()
HStack {
if instances.defaultAccount != account {
Button("Make default", action: makeDefault)
} else {
Button("Reset default", action: resetDefault)
}
Button("Remove", role: .destructive) {
presentingRemovalConfirmationDialog = true
}
@ -34,4 +45,12 @@ struct AccountSettingsView: View {
.opacity(account == selectedAccount ? 1 : 0)
}
}
private func makeDefault() {
instances.setDefaultAccount(account)
}
private func resetDefault() {
instances.resetDefaultAccount()
}
}

View File

@ -16,14 +16,24 @@ struct InstanceDetailsSettingsView: View {
List {
Section(header: Text("Accounts")) {
ForEach(instances.accounts(instanceID)) { account in
HStack(spacing: 2) {
Text(account.description)
#if !os(tvOS)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button("Remove", role: .destructive) {
instances.removeAccount(account)
accountsChanged.toggle()
if instances.defaultAccount == account {
Text("— default")
.foregroundColor(.secondary)
}
}
#if !os(tvOS)
.swipeActions(edge: .leading, allowsFullSwipe: true) {
if instances.defaultAccount != account {
Button("Make Default", action: { makeDefault(account) })
} else {
Button("Reset Default", action: resetDefaultAccount)
}
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button("Remove", role: .destructive, action: { removeAccount(account) })
}
#endif
}
.redrawOn(change: accountsChanged)
@ -42,4 +52,19 @@ struct InstanceDetailsSettingsView: View {
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()
}
}

View File

@ -34,7 +34,7 @@ struct InstancesSettingsView: View {
var body: some View {
Group {
#if os(iOS)
Section(header: instancesHeader) {
Section(header: instancesHeader, footer: instancesFooter) {
ForEach(instances) { instance in
Button(action: {
self.selectedInstanceID = instance.id
@ -81,7 +81,7 @@ struct InstancesSettingsView: View {
.foregroundColor(.secondary)
}
if let instance = selectedInstance {
if !selectedInstance.isNil {
if accounts.isEmpty {
Text("You have no accounts for this instance")
.font(.caption)
@ -90,7 +90,7 @@ struct InstancesSettingsView: View {
Text("Accounts")
List(selection: $selectedAccount) {
ForEach(accounts) { account in
AccountSettingsView(instance: instance, account: account,
AccountSettingsView(account: account,
selectedAccount: $selectedAccount)
.tag(account)
}
@ -131,6 +131,9 @@ struct InstancesSettingsView: View {
Button("Add Instance...") {
presentingInstanceForm = true
}
defaultAccountSection
.padding(.top, 10)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
@ -154,6 +157,24 @@ struct InstancesSettingsView: View {
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 {
NavigationLink(
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 {
selectedInstanceID = id
savedFormInstanceID = nil
@ -172,6 +216,10 @@ struct InstancesSettingsView: View {
struct InstancesSettingsView_Previews: PreviewProvider {
static var previews: some View {
VStack {
InstancesSettingsView()
}
.frame(width: 400, height: 270)
.environmentObject(InstancesModel())
}
}

View File

@ -29,17 +29,13 @@ struct SettingsView: View {
.tag(Tabs.playback)
}
.padding(20)
.frame(width: 400, height: 270)
.frame(width: 400, height: 310)
#else
NavigationView {
List {
InstancesSettingsView()
PlaybackSettingsView()
}
#if os(iOS)
.listStyle(.insetGrouped)
#endif
.navigationTitle("Settings")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
@ -51,6 +47,9 @@ struct SettingsView: View {
#endif
}
}
#if os(iOS)
.listStyle(.insetGrouped)
#endif
}
#endif
}