From 8cf4f76d4ec9bf4a1558689fe91c0bce41cbf5cd Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 8 May 2023 17:59:55 +0200 Subject: [PATCH] Convert channel group dialogs to bottom sheets --- .../ui/adapters/SubscriptionGroupsAdapter.kt | 4 +- .../ui/dialogs/ChannelGroupsDialog.kt | 47 ----------------- .../ui/fragments/SubscriptionsFragment.kt | 4 +- .../libretube/ui/sheets/ChannelGroupsSheet.kt | 51 +++++++++++++++++++ .../EditChannelGroupSheet.kt} | 41 ++++++++------- .../res/layout/dialog_edit_channel_group.xml | 39 +++++++++++--- .../res/layout/dialog_subscription_groups.xml | 39 ++++++++++++-- .../layout/subscription_group_channel_row.xml | 2 +- .../res/layout/subscription_group_row.xml | 2 +- 9 files changed, 147 insertions(+), 82 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt create mode 100644 app/src/main/java/com/github/libretube/ui/sheets/ChannelGroupsSheet.kt rename app/src/main/java/com/github/libretube/ui/{dialogs/EditChannelGroupDialog.kt => sheets/EditChannelGroupSheet.kt} (74%) 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 03c1c402fe..a54be1c274 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 @@ -7,7 +7,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.SubscriptionGroupRowBinding 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.sheets.EditChannelGroupSheet import com.github.libretube.ui.viewholders.SubscriptionGroupsViewHolder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking @@ -44,7 +44,7 @@ class SubscriptionGroupsAdapter( notifyItemRangeChanged(position, itemCount) } editGroup.setOnClickListener { - EditChannelGroupDialog(subscriptionGroup) { + EditChannelGroupSheet(subscriptionGroup) { groups[position] = it runBlocking(Dispatchers.IO) { // delete the old one as it might have a different name 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 deleted file mode 100644 index 0c6cd335a7..0000000000 --- a/app/src/main/java/com/github/libretube/ui/dialogs/ChannelGroupsDialog.kt +++ /dev/null @@ -1,47 +0,0 @@ -package com.github.libretube.ui.dialogs - -import android.app.Dialog -import android.os.Bundle -import androidx.fragment.app.DialogFragment -import androidx.recyclerview.widget.LinearLayoutManager -import com.github.libretube.R -import com.github.libretube.databinding.DialogSubscriptionGroupsBinding -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( - private val groups: MutableList, - private val onGroupsChanged: (List) -> Unit -) : DialogFragment() { - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val binding = DialogSubscriptionGroupsBinding.inflate(layoutInflater) - - binding.groupsRV.layoutManager = LinearLayoutManager(context) - val adapter = SubscriptionGroupsAdapter( - groups.toMutableList(), - parentFragmentManager, - onGroupsChanged - ) - binding.groupsRV.adapter = adapter - - return MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.channel_groups) - .setView(binding.root) - .setPositiveButton(R.string.okay, null) - .setNeutralButton(R.string.new_group) { _, _ -> - EditChannelGroupDialog(SubscriptionGroup("", mutableListOf())) { - runBlocking(Dispatchers.IO) { - DatabaseHolder.Database.subscriptionGroupsDao().createGroup(it) - } - groups.add(it) - adapter.insertItem(it) - onGroupsChanged(groups) - }.show(parentFragmentManager, null) - } - .create() - } -} 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 f0ce007059..0cd24f9107 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 @@ -25,9 +25,9 @@ import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.adapters.LegacySubscriptionAdapter import com.github.libretube.ui.adapters.SubscriptionChannelAdapter import com.github.libretube.ui.adapters.VideosAdapter -import com.github.libretube.ui.dialogs.ChannelGroupsDialog import com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.sheets.BaseBottomSheet +import com.github.libretube.ui.sheets.ChannelGroupsSheet import com.google.android.material.chip.Chip import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -197,7 +197,7 @@ class SubscriptionsFragment : Fragment() { } binding.editGroups.setOnClickListener { - ChannelGroupsDialog(channelGroups.toMutableList()) { + ChannelGroupsSheet(channelGroups.toMutableList()) { lifecycleScope.launch { initChannelGroups() } }.show(childFragmentManager, null) } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/ChannelGroupsSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/ChannelGroupsSheet.kt new file mode 100644 index 0000000000..7cca64f7be --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/sheets/ChannelGroupsSheet.kt @@ -0,0 +1,51 @@ +package com.github.libretube.ui.sheets + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.LinearLayoutManager +import com.github.libretube.databinding.DialogSubscriptionGroupsBinding +import com.github.libretube.db.DatabaseHolder +import com.github.libretube.db.obj.SubscriptionGroup +import com.github.libretube.ui.adapters.SubscriptionGroupsAdapter +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking + +class ChannelGroupsSheet( + private val groups: MutableList, + private val onGroupsChanged: (List) -> Unit +) : ExpandedBottomSheet() { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + val binding = DialogSubscriptionGroupsBinding.inflate(layoutInflater) + + binding.groupsRV.layoutManager = LinearLayoutManager(context) + val adapter = SubscriptionGroupsAdapter( + groups.toMutableList(), + parentFragmentManager, + onGroupsChanged + ) + binding.groupsRV.adapter = adapter + + binding.newGroup.setOnClickListener { + EditChannelGroupSheet(SubscriptionGroup("", mutableListOf())) { + runBlocking(Dispatchers.IO) { + DatabaseHolder.Database.subscriptionGroupsDao().createGroup(it) + } + groups.add(it) + adapter.insertItem(it) + onGroupsChanged(groups) + }.show(parentFragmentManager, null) + } + + binding.confirm.setOnClickListener { + dismiss() + } + + return binding.root + } +} diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/EditChannelGroupDialog.kt b/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt similarity index 74% rename from app/src/main/java/com/github/libretube/ui/dialogs/EditChannelGroupDialog.kt rename to app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt index 2204d2c4d1..7924ea98aa 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/EditChannelGroupDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt @@ -1,34 +1,37 @@ -package com.github.libretube.ui.dialogs +package com.github.libretube.ui.sheets -import android.app.Dialog import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import androidx.core.view.isVisible import androidx.core.widget.addTextChangedListener -import androidx.fragment.app.DialogFragment 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 import com.github.libretube.db.obj.SubscriptionGroup import com.github.libretube.ui.adapters.SubscriptionGroupChannelsAdapter import com.github.libretube.ui.models.SubscriptionsViewModel -import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class EditChannelGroupDialog( +class EditChannelGroupSheet( private var group: SubscriptionGroup, private val onGroupChanged: (SubscriptionGroup) -> Unit -) : DialogFragment() { +) : ExpandedBottomSheet() { private val subscriptionsModel: SubscriptionsViewModel by activityViewModels() private lateinit var binding: DialogEditChannelGroupBinding private var channels: List = listOf() - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { binding = DialogEditChannelGroupBinding.inflate(layoutInflater) binding.groupName.setText(group.name) @@ -39,16 +42,18 @@ class EditChannelGroupDialog( showChannels(channels, it?.toString()) } - return MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.edit_group) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.okay) { _, _ -> - group.name = binding.groupName.text.toString() - if (group.name.isBlank()) return@setPositiveButton - onGroupChanged(group) - } - .setView(binding.root) - .create() + binding.cancel.setOnClickListener { + dismiss() + } + + binding.confirm.setOnClickListener { + group.name = binding.groupName.text.toString() + if (group.name.isBlank()) return@setOnClickListener + onGroupChanged(group) + dismiss() + } + + return binding.root } private fun fetchSubscriptions() { 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 b30c4b07da..e63296b72b 100644 --- a/app/src/main/res/layout/dialog_edit_channel_group.xml +++ b/app/src/main/res/layout/dialog_edit_channel_group.xml @@ -1,9 +1,9 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + android:layout_height="wrap_content"/> @@ -37,7 +37,7 @@ + android:layout_height="wrap_content"/> @@ -45,7 +45,30 @@ android:id="@+id/channelsRV" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="10dp" /> + android:layout_marginTop="10dp"/> + + + + + + + + @@ -54,6 +77,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" - android:layout_marginVertical="200dp" /> + android:layout_marginVertical="20dp"/> \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_subscription_groups.xml b/app/src/main/res/layout/dialog_subscription_groups.xml index 623eb5943f..049ec40d43 100644 --- a/app/src/main/res/layout/dialog_subscription_groups.xml +++ b/app/src/main/res/layout/dialog_subscription_groups.xml @@ -1,12 +1,45 @@ + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + + + android:layout_marginTop="20dp"/> + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/subscription_group_channel_row.xml b/app/src/main/res/layout/subscription_group_channel_row.xml index 4255933d55..e5caed986f 100644 --- a/app/src/main/res/layout/subscription_group_channel_row.xml +++ b/app/src/main/res/layout/subscription_group_channel_row.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" - android:paddingHorizontal="20dp" > + android:paddingHorizontal="10dp" >