From 7f72a3eaebaccaa52a22abe6147d363363f5460e Mon Sep 17 00:00:00 2001 From: FineFindus Date: Sat, 19 Aug 2023 13:47:57 +0200 Subject: [PATCH 1/2] feat(channelGroups): disable confirm button if creation is not possible --- .../ui/sheets/EditChannelGroupSheet.kt | 21 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 2 files changed, 22 insertions(+) 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 728f6a59a..89fe182a6 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 @@ -9,6 +9,7 @@ import androidx.core.widget.addTextChangedListener import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager +import com.github.libretube.R import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.obj.Subscription import com.github.libretube.databinding.DialogEditChannelGroupBinding @@ -38,6 +39,10 @@ class EditChannelGroupSheet( binding.channelsRV.layoutManager = LinearLayoutManager(context) fetchSubscriptions() + binding.groupName.addTextChangedListener { + updateConfirmStatus() + } + binding.searchInput.addTextChangedListener { showChannels(channels, it?.toString()) } @@ -46,6 +51,7 @@ class EditChannelGroupSheet( dismiss() } + updateConfirmStatus() binding.confirm.setOnClickListener { group.name = binding.groupName.text.toString() if (group.name.isBlank()) return@setOnClickListener @@ -78,8 +84,23 @@ class EditChannelGroupSheet( group ) { group = it + updateConfirmStatus() } binding.subscriptionsContainer.isVisible = true binding.progress.isVisible = false } + + private fun updateConfirmStatus() { + with(binding) { + val isGroupNameBlank = groupName.text?.isBlank() == true + + groupName.error = if (isGroupNameBlank) { + requireContext().getString(R.string.group_name_error) + } else { + null + } + + confirm.isEnabled = !isGroupNameBlank && group.channels.isNotEmpty() + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 816454555..a440d178e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -406,6 +406,7 @@ Channel groups New Group name + Please enter a name Edit group Play automatically Start playing video automatically when selecting From b2f3c8ecbeabe8a43e037d0e82bcd54f6d22a1e5 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Sun, 20 Aug 2023 10:58:02 +0200 Subject: [PATCH 2/2] feat(channelGroups): disallow creation of already existing groups --- .../libretube/db/dao/SubscriptionGroupsDao.kt | 3 ++ .../ui/sheets/EditChannelGroupSheet.kt | 30 +++++++++++++------ app/src/main/res/values/strings.xml | 3 +- 3 files changed, 26 insertions(+), 10 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 56fa1e187..7b4279f4d 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 89fe182a6..c8ff20315 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 = !isGroupNameBlank && group.channels.isNotEmpty() + confirm.isEnabled = groupName.error == null && 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 a440d178e..aad505270 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