|
|
|
//
|
|
|
|
// 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)
|
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
tableViewStyle = .insetGrouped
|
|
|
|
} else {
|
|
|
|
// Fallback on earlier versions
|
|
|
|
// 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 = UserDefaultsManagerFrieBoltt.getUserInfo() else { return }
|
|
|
|
// 保存
|
|
|
|
userInfo.metricUnit = indexPath.row
|
|
|
|
UserDefaultsManagerFrieBoltt.saveUserInfo(userInfo: userInfo)
|
|
|
|
// 发送蓝牙指令
|
|
|
|
BluetoothService.shared.unitSetting(metric: indexPath.row)
|
|
|
|
// 发出更新通知
|
|
|
|
kNotificationCenter.post(Notification(name: Notification.Name(rawValue: UnitChangeFireBoltt)))
|
|
|
|
// 刷新界面
|
|
|
|
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 = UserDefaultsManagerFrieBoltt.getUserInfo() else { return }
|
|
|
|
let isMetric = userInfo.metricUnit == 0 ? true : false
|
|
|
|
let funcationList: [String] = [MultiLanguageKey_FB.metricFB.localized, MultiLanguageKey_FB.britishFB.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.fb_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)])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|