-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathTotpView.swift
86 lines (76 loc) · 2.45 KB
/
TotpView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// TotpView.swift
// Strongbox
//
// Created by Strongbox on 29/07/2024.
// Copyright © 2024 Mark McGuill. All rights reserved.
//
import Combine
import SwiftUI
struct TotpView: View {
var totp: OTPToken
var easyReadSeparator: Bool
@State private var totpString: String? = nil
@State private var totpColor: Color = .primary
@State private var animationOpacity = 1.0
#if DEBUG
var previewDummyTestTimer: AnyPublisher<Date, Never> = Combine.Empty<Date, Never>(completeImmediately: false).eraseToAnyPublisher()
#endif
var body: some View {
if #available(iOS 16.0, *) {
HStack {
TwoFactorCodeView(totp: totp,
updateMode: .automatic,
easyReadSeparator: easyReadSeparator,
isPro: true,
limitNonPro: false,
font: Font(FontManager.sharedInstance().easyReadBoldFont),
separatorSize: 4, colorize: true)
}
} else {
Text(totpString ?? "")
.foregroundStyle(totpColor)
.opacity(animationOpacity)
.onAppear(perform: {
updateTotp()
})
.onReceive(NotificationCenter.default.publisher(for: .centralUpdateOtpUi, object: nil)) { _ in
updateTotp()
}
#if DEBUG
.onReceive(previewDummyTestTimer) { _ in
updateTotp()
}
#endif
}
}
func updateTotp() {
totpString = totp.codeDisplayString
if let color = totp.color {
#if os(iOS)
totpColor = Color(uiColor: color)
#else
totpColor = Color(nsColor: color)
#endif
} else {
totpColor = .blue
}
if totp.remainingSeconds < 10 {
withAnimation(.easeOut(duration: 0.65).repeatForever()) {
animationOpacity = 0.35
}
} else {
withAnimation {
animationOpacity = 1
}
}
}
}
#if DEBUG
#Preview {
let timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common)
.autoconnect()
.eraseToAnyPublisher()
return TotpView(totp: OTPToken(url: URL(string: "otpauth:
}
#endif