receive the app hash from Nugget pc

This commit is contained in:
leminlimez committed 2025-06-03 14:46:19 -04:00
1 parent fb054a5d19
commit b0db8da590
1 file changed
+51
+51
View File
@@ -11,6 +11,9 @@ struct SettingsView: View {
// Prefs
@AppStorage("pbHash") var pbHash: String = ""
@State var checkingForHash: Bool = false
@State var hashCheckTask: Task<Void, any Error>? = nil
@State var showErrorAlert = false
@State var errorAlertTitle: String?
@State var errorAlertDescr: String?
@@ -19,10 +22,26 @@ struct SettingsView: View {
ScrollView {
Section {
HStack {
// Add button to run task to check until file exists from Nugget pc over AFC
Text("App Hash:")
.font(.headline)
.foregroundColor(.primary)
Spacer()
Button(action: {
UIApplication.shared.confirmAlert(title: "Waiting for app hash...", body: "Connect your device to Nugget and click the \"Pocket Poster Helper\" button.", confirmTitle: "Cancel", onOK: {
cancelWaitForHash()
}, noCancel: true)
startWaitForHash()
}) {
Text("Detect")
}
.buttonStyle(TintedButton(color: .green, fullwidth: false))
.onChange(of: checkingForHash) {
if !checkingForHash {
// hide ui
UIApplication.shared.dismissAlert(animated: true)
}
}
}
TextField("Enter App Hash", text: $pbHash)
.textFieldStyle(RoundedBorderTextFieldStyle())
@@ -92,4 +111,36 @@ struct SettingsView: View {
Text(errorAlertDescr ?? "???")
}
}
func startWaitForHash() {
checkingForHash = true
hashCheckTask = Task {
let filePath = SymHandler.getDocumentsDirectory().appendingPathComponent("NuggetAppHash")
while !FileManager.default.fileExists(atPath: filePath.path()) {
try? await Task.sleep(nanoseconds: 500_000_000) // Sleep 0.5s
try Task.checkCancellation()
}
do {
let contents = try String(contentsOf: filePath)
try? FileManager.default.removeItem(at: filePath)
await MainActor.run {
pbHash = contents
}
} catch {
print(error.localizedDescription)
}
await MainActor.run {
checkingForHash = false
hashCheckTask = nil
}
}
}
func cancelWaitForHash() {
hashCheckTask?.cancel()
hashCheckTask = nil
checkingForHash = false
}
}