Add UI to allow selecting a category for unassigned Apps

This commit is contained in:
Jonas L 2019-01-24 20:21:16 +01:00
parent 2f2d6cef44
commit 45706a509c
9 changed files with 205 additions and 0 deletions

View file

@ -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)
}

View file

@ -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

View file

@ -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()

View file

@ -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() }

View file

@ -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
)
)
}
}
}
}