diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/TaskListActivity.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/TaskListActivity.kt index e27ecbda2..67304fcef 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/TaskListActivity.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/TaskListActivity.kt @@ -67,7 +67,9 @@ class TaskListActivity : BaseActivity Unit) { + //Clear shared pref values for saved to-do tasks + sharedPreferences.edit { putString("to_do_tasks", null) } viewModelScope.launch(exceptionBuilder.userFacing(this)) { appStateManager.startLoading() for (task in tasksToComplete) { diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/viewmodels/TaskListViewModel.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/viewmodels/TaskListViewModel.kt index a69ff7478..28f0573a9 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/viewmodels/TaskListViewModel.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/viewmodels/TaskListViewModel.kt @@ -1,9 +1,13 @@ package com.habitrpg.wearos.habitica.ui.viewmodels +import android.content.SharedPreferences +import androidx.core.content.edit import androidx.lifecycle.MutableLiveData import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.asLiveData import androidx.lifecycle.viewModelScope +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import com.habitrpg.common.habitica.models.responses.TaskDirection import com.habitrpg.common.habitica.models.responses.TaskScoringResult import com.habitrpg.common.habitica.models.tasks.TaskType @@ -17,6 +21,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch +import java.lang.reflect.Type +import java.util.ArrayList import javax.inject.Inject @HiltViewModel @@ -24,26 +30,19 @@ class TaskListViewModel @Inject constructor( savedStateHandle: SavedStateHandle, taskRepository: TaskRepository, userRepository: UserRepository, + val sharedPreferences: SharedPreferences, exceptionBuilder: ExceptionHandlerBuilder, appStateManager: AppStateManager ) : BaseViewModel(userRepository, taskRepository, exceptionBuilder, appStateManager) { + private val gson = Gson() + private val tasksString = sharedPreferences.getString("to_do_tasks", null) val taskType = TaskType.from(savedStateHandle.get("task_type")) val taskCount = MutableLiveData(0) val tasks = taskRepository.getTasks(taskType ?: TaskType.HABIT) .map { - if (taskType == TaskType.DAILY || taskType == TaskType.TODO) { - val taskList: MutableList = it.filter { it.isDue == true || it.type == TaskType.TODO }.sortedBy { it.completed }.toMutableList() - val firstCompletedIndex = taskList.indexOfFirst { it is Task && it.completed } - if (firstCompletedIndex >= 0) { - // since this is the index of the first completed task, this is also the number of incomplete tasks - taskCount.value = firstCompletedIndex - taskList.add(firstCompletedIndex, "Done today") - } else { - taskCount.value = taskList.size - } - taskList - } else { - taskCount.value = it.size - it + when(taskType) { + TaskType.DAILY -> mapDaily(it) + TaskType.TODO -> mapTodos(it) + else -> map(it) } } .asLiveData() @@ -65,4 +64,90 @@ class TaskListViewModel @Inject constructor( onResult(result) } } + + private fun map(tasks: List): List { + taskCount.value = tasks.size + return tasks + } + + private fun mapDaily(tasks: List): MutableList { + val taskList: MutableList = tasks.filter { it.isDue == true || it.type == TaskType.TODO }.sortedBy { it.completed }.toMutableList() + val firstCompletedIndex = taskList.indexOfFirst { it is Task && it.completed } + if (firstCompletedIndex >= 0) { + // since this is the index of the first completed task, this is also the number of incomplete tasks + taskCount.value = firstCompletedIndex + taskList.add(firstCompletedIndex, "Done today") + } else { + taskCount.value = taskList.size + } + return taskList + } + + private fun getCurrentTasks(): List? { + val gson = Gson() + val data = mutableListOf() + val tasksString = sharedPreferences.getString("to_do_tasks", null) + if (tasksString != null) { + val type: Type = object : TypeToken?>() {}.type + val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList + val list = savedCurrentTasks.sortedBy { it.completed } + val firstCompletedIndex = list.indexOfFirst { it.completed } + return if (firstCompletedIndex >= 0) { + // since this is the index of the first completed task, this is also the number of incomplete tasks + taskCount.value = firstCompletedIndex + data.addAll(list) + data.add(firstCompletedIndex, "Done today") + data + } else { + savedCurrentTasks + } + } + return null + } + + private fun mapTodos(tasks: List): List? { + saveCurrentTasks(tasks) + return getCurrentTasks() + } + + private fun saveCurrentTasks(tasks: List) { + val taskList = mutableListOf() + val type: Type = object : TypeToken?>() {}.type + if (tasksString != null) { + val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList + if (!savedCurrentTasks.isNullOrEmpty()) { + for (task in tasks) { + if (!savedCurrentTasks.contains(task)) { + taskList.add(task) + } + } + } + } else { + taskList.addAll(tasks) + } + if (!taskList.isNullOrEmpty()) { + sharedPreferences.edit { + putString("to_do_tasks", gson.toJson(taskList)) + } + } + } + + fun setCurrentToDoAsComplete(currentTask: Task) { + val gson = Gson() + val type: Type = object : TypeToken?>() {}.type + if (tasksString != null) { + val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList + if (!savedCurrentTasks.isNullOrEmpty()) { + savedCurrentTasks.let { tasks -> + val task = tasks[tasks.indexOf(currentTask)] + task.completed = !task.completed + tasks[tasks.indexOf(currentTask)] = task + sharedPreferences.edit { + putString("to_do_tasks", gson.toJson(tasks)) + } + } + } + } + } + } \ No newline at end of file