// PreferencesView.swift // Tusker // // Created by Shadowfacts on 6/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import SwiftUI import UserAccounts struct PreferencesView: View { let mastodonController: MastodonController @ObservedObject var navigationState: PreferencesNavigationState @ObservedObject private var userAccounts = UserAccountsManager.shared @State private var showingLogoutConfirmation = false init(mastodonController: MastodonController, navigationState: PreferencesNavigationState) { self.mastodonController = mastodonController self.navigationState = navigationState } var body: some View { List { accountsSection preferencesSection aboutSection } .listStyle(.insetGrouped) .appGroupedListBackground(container: PreferencesNavigationController.self) .navigationBarTitle("Preferences") .navigationBarTitleDisplayMode(.inline) } private var accountsSection: some View { Section { ForEach(userAccounts.accounts) { (account) in Button(action: { NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": account]) }) { HStack { PrefsAccountView(account: account) Spacer() if account == mastodonController.accountInfo! { Image(systemName: "checkmark") .renderingMode(.template) .foregroundColor(.secondary) } } }.onDrag { let activity = UserActivityManager.mainSceneActivity(accountID: account.id) return NSItemProvider(object: activity) } }.onDelete { (indices: IndexSet) in var indices = indices var logoutFromCurrent = false if let index = indices.first(where: { userAccounts.accounts[$0] == mastodonController.accountInfo! }) { logoutFromCurrent = true indices.remove(index) } indices.forEach { LogoutService(accountInfo: userAccounts.accounts[$0]).run() } if logoutFromCurrent { self.logoutPressed() } } Button(action: { NotificationCenter.default.post(name: .addAccount, object: nil) }) { Text("Add Account…") } Button(action: { self.showingLogoutConfirmation = true }) { Text("Logout from Current…") }.alert(isPresented: $showingLogoutConfirmation) { Alert(title: Text("Are you sure you want to logout?"), message: nil, primaryButton: .destructive(Text("Logout"), action: self.logoutPressed), secondaryButton: .cancel()) } Button { NotificationCenter.default.post(name: .showMastodonSettings, object: nil) } label: { Text("Account Settings") } } header: { Text("Accounts") } .appGroupedListRowBackground() } private var preferencesSection: some View { Section { NavigationLink(destination: AppearancePrefsView()) { PreferenceSectionLabel(title: "Appearance", systemImageName: "textformat", backgroundColor: .indigo) } NavigationLink(destination: BehaviorPrefsView()) { PreferenceSectionLabel(title: "Behavior", systemImageName: "flowchart.fill", backgroundColor: .green) } NavigationLink(isActive: $navigationState.showNotificationPreferences) { NotificationsPrefsView() } label: { PreferenceSectionLabel(title: "Notifications", systemImageName: "bell.fill", backgroundColor: .red) } NavigationLink(destination: ComposingPrefsView()) { PreferenceSectionLabel(title: "Composing", systemImageName: "pencil", backgroundColor: .blue) } NavigationLink(destination: WellnessPrefsView()) { PreferenceSectionLabel(title: "Digital Wellness", systemImageName: "brain.fill", backgroundColor: .purple) } NavigationLink(destination: AdvancedPrefsView()) { PreferenceSectionLabel(title: "Advanced", systemImageName: "gearshape.2.fill", backgroundColor: .gray) } } .appGroupedListRowBackground() } private var aboutSection: some View { Section { NavigationLink { AboutView() } label: { Label { Text("About") } icon: { Image("AboutIcon") .resizable() .clipShape(RoundedRectangle(cornerRadius: 6)) .frame(width: 30, height: 30) } } NavigationLink { TipJarView() } label: { // TODO: custom tip jar icon? PreferenceSectionLabel(title: "Tip Jar", systemImageName: "dollarsign.square.fill", backgroundColor: .yellow) } NavigationLink { AcknowledgementsView() } label: { PreferenceSectionLabel(title: "Acknowledgements", systemImageName: "doc.text.fill", backgroundColor: .gray) } } .appGroupedListRowBackground() } func logoutPressed() { NotificationCenter.default.post(name: .userLoggedOut, object: nil) } } private struct PreferenceSectionLabel: View { let title: LocalizedStringKey let systemImageName: String let backgroundColor: Color var body: some View { Label { Text(title) } icon: { Image(systemName: systemImageName) .imageScale(.medium) .foregroundStyle(.white) .frame(width: 30, height: 30) .background(backgroundColor, in: RoundedRectangle(cornerRadius: 6)) } } } //#if DEBUG //struct PreferencesView_Previews : PreviewProvider { // static var previews: some View { // return PreferencesView() // } //} //#endif