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
2022-12-22 16:54:23 +00:00
import TTTKit
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
2020-01-19 03:43:10 +00:00
@ ObservedObject var localData = LocalData . 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 {
// w o r k a r o u n d : t h e n a v i g a t i o n v i e w i s p r o v i d e d b y M y P r o f i l e T a b l e V i e w C o n t r o l l e r s o t h a t i t c a n i n j e c t t h e D o n e b u t t o n
// N a v i g a t i o n V i e w {
List {
2020-09-16 00:46:49 +00:00
Section ( header : Text ( " Accounts " ) ) {
2020-01-19 03:43:10 +00:00
ForEach ( localData . accounts , id : \ . accessToken ) { ( account ) in
Button ( action : {
NotificationCenter . default . post ( name : . activateAccount , object : nil , userInfo : [ " account " : account ] )
} ) {
HStack {
2020-05-10 18:46:27 +00:00
LocalAccountAvatarView ( localAccountInfo : account )
VStack ( alignment : . leading ) {
Text ( verbatim : account . username )
. foregroundColor ( . primary )
Text ( verbatim : account . instanceURL . host ! )
. font ( . caption )
. foregroundColor ( . primary )
}
2020-01-19 03:43:10 +00:00
Spacer ( )
2022-12-15 02:27:50 +00:00
if account = = mastodonController . accountInfo ! {
2020-01-19 03:43:10 +00:00
Image ( systemName : " checkmark " )
. renderingMode ( . template )
. foregroundColor ( . secondary )
}
}
2021-06-06 18:55:18 +00:00
} . onDrag {
let activity = UserActivityManager . mainSceneActivity ( accountID : account . id )
return NSItemProvider ( object : activity )
2020-01-19 03:43:10 +00:00
}
2020-09-17 02:17:20 +00:00
} . onDelete { ( indices : IndexSet ) in
var indices = indices
var logoutFromCurrent = false
2022-12-15 02:27:50 +00:00
if let index = indices . first ( where : { localData . accounts [ $0 ] = = mastodonController . accountInfo ! } ) {
2020-09-17 02:17:20 +00:00
logoutFromCurrent = true
indices . remove ( index )
}
2022-09-13 03:05:35 +00:00
indices . forEach { localData . removeAccount ( localData . accounts [ $0 ] ) }
2020-09-17 02:17:20 +00:00
if logoutFromCurrent {
self . logoutPressed ( )
}
2020-01-19 03:43:10 +00:00
}
2020-09-17 02:17:20 +00:00
2019-09-16 17:12:23 +00:00
Button ( action : {
2020-01-19 03:43:10 +00:00
NotificationCenter . default . post ( name : . addAccount , object : nil )
2019-09-16 17:12:23 +00:00
} ) {
2020-01-19 03:43:10 +00:00
Text ( " Add Account... " )
}
2020-01-19 16:52:06 +00:00
if localData . getMostRecentAccount ( ) != nil {
2020-01-19 03:43:10 +00:00
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 ( ) )
}
2019-09-16 17:12:23 +00:00
}
2019-06-14 00:53:17 +00:00
}
2019-09-16 17:12:23 +00:00
Section {
NavigationLink ( destination : AppearancePrefsView ( ) ) {
Text ( " Appearance " )
}
2020-02-22 18:19:31 +00:00
NavigationLink ( destination : ComposingPrefsView ( ) ) {
Text ( " Composing " )
}
NavigationLink ( destination : MediaPrefsView ( ) ) {
Text ( " Media " )
}
2019-09-16 17:12:23 +00:00
NavigationLink ( destination : BehaviorPrefsView ( ) ) {
Text ( " Behavior " )
}
NavigationLink ( destination : WellnessPrefsView ( ) ) {
Text ( " Digital Wellness " )
}
NavigationLink ( destination : AdvancedPrefsView ( ) ) {
Text ( " Advanced " )
}
2019-06-14 00:53:17 +00:00
}
2022-12-21 16:59:40 +00:00
Section {
2022-12-22 16:54:23 +00:00
NavigationLink ( " About " ) {
AboutView ( )
}
2022-12-21 16:59:40 +00:00
NavigationLink ( " Acknowledgements " ) {
AcknowledgementsView ( )
}
}
2019-06-14 00:53:17 +00:00
}
2021-02-06 18:47:45 +00:00
. listStyle ( InsetGroupedListStyle ( ) )
2020-09-16 00:46:49 +00:00
. navigationBarTitle ( Text ( " Preferences " ) , displayMode : . inline )
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