2019-06-14 00:53:17 +00:00
// P r e f e r e n c e s V i e w . s w i f t
// T u s k e r
//
// C r e a t e d b y S h a d o w f a c t s o n 6 / 1 3 / 1 9 .
// C o p y r i g h t © 2 0 1 9 S h a d o w f a c t s . A l l r i g h t s r e s e r v e d .
//
import SwiftUI
2023-03-05 19:35:25 +00:00
import UserAccounts
2019-06-14 00:53:17 +00:00
2020-01-19 03:43:10 +00:00
struct PreferencesView : View {
2022-12-15 02:27:50 +00:00
let mastodonController : MastodonController
2023-02-03 04:14:19 +00:00
2023-03-05 19:35:25 +00:00
@ ObservedObject private var userAccounts = UserAccountsManager . shared
2019-09-16 17:12:23 +00:00
@ State private var showingLogoutConfirmation = false
2022-12-15 02:27:50 +00:00
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 )
2023-02-06 23:42:55 +00:00
. appGroupedListBackground ( container : PreferencesNavigationController . self )
2023-02-03 04:14:19 +00:00
. navigationBarTitle ( " Preferences " )
. navigationBarTitleDisplayMode ( . inline )
}
private var accountsSection : some View {
Section {
2023-03-05 19:35:25 +00:00
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 )
Text ( verbatim : account . instanceURL . host ! )
. font ( . caption )
. foregroundColor ( . primary )
2020-09-17 02:17:20 +00:00
}
2023-02-03 04:14:19 +00:00
Spacer ( )
if account = = mastodonController . accountInfo ! {
Image ( systemName : " checkmark " )
. renderingMode ( . template )
. foregroundColor ( . secondary )
2020-01-19 03:43:10 +00:00
}
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
2023-03-05 19:35:25 +00:00
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
2023-03-05 19:35:25 +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 )
} ) {
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 " )
}
2023-02-06 23:42:55 +00:00
. 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 " )
}
}
2023-02-06 23:42:55 +00:00
. 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 ( )
}
}
2023-02-06 23:42:55 +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
}
2022-12-15 02:27:50 +00:00
// # i f D E B U G
// s t r u c t P r e f e r e n c e s V i e w _ P r e v i e w s : P r e v i e w P r o v i d e r {
// s t a t i c v a r p r e v i e w s : s o m e V i e w {
// r e t u r n P r e f e r e n c e s V i e w ( )
// }
// }
// # e n d i f