taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

commit 7ed1053d809cbfaefaa1bb744631b3e5ac144b99
parent e06d4ead7e8ae287448755f54ba22a384b066af4
Author: Marc Stibane <marc@taler.net>
Date:   Sat, 12 Oct 2024 19:07:52 +0200

QRButton gets camera API

Diffstat:
MTalerWallet1/Views/Actions/ActionsSheet.swift | 1-
MTalerWallet1/Views/HelperViews/Buttons.swift | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++++-
MTalerWallet1/Views/Main/MainView.swift | 71++++++++++-------------------------------------------------------------
3 files changed, 65 insertions(+), 63 deletions(-)

diff --git a/TalerWallet1/Views/Actions/ActionsSheet.swift b/TalerWallet1/Views/Actions/ActionsSheet.swift @@ -19,7 +19,6 @@ struct ActionsSheet: View { @Binding var showSpendingHint: Bool @Binding var amountToTransfer: Amount // does still have the wrong currency @Binding var summary: String - let cameraAction: () -> Void @AppStorage("minimalistic") var minimalistic: Bool = false @AppStorage("hideSpendingHint") var hideSpendingHint: Bool = false diff --git a/TalerWallet1/Views/HelperViews/Buttons.swift b/TalerWallet1/Views/HelperViews/Buttons.swift @@ -4,6 +4,7 @@ */ import SwiftUI import Foundation +import AVFoundation extension ShapeStyle where Self == Color { static var random: Color { @@ -62,11 +63,59 @@ struct QRButton : View { let action: () -> Void @AppStorage("minimalistic") var minimalistic: Bool = false + @State private var showCameraAlert: Bool = false + + private var openSettingsButton: some View { + Button("Open Settings") { + showCameraAlert = false + UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) + } + } + let closingAnnouncement = AttributedString(localized: "Closing Camera") + private var dismissAlertButton: some View { + Button("Cancel", role: .cancel) { + if #available(iOS 17.0, *) { + AccessibilityNotification.Announcement(closingAnnouncement).post() + } + showCameraAlert = false + } + } + var defaultPriorityAnnouncement = AttributedString(localized: "Opening Camera") + var lowPriorityAnnouncement: AttributedString { + var lowPriorityString = AttributedString ("Camera Loading") + if #available(iOS 17.0, *) { + lowPriorityString.accessibilitySpeechAnnouncementPriority = .low + } + return lowPriorityString + } + var highPriorityAnnouncement: AttributedString { + var highPriorityString = AttributedString("Camera Active") + if #available(iOS 17.0, *) { + highPriorityString.accessibilitySpeechAnnouncementPriority = .high + } + return highPriorityString + } + private func checkCameraAvailable() -> Void { + // Open Camera when QR-Button was tapped + if #available(iOS 17.0, *) { + AccessibilityNotification.Announcement(defaultPriorityAnnouncement).post() + } + AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) -> Void in + if granted { + action() + if #available(iOS 17.0, *) { + AccessibilityNotification.Announcement(highPriorityAnnouncement).post() + } + } else { + showCameraAlert = true + } + }) + } var body: some View { let scanText = String(localized: "Scan QR codes", comment: "Button title, a11y") let qr = "qrcode.viewfinder" - Button(action: action) { + Button(action: checkCameraAvailable) { if isNavBarItem { if !minimalistic { Text("Scan QR", comment: "Button title") @@ -89,6 +138,11 @@ struct QRButton : View { } } .accessibilityLabel(scanText) + .alert("Scanning QR-codes requires access to the camera", + isPresented: $showCameraAlert, + actions: { openSettingsButton + dismissAlertButton }, + message: { Text("Please allow camera access in settings.") }) // Scanning QR-codes } } diff --git a/TalerWallet1/Views/Main/MainView.swift b/TalerWallet1/Views/Main/MainView.swift @@ -44,63 +44,18 @@ struct MainView: View { @State private var urlToOpen: URL? = nil @State private var showQRScanner: Bool = false @State private var showCameraAlert: Bool = false + @State private var showActionSheet = false + @State private var showScanner = false func sheetDismissed() -> Void { logger.info("sheet dismiss") ViewState.shared.popToRootView(nil) } - private var openSettingsButton: some View { - Button("Open Settings") { - showCameraAlert = false - UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) - } - } - let ClosingAnnouncement = AttributedString(localized: "Closing Camera") - private var dismissAlertButton: some View { - Button("Cancel", role: .cancel) { - if #available(iOS 17.0, *) { - AccessibilityNotification.Announcement(ClosingAnnouncement).post() - } - showCameraAlert = false - } - } private func dismissingSheet() { - if #available(iOS 17.0, *) { - AccessibilityNotification.Announcement(ClosingAnnouncement).post() - } - } - - var defaultPriorityAnnouncement = AttributedString(localized: "Opening Camera") - var lowPriorityAnnouncement: AttributedString { - var lowPriorityString = AttributedString ("Camera Loading") - if #available(iOS 17.0, *) { - lowPriorityString.accessibilitySpeechAnnouncementPriority = .low - } - return lowPriorityString - } - var highPriorityAnnouncement: AttributedString { - var highPriorityString = AttributedString("Camera Active") - if #available(iOS 17.0, *) { - highPriorityString.accessibilitySpeechAnnouncementPriority = .high - } - return highPriorityString - } - private func checkCameraAvailable() -> Void { - // Open Camera when QR-Button was tapped - if #available(iOS 17.0, *) { - AccessibilityNotification.Announcement(defaultPriorityAnnouncement).post() - } - AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) -> Void in - if granted { - showQRScanner = true // present sheet - if #available(iOS 17.0, *) { - AccessibilityNotification.Announcement(highPriorityAnnouncement).post() - } - } else { - showCameraAlert = true - } - }) +// if #available(iOS 17.0, *) { +// AccessibilityNotification.Announcement(ClosingAnnouncement).post() +// } } var body: some View { @@ -114,7 +69,8 @@ struct MainView: View { balances: $balances, selectedBalance: $selectedBalance, talerFontIndex: $talerFontIndex, - cameraAction: checkCameraAvailable) + showActionSheet: $showActionSheet, + showScanner: $showScanner) .onAppear() { #if DEBUG if playSoundsI != 0 && playSoundsB && !soundPlayed { @@ -123,11 +79,6 @@ struct MainView: View { #endif soundPlayed = true } - .alert("Scanning QR-codes requires access to the camera", - isPresented: $showCameraAlert, - actions: { openSettingsButton - dismissAlertButton }, - message: { Text("Please allow camera access in settings.") }) // Scanning QR-codes .sheet(isPresented: $showQRScanner, onDismiss: dismissingSheet) { let sheet = AnyView(QRSheet(stack: stack.push(".sheet"), balances: $balances, @@ -233,7 +184,8 @@ extension MainView { @Binding var balances: [Balance] @Binding var selectedBalance: Balance? @Binding var talerFontIndex: Int - let cameraAction: () -> Void + @Binding var showActionSheet: Bool + @Binding var showScanner: Bool #if DEBUG @AppStorage("developerMode") var developerMode: Bool = true @@ -254,7 +206,6 @@ extension MainView { @State private var showKycAlert: Bool = false @State private var kycURI: URL? - @State private var showActionSheet = false @State private var sheetHeight: CGFloat = .zero @State private var amountToTransfer = Amount.zero(currency: EMPTYSTRING) // Update currency when used @State private var amountLastUsed = Amount.zero(currency: EMPTYSTRING) // Update currency when used @@ -392,7 +343,6 @@ extension MainView { .onNotification(.RequestAction) { navModel.actionSelected = 2 } .onNotification(.DepositAction) { navModel.actionSelected = 3 } .onNotification(.WithdrawAction){ navModel.actionSelected = 4 } - .onNotification(.QrScanAction) { cameraAction() } }.id(viewState.rootViewId) // any change to rootViewId triggers popToRootView behaviour .navigationViewStyle(.stack) .tag(Tab.balances) @@ -440,8 +390,7 @@ extension MainView { balances: $balances, showSpendingHint: $showSpendingHint, amountToTransfer: $amountToTransfer, - summary: $summary, - cameraAction: cameraAction) + summary: $summary) .padding(.bottom, 32) } if #available(iOS 16, *) {