Sesame-iOS/Sesame-Watch Watch App/UnlockView.swift

84 lines
2.4 KiB
Swift
Raw Permalink Normal View History

2023-08-07 15:57:09 +02:00
import SwiftUI
2023-12-12 17:33:42 +01:00
import SwiftData
2023-08-09 16:29:18 +02:00
import SFSafeSymbols
2023-08-07 15:57:09 +02:00
import CryptoKit
import UIKit
2023-08-07 15:57:09 +02:00
struct ContentView: View {
@Binding
var didLaunchFromComplication: Bool
2023-08-14 10:39:29 +02:00
2023-12-12 17:33:42 +01:00
@ObservedObject
var coordinator: RequestCoordinator
2023-08-07 15:57:09 +02:00
2023-12-12 17:33:42 +01:00
init(coordinator: RequestCoordinator, didLaunchFromComplication: Binding<Bool>) {
self._didLaunchFromComplication = didLaunchFromComplication
self.coordinator = coordinator
2023-08-14 10:39:29 +02:00
}
2023-08-09 16:29:18 +02:00
2023-08-07 15:57:09 +02:00
var buttonBackground: Color {
2023-12-12 17:33:42 +01:00
.white.opacity(0.2)
2023-08-07 15:57:09 +02:00
}
var buttonColor: Color {
2023-12-12 17:33:42 +01:00
.white
2023-08-07 15:57:09 +02:00
}
private var stateText: String {
if coordinator.state == .notChecked {
return "Unlock"
}
return coordinator.state.description
}
2023-08-07 15:57:09 +02:00
var body: some View {
2023-08-09 16:29:18 +02:00
HStack {
2023-08-07 15:57:09 +02:00
Spacer()
2023-08-09 16:29:18 +02:00
VStack(alignment: .center) {
Image(systemSymbol: .lock)
.resizable()
.aspectRatio(contentMode: .fit)
.fontWeight(.ultraLight)
.padding()
2023-12-31 15:24:20 +01:00
.onTapGesture { coordinator.startUnlock() }
2023-12-12 17:33:42 +01:00
if coordinator.isPerformingRequest {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.frame(width: 20, height: 20)
} else {
Text(stateText)
.font(.subheadline)
}
2023-08-07 15:57:09 +02:00
}
Spacer()
}
2023-12-12 17:33:42 +01:00
.background(coordinator.state.color)
.animation(.easeInOut, value: coordinator.state.color)
.onChange(of: didLaunchFromComplication) { _, launched in
guard launched else {
return
}
2023-12-31 15:24:20 +01:00
coordinator.startUnlock(quitAfterSuccess: true)
didLaunchFromComplication = false
}
2023-08-07 15:57:09 +02:00
}
2023-08-14 10:39:29 +02:00
}
2023-12-12 17:33:42 +01:00
#Preview {
do {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: HistoryItem.self, configurations: config)
let item = HistoryItem.mock
container.mainContext.insert(item)
try container.mainContext.save()
let coordinator = RequestCoordinator(modelContext: container.mainContext)
return ContentView(coordinator: coordinator, didLaunchFromComplication: .constant(false))
.modelContainer(container)
} catch {
fatalError("Failed to create model container.")
2023-08-14 10:39:29 +02:00
}
}