wip
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.mycrib.repository
|
||||
|
||||
import com.mycrib.shared.models.*
|
||||
import com.mycrib.shared.network.ApiResult
|
||||
import com.mycrib.shared.network.LookupsApi
|
||||
import com.mycrib.storage.TokenStorage
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Singleton repository for managing lookup data across the entire app.
|
||||
* Fetches data once on initialization and caches it for the app session.
|
||||
*/
|
||||
object LookupsRepository {
|
||||
private val lookupsApi = LookupsApi()
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
private val _residenceTypes = MutableStateFlow<List<ResidenceType>>(emptyList())
|
||||
val residenceTypes: StateFlow<List<ResidenceType>> = _residenceTypes
|
||||
|
||||
private val _taskFrequencies = MutableStateFlow<List<TaskFrequency>>(emptyList())
|
||||
val taskFrequencies: StateFlow<List<TaskFrequency>> = _taskFrequencies
|
||||
|
||||
private val _taskPriorities = MutableStateFlow<List<TaskPriority>>(emptyList())
|
||||
val taskPriorities: StateFlow<List<TaskPriority>> = _taskPriorities
|
||||
|
||||
private val _taskStatuses = MutableStateFlow<List<TaskStatus>>(emptyList())
|
||||
val taskStatuses: StateFlow<List<TaskStatus>> = _taskStatuses
|
||||
|
||||
private val _taskCategories = MutableStateFlow<List<TaskCategory>>(emptyList())
|
||||
val taskCategories: StateFlow<List<TaskCategory>> = _taskCategories
|
||||
|
||||
private val _isLoading = MutableStateFlow(false)
|
||||
val isLoading: StateFlow<Boolean> = _isLoading
|
||||
|
||||
private val _isInitialized = MutableStateFlow(false)
|
||||
val isInitialized: StateFlow<Boolean> = _isInitialized
|
||||
|
||||
/**
|
||||
* Load all lookups from the API.
|
||||
* This should be called once when the user logs in.
|
||||
*/
|
||||
fun initialize() {
|
||||
// Only initialize once per app session
|
||||
if (_isInitialized.value) {
|
||||
return
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
_isLoading.value = true
|
||||
val token = TokenStorage.getToken()
|
||||
|
||||
if (token != null) {
|
||||
// Load all lookups in parallel
|
||||
launch {
|
||||
when (val result = lookupsApi.getResidenceTypes(token)) {
|
||||
is ApiResult.Success -> _residenceTypes.value = result.data.results
|
||||
else -> {} // Keep empty list on error
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
when (val result = lookupsApi.getTaskFrequencies(token)) {
|
||||
is ApiResult.Success -> _taskFrequencies.value = result.data.results
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
when (val result = lookupsApi.getTaskPriorities(token)) {
|
||||
is ApiResult.Success -> _taskPriorities.value = result.data.results
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
when (val result = lookupsApi.getTaskStatuses(token)) {
|
||||
is ApiResult.Success -> _taskStatuses.value = result.data.results
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
when (val result = lookupsApi.getTaskCategories(token)) {
|
||||
is ApiResult.Success -> _taskCategories.value = result.data.results
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_isInitialized.value = true
|
||||
_isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached data.
|
||||
* This should be called when the user logs out.
|
||||
*/
|
||||
fun clear() {
|
||||
_residenceTypes.value = emptyList()
|
||||
_taskFrequencies.value = emptyList()
|
||||
_taskPriorities.value = emptyList()
|
||||
_taskStatuses.value = emptyList()
|
||||
_taskCategories.value = emptyList()
|
||||
_isInitialized.value = false
|
||||
_isLoading.value = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Force refresh all lookups from the API.
|
||||
*/
|
||||
fun refresh() {
|
||||
_isInitialized.value = false
|
||||
initialize()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user