BaseLandingVC.swift 2.23 KB
Newer Older
jzhang's avatar
jzhang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  BaseLandingVC.swift
//  BreastFeedingDemo
//
//  Created by Jie Zhang on 2022/10/7.
//

import AVFoundation
import Flutter
import UIKit

class BaseLandingVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel.teachingEndHandler = { [weak self] in
jzhang's avatar
jzhang committed
17
            self?.navigationController?.popToRootViewController(animated: true)
jzhang's avatar
jzhang committed
18
        }
19

jzhang's avatar
jzhang committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        viewModel.completeHandler = { [weak self] in
            self?.toCompleteVC()
        }
        // Do any additional setup after loading the view.
    }

    /// 检查权限
    /// - Returns: 是否有相机权限
    func checkCameraPermission() -> Bool? {
        let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)

        switch cameraAuthorizationStatus {
        case .notDetermined:
            return nil
        case .authorized:
            return true
        default:
            showPermissionChalenge()
            return false
        }
    }

    /// 提示权限问题
    func showPermissionChalenge() {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "PermissionAlertVC") as! PermissionAlertVC

        vc.modalPresentationStyle = .overCurrentContext
        present(vc, animated: true)
    }

    func toCompleteVC() {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "CompleteVC") as! CompleteVC
        navigationController?.pushViewController(vc, animated: true)
    }

    func showFlutterScreen() {
58 59 60
        let scanVC = FlutterViewController(engine: viewModel.flutterEngine, nibName: nil, bundle: nil)
        scanVC.modalPresentationStyle = .overFullScreen
        navigationController?.pushViewController(scanVC, animated: true)
jzhang's avatar
jzhang committed
61
    }
62

jzhang's avatar
jzhang committed
63 64 65 66 67 68 69
    @IBAction func startAction(_: Any) {
        if checkCameraPermission() == false {
            return
        }

        if !viewModel.isFirstLoad {
            viewModel.methodChannel?.invokeMethod("reload", arguments: nil)
朱国瑞's avatar
朱国瑞 committed
70 71
        } else{
            FlutterViewController(engine: viewModel.flutterEngine, nibName: nil, bundle: nil).pushRoute("/camera")
jzhang's avatar
jzhang committed
72 73 74 75 76
        }
        viewModel.isFirstLoad = false
        showFlutterScreen()
    }
}