FirstAidSettingsView.swift (4861B)
1 // 2 // FirstAidSettingsView.swift 3 // damus 4 // 5 // Created by Daniel D’Aquino on 2024-04-19. 6 // 7 8 import SwiftUI 9 10 struct FirstAidSettingsView: View { 11 let damus_state: DamusState 12 @ObservedObject var settings: UserSettingsStore 13 @State var reset_contact_list_state: ContactListResetState = .not_started 14 15 enum ContactListResetState: Equatable { 16 case not_started 17 case confirming_with_user 18 case error(String) 19 case in_progress 20 case completed 21 } 22 23 24 var body: some View { 25 Form { 26 if damus_state.contacts.event == nil { 27 Section( 28 header: Text(NSLocalizedString("Contact list (Follows + Relay list)", comment: "Section title for Contact list first aid tools")), 29 footer: Text(NSLocalizedString("No contact list was found. You might experience issues using the app. If you suspect you have permanently lost your contact list (or if you never had one), you can fix this by resetting it", comment: "Section footer for Contact list first aid tools")) 30 ) { 31 Button(action: { 32 reset_contact_list_state = .confirming_with_user 33 }, label: { 34 HStack(spacing: 6) { 35 switch reset_contact_list_state { 36 case .not_started, .error: 37 Label(NSLocalizedString("Reset contact list", comment: "Button to reset contact list."), image: "broom") 38 .frame(maxWidth: .infinity, alignment: .leading) 39 .foregroundColor(.red) 40 case .confirming_with_user, .in_progress: 41 ProgressView() 42 Text(NSLocalizedString("In progress…", comment: "Loading message indicating that a contact list reset operation is in progress.")) 43 case .completed: 44 Image(systemName: "checkmark.circle.fill") 45 .foregroundColor(.green) 46 Text(NSLocalizedString("Contact list has been reset", comment: "Message indicating that the contact list was successfully reset.")) 47 } 48 } 49 }) 50 .disabled(reset_contact_list_state == .in_progress || reset_contact_list_state == .completed) 51 52 if case let .error(error_message) = reset_contact_list_state { 53 Text(error_message) 54 .foregroundStyle(.red) 55 } 56 } 57 .alert(NSLocalizedString("WARNING:\n\nThis will reset your contact list, including the list of everyone you follow and the list of all relays you usually connect to. ONLY PROCEED IF YOU ARE SURE YOU HAVE LOST YOUR CONTACT LIST BEYOND RECOVERABILITY.", comment: "Alert for resetting the user's contact list."), 58 isPresented: Binding(get: { reset_contact_list_state == .confirming_with_user }, set: { _ in return }) 59 ) { 60 Button(NSLocalizedString("Cancel", comment: "Cancel resetting the contact list."), role: .cancel) { 61 reset_contact_list_state = .not_started 62 } 63 Button(NSLocalizedString("Continue", comment: "Continue with resetting the contact list.")) { 64 guard let new_contact_list_event = make_first_contact_event(keypair: damus_state.keypair) else { 65 reset_contact_list_state = .error(NSLocalizedString("An unexpected error happened while trying to create the new contact list. Please contact support.", comment: "Error message for a failed contact list reset operation")) 66 return 67 } 68 damus_state.pool.send(.event(new_contact_list_event)) 69 reset_contact_list_state = .completed 70 } 71 } 72 } 73 74 if damus_state.contacts.event != nil { 75 Text(NSLocalizedString("We did not detect any issues that we can automatically fix for you. If you are having issues, please contact Damus support", comment: "Message indicating that no First Aid actions are available.")) 76 } 77 } 78 .navigationTitle(NSLocalizedString("First Aid", comment: "Navigation title for first aid settings and tools")) 79 } 80 } 81 82 #Preview { 83 FirstAidSettingsView(damus_state: test_damus_state, settings: test_damus_state.settings) 84 }