From af8a9faaebb6e7dca193493ee77c4052a1a607bb Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 2 Feb 2023 23:14:19 -0500 Subject: [PATCH] Cleanup PreferencesView --- .../Screens/Preferences/PreferencesView.swift | 186 +++++++++--------- .../Profile/MyProfileViewController.swift | 1 + 2 files changed, 98 insertions(+), 89 deletions(-) diff --git a/Tusker/Screens/Preferences/PreferencesView.swift b/Tusker/Screens/Preferences/PreferencesView.swift index de97071d..aec06876 100644 --- a/Tusker/Screens/Preferences/PreferencesView.swift +++ b/Tusker/Screens/Preferences/PreferencesView.swift @@ -6,11 +6,11 @@ // import SwiftUI -import TTTKit struct PreferencesView: View { let mastodonController: MastodonController - @ObservedObject var localData = LocalData.shared + + @ObservedObject private var localData = LocalData.shared @State private var showingLogoutConfirmation = false init(mastodonController: MastodonController) { @@ -18,101 +18,109 @@ struct PreferencesView: View { } var body: some View { - // workaround: the navigation view is provided by MyProfileTableViewController so that it can inject the Done button -// NavigationView { - List { - Section(header: Text("Accounts")) { - 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) + List { + accountsSection + preferencesSection + aboutSection + } + .listStyle(.insetGrouped) + .navigationBarTitle("Preferences") + .navigationBarTitleDisplayMode(.inline) + } + + 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) } - }.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...") - } - if localData.getMostRecentAccount() != nil { - 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()) + 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) } - 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") - } - } + indices.forEach { LogoutService(accountInfo: localData.accounts[$0]).run() } - Section { - NavigationLink("About") { - AboutView() - } - NavigationLink("Tip Jar") { - TipJarView() - } - NavigationLink("Acknowledgements") { - AcknowledgementsView() - } + if logoutFromCurrent { + self.logoutPressed() } } - .listStyle(InsetGroupedListStyle()) - .navigationBarTitle(Text("Preferences"), displayMode: .inline) -// } + + 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") + } + } + + 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") + } + } + } + + private var aboutSection: some View { + Section { + NavigationLink("About") { + AboutView() + } + NavigationLink("Tip Jar") { + TipJarView() + } + NavigationLink("Acknowledgements") { + AcknowledgementsView() + } + } } func logoutPressed() { diff --git a/Tusker/Screens/Profile/MyProfileViewController.swift b/Tusker/Screens/Profile/MyProfileViewController.swift index f0c02ff6..6beb1057 100644 --- a/Tusker/Screens/Profile/MyProfileViewController.swift +++ b/Tusker/Screens/Profile/MyProfileViewController.swift @@ -8,6 +8,7 @@ import UIKit import Pachyderm +import SwiftUI class MyProfileViewController: ProfileViewController {