wip
This commit is contained in:
@@ -4,6 +4,7 @@ import com.mycrib.shared.models.*
|
||||
import com.mycrib.shared.network.ApiResult
|
||||
import com.mycrib.shared.network.LookupsApi
|
||||
import com.mycrib.storage.TokenStorage
|
||||
import com.mycrib.storage.TaskCacheStorage
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -33,6 +34,9 @@ object LookupsRepository {
|
||||
private val _taskCategories = MutableStateFlow<List<TaskCategory>>(emptyList())
|
||||
val taskCategories: StateFlow<List<TaskCategory>> = _taskCategories
|
||||
|
||||
private val _allTasks = MutableStateFlow<List<CustomTask>>(emptyList())
|
||||
val allTasks: StateFlow<List<CustomTask>> = _allTasks
|
||||
|
||||
private val _isLoading = MutableStateFlow(false)
|
||||
val isLoading: StateFlow<Boolean> = _isLoading
|
||||
|
||||
@@ -51,6 +55,14 @@ object LookupsRepository {
|
||||
|
||||
scope.launch {
|
||||
_isLoading.value = true
|
||||
|
||||
// Load cached tasks from disk immediately for offline access
|
||||
val cachedTasks = TaskCacheStorage.getTasks()
|
||||
if (cachedTasks != null) {
|
||||
_allTasks.value = cachedTasks
|
||||
println("Loaded ${cachedTasks.size} tasks from cache")
|
||||
}
|
||||
|
||||
val token = TokenStorage.getToken()
|
||||
|
||||
if (token != null) {
|
||||
@@ -89,6 +101,20 @@ object LookupsRepository {
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
when (val result = lookupsApi.getAllTasks(token)) {
|
||||
is ApiResult.Success -> {
|
||||
_allTasks.value = result.data
|
||||
// Save to disk cache for offline access
|
||||
TaskCacheStorage.saveTasks(result.data)
|
||||
println("Fetched and cached ${result.data.size} tasks from API")
|
||||
}
|
||||
else -> {
|
||||
println("Failed to fetch tasks from API, using cached data if available")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_isInitialized.value = true
|
||||
@@ -106,6 +132,9 @@ object LookupsRepository {
|
||||
_taskPriorities.value = emptyList()
|
||||
_taskStatuses.value = emptyList()
|
||||
_taskCategories.value = emptyList()
|
||||
_allTasks.value = emptyList()
|
||||
// Clear disk cache on logout
|
||||
TaskCacheStorage.clearTasks()
|
||||
_isInitialized.value = false
|
||||
_isLoading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user