You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
11 KiB

//
// UpdataQRCodeImgViewControllers.swift
// FireBoltt
//
// Created by ecell on 2022/9/23.
// Copyright © 2022 Sheldon. All rights reserved.
//
import UIKit
import AVFoundation
class UpdataQRCodeImgViewControllers: ViewController {
var titleStr: String = ""
var paytype: Int = 1
let codeButton = UIButton()
var seleceImg = UIImage()
var viewTypeStr: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.monitor()
self.view.backgroundColor = UIColor.white
// Do any additional setup after loading the view.
codeButton.backgroundColor = UIColor.white
codeButton.setImage(R.image.fb_Qrcode_bg_icon(), for: .normal)
codeButton.layer.cornerRadius = 5.0
//button.layer.masksToBounds = true
codeButton.layer.shadowColor = UIColor(red: 255/255, green: 192/255, blue: 125/255, alpha: 0.28).cgColor
codeButton.layer.shadowRadius = 8
codeButton.layer.shadowOffset = CGSize(width: 0, height: 2)
codeButton.layer.shadowOpacity = 1
codeButton.layer.backgroundColor = UIColor.white.cgColor
codeButton.addTarget(self, action: #selector(selectImg), for: .touchUpInside)
self.view.addSubview(codeButton)
codeButton.snp.makeConstraints { (make) in
make.top.equalTo(self.view).offset(20)
make.centerX.equalTo(self.view)
make.height.width.equalTo(kScaleWidth(180))
//make.width.equalTo(kScaleWidth(160))
}
guard let objData = UserDefaults.standard.data(forKey: String(format: "QRCPDE%d%@", self.paytype,self.viewTypeStr)) else { return }
let decoded = try! PropertyListDecoder().decode(Data.self, from: objData)
let image = UIImage(data: decoded)
if (image != nil) {
self.codeButton.setImage(image, for: .normal)
seleceImg = image!
}
}
override func bindViewModel() {
guard let viewModel = viewModel as? UpdataQRCodeImgViewModels else { return }
print("%@",viewModel.titleName)
self.paytype = viewModel.payType
self.titleStr = viewModel.titleName
self.viewTypeStr = viewModel.viewType
self.makesUI()
}
func makesUI() {
let tiLabel:UILabel = UILabel()
tiLabel.textColor = kRGBA(102, 102, 102, 1)
tiLabel.font = SystemRegularFont(15)
tiLabel.numberOfLines = 0
tiLabel.text = String(format: "%@%@%@%@%@%@%@\n\n%@", MultiLanguageKey_FB.bindingSteps_1FB.localized,
self.titleStr.localized,
MultiLanguageKey_FB.bindingSteps_2FB.localized,
self.titleStr.localized,
MultiLanguageKey_FB.bindingSteps_3FB.localized,
self.titleStr.localized,
MultiLanguageKey_FB.bindingSteps_4FB.localized
,MultiLanguageKey_FB.attentionFB.localized)
self.view.addSubview(tiLabel);
tiLabel.snp.makeConstraints { (make) in
make.top.equalTo(self.view).offset(45+kScaleWidth(180))
make.left.equalTo(self.view).offset(15)
make.right.equalTo(self.view.snp.right).inset(15)
}
let button = UIButton()
button.gradient(colors: [kHexColor(0x59AAFF), kHexColor(0x2B75FF)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5), locations: [0, 1], autoLaoutFrame: CGRect(x: 0, y: 0, width: self.view.frame.size.width-60, height: kScaleWidth(44)))
button.layer.cornerRadius = kScaleWidth(22)
button.layer.masksToBounds = true
button.setTitle(MultiLanguageKey_FB.bindingQrCodeFB.localized, for: .normal)//
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = SystemRegularFont(15)
self.view.addSubview(button);
button.snp.makeConstraints { make in
make.bottom.equalTo(self.view.bottom).inset(30)
make.left.equalTo(self.view).offset(30)
make.right.equalTo(self.view.snp.right).inset(30)
make.height.equalTo(kScaleWidth(44))
}
button.addTarget(self, action: #selector(bindingTouch), for: .touchUpInside)
}
@objc func bindingTouch(){
print("绑定")
let str: String = String(stringLiteral:self.recognitionQRCode(qrCodeImage: self.seleceImg)!)
if (str.lengthOfBytes(using: String.Encoding.utf16) > 0) {
SVProgressHUD.show()
BluetoothService.shared.qrCodePushWithBleCmdType(payImgStr: self.seleceImg, type: self.paytype)
}
}
///
@objc func selectImg(){
self.setAvatorFB()
}
/* * @param qrCodeImage
* @return */
func recognitionQRCode(qrCodeImage: UIImage) -> String? {
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: nil)
guard let ciImage = CIImage(image: qrCodeImage) else { return nil }
guard let features = detector?.features(in: ciImage) else { return nil }
var codeStr = String()
for feature in features {
codeStr = (feature as! CIQRCodeFeature).messageString!
}
return codeStr
}
func fbjudgeQRCode(qrCodeImage: UIImage){
let str: String = String(stringLiteral:self.recognitionQRCode(qrCodeImage: qrCodeImage)!)
if (str.lengthOfBytes(using: String.Encoding.utf16) > 0) {
self.seleceImg = qrCodeImage
self.codeButton.setImage(qrCodeImage, for: .normal)
}
else
{
SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.invalidQrCodeFB.localized)
}
}
func monitor() {
///
Observable.of(kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdSuccess)),
kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdTimeout)))
.merge()
.subscribe(onNext: { [weak self] notification in
guard let `self` = self, let cmd = notification.object as? BleCMD_FireBoltt else { return }
let isSuccess = notification.name.rawValue == CmdSuccess
switch cmd {
case .qrCodePush:
if isSuccess {
SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.syncDataSucceedFB.localized)
//self.navigationController?.popViewController(animated: true)
guard let data = self.seleceImg.jpegData(compressionQuality: 0.5) else { return }
let encoded = try! PropertyListEncoder().encode(data)
UserDefaults.standard.set(encoded, forKey: String(format: "QRCPDE%d%@", self.paytype,self.viewTypeStr))
return
}
SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.setFailFB.localized)
default: break
}
})
.disposed(by: rx.disposeBag)
}
}
extension UpdataQRCodeImgViewControllers {
func setAvatorFB() {
let alertC = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
alertC.addAction(UIAlertAction.init(title: MultiLanguageKey_FB.cancelFB.localized, style: UIAlertAction.Style.cancel, handler: nil))
let camera = UIAlertAction.init(title: MultiLanguageKey_FB.cameraFB.localized, style: UIAlertAction.Style.default, handler: {[weak self] (_) in
guard let `self` = self else { return }
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
if authStatus == .denied || authStatus == .restricted {
let alertVC = UIAlertController.init(title: MultiLanguageKey_FB.tipFB.localized, message: MultiLanguageKey_FB.carmaPowerFB.localized, preferredStyle: .alert)
let goToAction = UIAlertAction.init(title: MultiLanguageKey_FB.gotoFB.localized, style: .default) { (action) in
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
}
let cancelAction = UIAlertAction(title: MultiLanguageKey_FB.cancelFB.localized, style: .cancel, handler: nil)
alertVC.addAction(goToAction)
alertVC.addAction(cancelAction)
self.present(alertVC, animated: true, completion: nil)
return
}
self.cameraAction()
})
let gallery = UIAlertAction.init(title: MultiLanguageKey_FB.photoSelectFB.localized, style: UIAlertAction.Style.default, handler: { [weak self](_) in
guard let `self` = self else { return }
self.galleryAction()
})
alertC.addAction(camera)
alertC.addAction(gallery)
self.present(alertC, animated: true, completion: nil)
}
func cameraAction() {
UIImagePickerController.rx.createWithParent(self, animated: true) { (picker) in
picker.allowsEditing = true
picker.sourceType = .camera
}
.flatMap {
$0.rx.didFinishPickingMediaWithInfo
}
.take(1)
.map { info in
return info[UIImagePickerController.InfoKey.editedImage.rawValue] as? UIImage
}
.subscribe(onNext: { [weak self](chooseImg) in
guard let `self` = self, let chooseImg = chooseImg else { return }
self.fbjudgeQRCode(qrCodeImage: chooseImg)
})
.disposed(by: rx.disposeBag)
}
func galleryAction() {
UIImagePickerController.rx.createWithParent(self, animated: true) { (picker) in
picker.allowsEditing = true
picker.sourceType = .photoLibrary
}
.flatMap {
$0.rx.didFinishPickingMediaWithInfo
}
.take(1)
.map { info in
return info[UIImagePickerController.InfoKey.editedImage.rawValue] as? UIImage
}
.subscribe(onNext: { [weak self](chooseImg) in
guard let `self` = self, let chooseImg = chooseImg else { return }
self.fbjudgeQRCode(qrCodeImage: chooseImg)
})
.disposed(by: rx.disposeBag)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/