From 4402bf1baf3d808ca285809606953e3ed9e192df Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 28 Mar 2023 17:40:48 +0200 Subject: [PATCH] Fix some minor subscription group issues --- .../libretube/db/dao/SubscriptionGroupsDao.kt | 4 ---- .../ui/adapters/SubscriptionGroupsAdapter.kt | 11 ++++++--- .../ui/dialogs/ChannelGroupsDialog.kt | 3 ++- .../ui/fragments/SubscriptionsFragment.kt | 24 ++++++++++++------- .../res/layout/dialog_edit_channel_group.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 27 insertions(+), 17 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 51bbaa484..22fc79a37 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 @@ -4,7 +4,6 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query -import androidx.room.Update import com.github.libretube.db.obj.SubscriptionGroup @Dao() @@ -15,9 +14,6 @@ interface SubscriptionGroupsDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun createGroup(subscriptionGroup: SubscriptionGroup) - @Update - suspend fun updateGroup(subscriptionGroup: SubscriptionGroup) - @Query("DELETE FROM subscriptionGroups WHERE name = :name") suspend fun deleteGroup(name: String) } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionGroupsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionGroupsAdapter.kt index 9f9097241..ec4422129 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionGroupsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionGroupsAdapter.kt @@ -9,6 +9,7 @@ import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.SubscriptionGroup import com.github.libretube.ui.dialogs.EditChannelGroupDialog import com.github.libretube.ui.viewholders.SubscriptionGroupsViewHolder +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking class SubscriptionGroupsAdapter( @@ -33,7 +34,7 @@ class SubscriptionGroupsAdapter( groupName.text = subscriptionGroup.name deleteGroup.setOnClickListener { groups.remove(subscriptionGroup) - runBlocking { + runBlocking(Dispatchers.IO) { DatabaseHolder.Database.subscriptionGroupsDao().deleteGroup( subscriptionGroup.name ) @@ -44,8 +45,12 @@ class SubscriptionGroupsAdapter( editGroup.setOnClickListener { EditChannelGroupDialog(subscriptionGroup) { groups[position] = it - runBlocking { - DatabaseHolder.Database.subscriptionGroupsDao().updateGroup(it) + runBlocking(Dispatchers.IO) { + // delete the old one as it might have a different name + DatabaseHolder.Database.subscriptionGroupsDao().deleteGroup( + subscriptionGroup.name + ) + DatabaseHolder.Database.subscriptionGroupsDao().createGroup(it) } notifyItemChanged(position) onGroupsChanged(groups) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt index e33ddd02d..0c6cd335a 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt @@ -10,6 +10,7 @@ import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.SubscriptionGroup import com.github.libretube.ui.adapters.SubscriptionGroupsAdapter import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking class ChannelGroupsDialog( @@ -33,7 +34,7 @@ class ChannelGroupsDialog( .setPositiveButton(R.string.okay, null) .setNeutralButton(R.string.new_group) { _, _ -> EditChannelGroupDialog(SubscriptionGroup("", mutableListOf())) { - runBlocking { + runBlocking(Dispatchers.IO) { DatabaseHolder.Database.subscriptionGroupsDao().createGroup(it) } groups.add(it) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt index 46f3cbb72..3db87c0ca 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt @@ -166,25 +166,26 @@ class SubscriptionsFragment : Fragment() { channelGroups = DatabaseHolder.Database.subscriptionGroupsDao().getAll() binding.chipAll.isSelected = true - binding.channelGroups.children.forEachIndexed { index, view -> - if (index != 0) binding.channelGroups.removeView(view) + binding.channelGroups.children.forEach { view -> + if (view.id != R.id.chip_all) binding.channelGroups.removeView(view) } - channelGroups.forEachIndexed { index, group -> + channelGroups.forEach { group -> val chip = Chip(context, null, R.style.ElevatedFilterChip).apply { id = View.generateViewId() isCheckable = true isClickable = true text = group.name - setOnClickListener { - selectedFilterGroup = index + 1 // since the first one is "All" - showFeed() - } } binding.channelGroups.addView(chip) } + binding.channelGroups.setOnCheckedStateChangeListener { group, checkedIds -> + selectedFilterGroup = group.children.indexOfFirst { it.id == checkedIds.first() } + showFeed() + } + binding.editGroups.setOnClickListener { ChannelGroupsDialog(channelGroups.toMutableList()) { lifecycleScope.launch { initChannelGroups() } @@ -203,7 +204,8 @@ class SubscriptionsFragment : Fragment() { true } else { val channelId = streamItem.uploaderUrl.orEmpty().toID() - channelGroups.getOrNull(selectedFilterGroup + 1)?.channels?.contains(channelId) != false + val group = channelGroups.getOrNull(selectedFilterGroup - 1) + group?.channels?.contains(channelId) != false } } .filter { @@ -216,7 +218,11 @@ class SubscriptionsFragment : Fragment() { } }.let { streams -> runBlocking { - if (!PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false)) { + if (!PreferenceHelper.getBoolean( + PreferenceKeys.HIDE_WATCHED_FROM_FEED, + false + ) + ) { streams } else { removeWatchVideosFromFeed(streams) diff --git a/app/src/main/res/layout/dialog_edit_channel_group.xml b/app/src/main/res/layout/dialog_edit_channel_group.xml index ccc16a090..b30c4b07d 100644 --- a/app/src/main/res/layout/dialog_edit_channel_group.xml +++ b/app/src/main/res/layout/dialog_edit_channel_group.xml @@ -8,6 +8,7 @@ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8ecc5c8b0..c15b448ef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -460,6 +460,7 @@ New Group name Edit group + Group already exists! Download Service