Skip to content

Commit

Permalink
Restructuring, small fix for withCode sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hrabkin committed Nov 5, 2023
1 parent b008574 commit 58612a7
Show file tree
Hide file tree
Showing 50 changed files with 52 additions and 53 deletions.
6 changes: 3 additions & 3 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
);
mainGroup = 1926CA042AC8553D004EAD53;
packageReferences = (
19CC78E32ACD702E00172137 /* XCLocalSwiftPackageReference "../OpenFoodFactsSDK" */,
19E1268B2AF7D86A00C0CD4A /* XCLocalSwiftPackageReference ".." */,
);
productRefGroup = 1926CA0E2AC8553D004EAD53 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -547,9 +547,9 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
19CC78E32ACD702E00172137 /* XCLocalSwiftPackageReference "../OpenFoodFactsSDK" */ = {
19E1268B2AF7D86A00C0CD4A /* XCLocalSwiftPackageReference ".." */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ../OpenFoodFactsSDK;
relativePath = ..;
};
/* End XCLocalSwiftPackageReference section */

Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleCodeFromScanner/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ContentView: View {
}
}
.fullScreenCover(isPresented: $isEditingProduct) {
ProductPage(barcode: $barcode, isPresented: $isEditingProduct).onDisappear() {
ProductPage(barcode: $barcode).onDisappear() {
isScanning = true
}
}
Expand Down
49 changes: 21 additions & 28 deletions Example/ExampleWithCode/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,35 @@ import OpenFoodFactsSDK
struct ContentView: View {

@State var barcode: String = ""
@State var isProductEditorPresent: Bool = false
@State var isInvalidBarcode: Bool = false

@State var submitProduct: [String: String]? = nil {
didSet {
print(submitProduct ?? "")
}
}
@State var isValidBarcode: Bool = false
@State var submitProduct: [String: String]? = nil

var body: some View {
NavigationView {
VStack(alignment: .center) {
VStack(alignment: .center, spacing: 10) {
Spacer()
TextField("Enter barcode for e.g. 5900102025473", text: $barcode)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()

Button("Start") {
if barcode.isAValidBarcode() {
isProductEditorPresent = true
} else {
isInvalidBarcode = true
Text("Expected format should have 7,8,12 or 13 digits.")
HStack {
TextField("Enter barcode for e.g. 5900102025473", text: $barcode)
.textFieldStyle(RoundedBorderTextFieldStyle())
.onChange(of: barcode) { newValue in
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)
Spacer()
// added for testing that editor is loaded with NavigatorView
NavigationLink("", destination: ProductPage(barcode: self.$barcode, isPresented: self.$isProductEditorPresent, submitProduct: $submitProduct), isActive: $isProductEditorPresent).opacity(0)
}
}
.alert("Invalid barcode", isPresented: $isInvalidBarcode) {
Button("OK") {
self.isInvalidBarcode = false
}
} message: {
Text("Barcode \(barcode) is invalid. Expected format should have 7,8,12 or 13 digits.")
}
}
}

#Preview {
ContentView()
}
File renamed without changes.
8 changes: 0 additions & 8 deletions OpenFoodFactsSDK/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions OpenFoodFactsSDK/README.md

This file was deleted.

9 changes: 9 additions & 0 deletions OpenFoodFactsSDK/Package.resolved → Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "0.1.4"
}
},
{
"identity" : "tocropviewcontroller",
"kind" : "remoteSourceControl",
"location" : "https://github.com/TimOliver/TOCropViewController.git",
"state" : {
"revision" : "d0470491f56e734731bbf77991944c0dfdee3e0e",
"version" : "2.6.1"
}
},
{
"identity" : "viewinspector",
"kind" : "remoteSourceControl",
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
Swift package for the Open Food Facts API. Easily access to more than 1.9 million products from all around the world.
Open Food Facts is powered by contributors from around the world and is constantly growing thanks to them.

## Compatibility

iOS 15+, verified compatibility only with Swift 5.9 for now

## Prerequisits

For adding product's pictures the SDK uses camera and gallery. App which integrates it should add NSCameraUsageDescription and NSPhotoLibraryUsageDescription.

## How to use ?
See `ExampleApp` for two scenarious of starting screen from scanning product or entering it manually

You can integrate the SDK into your iOS app using SPM (Swift Package Manager) by searching the repo using github's url

There are two modes for now: new and view

Sample codes:
existing 5900102025473 [pl] / 8711258029584 [nl], missing 5701377101134 [pl]

SDK has screen to view or submit product
```
ProductPage(barcode: $barcode, isPresented: $isProductEditorPresent, submitProduct: $submitProduct)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// TODO: add localizations
// TODO: OCR on client for nutriments, product name maybe
// TODO: match OFF requirement from README
// TODO: coverage by tests
//
// Created by Henadzi Rabkin on 15/10/2023.
//
Expand All @@ -16,8 +17,9 @@ import SwiftUI

public struct ProductPage: View {

@Environment(\.presentationMode) var presentationMode

@Binding public var barcode: String
@Binding public var isPresented: Bool
@Binding public var uploadedProduct: [String: String]?

@State private var isAlertPresent = false
Expand All @@ -27,9 +29,8 @@ public struct ProductPage: View {
@StateObject var pageConfig = ProductPageConfig()
@StateObject var imagesHelper = ImagesHelper()

public init(barcode: Binding<String>, isPresented: Binding<Bool>, submitProduct: Binding<[String: String]?> = Binding.constant(nil)) {
public init(barcode: Binding<String>, submitProduct: Binding<[String: String]?> = Binding.constant(nil)) {
_barcode = barcode
_isPresented = isPresented
_uploadedProduct = submitProduct
}

Expand Down Expand Up @@ -62,7 +63,7 @@ public struct ProductPage: View {
}
.alert("What's next?", isPresented: $pageConfig.isProductJustUploaded) {
Button("Leave") {
self.isPresented = false
presentationMode.wrappedValue.dismiss()
}
Button("View") {
pageConfig.isProductJustUploaded = false
Expand Down Expand Up @@ -128,7 +129,7 @@ public struct ProductPage: View {
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
isPresented = false
presentationMode.wrappedValue.dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Expand Down Expand Up @@ -159,5 +160,5 @@ public struct ProductPage: View {
}

#Preview {
ProductPage(barcode: .constant("5900259127761"), isPresented: .constant(true))
ProductPage(barcode: .constant("5900259127761"))
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 58612a7

Please sign in to comment.