Files
checker/ios/Runner/ScannerViewController.swift
asuchkov b6b5acd823 bug fix
2017-10-19 19:30:22 +03:00

164 lines
5.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// ScannerViewController.swift
// Runner
//
// Created by Ivan Murashov on 13/07/2017.
// Copyright © 2017 The Chromium Authors. All rights reserved.
//
import UIKit
import Flutter
extension ZBarSymbolSet: Sequence {
//public typealias Element = ZBarSymbol
//public typealias Iterator = NSFastEnumerationIterator
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
@objc class ScannerViewController: UIViewController, ZBarReaderDelegate, UITextFieldDelegate {
var platformChannel: FlutterMethodChannel?
let readerViewController = ZBarReaderViewController()
//Вьюшка для верхнего меню
let topView = UIView()
//Окно ввода кода
let textField = UITextField()
//Кнопка настроек
var settingButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
readerViewController.readerDelegate = self
readerViewController.readerView.zoom = 1.0
readerViewController.showsZBarControls = false
settingButton = UIButton(type: .system)
settingButton.addTarget(self, action: #selector(ScannerViewController.buttonTouch), for: .touchUpInside)
self.addChildViewController(readerViewController)
self.view.addSubview(readerViewController.view)
readerViewController.view.addSubview(topView)
topView.addSubview(textField)
topView.addSubview(settingButton)
textField.delegate = self
textField.keyboardType = .numberPad
//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(ScannerViewController.hideKeyboard))
readerViewController.view.addGestureRecognizer(tap)
}
func hideKeyboard() {
view.endEditing(false)
}
func buttonTouch(){
sendResult(textField.text!)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
sendResult(textField.text!)
return true
}
func sendResult(_ str: String) {
platformChannel?.invokeMethod("findUserAndPurchase", arguments: [str], result: { (result: Any?) in
if result is FlutterError {
self.showErrorAlert()
} else {
self.presentingViewController?.dismiss(animated: false, completion: { self.dismiss(animated: true) })
}
print("result: \(result.debugDescription )")
})
}
func showErrorAlert() {
let alertController = UIAlertController(title: "Error", message:
"Wrong code", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
}
override func viewWillAppear(_ animated: Bool) {
topView.backgroundColor = UIColor.white
settingButton.setTitle("+", for: .normal)
textField.placeholder = "Enter code"
textField.borderStyle = .roundedRect
//self.present(readerViewController, animated: true)
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillLayoutSubviews() {
readerViewController.view.frame = view.bounds
topView.frame = CGRect(x: 0, y: 0, width: readerViewController.view.frame.size.width, height: 40)
textField.frame = CGRect(x: 8, y: 5, width: readerViewController.view.frame.size.width - 50, height: 30)
settingButton.frame = CGRect(x: textField.frame.maxX + 8, y: 10, width: 20, height: 20)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let symbols = info[ZBarReaderControllerResults] as? ZBarSymbolSet else { return }
for symbol in symbols {
if let symbol = symbol as? ZBarSymbol, let data = symbol.data {
sendResult(data)
/*
if let platformChannel = self.platformChannel {
// self.dismiss(animated: true, completion: {
// self.presentingViewController?.dismiss(animated: true, completion: {
platformChannel.invokeMethod("findUserAndPurchase", arguments: [data], result: { (result: Any?) in
print("result: \(result ?? "")")
self.presentingViewController?.dismiss(animated: false, completion: { self.dismiss(animated: true) })
})
// })
// })
// let result = platformChannel.invokeMethod("getUserByCode", arguments: [data], handleResult(result: FlutterResult))
// print(result);
}
/*
let toast = UIAlertController()
toast.message = data
toast.add
toast.show()
*/
//navigationController?.popViewController(animated: true)
*/
}
}
//navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}