Last active
July 19, 2018 08:10
-
-
Save carstenhag/971099958cde6091346c7a36d0f857e4 to your computer and use it in GitHub Desktop.
svw with scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StartViewController.swift | |
// incidents4 | |
// | |
// Created by chag0709 on 17.07.18. | |
// Copyright © 2018 SAP. All rights reserved. | |
// | |
import UIKit | |
import ReSwift | |
import SAPFiori | |
import SAPOData | |
import SAPFoundation | |
import SAPCommon | |
class StartViewController: UIViewController, StoreSubscriber, FUIBarcodeScanViewControllerDelegate { | |
// ? https://github.com/Goktug/Redux-Twitter/blob/a324e7f160658e420c3b9e416639ffad4769dc40/Redux-Twitter/Scenes/Search/SearchViewController.swift#L19 | |
typealias StoreSubscriberStateType = AppState | |
private let appDelegate = UIApplication.shared.delegate as! AppDelegate | |
private var zfmobincidentsrvEntities: ZFMOBINCIDENTSRVEntities<OnlineODataProvider> { | |
return self.appDelegate.zfmobincidentsrvEntities | |
} | |
private let logger: Logger = Logger.shared(named: "StartViewController") | |
private var scanViewController: FUIBarcodeScanViewController? | |
@IBOutlet weak var requestStoresButton: UIButton! | |
@IBOutlet weak var statusLabel: UILabel! | |
@IBOutlet weak var startScanView: UIButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
scanViewController = FUIBarcodeScanViewController.createInstanceFromStoryboard() | |
guard var safeScanViewController = scanViewController else { | |
return | |
} | |
safeScanViewController.barcodeScanner.scanMode = .qr | |
safeScanViewController.barcodeScanner.indicatorBorderColor = UIColor.red.cgColor | |
safeScanViewController.barcodeScanner.indicatorBorderWidth = 20 | |
safeScanViewController.barcodeScanner.promptMessage = "Scan A QR Code" | |
safeScanViewController.barcodeScanner.scanResultTransformer = { s in | |
return "transformed" | |
} | |
safeScanViewController.delegate = self | |
self.startScanView.addTarget(nil, action: #selector(showScanView), for: .touchUpInside) | |
} | |
@objc func showScanView() { | |
guard let safeScanViewController = scanViewController else { | |
return | |
} | |
let navController = UINavigationController(rootViewController: safeScanViewController) | |
self.navigationController?.present(navController, animated: true, completion: nil) | |
} | |
func barcodeScanViewController(_ barcodeScanViewController: FUIBarcodeScanViewController, didReceiveScanResult scanResult: FUIBarcodeScanResult?) { | |
print("scan result: \(String(describing: scanResult?.scanResultString))") | |
if scanResult?.scanResultString != "success" { | |
print("mongo qr, invalid") | |
} else { | |
barcodeScanViewController.dismiss(animated: true, completion: nil) | |
} | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
store.subscribe(self) | |
requestStoresButton.addTarget(nil, action: #selector(requestStoresAction), for: .touchUpInside) | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
store.unsubscribe(self) | |
} | |
func newState(state: AppState) { | |
DispatchQueue.main.async { | |
self.statusLabel.text = "API: \(state.apiStores?.count ?? 0) Stores\nODATA: \(state.odataStores?.count ?? 0) Stores\nMapped: \(state.locations?.count ?? 0) Stores" | |
} | |
} | |
// TODO: Use Promise, Future, better Callbacks, etc to load both API at the same time, and process Mapping when both are done | |
@objc func requestStoresAction() { | |
let query = DataQuery() | |
.from(ZFMOBINCIDENTSRVEntitiesMetadata.EntitySets.storeSet) | |
.selectAll() | |
.top(50) | |
let url = URL(string: "https://api.kaufland.net/stores/api/v1/") | |
let task = URLSession.shared.apiStoreTask(with: url!) { apiStores, response, error in | |
if let apiStores = apiStores { | |
store.dispatch(UpdateAPIStores(apiStores: apiStores)) | |
self.zfmobincidentsrvEntities.fetchStoreSet(matching: query) { storeSet, error in | |
guard let storeSet = storeSet else { | |
print("Fetch failed somehow") | |
return | |
} | |
store.dispatch(UpdateODATAStores(odataStores: storeSet)) | |
LocationMappingUtil().mapLocations() | |
} | |
} else { | |
// TODO: Add logging | |
} | |
} | |
task.resume() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment