Tusker/Tusker/Screens/Preferences/PreferencesView.swift

184 lines
6.6 KiB
Swift
Raw Permalink 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
2019-06-14 00:53:17 +00:00
struct PreferencesView: View {
let mastodonController: MastodonController
@ObservedObject var navigationState: PreferencesNavigationState
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, navigationState: PreferencesNavigationState) {
self.mastodonController = mastodonController
self.navigationState = navigationState
}
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) { (account) in
2023-02-03 04:14:19 +00:00
Button(action: {
NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": account])
}) {
HStack {
PrefsAccountView(account: account)
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
}
2024-04-15 03:36:35 +00:00
private var preferencesSection: some View {
2024-04-07 18:04:42 +00:00
Section {
2024-04-15 03:36:35 +00:00
NavigationLink(destination: AppearancePrefsView()) {
2024-04-15 04:12:56 +00:00
PreferenceSectionLabel(title: "Appearance", systemImageName: "textformat", backgroundColor: .indigo)
2024-04-15 03:36:35 +00:00
}
NavigationLink(destination: BehaviorPrefsView()) {
2024-04-15 04:12:56 +00:00
PreferenceSectionLabel(title: "Behavior", systemImageName: "flowchart.fill", backgroundColor: .green)
2024-04-15 03:36:35 +00:00
}
NavigationLink(isActive: $navigationState.showNotificationPreferences) {
NotificationsPrefsView()
} label: {
2024-04-15 04:12:56 +00:00
PreferenceSectionLabel(title: "Notifications", systemImageName: "bell.fill", backgroundColor: .red)
2024-04-07 18:04:42 +00:00
}
2023-02-03 04:14:19 +00:00
NavigationLink(destination: ComposingPrefsView()) {
2024-04-15 04:12:56 +00:00
PreferenceSectionLabel(title: "Composing", systemImageName: "pencil", backgroundColor: .blue)
2023-02-03 04:14:19 +00:00
}
NavigationLink(destination: WellnessPrefsView()) {
let brainImageName = if #available(iOS 17.0, *) {
"brain.fill"
} else {
"brain"
}
PreferenceSectionLabel(title: "Digital Wellness", systemImageName: brainImageName, backgroundColor: .purple)
2023-02-03 04:14:19 +00:00
}
NavigationLink(destination: AdvancedPrefsView()) {
2024-04-15 04:12:56 +00:00
PreferenceSectionLabel(title: "Advanced", systemImageName: "gearshape.2.fill", backgroundColor: .gray)
2023-02-03 04:14:19 +00:00
}
}
.appGroupedListRowBackground()
2023-02-03 04:14:19 +00:00
}
private var aboutSection: some View {
Section {
2024-04-15 04:12:56 +00:00
NavigationLink {
2023-02-03 04:14:19 +00:00
AboutView()
2024-04-15 04:12:56 +00:00
} label: {
Label {
Text("About")
} icon: {
Image("AboutIcon")
.resizable()
.clipShape(RoundedRectangle(cornerRadius: 6))
.frame(width: 30, height: 30)
}
2023-02-03 04:14:19 +00:00
}
2024-04-15 04:12:56 +00:00
NavigationLink {
2023-02-03 04:14:19 +00:00
TipJarView()
2024-04-15 04:12:56 +00:00
} label: {
// TODO: custom tip jar icon?
PreferenceSectionLabel(title: "Tip Jar", systemImageName: "dollarsign.square.fill", backgroundColor: .yellow)
2023-02-03 04:14:19 +00:00
}
2024-04-15 04:12:56 +00:00
NavigationLink {
2023-02-03 04:14:19 +00:00
AcknowledgementsView()
2024-04-15 04:12:56 +00:00
} label: {
PreferenceSectionLabel(title: "Acknowledgements", systemImageName: "doc.text.fill", backgroundColor: .gray)
2023-02-03 04:14:19 +00:00
}
}
.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
}
2024-04-15 04:12:56 +00:00
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