jsdw_ios/QuickLocation/Section/Mine/MoodStatus/MoodStatusViewModel.swift

61 lines
1.4 KiB
Swift

//
// MoodStatusViewModel.swift
// QuickLocation
//
import Foundation
import RxSwift
import RxCocoa
final class MoodStatusViewModel {
enum SaveValidation {
case missing
case ready(Int)
}
let items = MoodStatusCatalog.items
let selectedMoodIndex = BehaviorRelay<Int?>(value: nil)
private(set) var isSaving = false
init() {
let mood = AppContextManager.shared.account?.mood ?? 0
if (1...36).contains(mood) {
selectedMoodIndex.accept(mood)
} else if let firstMoodIndex = items.first?.moodIndex {
selectedMoodIndex.accept(firstMoodIndex)
}
}
func select(moodIndex: Int) {
selectedMoodIndex.accept(moodIndex)
}
func applyServerMood(_ moodIndex: Int) {
if MoodStatusCatalog.item(moodIndex: moodIndex) != nil {
selectedMoodIndex.accept(moodIndex)
} else {
selectedMoodIndex.accept(items.first?.moodIndex)
}
}
func selectedItem() -> MoodStatusItem? {
guard let moodIndex = selectedMoodIndex.value else { return nil }
return MoodStatusCatalog.item(moodIndex: moodIndex)
}
func validateSave() -> SaveValidation {
guard let moodIndex = selectedMoodIndex.value else { return .missing }
return .ready(moodIndex)
}
func beginSaving() {
isSaving = true
}
func endSaving() {
isSaving = false
}
}