diff --git a/ElementX/Sources/Screens/AppLock/AppLockScreen/View/AppLockScreenPINKeypad.swift b/ElementX/Sources/Screens/AppLock/AppLockScreen/View/AppLockScreenPINKeypad.swift index fa5726daa..4855113e9 100644 --- a/ElementX/Sources/Screens/AppLock/AppLockScreen/View/AppLockScreenPINKeypad.swift +++ b/ElementX/Sources/Screens/AppLock/AppLockScreen/View/AppLockScreenPINKeypad.swift @@ -10,6 +10,7 @@ import SwiftUI /// The custom keypad shown on the App Lock screen when biometrics are disabled. struct AppLockScreenPINKeypad: View { @Binding var pinCode: String + @FocusState private var isFocused var body: some View { Grid(horizontalSpacing: 24, verticalSpacing: 16) { @@ -36,6 +37,30 @@ struct AppLockScreenPINKeypad: View { } } .buttonStyle(KeypadButtonStyle()) + .focusable() + .focused($isFocused) + .onKeyPress(keys: Self.supportedKeys) { keyPress in + guard keyPress.key != .delete else { + // Since the key press event is handled through a view update we need to update the view on the next main loop + DispatchQueue.main.async { + pressDelete() + } + return .handled + } + + guard let digit = Int(keyPress.characters) else { + return .ignored + } + + // Since the key press event is handled through a view update we need to update the view on the next main loop + DispatchQueue.main.async { + press(digit) + } + return .handled + } + .onAppear { + isFocused = true + } } func press(_ digit: Int) { @@ -66,6 +91,20 @@ private struct KeypadButtonStyle: ButtonStyle { } } +private extension AppLockScreenPINKeypad { + static let supportedKeys: Set = [.init("0"), + .init("1"), + .init("2"), + .init("3"), + .init("4"), + .init("5"), + .init("6"), + .init("7"), + .init("8"), + .init("9"), + .delete] +} + // MARK: - Previews struct AppLockScreenPINKeypad_Previews: PreviewProvider {