AppearanceSettingsView.swift (10277B)
1 // 2 // TextFormattingSettings.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-04-05. 6 // 7 8 import SwiftUI 9 10 fileprivate let CACHE_CLEAR_BUTTON_RESET_TIME_IN_SECONDS: Double = 60 11 fileprivate let MINIMUM_CACHE_CLEAR_BUTTON_DELAY_IN_SECONDS: Double = 1 12 13 /// A simple type to keep track of the cache clearing state 14 fileprivate enum CacheClearingState { 15 case not_cleared 16 case clearing 17 case cleared 18 } 19 20 struct ResizedEventPreview: View { 21 let damus_state: DamusState 22 @ObservedObject var settings: UserSettingsStore 23 24 var body: some View { 25 EventView(damus: damus_state, event: test_note, pubkey: test_note.pubkey, options: [.wide, .no_action_bar]) 26 } 27 } 28 29 struct AppearanceSettingsView: View { 30 let damus_state: DamusState 31 @ObservedObject var settings: UserSettingsStore 32 @Environment(\.dismiss) var dismiss 33 @State fileprivate var cache_clearing_state: CacheClearingState = .not_cleared 34 @State var showing_cache_clear_alert: Bool = false 35 36 @State var showing_enable_animation_alert: Bool = false 37 @State var enable_animation_toggle_is_user_initiated: Bool = true 38 39 var FontSize: some View { 40 VStack(alignment: .leading) { 41 Slider(value: $settings.font_size, in: 0.5...2.0, step: 0.1) 42 .padding() 43 44 // Sample text to show how the font size would look 45 ResizedEventPreview(damus_state: damus_state, settings: settings) 46 47 } 48 } 49 50 var body: some View { 51 Form { 52 Section(NSLocalizedString("Font Size", comment: "Section label for font size settings.")) { 53 FontSize 54 } 55 56 // MARK: - Text Truncation 57 Section(header: Text("Text Truncation", comment: "Section header for damus text truncation user configuration")) { 58 Toggle(NSLocalizedString("Truncate timeline text", comment: "Setting to truncate text in timeline"), isOn: $settings.truncate_timeline_text) 59 .toggleStyle(.switch) 60 Toggle(NSLocalizedString("Truncate notification mention text", comment: "Setting to truncate text in mention notifications"), isOn: $settings.truncate_mention_text) 61 .toggleStyle(.switch) 62 } 63 64 Section(header: Text("User Statuses", comment: "Section header for user profile status settings.")) { 65 Toggle(NSLocalizedString("Show general statuses", comment: "Settings toggle for enabling general user statuses"), isOn: $settings.show_general_statuses) 66 .toggleStyle(.switch) 67 68 Toggle(NSLocalizedString("Show music statuses", comment: "Settings toggle for enabling now playing music statuses"), isOn: $settings.show_music_statuses) 69 .toggleStyle(.switch) 70 } 71 72 // MARK: - Accessibility 73 Section(header: Text("Accessibility", comment: "Section header for accessibility settings")) { 74 Toggle(NSLocalizedString("Left Handed", comment: "Moves the post button to the left side of the screen"), isOn: $settings.left_handed) 75 .toggleStyle(.switch) 76 } 77 78 // MARK: - Images 79 Section(NSLocalizedString("Images", comment: "Section title for images configuration.")) { 80 self.EnableAnimationsToggle 81 Toggle(NSLocalizedString("Blur images", comment: "Setting to blur images"), isOn: $settings.blur_images) 82 .toggleStyle(.switch) 83 84 Toggle(NSLocalizedString("Media previews", comment: "Setting to show media"), isOn: $settings.media_previews) 85 .toggleStyle(.switch) 86 87 Picker(NSLocalizedString("Image uploader", comment: "Prompt selection of user's image uploader"), 88 selection: $settings.default_media_uploader) { 89 ForEach(MediaUploader.allCases, id: \.self) { uploader in 90 Text(uploader.model.displayName) 91 .tag(uploader.model.tag) 92 } 93 } 94 95 self.ClearCacheButton 96 } 97 98 // MARK: - Content filters and moderation 99 Section( 100 header: Text("Content filters", comment: "Section title for content filtering/moderation configuration."), 101 footer: Text("Notes with the #nsfw tag usually contains adult content or other \"Not safe for work\" content", comment: "Section footer clarifying what #nsfw (not safe for work) tags mean") 102 ) { 103 Toggle(NSLocalizedString("Show replies from your trusted network first", comment: "Setting to show replies in threads from the current user's trusted network first."), isOn: $settings.show_trusted_replies_first) 104 .toggleStyle(.switch) 105 Toggle(NSLocalizedString("Hide notes with #nsfw tags", comment: "Setting to hide notes with the #nsfw (not safe for work) tags"), isOn: $settings.hide_nsfw_tagged_content) 106 .toggleStyle(.switch) 107 } 108 109 // MARK: - Profiles 110 Section( 111 header: Text("Profiles", comment: "Section title for profile view configuration."), 112 footer: Text("Profile action sheets allow you to follow, zap, or DM profiles more quickly without having to view their full profile", comment: "Section footer clarifying what the profile action sheet feature does") 113 .padding(.bottom, tabHeight + getSafeAreaBottom()) 114 ) { 115 Toggle(NSLocalizedString("Show profile action sheets", comment: "Setting to show profile action sheets when clicking on a user's profile picture"), isOn: $settings.show_profile_action_sheet_on_pfp_click) 116 .toggleStyle(.switch) 117 } 118 119 120 } 121 .navigationTitle(NSLocalizedString("Appearance", comment: "Navigation title for text and appearance settings.")) 122 .onReceive(handle_notify(.switched_timeline)) { _ in 123 dismiss() 124 } 125 } 126 127 func clear_cache_button_action() { 128 cache_clearing_state = .clearing 129 130 let group = DispatchGroup() 131 132 group.enter() 133 DamusCacheManager.shared.clear_cache(damus_state: self.damus_state, completion: { 134 group.leave() 135 }) 136 137 // Make clear cache button take at least a second or so to avoid issues with labor perception bias (https://growth.design/case-studies/labor-perception-bias) 138 group.enter() 139 DispatchQueue.main.asyncAfter(deadline: .now() + MINIMUM_CACHE_CLEAR_BUTTON_DELAY_IN_SECONDS) { 140 group.leave() 141 } 142 143 group.notify(queue: .main) { 144 cache_clearing_state = .cleared 145 DispatchQueue.main.asyncAfter(deadline: .now() + CACHE_CLEAR_BUTTON_RESET_TIME_IN_SECONDS) { 146 cache_clearing_state = .not_cleared 147 } 148 } 149 } 150 151 var EnableAnimationsToggle: some View { 152 Toggle(NSLocalizedString("Animations", comment: "Toggle to enable or disable image animation"), isOn: $settings.enable_animation) 153 .toggleStyle(.switch) 154 .onChange(of: settings.enable_animation) { _ in 155 if self.enable_animation_toggle_is_user_initiated { 156 self.showing_enable_animation_alert = true 157 } 158 else { 159 self.enable_animation_toggle_is_user_initiated = true 160 } 161 } 162 .alert(isPresented: $showing_enable_animation_alert) { 163 Alert(title: Text("Confirmation", comment: "Confirmation dialog title"), 164 message: Text("Changing this setting will cause the cache to be cleared. This will free space, but images may take longer to load again. Are you sure you want to proceed?", comment: "Message explaining consequences of changing the 'enable animation' setting"), 165 primaryButton: .default(Text("OK", comment: "Button label indicating user wants to proceed.")) { 166 self.clear_cache_button_action() 167 }, 168 secondaryButton: .cancel() { 169 // Toggle back if user cancels action 170 self.enable_animation_toggle_is_user_initiated = false 171 settings.enable_animation.toggle() 172 } 173 ) 174 } 175 } 176 177 var ClearCacheButton: some View { 178 Button(action: { self.showing_cache_clear_alert = true }, label: { 179 HStack(spacing: 6) { 180 switch cache_clearing_state { 181 case .not_cleared: 182 Text("Clear Cache", comment: "Button to clear image cache.") 183 case .clearing: 184 ProgressView() 185 Text("Clearing Cache", comment: "Loading message indicating that the cache is being cleared.") 186 case .cleared: 187 Image(systemName: "checkmark.circle.fill") 188 .foregroundColor(.green) 189 Text("Cache has been cleared", comment: "Message indicating that the cache was successfully cleared.") 190 } 191 } 192 }) 193 .disabled(self.cache_clearing_state != .not_cleared) 194 .alert(isPresented: $showing_cache_clear_alert) { 195 Alert(title: Text("Confirmation", comment: "Confirmation dialog title"), 196 message: Text("Are you sure you want to clear the cache? This will free space, but images may take longer to load again.", comment: "Message explaining what it means to clear the cache, asking if user wants to proceed."), 197 primaryButton: .default(Text("OK", comment: "Button label indicating user wants to proceed.")) { 198 self.clear_cache_button_action() 199 }, 200 secondaryButton: .cancel()) 201 } 202 } 203 } 204 205 206 struct TextFormattingSettings_Previews: PreviewProvider { 207 static var previews: some View { 208 AppearanceSettingsView(damus_state: test_damus_state, settings: UserSettingsStore()) 209 } 210 }