diff --git a/Example/ExampleCodeFromScanner/ContentView.swift b/Example/ExampleCodeFromScanner/ContentView.swift index 0ce13a2..5b4e09a 100644 --- a/Example/ExampleCodeFromScanner/ContentView.swift +++ b/Example/ExampleCodeFromScanner/ContentView.swift @@ -26,7 +26,9 @@ struct ContentView: View { } } .fullScreenCover(isPresented: $isEditingProduct) { - ProductPage(barcode: $barcode).onDisappear() { + ProductPage(barcode: barcode) { uploadedProduct in + print(uploadedProduct ?? "") + }.onDisappear() { isScanning = true } } diff --git a/Example/ExampleWithCode/ContentView.swift b/Example/ExampleWithCode/ContentView.swift index abc0885..dc12e22 100644 --- a/Example/ExampleWithCode/ContentView.swift +++ b/Example/ExampleWithCode/ContentView.swift @@ -14,7 +14,6 @@ struct ContentView: View { @State var barcode: String = "" @State var isValidBarcode: Bool = false - @State var submitProduct: [String: String]? = nil var body: some View { NavigationView { @@ -28,14 +27,15 @@ struct ContentView: View { isValidBarcode = newValue.isAValidBarcode() print("\(barcode) and \(isValidBarcode)") } - .onChange(of: submitProduct) { newValue in - print(submitProduct ?? "") - } Image(systemName: isValidBarcode ? "checkmark" : "exclamationmark.octagon.fill") .renderingMode(.template).foregroundColor(isValidBarcode ? .green : .red) }.padding() // Added for testing that editor is loaded with NavigatorView - NavigationLink("Check", destination: ProductPage(barcode: self.$barcode, submitProduct: $submitProduct)).disabled(!isValidBarcode) + NavigationLink("Check") { + ProductPage(barcode: self.barcode) { product in + print(product ?? "") + }.disabled(!isValidBarcode) + } Spacer() } } diff --git a/README.md b/README.md index b212444..5a9d70b 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,10 @@ existing 5900102025473 [pl] / 8711258029584 [nl], missing 5701377101134 [pl] SDK has screen to view or submit product ``` -ProductPage(barcode: $barcode, isPresented: $isProductEditorPresent, submitProduct: $submitProduct) -``` +ProductPage(barcode: barcode) { uploadedProduct in -Before submitting new product you may create State reference `@State var submitProduct: [String: String]? = nil` and receive uploaded product by passing `submitProduct`, this is optional parameter +} +``` ### Submit new diff --git a/Sources/ProductPage.swift b/Sources/ProductPage.swift index e8678c3..351c7af 100644 --- a/Sources/ProductPage.swift +++ b/Sources/ProductPage.swift @@ -19,9 +19,6 @@ public struct ProductPage: View { @Environment(\.presentationMode) var presentationMode - @Binding public var barcode: String - @Binding public var uploadedProduct: [String: String]? - @State private var isAlertPresent = false @State private var alertMessage = "" @State private var alertTitle = "" @@ -29,9 +26,12 @@ public struct ProductPage: View { @StateObject var pageConfig = ProductPageConfig() @StateObject var imagesHelper = ImagesHelper() - public init(barcode: Binding, submitProduct: Binding<[String: String]?> = Binding.constant(nil)) { - _barcode = barcode - _uploadedProduct = submitProduct + public let barcode: String + public let onUploadingDone: ([String: String]?) -> Void + + public init(barcode: String, onUploadingDone: @escaping ([String : String]?) -> Void = { _ in }) { + self.barcode = barcode + self.onUploadingDone = onUploadingDone } public var body: some View { @@ -40,7 +40,7 @@ public struct ProductPage: View { case .loading, .completed: PageOverlay(state: $pageConfig.pageState, stateAfterCompleted: .productDetails) case .productDetails: - ProductDetails(barcode: $barcode) + ProductDetails(barcode: barcode) .environmentObject(pageConfig) .environmentObject(imagesHelper) .actionSheet(isPresented: $imagesHelper.isPresentedSourcePicker) { () -> ActionSheet in @@ -124,7 +124,7 @@ public struct ProductPage: View { } } .onChange(of: pageConfig.submittedProduct) { newValue in - self.uploadedProduct = newValue + self.onUploadingDone(newValue) } .toolbar { ToolbarItem(placement: .cancellationAction) { @@ -160,5 +160,5 @@ public struct ProductPage: View { } #Preview { - ProductPage(barcode: .constant("5900259127761")) + ProductPage(barcode: "5900259127761") } diff --git a/Sources/Views/ProductDetails.swift b/Sources/Views/ProductDetails.swift index f725619..b3add35 100644 --- a/Sources/Views/ProductDetails.swift +++ b/Sources/Views/ProductDetails.swift @@ -10,7 +10,7 @@ import SwiftUI struct ProductDetails: View { - @Binding var barcode: String + let barcode: String @EnvironmentObject var pageConfig: ProductPageConfig @EnvironmentObject var pickerModel: ImagesHelper