mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-17 19:29:02 +00:00
Implement task sorting
This commit is contained in:
parent
442ea640ca
commit
8d2fd994d1
11 changed files with 59 additions and 22 deletions
|
|
@ -1,6 +1,15 @@
|
|||
package com.habitrpg.common.habitica.models.tasks
|
||||
|
||||
class TasksOrder {
|
||||
fun positionOf(key: String, type: TaskType): Int {
|
||||
return when (type) {
|
||||
TaskType.HABIT -> habits.indexOf(key)
|
||||
TaskType.DAILY -> dailys.indexOf(key)
|
||||
TaskType.TODO -> todos.indexOf(key)
|
||||
TaskType.REWARD -> rewards.indexOf(key)
|
||||
}
|
||||
}
|
||||
|
||||
var habits: List<String> = listOf()
|
||||
var dailys: List<String> = listOf()
|
||||
var todos: List<String> = listOf()
|
||||
|
|
|
|||
|
|
@ -79,6 +79,8 @@ dependencies {
|
|||
implementation "com.google.dagger:hilt-android:$daggerhilt_version"
|
||||
kapt "com.google.dagger:hilt-compiler:$daggerhilt_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import com.habitrpg.wearos.habitica.ui.activities.BaseActivity
|
|||
import com.habitrpg.wearos.habitica.ui.activities.FaintActivity
|
||||
import com.habitrpg.wearos.habitica.ui.activities.RYAActivity
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
|
@ -42,9 +43,13 @@ class MainApplication : Application() {
|
|||
}
|
||||
}.collect()
|
||||
}
|
||||
MainScope().launch {
|
||||
userRepository.retrieveUser()
|
||||
taskRepository.retrieveTasks()
|
||||
if (userRepository.hasAuthentication) {
|
||||
MainScope().launch(CoroutineExceptionHandler { coroutineContext, throwable ->
|
||||
|
||||
}) {
|
||||
val user = userRepository.retrieveUser()
|
||||
taskRepository.retrieveTasks(user?.tasksOrder)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -161,4 +161,5 @@ class ApiClient @Inject constructor(
|
|||
process(apiService.scoreTask(id, direction))
|
||||
|
||||
suspend fun createTask(task: Task) = process(apiService.createTask(task))
|
||||
fun hasAuthentication() = hostConfig.hasAuthentication()
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.habitrpg.wearos.habitica.data.repositories
|
|||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.asFlow
|
||||
import com.habitrpg.common.habitica.models.tasks.TaskType
|
||||
import com.habitrpg.common.habitica.models.tasks.TasksOrder
|
||||
import com.habitrpg.wearos.habitica.models.tasks.Task
|
||||
import com.habitrpg.wearos.habitica.models.tasks.TaskList
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -28,24 +29,40 @@ class TaskLocalRepository @Inject constructor() {
|
|||
return tasks[type]?.asFlow() ?: emptyFlow()
|
||||
}
|
||||
|
||||
fun saveTasks(tasks: TaskList) {
|
||||
fun saveTasks(tasks: TaskList, order: TasksOrder?) {
|
||||
val taskMap = mutableMapOf(
|
||||
TaskType.HABIT to mutableListOf<Task>(),
|
||||
TaskType.DAILY to mutableListOf<Task>(),
|
||||
TaskType.TODO to mutableListOf<Task>(),
|
||||
TaskType.REWARD to mutableListOf<Task>()
|
||||
TaskType.HABIT to sortTasks(tasks.tasks, order?.habits ?: emptyList(), TaskType.HABIT),
|
||||
TaskType.DAILY to sortTasks(tasks.tasks, order?.dailys ?: emptyList(), TaskType.DAILY),
|
||||
TaskType.TODO to sortTasks(tasks.tasks, order?.todos ?: emptyList(), TaskType.TODO),
|
||||
TaskType.REWARD to sortTasks(tasks.tasks, order?.rewards ?: emptyList(), TaskType.REWARD)
|
||||
)
|
||||
for (task in tasks.tasks) {
|
||||
if (task.value.type != null) {
|
||||
taskMap[task.value.type]?.add(task.value)
|
||||
}
|
||||
}
|
||||
for (type in taskMap) {
|
||||
this.tasks[type.key]?.value = type.value
|
||||
}
|
||||
taskCountHelperValue.value = Date().time
|
||||
}
|
||||
|
||||
private fun sortTasks(taskMap: MutableMap<String, Task>, taskOrder: List<String>, type: TaskType): List<Task> {
|
||||
val taskList = ArrayList<Task>()
|
||||
var position = 0
|
||||
for (taskId in taskOrder) {
|
||||
val task = taskMap[taskId]
|
||||
if (task != null) {
|
||||
task.position = position
|
||||
taskList.add(task)
|
||||
position++
|
||||
taskMap.remove(taskId)
|
||||
}
|
||||
}
|
||||
for (task in taskMap.values) {
|
||||
if (task.type != type) continue
|
||||
task.position = position
|
||||
taskList.add(task)
|
||||
position++
|
||||
}
|
||||
return taskList
|
||||
}
|
||||
|
||||
fun updateTask(task: Task) {
|
||||
val oldList = tasks[task.type]?.value?.toMutableList()
|
||||
val index = oldList?.indexOfFirst { task.id == it.id }
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.habitrpg.wearos.habitica.data.repositories
|
|||
import com.habitrpg.common.habitica.models.responses.TaskDirection
|
||||
import com.habitrpg.common.habitica.models.responses.TaskScoringResult
|
||||
import com.habitrpg.common.habitica.models.tasks.TaskType
|
||||
import com.habitrpg.common.habitica.models.tasks.TasksOrder
|
||||
import com.habitrpg.wearos.habitica.data.ApiClient
|
||||
import com.habitrpg.wearos.habitica.models.tasks.Task
|
||||
import com.habitrpg.wearos.habitica.models.tasks.TaskList
|
||||
|
|
@ -13,9 +14,9 @@ import javax.inject.Inject
|
|||
|
||||
class TaskRepository @Inject constructor(val apiClient: ApiClient, val localRepository: TaskLocalRepository, val userLocalRepository: UserLocalRepository) {
|
||||
|
||||
suspend fun retrieveTasks(): TaskList? {
|
||||
suspend fun retrieveTasks(order: TasksOrder?): TaskList? {
|
||||
val tasks = apiClient.getTasks()
|
||||
tasks?.let { localRepository.saveTasks(tasks) }
|
||||
tasks?.let { localRepository.saveTasks(tasks, order) }
|
||||
return tasks
|
||||
}
|
||||
fun getTasks(taskType: TaskType) = localRepository.getTasks(taskType)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ import com.habitrpg.wearos.habitica.models.user.User
|
|||
import javax.inject.Inject
|
||||
|
||||
class UserRepository @Inject constructor(val apiClient: ApiClient, val localRepository: UserLocalRepository) {
|
||||
|
||||
val hasAuthentication: Boolean
|
||||
get() = apiClient.hasAuthentication()
|
||||
fun getUser() = localRepository.getUser()
|
||||
|
||||
suspend fun retrieveUser(): User? {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
package com.habitrpg.wearos.habitica.models.user
|
||||
|
||||
import com.habitrpg.common.habitica.models.Avatar
|
||||
import com.habitrpg.common.habitica.models.tasks.TasksOrder
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class User: Avatar {
|
||||
val tasksOrder: TasksOrder? = null
|
||||
val isDead: Boolean
|
||||
get() = (stats?.hp ?: 0.0) <= 0.0
|
||||
override val currentMount: String?
|
||||
|
|
|
|||
|
|
@ -31,8 +31,8 @@ class SettingsViewModel @Inject constructor(userRepository: UserRepository,
|
|||
fun resyncData() {
|
||||
viewModelScope.launch(exceptionBuilder.userFacing(this)) {
|
||||
loadingManager.startLoading()
|
||||
userRepository.retrieveUser()
|
||||
taskRepository.retrieveTasks()
|
||||
val user = userRepository.retrieveUser()
|
||||
taskRepository.retrieveTasks(user?.tasksOrder)
|
||||
loadingManager.endLoading()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import androidx.core.content.edit
|
|||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.android.gms.wearable.MessageClient
|
||||
import com.google.android.gms.wearable.MessageEvent
|
||||
import com.habitrpg.common.habitica.api.HostConfig
|
||||
import com.habitrpg.common.habitica.helpers.KeyHelper
|
||||
import com.habitrpg.wearos.habitica.data.ApiClient
|
||||
import com.habitrpg.wearos.habitica.data.repositories.UserRepository
|
||||
|
|
@ -18,7 +17,6 @@ import javax.inject.Inject
|
|||
@HiltViewModel
|
||||
class SplashViewModel @Inject constructor(userRepository: UserRepository,
|
||||
exceptionBuilder: ExceptionHandlerBuilder,
|
||||
val hostConfig: HostConfig,
|
||||
val apiClient: ApiClient,
|
||||
val sharedPreferences: SharedPreferences,
|
||||
val keyHelper: KeyHelper?, loadingManager: LoadingManager
|
||||
|
|
@ -26,7 +24,7 @@ class SplashViewModel @Inject constructor(userRepository: UserRepository,
|
|||
lateinit var onLoginCompleted: (Boolean) -> Unit
|
||||
val hasAuthentication: Boolean
|
||||
get() {
|
||||
return hostConfig.hasAuthentication()
|
||||
return apiClient.hasAuthentication()
|
||||
}
|
||||
|
||||
override fun onMessageReceived(event: MessageEvent) {
|
||||
|
|
|
|||
|
|
@ -41,7 +41,8 @@ class TaskListViewModel @Inject constructor(
|
|||
|
||||
fun retrieveTasks() {
|
||||
viewModelScope.launch(exceptionBuilder.userFacing(this)) {
|
||||
taskRepository.retrieveTasks()
|
||||
val user = userRepository.retrieveUser()
|
||||
taskRepository.retrieveTasks(user?.tasksOrder)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue