|
| 1 | +package com.example.util.simpletimetracker.feature_widget.grid.settings |
| 2 | + |
| 3 | +import androidx.lifecycle.LiveData |
| 4 | +import androidx.lifecycle.MutableLiveData |
| 5 | +import androidx.lifecycle.ViewModel |
| 6 | +import androidx.lifecycle.viewModelScope |
| 7 | +import com.example.util.simpletimetracker.core.extension.set |
| 8 | +import com.example.util.simpletimetracker.core.mapper.RecordTypeViewDataMapper |
| 9 | +import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor |
| 10 | +import com.example.util.simpletimetracker.domain.recordType.interactor.RecordTypeInteractor |
| 11 | +import com.example.util.simpletimetracker.domain.recordType.model.RecordType |
| 12 | +import com.example.util.simpletimetracker.domain.widget.interactor.WidgetInteractor |
| 13 | +import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType |
| 14 | +import com.example.util.simpletimetracker.feature_base_adapter.loader.LoaderViewData |
| 15 | +import com.example.util.simpletimetracker.feature_base_adapter.recordType.RecordTypeViewData |
| 16 | +import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView |
| 17 | +import dagger.hilt.android.lifecycle.HiltViewModel |
| 18 | +import kotlinx.coroutines.launch |
| 19 | +import javax.inject.Inject |
| 20 | + |
| 21 | +@HiltViewModel |
| 22 | +class WidgetGridSettingsViewModel @Inject constructor( |
| 23 | + private val recordTypeInteractor: RecordTypeInteractor, |
| 24 | + private val prefsInteractor: PrefsInteractor, |
| 25 | + private val widgetInteractor: WidgetInteractor, |
| 26 | + private val recordTypeViewDataMapper: RecordTypeViewDataMapper, |
| 27 | +) : ViewModel() { |
| 28 | + |
| 29 | + lateinit var extra: WidgetGridSettingsExtra |
| 30 | + |
| 31 | + val types: LiveData<List<ViewHolderType>> by lazy { |
| 32 | + return@lazy MutableLiveData<List<ViewHolderType>>().let { initial -> |
| 33 | + viewModelScope.launch { |
| 34 | + initializeWidgetData() |
| 35 | + initial.value = listOf(LoaderViewData()) |
| 36 | + initial.value = loadTypesViewData() |
| 37 | + } |
| 38 | + initial |
| 39 | + } |
| 40 | + } |
| 41 | + val handled: LiveData<Int> = MutableLiveData() |
| 42 | + |
| 43 | + private var recordTypesCache: List<RecordType>? = null |
| 44 | + private var filteredTypeIds: Set<Long> = emptySet() |
| 45 | + private var initialized: Boolean = false |
| 46 | + |
| 47 | + fun onRecordTypeClick(item: RecordTypeViewData) = viewModelScope.launch { |
| 48 | + filteredTypeIds = filteredTypeIds.toMutableSet().apply { |
| 49 | + if (item.id in this) remove(item.id) else add(item.id) |
| 50 | + } |
| 51 | + updateTypesViewData() |
| 52 | + } |
| 53 | + |
| 54 | + fun onShowAllClick() = viewModelScope.launch { |
| 55 | + filteredTypeIds = emptySet() |
| 56 | + updateTypesViewData() |
| 57 | + } |
| 58 | + |
| 59 | + fun onHideAllClick() = viewModelScope.launch { |
| 60 | + filteredTypeIds = getTypesCache().map(RecordType::id).toSet() |
| 61 | + updateTypesViewData() |
| 62 | + } |
| 63 | + |
| 64 | + fun onSaveClick() { |
| 65 | + viewModelScope.launch { |
| 66 | + prefsInteractor.setGridWidgetFilteredTypes(extra.widgetId, filteredTypeIds) |
| 67 | + widgetInteractor.updateGridWidget(extra.widgetId) |
| 68 | + (handled as MutableLiveData).value = extra.widgetId |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private suspend fun initializeWidgetData() { |
| 73 | + if (initialized) return |
| 74 | + filteredTypeIds = prefsInteractor.getGridWidgetFilteredTypes(extra.widgetId) |
| 75 | + initialized = true |
| 76 | + } |
| 77 | + |
| 78 | + private fun updateTypesViewData() = viewModelScope.launch { |
| 79 | + types.set(loadTypesViewData()) |
| 80 | + } |
| 81 | + |
| 82 | + private suspend fun loadTypesViewData(): List<ViewHolderType> { |
| 83 | + val isDarkTheme = prefsInteractor.getDarkMode() |
| 84 | + val numberOfCards = prefsInteractor.getNumberOfCards() |
| 85 | + |
| 86 | + return getTypesCache() |
| 87 | + .map { type -> |
| 88 | + recordTypeViewDataMapper.mapFiltered( |
| 89 | + recordType = type, |
| 90 | + numberOfCards = numberOfCards, |
| 91 | + isDarkTheme = isDarkTheme, |
| 92 | + isFiltered = type.id in filteredTypeIds, |
| 93 | + checkState = GoalCheckmarkView.CheckState.HIDDEN, |
| 94 | + isComplete = false, |
| 95 | + ) |
| 96 | + } |
| 97 | + .takeUnless { it.isEmpty() } |
| 98 | + ?: recordTypeViewDataMapper.mapToEmpty() |
| 99 | + } |
| 100 | + |
| 101 | + private suspend fun getTypesCache(): List<RecordType> { |
| 102 | + return recordTypesCache ?: run { |
| 103 | + recordTypeInteractor.getAll() |
| 104 | + .filter { !it.hidden } |
| 105 | + .also { recordTypesCache = it } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments