From 358b66d48b3ee8f431cdb86d38b38c8529ca2bc0 Mon Sep 17 00:00:00 2001 From: Hafiz Date: Tue, 2 Aug 2022 10:52:56 -0400 Subject: [PATCH] Update completedToDo methods ty phillip --- .../wearos/habitica/modules/AppModule.kt | 23 ++-- .../ui/activities/TaskListActivity.kt | 3 - .../ui/viewmodels/TaskListViewModel.kt | 110 +++++++----------- .../util/HabiticaCoroutineExceptionhandler.kt | 4 +- 4 files changed, 60 insertions(+), 80 deletions(-) diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/modules/AppModule.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/modules/AppModule.kt index c1dab7e00..74e670bc2 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/modules/AppModule.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/modules/AppModule.kt @@ -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( 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 67304fcef..d3fefdfac 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 @@ -82,9 +82,6 @@ class TaskListActivity : BaseActivity> = moshi.adapter(type) val taskType = TaskType.from(savedStateHandle.get("task_type")) val taskCount = MutableLiveData(0) + val completedToDos: MutableList 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? { - 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 + override fun onCleared() { + saveCurrentToDos() + super.onCleared() } - private fun mapTodos(tasks: List): List? { - saveCurrentToDos(tasks) - return getCurrentToDos() + private fun mapTodos(tasks: List): List { + val taskList: MutableList = 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) { - 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)) - } - } - } + private fun saveCurrentToDos() { + sharedPreferences.edit { + putString("to_do_tasks", moshiAdapter.toJson(completedToDos)) } } diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/util/HabiticaCoroutineExceptionhandler.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/util/HabiticaCoroutineExceptionhandler.kt index a97962e85..b9532eb9f 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/util/HabiticaCoroutineExceptionhandler.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/util/HabiticaCoroutineExceptionhandler.kt @@ -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() } }