88 lines
2.5 KiB
Swift
88 lines
2.5 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
import SFSafeSymbols
|
|
import CryptoKit
|
|
import UIKit
|
|
|
|
struct ContentView: View {
|
|
|
|
@Binding
|
|
var didLaunchFromComplication: Bool
|
|
|
|
@ObservedObject
|
|
var coordinator: RequestCoordinator
|
|
|
|
init(coordinator: RequestCoordinator, didLaunchFromComplication: Binding<Bool>) {
|
|
self._didLaunchFromComplication = didLaunchFromComplication
|
|
self.coordinator = coordinator
|
|
}
|
|
|
|
var buttonBackground: Color {
|
|
.white.opacity(0.2)
|
|
}
|
|
|
|
var buttonColor: Color {
|
|
.white
|
|
}
|
|
|
|
private var stateText: String {
|
|
if coordinator.state == .notChecked {
|
|
return "Unlock"
|
|
}
|
|
return coordinator.state.description
|
|
}
|
|
|
|
var body: some View {
|
|
HStack {
|
|
Spacer()
|
|
VStack(alignment: .center) {
|
|
Image(systemSymbol: .lock)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.fontWeight(.ultraLight)
|
|
.padding()
|
|
.onTapGesture { unlock(quit: true) }
|
|
if coordinator.isPerformingRequest {
|
|
ProgressView()
|
|
.progressViewStyle(CircularProgressViewStyle())
|
|
.frame(width: 20, height: 20)
|
|
} else {
|
|
Text(stateText)
|
|
.font(.subheadline)
|
|
}
|
|
}
|
|
Spacer()
|
|
}
|
|
.background(coordinator.state.color)
|
|
.animation(.easeInOut, value: coordinator.state.color)
|
|
.onChange(of: didLaunchFromComplication) { _, launched in
|
|
guard launched else {
|
|
return
|
|
}
|
|
unlock(quit: true)
|
|
didLaunchFromComplication = false
|
|
}
|
|
}
|
|
|
|
private func unlock(quit: Bool) {
|
|
coordinator.startUnlock(quitAfterSuccess: quit)
|
|
}
|
|
}
|
|
|
|
#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.")
|
|
}
|
|
}
|
|
|