Skip to content

Commit

Permalink
Uploaded product closure
Browse files Browse the repository at this point in the history
  • Loading branch information
hrabkin committed Dec 27, 2023
1 parent 58612a7 commit ac13cff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Example/ExampleCodeFromScanner/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ struct ContentView: View {
}
}
.fullScreenCover(isPresented: $isEditingProduct) {
ProductPage(barcode: $barcode).onDisappear() {
ProductPage(barcode: barcode) { uploadedProduct in
print(uploadedProduct ?? "")
}.onDisappear() {
isScanning = true
}
}
Expand Down
10 changes: 5 additions & 5 deletions Example/ExampleWithCode/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions Sources/ProductPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ 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 = ""

@StateObject var pageConfig = ProductPageConfig()
@StateObject var imagesHelper = ImagesHelper()

public init(barcode: Binding<String>, 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 {
Expand All @@ -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
Expand Down Expand Up @@ -124,7 +124,7 @@ public struct ProductPage: View {
}
}
.onChange(of: pageConfig.submittedProduct) { newValue in
self.uploadedProduct = newValue
self.onUploadingDone(newValue)
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Expand Down Expand Up @@ -160,5 +160,5 @@ public struct ProductPage: View {
}

#Preview {
ProductPage(barcode: .constant("5900259127761"))
ProductPage(barcode: "5900259127761")
}
2 changes: 1 addition & 1 deletion Sources/Views/ProductDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ProductDetails: View {

@Binding var barcode: String
let barcode: String

@EnvironmentObject var pageConfig: ProductPageConfig
@EnvironmentObject var pickerModel: ImagesHelper
Expand Down

0 comments on commit ac13cff

Please sign in to comment.