You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
3.0 KiB

//
// UnitSetVC.swift
// HPlusFit
//
// Created by lemo. on 2019/9/2.
// Copyright © 2019 lemo. All rights reserved.
//
import UIKit
// MARK:-
fileprivate enum Resuable {
static let tableViewCell = ReusableCell<CommonTableViewCell>(nibName: "CommonTableViewCell")
}
class UnitSetVC: TableViewController {
private let relay = BehaviorRelay<[SectionModel<String,TableViewCellModel>]>(value: [])
private let rxDataSource = RxTableViewSectionedReloadDataSource
<SectionModel<String, TableViewCellModel>>(configureCell: {
(dataSource, tv, indexPath, element) in
let cell = tv.dequeue(Resuable.tableViewCell, for: indexPath)
cell.bindViewModel(model: element)
return cell
})
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
refreshUnit()
}
private func setupTableView() {
view.addSubview(tableView)
tableViewStyle = .grouped
tableView.register(Resuable.tableViewCell)
// tableView.sectionHeaderHeight = 0
tableView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
tableView.rx.setDelegate(self)
.disposed(by: rx.disposeBag)
tableView.rx.itemSelected
.subscribe(onNext: { [weak self] indexPath in
guard let `self` = self else { return }
guard var userInfo = UserDefaultsManager.getUserInfo() else { return }
//
userInfo.metricUnit = indexPath.row
UserDefaultsManager.saveUserInfo(userInfo: userInfo)
//
BluetoothService.shared.unitSetting(metric: indexPath.row)
//
kNotificationCenter.post(Notification(name: Notification.Name(rawValue: UnitChange)))
//
self.refreshUnit()
})
.disposed(by: rx.disposeBag)
}
override func bindViewModel() {
relay.asObservable()
.bind(to: tableView.rx.items(dataSource: rxDataSource))
.disposed(by: rx.disposeBag)
}
private func refreshUnit() {
guard let userInfo = UserDefaultsManager.getUserInfo() else { return }
let isMetric = userInfo.metricUnit == 0 ? true : false
let funcationList: [String] = [MultiLanguageKey.metric.localized, MultiLanguageKey.british .localized]
var items: [TableViewCellModel] = []
funcationList.enumerated().forEach { (offset, index) in
let isHiddenBottomLine = offset == funcationList.count - 1
let rightIcon = (isMetric && offset == 0) || (!isMetric && offset == 1) ? R.image.icon_tick() : nil
let cellModel = TableViewCellModel(title: index, isSwitch: false, description: nil, isArrows: false, isOn: false, image: nil, isBottomLine: !isHiddenBottomLine, righeImg: rightIcon)
items.append(cellModel)
}
relay.accept([SectionModel(model: "", items: items)])
}
}