From c006b8150f5327f48071d8e52ad96e3b383f122e Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Wed, 8 Feb 2023 13:39:41 +0300 Subject: [PATCH] ios: ask permission to open profiles (#1910) --- .../Views/Helpers/LocalAuthenticationUtils.swift | 10 ++++++++++ apps/ios/Shared/Views/TerminalView.swift | 10 +--------- .../Shared/Views/UserSettings/SettingsView.swift | 1 - .../Views/UserSettings/UserProfilesView.swift | 13 +++++++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/apps/ios/Shared/Views/Helpers/LocalAuthenticationUtils.swift b/apps/ios/Shared/Views/Helpers/LocalAuthenticationUtils.swift index 4776f660c3..fea1c9843d 100644 --- a/apps/ios/Shared/Views/Helpers/LocalAuthenticationUtils.swift +++ b/apps/ios/Shared/Views/Helpers/LocalAuthenticationUtils.swift @@ -15,6 +15,16 @@ enum LAResult { case unavailable(authError: String?) } +func authorize(_ text: String, _ authorized: Binding) { + authenticate(reason: text) { laResult in + switch laResult { + case .success: authorized.wrappedValue = true + case .unavailable: authorized.wrappedValue = true + case .failed: authorized.wrappedValue = false + } + } +} + func authenticate(reason: String, completed: @escaping (LAResult) -> Void) { let laContext = LAContext() var authAvailabilityError: NSError? diff --git a/apps/ios/Shared/Views/TerminalView.swift b/apps/ios/Shared/Views/TerminalView.swift index ee97f14bfe..5b063e8bae 100644 --- a/apps/ios/Shared/Views/TerminalView.swift +++ b/apps/ios/Shared/Views/TerminalView.swift @@ -30,15 +30,7 @@ struct TerminalView: View { } } - private func runAuth() { - authenticate(reason: NSLocalizedString("Open chat console", comment: "authentication reason")) { laResult in - switch laResult { - case .success: authorized = true - case .unavailable: authorized = true - case .failed: authorized = false - } - } - } + private func runAuth() { authorize(NSLocalizedString("Open chat console", comment: "authentication reason"), $authorized) } private func terminalView() -> some View { VStack { diff --git a/apps/ios/Shared/Views/UserSettings/SettingsView.swift b/apps/ios/Shared/Views/UserSettings/SettingsView.swift index a05a43ad21..cb08dab92f 100644 --- a/apps/ios/Shared/Views/UserSettings/SettingsView.swift +++ b/apps/ios/Shared/Views/UserSettings/SettingsView.swift @@ -121,7 +121,6 @@ struct SettingsView: View { NavigationLink { UserProfilesView() - .navigationTitle("Your chat profiles") } label: { settingsRow("person.crop.rectangle.stack") { Text("Your chat profiles") } } diff --git a/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift b/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift index d41982b706..d78b7ea665 100644 --- a/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift +++ b/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift @@ -12,6 +12,7 @@ struct UserProfilesView: View { @State private var showDeleteConfirmation = false @State private var userToDelete: Int? @State private var alert: UserProfilesAlert? + @State var authorized = !UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA) private enum UserProfilesAlert: Identifiable { case deleteUser(index: Int, delSMPQueues: Bool) @@ -28,6 +29,17 @@ struct UserProfilesView: View { } var body: some View { + if authorized { + userProfilesView() + } else { + Button(action: runAuth) { Label("Unlock", systemImage: "lock") } + .onAppear(perform: runAuth) + } + } + + private func runAuth() { authorize(NSLocalizedString("Open user profiles", comment: "authentication reason"), $authorized) } + + private func userProfilesView() -> some View { List { Section { ForEach(m.users) { u in @@ -52,6 +64,7 @@ struct UserProfilesView: View { } } .toolbar { EditButton() } + .navigationTitle("Your chat profiles") .confirmationDialog("Delete chat profile?", isPresented: $showDeleteConfirmation, titleVisibility: .visible) { deleteModeButton("Profile and server connections", true) deleteModeButton("Local profile data only", false)