100 lines
3.5 KiB
Swift
100 lines
3.5 KiB
Swift
//
|
|
// EmergencyContactVC.swift
|
|
// QuickLocation
|
|
//
|
|
// Created by 八条 on 2026/6/11.
|
|
//
|
|
|
|
import UIKit
|
|
import RxSwift
|
|
import RxCocoa
|
|
import RxDataSources
|
|
|
|
class EmergencyContactVC: BaseViewController {
|
|
|
|
fileprivate var rootView: EmergencyContactView!
|
|
private var contacts: [EmergencyContactModel] = []
|
|
|
|
override func loadView() {
|
|
rootView = EmergencyContactView(frame: UIScreen.main.bounds)
|
|
view = rootView
|
|
}
|
|
|
|
private var viewModel = EmergencyContactViewModel()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
bindViewModel()
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
requestEmergencyContactList()
|
|
}
|
|
|
|
private func bindViewModel() {
|
|
viewModel.output.sectionedItems
|
|
.bind(to: rootView.tableView.rx.items(dataSource: dataSource))
|
|
.disposed(by: disposeBag)
|
|
rootView.tableView.rx.setDelegate(rootView)
|
|
.disposed(by: disposeBag)
|
|
rootView.tableView.rx.itemSelected
|
|
.subscribe(onNext: { [weak self] _ in
|
|
self?.rootView.closeOpenedCell()
|
|
})
|
|
.disposed(by: disposeBag)
|
|
}
|
|
|
|
lazy private var dataSource: RxTableViewSectionedReloadDataSource<EmergencyContactListSectionModel> = {
|
|
return RxTableViewSectionedReloadDataSource<EmergencyContactListSectionModel>(
|
|
configureCell: { [weak self] (_, tableView, indexPath, model) in
|
|
let cell: EmergencyContactCell = tableView.dequeueReusableCell(for: indexPath)
|
|
cell.configure(model)
|
|
cell.onDelete = { [weak self] in
|
|
self?.showConfirmPop(title: "温馨提示",
|
|
message: "确定删除联系人【\(model.name)】吗?",
|
|
confirmText: "是",
|
|
confirmBlock: {
|
|
self?.requestDelete(model)
|
|
}, cancelText: "否")
|
|
}
|
|
cell.onSwipeChanged = { [weak self] current, isOpen in
|
|
guard let self else { return }
|
|
if isOpen {
|
|
self.closeOtherCells(except: current)
|
|
self.rootView.setOpenedCell(current)
|
|
} else {
|
|
self.rootView.setOpenedCell(nil)
|
|
}
|
|
}
|
|
return cell
|
|
})
|
|
}()
|
|
|
|
private func closeOtherCells(except current: EmergencyContactCell) {
|
|
rootView.tableView.visibleCells
|
|
.compactMap { $0 as? EmergencyContactCell }
|
|
.filter { $0 !== current }
|
|
.forEach { $0.closeSwipe(animated: true) }
|
|
}
|
|
|
|
private func requestEmergencyContactList() {
|
|
DLToast.showLoading()
|
|
UserService.emergencyContactList().subscribe(onNext: { [weak self] response in
|
|
guard let self else { return }
|
|
self.contacts = response.list
|
|
self.viewModel.loadData(response.list)
|
|
self.rootView.updateCount(response.list.count)
|
|
}, onError: { _ in }).disposed(by: disposeBag)
|
|
}
|
|
|
|
private func requestDelete(_ model: EmergencyContactModel) {
|
|
DLToast.showLoading()
|
|
UserService.emergencyContactDelete(id: model.id, phone: model.phone)
|
|
.subscribe(onNext: { [weak self] _ in
|
|
DLToast.show(text: "已删除")
|
|
self?.requestEmergencyContactList()
|
|
}, onError: { _ in }).disposed(by: disposeBag)
|
|
}
|
|
}
|