This commit is contained in:
Trey t
2025-11-05 18:16:46 -06:00
parent b2b8cc62de
commit 6bad8d6b5a
13 changed files with 401 additions and 1 deletions

View File

@@ -88,4 +88,21 @@ class LookupsApi(private val client: HttpClient = ApiClient.httpClient) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
suspend fun getAllTasks(token: String): ApiResult<List<CustomTask>> {
return try {
val response = client.get("$baseUrl/tasks/") {
header("Authorization", "Token $token")
}
if (response.status.isSuccess()) {
val data: PaginatedResponse<CustomTask> = response.body()
ApiResult.Success(data.results)
} else {
ApiResult.Error("Failed to fetch tasks", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
}

View File

@@ -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
}

View File

@@ -0,0 +1,12 @@
package com.mycrib.storage
/**
* Platform-specific task cache manager interface for persistent storage.
* Each platform implements this using their native storage mechanisms.
*/
@Suppress("NO_ACTUAL_FOR_EXPECT")
expect class TaskCacheManager {
fun saveTasks(tasksJson: String)
fun getTasks(): String?
fun clearTasks()
}

View File

@@ -0,0 +1,50 @@
package com.mycrib.storage
import com.mycrib.shared.models.CustomTask
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.decodeFromString
/**
* Task cache storage that provides a unified interface for accessing platform-specific
* persistent storage. This allows tasks to persist across app restarts for offline access.
*/
object TaskCacheStorage {
private var cacheManager: TaskCacheManager? = null
private val json = Json { ignoreUnknownKeys = true }
/**
* Initialize TaskCacheStorage with a platform-specific TaskCacheManager.
* This should be called once during app initialization.
*/
fun initialize(manager: TaskCacheManager) {
cacheManager = manager
}
fun saveTasks(tasks: List<CustomTask>) {
try {
val tasksJson = json.encodeToString(tasks)
cacheManager?.saveTasks(tasksJson)
} catch (e: Exception) {
println("Error saving tasks to cache: ${e.message}")
}
}
fun getTasks(): List<CustomTask>? {
return try {
val tasksJson = cacheManager?.getTasks()
if (tasksJson != null) {
json.decodeFromString<List<CustomTask>>(tasksJson)
} else {
null
}
} catch (e: Exception) {
println("Error loading tasks from cache: ${e.message}")
null
}
}
fun clearTasks() {
cacheManager?.clearTasks()
}
}