Repository navigation
Expand file tree
/
Copy pathContentView.swift
More file actions
70 lines (62 loc) · 2.53 KB
/
Copy pathContentView.swift
File metadata and controls
70 lines (62 loc) · 2.53 KB
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
import SwiftUI
struct ContentView: View {
/// The name of a flow in your Flux account, exactly as in the Flux web app.
@AppStorage("flowName") private var flowName = ""
/// Optional: a barcode your app already knows, stored on the Flux item.
@State private var code = ""
@State private var result: FluxLink.Result?
@Environment(\.openURL) private var openURL
/// Registered in Info.plist (CFBundleURLSchemes). Use your own scheme or universal link.
private let callback = URL(string: "fluxexample://result")!
var body: some View {
NavigationStack {
Form {
Section("Request") {
TextField("Flow name", text: $flowName)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
TextField("Barcode (optional)", text: $code)
Button("Start scan in Flux") {
openURL(FluxLink.openURL(flowName: flowName, callback: callback, code: code.isEmpty ? nil : code))
}
.disabled(flowName.isEmpty)
}
if let result {
Section("Result") { ResultView(result: result) }
}
}
.navigationTitle("Flux integration")
}
.onOpenURL { url in
result = FluxLink.Result(url: url)
}
}
}
private struct ResultView: View {
let result: FluxLink.Result
var body: some View {
switch result {
case .completed(let json):
LabeledContent("Status", value: "completed")
if let d = FluxLink.dimensions(in: json) {
LabeledContent("Dimensions", value: "\(d.width) × \(d.height) × \(d.depth) \(d.unit)")
}
Text(prettyPrinted(json))
.font(.caption.monospaced())
.textSelection(.enabled)
case .cancelled:
LabeledContent("Status", value: "cancelled")
case .error(let reason):
LabeledContent("Status", value: "error")
LabeledContent("Reason", value: reason)
}
}
private func prettyPrinted(_ json: Data) -> String {
guard let object = try? JSONSerialization.jsonObject(with: json),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .sortedKeys]) else {
return String(decoding: json, as: UTF8.self)
}
return String(decoding: data, as: UTF8.self)
}
}
#Preview { ContentView() }