Impressia/Vernissage/Views/SettingsView/Subviews/AccountsSectionView.swift

85 lines
3.0 KiB
Swift
Raw Normal View History

2023-01-13 13:37:01 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-13 13:37:01 +01:00
//
import SwiftUI
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-01-13 13:37:01 +01:00
2023-02-21 07:05:06 +01:00
struct AccountsSectionView: View {
2023-01-27 17:31:28 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-02-23 08:09:02 +01:00
@EnvironmentObject var client: Client
2023-01-27 17:31:28 +01:00
@State private var accounts: [AccountModel] = []
@State private var dbAccounts: [AccountData] = []
2023-01-13 13:37:01 +01:00
var body: some View {
2023-03-13 13:53:36 +01:00
Section("settings.title.accounts") {
2023-01-13 13:37:01 +01:00
ForEach(self.accounts) { account in
2023-01-27 17:31:28 +01:00
HStack(alignment: .center) {
UsernameRow(accountId: account.id,
accountAvatar: account.avatar,
accountDisplayName: account.displayName,
accountUsername: account.username)
Spacer()
if self.applicationState.account?.id == account.id {
2023-01-27 17:31:28 +01:00
Image(systemName: "checkmark")
.foregroundColor(self.applicationState.tintColor.color())
}
}
2023-02-23 08:09:02 +01:00
.deleteDisabled(self.deleteDisabled(for: account))
2023-01-13 13:37:01 +01:00
}
2023-01-27 17:31:28 +01:00
.onDelete(perform: delete)
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.signIn) {
2023-01-13 13:37:01 +01:00
HStack {
2023-03-13 13:53:36 +01:00
Text("settings.title.newAccount", comment: "New account")
2023-01-13 13:37:01 +01:00
Spacer()
Image(systemName: "person.crop.circle.badge.plus")
}
}
}
.onAppear {
self.dbAccounts = AccountDataHandler.shared.getAccountsData()
2023-04-07 14:20:12 +02:00
self.accounts = self.dbAccounts.map({ $0.toAccountModel() })
}
2023-01-13 13:37:01 +01:00
}
2023-02-23 08:09:02 +01:00
private func deleteDisabled(for account: AccountModel) -> Bool {
self.applicationState.account?.id == account.id && self.accounts.count > 1
}
2023-02-23 08:09:02 +01:00
private func delete(at offsets: IndexSet) {
2023-01-27 17:31:28 +01:00
let accountsToDelete = offsets.map { self.accounts[$0] }
2023-02-23 08:09:02 +01:00
var shouldClearApplicationState = false
2023-02-23 08:09:02 +01:00
// Delete from database.
2023-01-27 17:31:28 +01:00
for account in accountsToDelete {
2023-02-23 08:09:02 +01:00
// Check if we are deleting active user.
if account.id == self.applicationState.account?.id {
shouldClearApplicationState = true
}
2023-02-23 08:09:02 +01:00
if let dbAccount = self.dbAccounts.first(where: {$0.id == account.id }) {
AccountDataHandler.shared.remove(accountData: dbAccount)
}
}
2023-02-23 08:09:02 +01:00
// Delete from local state.
self.accounts.remove(atOffsets: offsets)
2023-02-23 08:09:02 +01:00
// When we are deleting active user then we have to switch to sing in view.
if shouldClearApplicationState {
// We have to do this after animation of deleting row is ended.
2023-04-22 08:23:03 +02:00
self.asyncAfter(0.5) {
2023-03-29 17:06:41 +02:00
ApplicationSettingsHandler.shared.set(accountId: nil)
2023-02-23 08:09:02 +01:00
self.applicationState.clearApplicationState()
self.client.clearAccount()
}
2023-01-27 17:31:28 +01:00
}
}
2023-01-13 13:37:01 +01:00
}