mirror of
https://codeberg.org/timelimit/opentimelimit-android.git
synced 2025-10-05 10:49:29 +02:00
Add UI to allow selecting a category for unassigned Apps
This commit is contained in:
parent
2f2d6cef44
commit
45706a509c
9 changed files with 205 additions and 0 deletions
|
@ -59,4 +59,7 @@ abstract class UserDao {
|
|||
|
||||
@Query("SELECT * FROM user LIMIT :pageSize OFFSET :offset")
|
||||
abstract fun getUserPageSync(offset: Int, pageSize: Int): List<User>
|
||||
|
||||
@Query("UPDATE user SET category_for_not_assigned_apps = :categoryId WHERE id = :childId")
|
||||
abstract fun updateCategoryForUnassignedApps(childId: String, categoryId: String)
|
||||
}
|
||||
|
|
|
@ -126,6 +126,17 @@ data class UpdateCategoryTemporarilyBlockedAction(val categoryId: String, val bl
|
|||
IdGenerator.assertIdValid(categoryId)
|
||||
}
|
||||
}
|
||||
data class SetCategoryForUnusedApps(val childId: String, val categoryId: String): ParentAction() {
|
||||
// category id can be empty
|
||||
|
||||
init {
|
||||
IdGenerator.assertIdValid(childId)
|
||||
|
||||
if (categoryId.isNotEmpty()) {
|
||||
IdGenerator.assertIdValid(categoryId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeviceDao
|
||||
|
||||
|
|
|
@ -264,6 +264,22 @@ object LocalDatabaseParentActionDispatcher {
|
|||
|
||||
database.device().updateDeviceEntry(deviceEntry)
|
||||
}
|
||||
is SetCategoryForUnusedApps -> {
|
||||
DatabaseValidation.assertChildExists(database, action.childId)
|
||||
|
||||
if (action.categoryId.isNotEmpty()) {
|
||||
val category = database.category().getCategoryByIdSync(action.categoryId)!!
|
||||
|
||||
if (category.childId != action.childId) {
|
||||
throw IllegalArgumentException("category does not belong to child")
|
||||
}
|
||||
}
|
||||
|
||||
database.user().updateCategoryForUnassignedApps(
|
||||
categoryId = action.categoryId,
|
||||
childId = action.childId
|
||||
)
|
||||
}
|
||||
}.let { }
|
||||
|
||||
database.setTransactionSuccessful()
|
||||
|
|
|
@ -49,6 +49,15 @@ class CategorySettingsFragment : Fragment() {
|
|||
|
||||
val categoryEntry = appLogic.database.category().getCategoryByChildIdAndId(params.childId, params.categoryId)
|
||||
|
||||
ManageCategoryForUnassignedApps.bind(
|
||||
binding = binding.categoryForUnassignedApps,
|
||||
lifecycleOwner = this,
|
||||
categoryId = params.categoryId,
|
||||
childId = params.childId,
|
||||
database = appLogic.database,
|
||||
auth = auth
|
||||
)
|
||||
|
||||
binding.btnDeleteCategory.setOnClickListener { deleteCategory() }
|
||||
binding.editCategoryTitleGo.setOnClickListener { renameCategory() }
|
||||
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package io.timelimit.android.ui.manage.category.settings
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.Observer
|
||||
import io.timelimit.android.data.Database
|
||||
import io.timelimit.android.databinding.ManageCategoryForUnassignedAppsBinding
|
||||
import io.timelimit.android.livedata.ignoreUnchanged
|
||||
import io.timelimit.android.livedata.map
|
||||
import io.timelimit.android.sync.actions.SetCategoryForUnusedApps
|
||||
import io.timelimit.android.ui.main.ActivityViewModel
|
||||
|
||||
object ManageCategoryForUnassignedApps {
|
||||
fun bind(
|
||||
binding: ManageCategoryForUnassignedAppsBinding,
|
||||
categoryId: String,
|
||||
childId: String,
|
||||
auth: ActivityViewModel,
|
||||
database: Database,
|
||||
lifecycleOwner: LifecycleOwner
|
||||
) {
|
||||
val userEntry = database.user().getUserByIdLive(childId)
|
||||
val isCurrentlyChosen = userEntry.map { it?.categoryForNotAssignedApps == categoryId }.ignoreUnchanged()
|
||||
|
||||
isCurrentlyChosen.observe(lifecycleOwner, Observer { binding.isThisCategoryUsed = it })
|
||||
|
||||
binding.changeModeButton.setOnClickListener {
|
||||
val chosen = isCurrentlyChosen.value
|
||||
|
||||
if (chosen == true) {
|
||||
auth.tryDispatchParentAction(
|
||||
SetCategoryForUnusedApps(
|
||||
childId = childId,
|
||||
categoryId = ""
|
||||
)
|
||||
)
|
||||
} else if (chosen == false) {
|
||||
auth.tryDispatchParentAction(
|
||||
SetCategoryForUnusedApps(
|
||||
childId = childId,
|
||||
categoryId = categoryId
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue