53 lines
1.2 KiB
Swift
53 lines
1.2 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 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
|
|
}
|
|
}
|