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.
 
 
 

92 lines
3.0 KiB

//
// MineViewModel.swift
// Lookfit
//
// Created by lemo. on 2020/3/7.
// Copyright © 2020 Sheldon. All rights reserved.
//
import UIKit
class MineViewModel: ViewModel {
var relay = BehaviorRelay(value: [SectionModel<String,TableViewCellModel>]())
let userIcon = BehaviorRelay<UIImage?>(value: R.image.icon_header())
let userNickname = BehaviorRelay<String>(value: "")
override init() {
super.init()
loadUserData()
loadMeList()
monitor()
}
func loadUserData() {
guard let userInfo = UserDefaultsManager.getUserInfo() else { return }
if let avatarImg = userInfo.avatarImg {
userIcon.accept(avatarImg)
}
userNickname.accept(userInfo.nickname)
}
func loadMeList() {
//
var meList: [[MineSetType]] = [[], [.about,.privacy,.service]]
//
if let adapterInfo = UserDefaultsManager.getDeviceInfo()?.adapterInfo {
if adapterInfo.unitSet { meList[0].append(.unitSet) }
if adapterInfo.targetSet { meList[0].append(.targetSet) }
}
var tempRelay: [SectionModel<String,TableViewCellModel>] = []
for i in 0..<meList.count {
var items: [TableViewCellModel] = []
meList[i].enumerated().forEach { (index, type) in
let isHiddenBottomLine = (index + 1) == meList[i].count
var cellModel = TableViewCellModel(title: type.title, isSwitch: false, description: nil, isArrows: true, isOn: false, image: nil, isBottomLine: isHiddenBottomLine, righeImg: nil)
cellModel.mineSetType = type
items.append(cellModel)
}
if items.count > 0 {
tempRelay.append(SectionModel(model: "", items: items))
}
}
relay.accept(tempRelay)
}
func monitor() {
//
kNotificationCenter.rx.notification(Notification.Name(rawValue: UserInfoUpdate))
.subscribe(onNext: { [weak self] (notification) in
guard let `self` = self else { return }
self.loadUserData()
})
.disposed(by: rx.disposeBag)
//
BluetoothService.shared.adapterInfoUpdate
.subscribe(onNext: { [weak self] (state) in
self?.loadMeList()
})
.disposed(by: rx.disposeBag)
}
}
enum MineSetType: String {
case unitSet = "单位设置"
case targetSet = "目标设置"
case about = "关于"
case privacy = "隐私政策"
case service = "用户协议"
var title: String {
switch self {
case .unitSet: return MultiLanguageKey.unitSet.localized
case .targetSet: return MultiLanguageKey.targetSet.localized
case .about: return MultiLanguageKey.about.localized
case .privacy: return MultiLanguageKey.privacy.localized
case .service: return MultiLanguageKey.service.localized
}
}
}