Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Extract TakePicture functionality to separate controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tovkal committed Jul 29, 2017
1 parent f4f6bca commit 214a769
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
16 changes: 14 additions & 2 deletions OpenFoodFacts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
9501634B1F2CE9A20057B8D5 /* MarqueeLabelSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9501633B1F2CE9780057B8D5 /* MarqueeLabelSwift.framework */; };
9501634C1F2CE9A20057B8D5 /* NotificationBanner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9501633D1F2CE9780057B8D5 /* NotificationBanner.framework */; };
9501634D1F2CEE700057B8D5 /* SnapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 950163431F2CE9780057B8D5 /* SnapKit.framework */; };
950163741F2D1EF20057B8D5 /* TakePictureViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950163731F2D1EF20057B8D5 /* TakePictureViewController.swift */; };
951FB09D1EB2989A005CAE33 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 951FB09F1EB2989A005CAE33 /* InfoPlist.strings */; };
9525708C1EE9BC91002FC40D /* CellImageTapable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9525708B1EE9BC91002FC40D /* CellImageTapable.swift */; };
953664F51ECA212100D6D398 /* NutrimentItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 953664F41ECA212100D6D398 /* NutrimentItem.swift */; };
Expand Down Expand Up @@ -162,6 +163,7 @@
950163471F2CE9780057B8D5 /* XCGLogger.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; path = XCGLogger.framework.dSYM; sourceTree = "<group>"; };
950163481F2CE9780057B8D5 /* XLPagerTabStrip.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = XLPagerTabStrip.framework; sourceTree = "<group>"; };
950163491F2CE9780057B8D5 /* XLPagerTabStrip.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; path = XLPagerTabStrip.framework.dSYM; sourceTree = "<group>"; };
950163731F2D1EF20057B8D5 /* TakePictureViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TakePictureViewController.swift; path = Common/TakePictureViewController.swift; sourceTree = "<group>"; };
951FB0991EB29852005CAE33 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9525708B1EE9BC91002FC40D /* CellImageTapable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CellImageTapable.swift; path = Protocols/CellImageTapable.swift; sourceTree = "<group>"; };
953664F41ECA212100D6D398 /* NutrimentItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NutrimentItem.swift; path = Nutriments/NutrimentItem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1131,6 +1133,14 @@
path = iOS;
sourceTree = "<group>";
};
950163721F2D1EDA0057B8D5 /* Common */ = {
isa = PBXGroup;
children = (
950163731F2D1EF20057B8D5 /* TakePictureViewController.swift */,
);
name = Common;
sourceTree = "<group>";
};
9525708A1EE9BC43002FC40D /* Protocols */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1397,6 +1407,7 @@
95C2652A1E96D6FA004212EC /* ViewControllers */ = {
isa = PBXGroup;
children = (
950163721F2D1EDA0057B8D5 /* Common */,
95DDAF5E1F25401D0096B74E /* Camera */,
95C2652B1E96D705004212EC /* Products */,
);
Expand Down Expand Up @@ -1798,6 +1809,7 @@
956FF94B1F1FF0CA0069D678 /* NutritionTableHeaderTableViewCell.swift in Sources */,
956FF95A1F1FF0CA0069D678 /* LoadingView.swift in Sources */,
95781B9B1EC26B24003E3256 /* Double.swift in Sources */,
950163741F2D1EF20057B8D5 /* TakePictureViewController.swift in Sources */,
95DDAF621F25401D0096B74E /* CameraViewController.swift in Sources */,
95DDAF661F2916D40096B74E /* ProductImage.swift in Sources */,
);
Expand Down Expand Up @@ -2740,7 +2752,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CURRENT_PROJECT_VERSION = 8;
CURRENT_PROJECT_VERSION = 9;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = JVHN2X67GT;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -2762,7 +2774,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CURRENT_PROJECT_VERSION = 8;
CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_TEAM = JVHN2X67GT;
FRAMEWORK_SEARCH_PATHS = (
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand Down
2 changes: 1 addition & 1 deletion OpenFoodFacts/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>8</string>
<string>9</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
6 changes: 4 additions & 2 deletions OpenFoodFacts/Network/ProductService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ struct ProductService: ProductApi {
}

func getProduct(byBarcode barcode: String, onSuccess: @escaping (ProductsResponse) -> Void, onError: @escaping (Error) -> Void) {
let url = getEndpoint + "/api/v0/product/\(barcode).json"
let url = postEndpoint + "/api/v0/product/\(barcode).json"
// let url = getEndpoint + "/api/v0/product/\(barcode).json"

Crashlytics.sharedInstance().setObjectValue(barcode, forKey: "product_search_barcode")
Crashlytics.sharedInstance().setObjectValue("by_barcode", forKey: "product_search_type")

let request: DataRequest = Alamofire.request(url)
log.debug(request.debugDescription)
request.responseObject { (response: DataResponse<ProductsResponse>) in
request.authenticate(user: "off", password: "off")
.responseObject { (response: DataResponse<ProductsResponse>) in
log.debug(response.debugDescription)
switch response.result {
case .success(let productResponse):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// TakePictureViewController.swift
// OpenFoodFacts
//
// Created by Andrés Pizá Bückmann on 29/07/2017.
// Copyright © 2017 Andrés Pizá Bückmann. All rights reserved.
//

import UIKit
import NotificationBanner

protocol TakePictureViewControllerDelegate: class {
func postImageSuccess()
}

class TakePictureViewController: UIViewController {
var productService: ProductService!
var barcode: String!
fileprivate var cameraController: CameraController?
weak var delegate: TakePictureViewControllerDelegate?

// Feedback banners
fileprivate lazy var uploadingImageBanner: StatusBarNotificationBanner = {
let banner = StatusBarNotificationBanner(title: NSLocalizedString("product-add.uploading-image-banner.title", comment: ""), style: .info)
banner.autoDismiss = false
return banner
}()
fileprivate lazy var uploadingImageErrorBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.image-upload-error-banner.title", comment: ""),
subtitle: NSLocalizedString("product-add.image-upload-error-banner.subtitle", comment: ""),
style: .danger)
return banner
}()
fileprivate lazy var uploadingImageSuccessBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.image-upload-success-banner.title", comment: ""), style: .success)
return banner
}()
fileprivate lazy var productAddSuccessBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.product-add-success-banner.title", comment: ""), style: .success)
return banner
}()

@IBAction func didTapTakePictureButton(_ sender: UIButton) {
if let cameraController = CameraController(presentingViewController: self) {
self.cameraController = cameraController
cameraController.delegate = self
cameraController.show()
}
}
}

extension TakePictureViewController: CameraControllerDelegate {
func didGetImage(image: UIImage) {
// For now, images will be always uploaded with type front
uploadingImageBanner.show()
productService.postImage(ProductImage(image: image, type: .front), barcode: barcode, onSuccess: {
self.uploadingImageBanner.dismiss()
self.uploadingImageSuccessBanner.show()
self.delegate?.postImageSuccess()
}, onError: { _ in
self.uploadingImageBanner.dismiss()
self.uploadingImageErrorBanner.show()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import NotificationBanner

class ProductAddViewController: UIViewController {
class ProductAddViewController: TakePictureViewController {
@IBOutlet weak var barcodeLabel: UILabel!
@IBOutlet weak var productNameField: UITextField!
@IBOutlet weak var brandsField: UITextField!
Expand All @@ -19,23 +19,7 @@ class ProductAddViewController: UIViewController {

fileprivate var activeField: UITextField?
fileprivate var contentInsetsBeforeKeyboard = UIEdgeInsets.zero
fileprivate var cameraController: CameraController?
fileprivate lazy var product = Product()
fileprivate lazy var uploadingImageBanner: StatusBarNotificationBanner = {
let banner = StatusBarNotificationBanner(title: NSLocalizedString("product-add.uploading-image-banner.title", comment: ""), style: .info)
banner.autoDismiss = false
return banner
}()
fileprivate lazy var uploadingImageErrorBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.image-upload-error-banner.title", comment: ""),
subtitle: NSLocalizedString("product-add.image-upload-error-banner.subtitle", comment: ""),
style: .danger)
return banner
}()
fileprivate lazy var uploadingImageSuccessBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.image-upload-success-banner.title", comment: ""), style: .success)
return banner
}()
fileprivate lazy var productAddSuccessBanner: NotificationBanner = {
let banner = NotificationBanner(title: NSLocalizedString("product-add.product-add-success-banner.title", comment: ""), style: .success)
return banner
Expand All @@ -50,12 +34,11 @@ class ProductAddViewController: UIViewController {
return alert
}()

var barcode: String! {
override var barcode: String! {
didSet {
product.barcode = barcode
}
}
var productService: ProductService!

override func viewDidLoad() {
productNameField.delegate = self
Expand All @@ -72,14 +55,6 @@ class ProductAddViewController: UIViewController {
barcodeLabel.text = barcode
}

@IBAction func didTapTakePictureButton(_ sender: UIButton) {
if let cameraController = CameraController(presentingViewController: self) {
self.cameraController = cameraController
cameraController.delegate = self
cameraController.show()
}
}

@IBAction func didTapSaveButton(_ sender: UIButton) {
activeField?.resignFirstResponder()
product.name = productNameField.text
Expand Down Expand Up @@ -136,18 +111,9 @@ extension ProductAddViewController: UITextFieldDelegate {
}
}

extension ProductAddViewController: CameraControllerDelegate {
func didGetImage(image: UIImage) {
// For now, images will be always uploaded with type front
uploadingImageBanner.show()
productService.postImage(ProductImage(image: image, type: .front), barcode: barcode, onSuccess: {
self.uploadingImageBanner.dismiss()
self.uploadingImageSuccessBanner.show()
self.uploadedImagesStackView.addArrangedSubview(self.createUploadedImageLabel())
}, onError: { _ in
self.uploadingImageBanner.dismiss()
self.uploadingImageErrorBanner.show()
})
extension ProductAddViewController: TakePictureViewControllerDelegate {
func postImageSuccess() {
self.uploadedImagesStackView.addArrangedSubview(self.createUploadedImageLabel())
}

fileprivate func createUploadedImageLabel() -> UILabel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<state key="normal" title="Take a picture"/>
<connections>
<action selector="didTapTakePictureButton:" destination="A8v-Ao-FSs" eventType="touchUpInside" id="PO1-cC-Yp8"/>
<action selector="didTapTakePictureButton:" destination="A8v-Ao-FSs" eventType="touchUpInside" id="pgc-Vz-LXr"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" axis="vertical" spacing="5" translatesAutoresizingMaskIntoConstraints="NO" id="Li4-Au-Fse">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit


@IBDesignable class PictureCallToActionView: UIView {

@IBOutlet weak var textLabel: UILabel!
Expand Down

0 comments on commit 214a769

Please sign in to comment.