jsdw_ios/QuickLocation/Section/Mine/CancelAccount/CancelAccountVC.swift

106 lines
3.3 KiB
Swift

//
// CancelAccountVC.swift
// QuickLocation
//
import UIKit
import RxSwift
import RxCocoa
#if !targetEnvironment(simulator)
import GeYanSdk
#endif
final class CancelAccountVC: BaseViewController {
private var rootView: CancelAccountView!
private let requiredPhrase = "我自愿注销本账号"
override func loadView() {
rootView = CancelAccountView(frame: UIScreen.main.bounds)
view = rootView
}
override func viewDidLoad() {
super.viewDidLoad()
bind()
}
private func bind() {
rootView.navView.onBackTapped = { [weak self] in
self?.navigationController?.popViewController(animated: true)
}
rootView.checkItems.forEach { item in
item.checkBtn.rx.tap
.subscribe(onNext: { [weak self, weak item] in
item?.checkBtn.isSelected.toggle()
self?.refreshSaveEnabled()
})
.disposed(by: disposeBag)
}
rootView.confirmInput.rx.text.orEmpty
.subscribe(onNext: { [weak self] _ in
self?.refreshSaveEnabled()
})
.disposed(by: disposeBag)
rootView.saveBtn.rx.tap
.subscribe(onNext: { [weak self] in
self?.submit()
})
.disposed(by: disposeBag)
}
private func refreshSaveEnabled() {
let allChecked = rootView.checkItems.allSatisfy(\.isChecked)
let phraseOK = rootView.confirmInput.text == requiredPhrase
rootView.setSaveEnabled(allChecked && phraseOK)
}
private func submit() {
guard rootView.checkItems.allSatisfy(\.isChecked),
rootView.confirmInput.text == requiredPhrase else { return }
DLToast.showLoading()
UserService.deleteAccount().subscribe(onNext: { [weak self] _ in
DLToast.dismiss()
self?.showSuccessToast {
NotificationCenter.default.post(name: .invalidatePopupQueue, object: nil)
#if !targetEnvironment(simulator)
GeYanSdk.deletePreResultCache()
#endif
AppContextManager.shared.deleteAccount()
GroupIMService.shared.logout()
AppDelegate.shared.showMainViewController()
}
}, onError: { _ in }).disposed(by: disposeBag)
}
private func showSuccessToast(completion: @escaping () -> Void) {
let toast = UIView()
toast.backgroundColor = UIColor.black.withAlphaComponent(0.75)
toast.layer.cornerRadius = 12
view.addSubview(toast)
toast.layoutChain.center().width(120).height(120)
let icon = UIImageView(image: UIImage(systemName: "checkmark.circle"))
icon.tintColor = .white
icon.contentMode = .scaleAspectFit
toast.addSubview(icon)
icon.layoutChain.top(28).centerX().width(36).height(36)
let lab = UILabel()
lab.text = "注销成功"
lab.textColor = .white
lab.font = .systemFont(ofSize: 14, weight: .medium)
lab.textAlignment = .center
toast.addSubview(lab)
lab.layoutChain.topToBottomOfView(icon, offset: 12).edgesHorzontal(8)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) {
toast.removeFromSuperview()
completion()
}
}
}