use qr code scanner + behavior
import SwiftUI
+import AVFoundation
import PhoenixShared
import UIKit
@@ -9,21 +10,27 @@ struct ScanView: MVIView {
@Binding var isShowing: Bool
+ @StateObject var toast = Toast()
+
var body: some View {
+ ZStack {
mvi(onModel: { model in
if model is Scan.ModelSending {
isShowing = false
+ } else if model is Scan.ModelBadRequest {
+ toast.toast(text: "Unexpected request format!")
}
}) { model, intent in
view(model: model, intent: intent)
}
- .navigationBarTitle("", displayMode: .inline)
+ toast.view()
+ }
}
@ViewBuilder
func view(model: Scan.Model, intent: @escaping IntentReceiver) -> some View {
switch model {
- case _ as Scan.ModelReady: ReadyView(intent: intent)
+ case _ as Scan.ModelReady, _ as Scan.ModelBadRequest: ReadyView(intent: intent)
case let m as Scan.ModelValidate: ValidateView(model: m, intent: intent)
case let m as Scan.ModelSending: SendingView(model: m)
default:
@@ -34,29 +41,41 @@ struct ScanView: MVIView {
struct ReadyView: View {
let intent: IntentReceiver
- @State var request: String = ""
-
var body: some View {
VStack {
- Text("Payment Request:")
- .font(.title)
- .padding()
- TextEditor(text: $request)
+ Spacer()
+ Text("Scan a QR code")
.padding()
+ .font(.title2)
+
+ QrCodeScannerView().found { request in
+ print(request)
+ intent(Scan.IntentParse(request: request))
+ }
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 4)
)
.padding()
+
+ Divider()
+ .padding([.leading, .trailing])
+
Button {
+ if let request = UIPasteboard.general.string {
intent(Scan.IntentParse(request: request))
+ }
} label: {
- Text("OK")
- .font(.title)
+ Image(systemName: "arrow.right.doc.on.clipboard")
+ Text("Paste from clipboard")
+ .font(.title2)
}
- .disabled(request.isEmpty)
+ .disabled(!UIPasteboard.general.hasStrings)
.padding()
+ Spacer()
}
+ .navigationBarTitle("Payment request", displayMode: .inline)
+
}
}
@@ -66,6 +85,7 @@ struct ScanView: MVIView {
let intent: IntentReceiver
var body: some View {
+ VStack {
Text("\(model.amountMsat ?? 0) msat")
.font(.title)
.padding()
@@ -80,7 +100,8 @@ struct ScanView: MVIView {
.font(.title)
.padding()
}
-
+ }
+ .navigationBarTitle("Pay", displayMode: .inline)
}
}
@@ -88,12 +109,15 @@ struct ScanView: MVIView {
let model: Scan.ModelSending
var body: some View {
+ VStack {
Text("Sending \(model.amountMsat) msat...")
.font(.title)
.padding()
Text(model.requestDescription ?? "")
.padding()
+ }
+ .navigationBarTitle("Sending payment", displayMode: .inline)
}
}
}
