jsdw_ios/QuickLocation/Section/Home/SOS/SOSViewController.swift

83 lines
2.6 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.

//
// SOSViewController.swift
// QuickLocation
//
import UIKit
import RxSwift
final class SOSViewController: BaseViewController {
private var rootView: SOSView!
private var isRequestingSOS = false
override func loadView() {
rootView = SOSView(frame: UIScreen.main.bounds)
view = rootView
}
override func viewDidLoad() {
super.viewDidLoad()
fd_interactivePopDisabled = true
bindActions()
rootView.setState(AppContextManager.shared.status == 1 ? .active : .idle)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
requestEmergencyContacts()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
rootView.stopTransientActivities()
}
private func bindActions() {
rootView.onCountdownCompleted = { [weak self] in
self?.requestSOS(enable: true)
}
rootView.onCancelActive = { [weak self] in
self?.requestSOS(enable: false)
}
rootView.onManageContacts = {
AppRouter.push(Route.emergencyContact)
}
}
private func requestEmergencyContacts() {
UserService.emergencyContactList()
.subscribe(onNext: { [weak self] response in
self?.rootView.configureContacts(response.list)
}, onError: { _ in })
.disposed(by: disposeBag)
}
private func requestSOS(enable: Bool) {
guard !isRequestingSOS else { return }
isRequestingSOS = true
dl.showLoading(text: enable ? "正在发送SOS" : "正在取消SOS")
SystemService.sos(enable: enable)
.subscribe(onNext: { [weak self] _ in
guard let self else { return }
self.isRequestingSOS = false
AppContextManager.shared.updateAccountStatus(enable ? 1 : 0)
self.rootView.setState(enable ? .active : .idle)
self.dl.showSuccess(text: enable ? "发送成功" : "取消成功")
}, onError: { [weak self] _ in
guard let self else { return }
self.isRequestingSOS = false
if enable {
self.rootView.setState(.idle)
} else {
self.rootView.setState(.active)
self.rootView.setActiveCancelEnabled(true)
}
self.dl.showError(text: enable ? "SOS发送失败请重试" : "SOS取消失败请重试")
})
.disposed(by: disposeBag)
}
}