twostraws / CodeScanner

A SwiftUI view that is able to scan barcodes, QR codes, and more, and send back what was found.
MIT License
1.01k stars 286 forks source link

Have to kill the app between scans #94

Closed oefterdal closed 1 year ago

oefterdal commented 1 year ago

I have to kill the app between scans, if I Dismiss the sheet and try to perform a new scan it won't scan.

    var body: some View {
        ZStack {
            Rectangle()
                .fill(Color.white)
                .frame(width: 300, height: 100)
                .cornerRadius(20)
                .overlay(
            CodeScannerView(codeTypes: [.ean8, .ean13], showViewfinder: true) { result in
                switch result {
                case .success(let code):
                    self.scannedResult = code.string
                    self.checkResult(result: self.scannedResult!)
                    self.showResult = true
                case .failure(let error):
                    print(error.localizedDescription)
                }
            })
            .sheet(isPresented: $showResult) {
                ResultView(scannedResult: self.$scannedResult, showResult: self.$showResult)
            }
        }
    }
}

struct ResultView: View {
    @Binding var scannedResult: String?
    @Binding var showResult: Bool

    var body: some View {
        VStack {
            Text("Scanned Result")
                .font(.headline)
            if let result = scannedResult {
                Text(result)
                    .font(.body)
            } else {
                Text("No result found")
                    .font(.body)
            }
            Button("Dismiss") {
                self.scannedResult = nil
                self.showResult = false
            }
        }
        .padding()
    }
}
nathanfallet commented 1 year ago

Duplicate of #98