// PreferencesView.swift // Tusker // // Created by Shadowfacts on 6/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import SwiftUI struct PreferencesView: View { let mastodonController: MastodonController @ObservedObject private var localData = LocalData.shared @State private var showingLogoutConfirmation = false init(mastodonController: MastodonController) { self.mastodonController = mastodonController } var body: some View { List { accountsSection preferencesSection aboutSection } .listStyle(.insetGrouped) .appGroupedScrollBackgroundIfAvailable() .navigationBarTitle("Preferences") .navigationBarTitleDisplayMode(.inline) .onAppear { if #unavailable(iOS 16.0) { UITableView.appearance(whenContainedInInstancesOf: [PreferencesNavigationController.self]).backgroundColor = .appGroupedBackground } } } private var accountsSection: some View { Section { ForEach(localData.accounts, id: \.accessToken) { (account) in Button(action: { NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": account]) }) { HStack { LocalAccountAvatarView(localAccountInfo: account) VStack(alignment: .leading) { Text(verbatim: account.username) .foregroundColor(.primary) Text(verbatim: account.instanceURL.host!) .font(.caption) .foregroundColor(.primary) } 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: { localData.accounts[$0] == mastodonController.accountInfo! }) { logoutFromCurrent = true indices.remove(index) } indices.forEach { LogoutService(accountInfo: localData.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()) } } header: { Text("Accounts") } .listRowBackground(Color.appGroupedCellBackground) } private var preferencesSection: some View { Section { NavigationLink(destination: AppearancePrefsView()) { Text("Appearance") } NavigationLink(destination: ComposingPrefsView()) { Text("Composing") } NavigationLink(destination: MediaPrefsView()) { Text("Media") } NavigationLink(destination: BehaviorPrefsView()) { Text("Behavior") } NavigationLink(destination: WellnessPrefsView()) { Text("Digital Wellness") } NavigationLink(destination: AdvancedPrefsView()) { Text("Advanced") } } .listRowBackground(Color.appGroupedCellBackground) } private var aboutSection: some View { Section { NavigationLink("About") { AboutView() } NavigationLink("Tip Jar") { TipJarView() } NavigationLink("Acknowledgements") { AcknowledgementsView() } } .listRowBackground(Color.appGroupedCellBackground) } func logoutPressed() { NotificationCenter.default.post(name: .userLoggedOut, object: nil) } } extension View { @available(iOS, obsoleted: 16.0) @ViewBuilder func appGroupedScrollBackgroundIfAvailable() -> some View { if #available(iOS 16.0, *) { self .scrollContentBackground(.hidden) .background(Color.appGroupedBackground) } else { self } } } //#if DEBUG //struct PreferencesView_Previews : PreviewProvider { // static var previews: some View { // return PreferencesView() // } //} //#endif