1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// ViewController.swift
// BreastFeedingDemo
//
// Created by Jay Zhang on 2022/6/2.
//
import AVFoundation
import Flutter
import UIKit
class ViewController: UIViewController {
@IBOutlet var titleL: UILabel!
@IBOutlet var stepL1: UILabel!
@IBOutlet var stepL2: UILabel!
@IBOutlet var stepL3: UILabel!
@IBOutlet var startBtn: UIButton!
@IBOutlet var panelV: UIStackView!
@IBOutlet var popupView: UIView!
@IBOutlet var switchBtn: UIButton!
@IBOutlet var permisionPopupV: UIView!
@IBOutlet var permissionTitle: UILabel!
@IBOutlet var permissionText: UILabel!
@IBOutlet var permissionOKBtn: UIButton!
@IBOutlet var permissionPanel: UIView!
var methodChannel: FlutterMethodChannel?
lazy var vm: ViewModel = .init()
var isFirstLoad: Bool = true
fileprivate func updateUI() {
titleL.text = vm.landingPageTitle
stepL1.text = vm.landingPageStep1
stepL2.text = vm.landingPageStep2
stepL3.text = vm.landingPageStep3
permissionText.text = vm.cameraText1
permissionTitle.text = vm.cameraText
permissionOKBtn.setTitle(vm.cameraButtonText, for: .normal)
startBtn.setTitle(vm.landingPageStart, for: .normal)
panelV.layer.cornerRadius = 8
permissionPanel.layer.cornerRadius = 8
permissionPanel.clipsToBounds = true
panelV.clipsToBounds = true
}
override func viewDidLoad() {
super.viewDidLoad()
overrideUserInterfaceStyle = .light
UIApplication.shared.isIdleTimerDisabled = true
navigationController?.setNavigationBarHidden(true, animated: false)
updateUI()
popupView.isHidden = true
if let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine {
methodChannel = FlutterMethodChannel(name: "com.wmdigit.breastcoachai.native",
binaryMessenger: flutterEngine.binaryMessenger)
methodChannel?.setMethodCallHandler { [weak self]
(call: FlutterMethodCall, result: @escaping FlutterResult) in
if let strongSelf = self {
switch call.method {
case "init":
result(strongSelf.vm.flutterArguments)
case "teachingEnd":
strongSelf.methodChannel?.invokeMethod("dispose", arguments: nil)
// ["totalDuration": 3, "reasonText": Concern about my personal privacy, "status": incomplete, "incompleteReasonCode": 2]
let argument: [String: Any] = try! JSONSerialization.jsonObject(with: (call.arguments as! String).data(using: .utf8)!) as! [String: Any]
// let totalDuration: Int = argument["totalDuration"] as! Int
// let reasonText: String = argument["reasonText"] as! String
// let incompleteReasonCode: Int = argument["incompleteReasonCode"] as! Int
let status: String = argument["status"] as! String
if status == "incomplete" {
// strongSelf.showError(reasonText)
} else if status == "complete" {
strongSelf.toCompleteVC()
return
}
strongSelf.navigationController?.popViewController(animated: true)
default:
// Unrecognized method name
print("Unrecognized method name: \(call.method)")
}
}
}
}
}
func checkCameraPermission() -> Bool? {
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
switch cameraAuthorizationStatus {
case .notDetermined:
return nil
case .authorized:
return true
default:
permisionPopupV.isHidden = false
return false
}
}
func showError(_ text: String?) {
let alertVC = UIAlertController(title: text, message: nil, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "OK", style: .cancel))
navigationController?.pushViewController(alertVC, animated: true)
}
func toCompleteVC() {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "CompleteVC") as! CompleteVC
vc.vm = vm
navigationController?.pushViewController(vc, animated: true)
}
@IBAction func toSetting(_: Any) {
if let appSettingsURL = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSettingsURL)
}
}
@IBAction func tapStart(_: Any) {
if checkCameraPermission() == false {
return
}
if !isFirstLoad {
methodChannel?.invokeMethod("reload", arguments: nil)
}
isFirstLoad = false
if let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine {
let scanVC = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
scanVC.modalPresentationStyle = .overFullScreen
navigationController?.pushViewController(scanVC, animated: true)
}
}
/// 选择语言
@IBAction func showPanel(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
popupView.isHidden = !sender.isSelected
//
(panelV.viewWithTag(1000) as! UIButton).isSelected = false
(panelV.viewWithTag(1001) as! UIButton).isSelected = false
(panelV.viewWithTag(1002) as! UIButton).isSelected = false
switch vm.language {
case .english:
(panelV.viewWithTag(1000) as! UIButton).isSelected = true
case .français:
(panelV.viewWithTag(1001) as! UIButton).isSelected = true
case .español:
(panelV.viewWithTag(1002) as! UIButton).isSelected = true
}
}
@IBAction func dismissPermissimPanel(_: Any) {
permisionPopupV.isHidden = true
}
@IBAction func selectLanguage(_ sender: UIButton) {
switch sender.tag {
case 1000:
vm.language = .english
case 1001:
vm.language = .français
case 1002:
vm.language = .español
default:
break
}
switchBtn.isSelected = false
popupView.isHidden = true
updateUI()
}
}