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
2024-04-11 22:26:58 +00:00
@ ObservedObject var navigationState : PreferencesNavigationState
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
2024-04-11 22:26:58 +00:00
init ( mastodonController : MastodonController , navigationState : PreferencesNavigationState ) {
2022-12-15 02:27:50 +00:00
self . mastodonController = mastodonController
2024-04-11 22:26:58 +00:00
self . navigationState = navigationState
2022-12-15 02:27:50 +00:00
}
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 {
2024-04-07 19:49:12 +00:00
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 {
2024-04-07 19:49:12 +00:00
PrefsAccountView ( account : account )
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 )
} ) {
2024-04-08 02:29:48 +00:00
Text ( " Add Account… " )
2023-02-03 04:14:19 +00:00
}
2024-04-08 02:28:30 +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 ( ) )
}
2024-04-08 02:28:30 +00:00
Button {
NotificationCenter . default . post ( name : . showMastodonSettings , object : nil )
} label : {
Text ( " Account Settings " )
}
2023-02-03 04:14:19 +00:00
} header : {
Text ( " Accounts " )
}
2023-02-06 23:42:55 +00:00
. 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
}
2024-04-11 22:26:58 +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 ( ) ) {
2024-04-17 21:12:35 +00:00
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
}
}
2023-02-06 23:42:55 +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: c u s t o m t i p j a r i c o n ?
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
}
}
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
}
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 ) )
}
}
}
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