Skip to content

Commit ed68e3b

Browse files
committed
Fully working toggle button that change between vertical and horizontal view
1 parent 082d7a3 commit ed68e3b

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import org.schabi.newpipe.local.subscription.dialog.FeedGroupReorderDialog
4545
import org.schabi.newpipe.local.subscription.item.ChannelItem
4646
import org.schabi.newpipe.local.subscription.item.EmptyPlaceholderItem
4747
import org.schabi.newpipe.local.subscription.item.FeedGroupAddItem
48-
import org.schabi.newpipe.local.subscription.item.FeedGroupAddItemVertical
48+
import org.schabi.newpipe.local.subscription.item.FeedGroupAddVerticalItem
4949
import org.schabi.newpipe.local.subscription.item.FeedGroupCardItem
5050
import org.schabi.newpipe.local.subscription.item.FeedGroupCardVerticalItem
5151
import org.schabi.newpipe.local.subscription.item.FeedGroupCarouselItem
@@ -78,10 +78,12 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
7878

7979
private val groupAdapter = GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>()
8080
private val feedGroupsSection = Section()
81+
private val feedGroupsVerticalSection = Section()
8182
private var feedGroupsCarousel: FeedGroupCarouselItem? = null
83+
private var feedGroupsVerticalCarousel: FeedGroupCarouselItem? = null
8284
private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem
8385
private val subscriptionsSection = Section()
84-
private var listView: Boolean = false
86+
private var defaultListView: Boolean = true
8587

8688
private val requestExportLauncher =
8789
registerForActivityResult(StartActivityForResult(), this::requestExportResult)
@@ -255,7 +257,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
255257
// ////////////////////////////////////////////////////////////////////////
256258

257259
private fun setupInitialLayout() {
258-
listView = false
260+
defaultListView = true
259261
Section().apply {
260262
val carouselAdapter = GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>()
261263

@@ -302,16 +304,17 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
302304
listOf(subscriptionsSection)
303305
)
304306
)
307+
view?.let { initViews(it, savedInstanceState = Bundle()) }
305308
}
306309

307310
private fun changeLayout() {
308-
listView = true
311+
defaultListView = false
309312
Section().apply {
310313
val carouselAdapter = GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>()
311314

312315
carouselAdapter.add(FeedGroupCardVerticalItem(-1, getString(R.string.all), FeedGroupIcon.RSS))
313-
carouselAdapter.add(feedGroupsSection)
314-
carouselAdapter.add(FeedGroupAddItemVertical())
316+
carouselAdapter.add(feedGroupsVerticalSection)
317+
carouselAdapter.add(FeedGroupAddVerticalItem())
315318

316319
carouselAdapter.setOnItemClickListener { item, _ ->
317320
listenerFeedVerticalGroups.selected(item)
@@ -325,7 +328,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
325328
listenerFeedVerticalGroups.held(item)
326329
return@setOnItemLongClickListener true
327330
}
328-
feedGroupsCarousel = FeedGroupCarouselItem(requireContext(), carouselAdapter, RecyclerView.VERTICAL)
331+
feedGroupsVerticalCarousel = FeedGroupCarouselItem(requireContext(), carouselAdapter, RecyclerView.VERTICAL)
329332

330333
feedGroupsSortMenuItem = HeaderWithMenuItem(
331334
getString(R.string.feed_groups_header_title),
@@ -334,7 +337,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
334337
listViewOnClickListener = ::setupInitialLayout,
335338
menuItemOnClickListener = ::openReorderDialog
336339
)
337-
add(Section(feedGroupsSortMenuItem, listOf(feedGroupsCarousel)))
340+
add(Section(feedGroupsSortMenuItem, listOf(feedGroupsVerticalCarousel)))
338341
groupAdapter.clear()
339342
groupAdapter.add(this)
340343
}
@@ -349,6 +352,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
349352
listOf(subscriptionsSection)
350353
)
351354
)
355+
view?.let { initViews(it, savedInstanceState = Bundle()) }
352356
}
353357

354358
override fun initViews(rootView: View, savedInstanceState: Bundle?) {
@@ -427,7 +431,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
427431
override fun selected(selectedItem: Item<*>?) {
428432
when (selectedItem) {
429433
is FeedGroupCardVerticalItem -> NavigationHelper.openFeedFragment(fm, selectedItem.groupId, selectedItem.name)
430-
is FeedGroupAddItemVertical -> FeedGroupDialog.newInstance().show(fm, null)
434+
is FeedGroupAddVerticalItem -> FeedGroupDialog.newInstance().show(fm, null)
431435
}
432436
}
433437

@@ -480,7 +484,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
480484
}
481485

482486
private fun handleFeedGroups(groups: List<Group>) {
483-
if (!listView) {
487+
if (defaultListView) {
484488
feedGroupsSection.update(groups)
485489

486490
if (feedGroupsListState != null) {
@@ -494,11 +498,11 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
494498
}
495499

496500
private fun handleFeedGroupsVertical(groups: List<Group>) {
497-
if (listView) {
498-
feedGroupsSection.update(groups)
501+
if (!defaultListView) {
502+
feedGroupsVerticalSection.update(groups)
499503

500504
if (feedGroupsListVerticalState != null) {
501-
feedGroupsCarousel?.onRestoreInstanceState(feedGroupsListVerticalState)
505+
feedGroupsVerticalCarousel?.onRestoreInstanceState(feedGroupsListVerticalState)
502506
feedGroupsListVerticalState = null
503507
}
504508

app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupAddItemVertical.kt renamed to app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupAddVerticalItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.xwray.groupie.viewbinding.BindableItem
55
import org.schabi.newpipe.R
66
import org.schabi.newpipe.databinding.FeedGroupAddNewItemVerticalBinding
77

8-
class FeedGroupAddItemVertical : BindableItem<FeedGroupAddNewItemVerticalBinding>() {
8+
class FeedGroupAddVerticalItem : BindableItem<FeedGroupAddNewItemVerticalBinding>() {
99
override fun getLayout(): Int = R.layout.feed_group_add_new_item_vertical
1010
override fun bind(viewBinding: FeedGroupAddNewItemVerticalBinding, position: Int) {}
1111
override fun initializeViewBinding(view: View) = FeedGroupAddNewItemVerticalBinding.bind(view)

0 commit comments

Comments
 (0)