Skip to content

Commit

Permalink
Fix shake passcode view animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Sep 29, 2023
1 parent cb5dedc commit 4f873bc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct SetPasscodeView: View {
passcode: $viewModel.passcode,
biometryType: Binding(get: { nil }, set: { _ in }),
lockoutState: Binding(get: { .unlocked(attemptsLeft: Int.max, maxAttempts: Int.max) }, set: { _ in }),
shakeTrigger: $viewModel.shakeTrigger,
randomEnabled: false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SetPasscodeViewModel: ObservableObject {
}
}
}
@Published var shakeTrigger: Int = 0

let passcodeManager: PasscodeManager

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct PasscodeView: View {

@Binding var biometryType: BiometryType?
@Binding var lockoutState: LockoutState
@Binding var shakeTrigger: Int
let randomEnabled: Bool
var onTapBiometry: (() -> Void)? = nil

Expand All @@ -33,7 +34,7 @@ struct PasscodeView: View {
VStack {
VStack {
switch lockoutState {
case let .unlocked(attemptsLeft, _):
case .unlocked:
Text(description)
.font(.themeSubhead2)
.foregroundColor(.themeGray)
Expand All @@ -50,9 +51,9 @@ struct PasscodeView: View {
.frame(width: .margin12, height: .margin12)
}
}
.modifier(Shake(animatableData: CGFloat(attemptsLeft)))
.modifier(Shake(animatableData: CGFloat(shakeTrigger)))
.padding(.vertical, .margin16)
.animation(.linear(duration: 0.3), value: attemptsLeft)
.animation(.linear(duration: 0.3), value: shakeTrigger)
.animation(.easeOut(duration: 0.1), value: passcode)

Text(errorText)
Expand Down Expand Up @@ -108,14 +109,14 @@ struct PasscodeView: View {
.padding(.bottom, .margin32)
}
}
}

struct Shake: GeometryEffect {
var amount: CGFloat = 8
var shakesPerUnit = 4
var animatableData: CGFloat
private struct Shake: GeometryEffect {
var amount: CGFloat = 8
var shakesPerUnit = 4
var animatableData: CGFloat

func effectValue(size _: CGSize) -> ProjectionTransform {
ProjectionTransform(CGAffineTransform(translationX: amount * sin(animatableData * .pi * CGFloat(shakesPerUnit)), y: 0))
func effectValue(size _: CGSize) -> ProjectionTransform {
ProjectionTransform(CGAffineTransform(translationX: amount * sin(animatableData * .pi * CGFloat(shakesPerUnit)), y: 0))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BaseUnlockViewModel: ObservableObject {
syncErrorText()
}
}
@Published var shakeTrigger: Int = 0

let finishSubject = PassthroughSubject<Void, Never>()
let unlockWithBiometrySubject = PassthroughSubject<Void, Never>()
Expand Down Expand Up @@ -89,6 +90,8 @@ class BaseUnlockViewModel: ObservableObject {
} else {
passcode = ""
lockoutManager.didFailUnlock()

shakeTrigger += 1
UINotificationFeedbackGenerator().notificationOccurred(.error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct UnlockView: View {
passcode: $viewModel.passcode,
biometryType: $viewModel.resolvedBiometryType,
lockoutState: $viewModel.lockoutState,
shakeTrigger: $viewModel.shakeTrigger,
randomEnabled: true,
onTapBiometry: {
unlockWithBiometry()
Expand Down

0 comments on commit 4f873bc

Please sign in to comment.