mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-04-14 19:56:32 +00:00
Update completedToDo methods
ty phillip
This commit is contained in:
parent
7e808c8c85
commit
358b66d48b
4 changed files with 60 additions and 80 deletions
|
|
@ -45,19 +45,24 @@ class AppModule {
|
|||
}
|
||||
|
||||
@Provides
|
||||
fun providesConverterFactory(): Converter.Factory {
|
||||
fun providesConverterFactory(moshi: Moshi): Converter.Factory {
|
||||
return MoshiConverterFactory.create(
|
||||
Moshi.Builder()
|
||||
.add(WrappedTasklistAdapter())
|
||||
.add(customDateAdapter)
|
||||
.add(FrequencyAdapter())
|
||||
.add(TaskTypeAdapter())
|
||||
.add(AttributeAdapter())
|
||||
.addLast(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
moshi
|
||||
).asLenient()
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesMoshi(): Moshi {
|
||||
return Moshi.Builder()
|
||||
.add(WrappedTasklistAdapter())
|
||||
.add(customDateAdapter)
|
||||
.add(FrequencyAdapter())
|
||||
.add(TaskTypeAdapter())
|
||||
.add(AttributeAdapter())
|
||||
.addLast(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesApiHClient(
|
||||
|
|
|
|||
|
|
@ -82,9 +82,6 @@ class TaskListActivity : BaseActivity<ActivityTasklistBinding, TaskListViewModel
|
|||
|
||||
|
||||
adapter.onTaskScore = {
|
||||
if (it.type == TaskType.TODO) {
|
||||
viewModel.setCurrentToDoAsComplete(it)
|
||||
}
|
||||
scoreTask(it)
|
||||
}
|
||||
adapter.onTaskTapped = {
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ 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
|
||||
|
|
@ -16,13 +14,15 @@ import com.habitrpg.wearos.habitica.data.repositories.UserRepository
|
|||
import com.habitrpg.wearos.habitica.managers.AppStateManager
|
||||
import com.habitrpg.wearos.habitica.models.tasks.Task
|
||||
import com.habitrpg.wearos.habitica.util.ExceptionHandlerBuilder
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
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
|
||||
|
|
@ -31,12 +31,20 @@ class TaskListViewModel @Inject constructor(
|
|||
taskRepository: TaskRepository,
|
||||
userRepository: UserRepository,
|
||||
val sharedPreferences: SharedPreferences,
|
||||
moshi: Moshi,
|
||||
exceptionBuilder: ExceptionHandlerBuilder, appStateManager: AppStateManager
|
||||
) : BaseViewModel(userRepository, taskRepository, exceptionBuilder, appStateManager) {
|
||||
private val gson = Gson()
|
||||
private val tasksString = sharedPreferences.getString("to_do_tasks", null)
|
||||
var type: Type = Types.newParameterizedType(
|
||||
MutableList::class.java,
|
||||
Task::class.java
|
||||
)
|
||||
private val moshiAdapter: JsonAdapter<MutableList<Task>> = moshi.adapter(type)
|
||||
val taskType = TaskType.from(savedStateHandle.get<String>("task_type"))
|
||||
val taskCount = MutableLiveData(0)
|
||||
val completedToDos: MutableList<Task> by lazy {
|
||||
val tasksString = sharedPreferences.getString("to_do_tasks", null) ?: return@lazy mutableListOf()
|
||||
return@lazy moshiAdapter.fromJson(tasksString) ?: mutableListOf()
|
||||
}
|
||||
val tasks = taskRepository.getTasks(taskType ?: TaskType.HABIT)
|
||||
.map {
|
||||
when(taskType) {
|
||||
|
|
@ -50,7 +58,22 @@ class TaskListViewModel @Inject constructor(
|
|||
.asLiveData()
|
||||
|
||||
fun scoreTask(task: Task, direction: TaskDirection, onResult: (TaskScoringResult?) -> Unit) {
|
||||
viewModelScope.launch(exceptionBuilder.userFacing(this)) {
|
||||
if (taskType == TaskType.TODO) {
|
||||
if (direction == TaskDirection.UP) {
|
||||
completedToDos.add(task)
|
||||
} else {
|
||||
completedToDos.remove(task)
|
||||
}
|
||||
}
|
||||
viewModelScope.launch(exceptionBuilder.userFacing(this) {
|
||||
if (taskType == TaskType.TODO) {
|
||||
if (direction == TaskDirection.UP) {
|
||||
completedToDos.remove(task)
|
||||
} else {
|
||||
completedToDos.add(task)
|
||||
}
|
||||
}
|
||||
}) {
|
||||
val result = taskRepository.scoreTask(
|
||||
userRepository.getUser().first(),
|
||||
task,
|
||||
|
|
@ -83,70 +106,25 @@ class TaskListViewModel @Inject constructor(
|
|||
return taskList
|
||||
}
|
||||
|
||||
private fun getCurrentToDos(): 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
|
||||
override fun onCleared() {
|
||||
saveCurrentToDos()
|
||||
super.onCleared()
|
||||
}
|
||||
|
||||
private fun mapTodos(tasks: List<Task>): List<Any>? {
|
||||
saveCurrentToDos(tasks)
|
||||
return getCurrentToDos()
|
||||
private fun mapTodos(tasks: List<Task>): List<Any> {
|
||||
val taskList: MutableList<Any> = tasks.filter { !it.completed }.toMutableList()
|
||||
taskCount.value = taskList.size
|
||||
if (completedToDos.isNotEmpty()) {
|
||||
taskList.add("Done today")
|
||||
taskList.addAll(completedToDos)
|
||||
}
|
||||
|
||||
return taskList
|
||||
}
|
||||
|
||||
private fun saveCurrentToDos(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))
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun saveCurrentToDos() {
|
||||
sharedPreferences.edit {
|
||||
putString("to_do_tasks", moshiAdapter.toJson(completedToDos))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class ExceptionHandlerBuilder @Inject constructor(val appStateManager: AppStateM
|
|||
}
|
||||
}
|
||||
|
||||
fun userFacing(errorPresenter: ErrorPresenter): CoroutineExceptionHandler {
|
||||
fun userFacing(errorPresenter: ErrorPresenter, handler: ((Throwable) -> Unit)? = null): CoroutineExceptionHandler {
|
||||
return CoroutineExceptionHandler { _, throwable ->
|
||||
Log.e("Coroutine Error", "Error: ${throwable.cause}", throwable)
|
||||
if (throwable is IOException) {
|
||||
|
|
@ -30,7 +30,7 @@ class ExceptionHandlerBuilder @Inject constructor(val appStateManager: AppStateM
|
|||
DisplayedError(R.drawable.error, it)
|
||||
}
|
||||
}
|
||||
|
||||
handler?.invoke(throwable)
|
||||
appStateManager.endLoading()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue