From b2f3c8ecbeabe8a43e037d0e82bcd54f6d22a1e5 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Sun, 20 Aug 2023 10:58:02 +0200 Subject: [PATCH] feat(channelGroups): disallow creation of already existing groups --- .../libretube/db/dao/SubscriptionGroupsDao.kt | 3 ++ .../ui/sheets/EditChannelGroupSheet.kt | 28 +++++++++++++------ app/src/main/res/values/strings.xml | 3 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt b/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt index 56fa1e1878..7b4279f4d9 100644 --- a/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt @@ -11,6 +11,9 @@ interface SubscriptionGroupsDao { @Query("SELECT * FROM subscriptionGroups") suspend fun getAll(): List + @Query("SELECT EXISTS(SELECT * FROM subscriptionGroups WHERE name = :name)") + suspend fun exists(name: String): Boolean + @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun createGroup(subscriptionGroup: SubscriptionGroup) diff --git a/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt index 89fe182a62..c8ff203152 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt @@ -13,11 +13,13 @@ import com.github.libretube.R import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.obj.Subscription import com.github.libretube.databinding.DialogEditChannelGroupBinding +import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.SubscriptionGroup import com.github.libretube.ui.adapters.SubscriptionGroupChannelsAdapter import com.github.libretube.ui.models.SubscriptionsViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext class EditChannelGroupSheet( @@ -32,7 +34,7 @@ class EditChannelGroupSheet( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { binding = DialogEditChannelGroupBinding.inflate(layoutInflater) binding.groupName.setText(group.name) @@ -92,15 +94,25 @@ class EditChannelGroupSheet( private fun updateConfirmStatus() { with(binding) { - val isGroupNameBlank = groupName.text?.isBlank() == true + val name = groupName.text.toString() + groupName.error = getGroupNameError(name) - groupName.error = if (isGroupNameBlank) { - requireContext().getString(R.string.group_name_error) - } else { - null - } + confirm.isEnabled = groupName.error == null && group.channels.isNotEmpty() + } + } - confirm.isEnabled = !isGroupNameBlank && group.channels.isNotEmpty() + private fun getGroupNameError(name: String): String? { + if (name.isBlank()) { + return getString(R.string.group_name_error_empty) } + + val groupExists = runBlocking(Dispatchers.IO) { + DatabaseHolder.Database.subscriptionGroupsDao().exists(name) + } + if (groupExists) { + return getString(R.string.group_name_error_exists) + } + + return null } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a440d178e3..aad5052704 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -406,7 +406,8 @@ Channel groups New Group name - Please enter a name + Please enter a name + Please choose a name that is unique Edit group Play automatically Start playing video automatically when selecting