Tusker/Tusker/Screens/Preferences/PreferencesView.swift

160 lines
5.4 KiB
Swift
Raw Normal View History

2019-06-14 00:53:17 +00:00
// PreferencesView.swift
// Tusker
//
// Created by Shadowfacts on 6/13/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import SwiftUI
import UserAccounts
import WebURL
2019-06-14 00:53:17 +00:00
struct PreferencesView: View {
let mastodonController: MastodonController
2023-02-03 04:14:19 +00:00
@ObservedObject private var userAccounts = UserAccountsManager.shared
2019-09-16 17:12:23 +00:00
@State private var showingLogoutConfirmation = false
init(mastodonController: MastodonController) {
self.mastodonController = mastodonController
}
2020-01-19 16:52:06 +00:00
2019-06-14 00:53:17 +00:00
var body: some View {
2023-02-03 04:14:19 +00:00
List {
accountsSection
preferencesSection
aboutSection
}
.listStyle(.insetGrouped)
.appGroupedListBackground(container: PreferencesNavigationController.self)
2023-02-03 04:14:19 +00:00
.navigationBarTitle("Preferences")
.navigationBarTitleDisplayMode(.inline)
}
private var accountsSection: some View {
Section {
ForEach(userAccounts.accounts, id: \.accessToken) { (account) in
2023-02-03 04:14:19 +00:00
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)
let instance = if let domain = WebURL.Domain(account.instanceURL.host!) {
domain.render(.uncheckedUnicodeString)
} else {
account.instanceURL.host!
}
Text(verbatim: instance)
2023-02-03 04:14:19 +00:00
.font(.caption)
.foregroundColor(.primary)
}
2023-02-03 04:14:19 +00:00
Spacer()
if account == mastodonController.accountInfo! {
Image(systemName: "checkmark")
.renderingMode(.template)
.foregroundColor(.secondary)
}
2019-09-16 17:12:23 +00:00
}
2023-02-03 04:14:19 +00:00
}.onDrag {
let activity = UserActivityManager.mainSceneActivity(accountID: account.id)
return NSItemProvider(object: activity)
2019-06-14 00:53:17 +00:00
}
2023-02-03 04:14:19 +00:00
}.onDelete { (indices: IndexSet) in
var indices = indices
var logoutFromCurrent = false
if let index = indices.first(where: { userAccounts.accounts[$0] == mastodonController.accountInfo! }) {
2023-02-03 04:14:19 +00:00
logoutFromCurrent = true
indices.remove(index)
2019-06-14 00:53:17 +00:00
}
2022-12-21 16:59:40 +00:00
indices.forEach { LogoutService(accountInfo: userAccounts.accounts[$0]).run() }
2023-02-03 04:14:19 +00:00
if logoutFromCurrent {
self.logoutPressed()
2022-12-21 16:59:40 +00:00
}
2019-06-14 00:53:17 +00:00
}
2023-02-03 04:14:19 +00:00
Button(action: {
NotificationCenter.default.post(name: .addAccount, object: nil)
}) {
2024-04-08 02:29:48 +00:00
Text("Add Account…")
2023-02-03 04:14:19 +00:00
}
2023-02-03 04:14:19 +00:00
Button(action: {
self.showingLogoutConfirmation = true
}) {
2024-04-08 02:29:48 +00:00
Text("Logout from Current…")
2023-02-03 04:14:19 +00:00
}.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")
}
2023-02-03 04:14:19 +00:00
} header: {
Text("Accounts")
}
.appGroupedListRowBackground()
2023-02-03 04:14:19 +00:00
}
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")
}
}
.appGroupedListRowBackground()
2023-02-03 04:14:19 +00:00
}
private var aboutSection: some View {
Section {
NavigationLink("About") {
AboutView()
}
NavigationLink("Tip Jar") {
TipJarView()
}
NavigationLink("Acknowledgements") {
AcknowledgementsView()
}
}
.appGroupedListRowBackground()
2019-06-14 00:53:17 +00:00
}
2019-09-16 17:12:23 +00:00
func logoutPressed() {
NotificationCenter.default.post(name: .userLoggedOut, object: nil)
}
2019-06-14 00:53:17 +00:00
}
extension Notification.Name {
static let showMastodonSettings = Notification.Name("Tusker.showMastodonSettings")
}
//#if DEBUG
//struct PreferencesView_Previews : PreviewProvider {
// static var previews: some View {
// return PreferencesView()
// }
//}
//#endif