Show all to-dos and reset after cron

This commit is contained in:
Hafiz 2022-08-02 00:16:03 -04:00
parent de36a0351e
commit 022ad81290
3 changed files with 110 additions and 15 deletions

View file

@ -67,7 +67,9 @@ class TaskListActivity : BaseActivity<ActivityTasklistBinding, TaskListViewModel
}
viewModel.tasks.observe(this) {
adapter.data = it
if (!it.isNullOrEmpty()) {
adapter.data = it
}
}
viewModel.taskCount.observe(this) {
adapter.title = getTitle(it)
@ -80,6 +82,9 @@ class TaskListActivity : BaseActivity<ActivityTasklistBinding, TaskListViewModel
adapter.onTaskScore = {
if (it.type == TaskType.TODO) {
viewModel.setCurrentToDoAsComplete(it)
}
scoreTask(it)
}
adapter.onTaskTapped = {

View file

@ -1,5 +1,7 @@
package com.habitrpg.wearos.habitica.ui.viewmodels
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.habitrpg.common.habitica.models.responses.TaskDirection
@ -19,6 +21,7 @@ import javax.inject.Inject
class RYAViewModel @Inject constructor(
userRepository: UserRepository,
taskRepository: TaskRepository,
val sharedPreferences: SharedPreferences,
exceptionBuilder: ExceptionHandlerBuilder, appStateManager: AppStateManager
) : BaseViewModel(userRepository, taskRepository, exceptionBuilder, appStateManager) {
var hasRunCron: Boolean = false
@ -50,6 +53,8 @@ class RYAViewModel @Inject constructor(
}
fun runCron(function: (Boolean) -> 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) {

View file

@ -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<String>("task_type"))
val taskCount = MutableLiveData(0)
val tasks = taskRepository.getTasks(taskType ?: TaskType.HABIT)
.map {
if (taskType == TaskType.DAILY || taskType == TaskType.TODO) {
val taskList: MutableList<Any> = 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<Task>): List<Task> {
taskCount.value = tasks.size
return tasks
}
private fun mapDaily(tasks: List<Task>): MutableList<Any> {
val taskList: MutableList<Any> = 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<Any>? {
val gson = Gson()
val data = mutableListOf<Any>()
val tasksString = sharedPreferences.getString("to_do_tasks", null)
if (tasksString != null) {
val type: Type = object : TypeToken<ArrayList<Task?>?>() {}.type
val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList<Task>
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<Task>): List<Any>? {
saveCurrentTasks(tasks)
return getCurrentTasks()
}
private fun saveCurrentTasks(tasks: List<Task>) {
val taskList = mutableListOf<Task>()
val type: Type = object : TypeToken<ArrayList<Task?>?>() {}.type
if (tasksString != null) {
val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList<Task>
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<ArrayList<Task?>?>() {}.type
if (tasksString != null) {
val savedCurrentTasks = gson.fromJson(tasksString, type) as MutableList<Task>
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))
}
}
}
}
}
}