Skip to content

Commit a69a197

Browse files
dhanuarfBnyro
authored andcommitted
fix: subscription list is displaying outdated data
Fetch subscription list on every lifecycle started in `SubscriptionsBottomSheet` and `EditChannelGroupSheet` to ensure the latest data is displayed.
1 parent 4312efd commit a69a197

3 files changed

Lines changed: 75 additions & 57 deletions

File tree

app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
9898
if (viewModel.videoFeed.value == null) {
9999
viewModel.fetchFeed(requireContext(), forceRefresh = false)
100100
}
101-
if (viewModel.subscriptions.value == null) {
102-
viewModel.fetchSubscriptions(requireContext())
103-
}
104101

105102
// only restore the previous state (i.e. scroll position) the first time the feed is shown
106103
// any other feed updates are caused by manual refreshing and thus should reset the scroll
@@ -131,7 +128,6 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
131128
}
132129

133130
binding.subRefresh.setOnRefreshListener {
134-
viewModel.fetchSubscriptions(requireContext())
135131
viewModel.fetchFeed(requireContext(), forceRefresh = true)
136132
}
137133

app/src/main/java/com/github/libretube/ui/sheets/EditChannelGroupSheet.kt

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import android.view.View
55
import androidx.core.view.isVisible
66
import androidx.core.widget.addTextChangedListener
77
import androidx.fragment.app.activityViewModels
8+
import androidx.lifecycle.Lifecycle
9+
import androidx.lifecycle.asFlow
810
import androidx.lifecycle.lifecycleScope
11+
import androidx.lifecycle.repeatOnLifecycle
912
import androidx.recyclerview.widget.LinearLayoutManager
1013
import com.github.libretube.R
11-
import com.github.libretube.api.SubscriptionHelper
1214
import com.github.libretube.api.obj.Subscription
1315
import com.github.libretube.databinding.DialogEditChannelGroupBinding
1416
import com.github.libretube.db.DatabaseHolder
@@ -18,9 +20,9 @@ import com.github.libretube.ui.models.EditChannelGroupsModel
1820
import com.github.libretube.ui.models.SubscriptionsViewModel
1921
import kotlinx.coroutines.CoroutineScope
2022
import kotlinx.coroutines.Dispatchers
23+
import kotlinx.coroutines.flow.collectLatest
2124
import kotlinx.coroutines.launch
2225
import kotlinx.coroutines.runBlocking
23-
import kotlinx.coroutines.withContext
2426

2527
class EditChannelGroupSheet : ExpandedBottomSheet(R.layout.dialog_edit_channel_group) {
2628
private var _binding: DialogEditChannelGroupBinding? = null
@@ -47,7 +49,6 @@ class EditChannelGroupSheet : ExpandedBottomSheet(R.layout.dialog_edit_channel_g
4749
val oldGroupName = channelGroupsModel.groupToEdit?.name.orEmpty()
4850

4951
binding.channelsRV.layoutManager = LinearLayoutManager(context)
50-
fetchSubscriptions()
5152

5253
binding.groupName.addTextChangedListener {
5354
updateConfirmStatus()
@@ -70,6 +71,22 @@ class EditChannelGroupSheet : ExpandedBottomSheet(R.layout.dialog_edit_channel_g
7071

7172
dismiss()
7273
}
74+
75+
lifecycleScope.launch {
76+
repeatOnLifecycle(Lifecycle.State.STARTED) {
77+
launch(Dispatchers.IO) {
78+
subscriptionsModel.fetchSubscriptions(requireContext())
79+
}
80+
launch {
81+
subscriptionsModel.subscriptions.asFlow().collectLatest { subscriptions ->
82+
subscriptions?.let {
83+
channels = it
84+
showChannels(it, null)
85+
}
86+
}
87+
}
88+
}
89+
}
7390
}
7491

7592
private fun saveGroup(group: SubscriptionGroup, oldGroupName: String) {
@@ -91,22 +108,6 @@ class EditChannelGroupSheet : ExpandedBottomSheet(R.layout.dialog_edit_channel_g
91108
_binding = null
92109
}
93110

94-
private fun fetchSubscriptions() {
95-
subscriptionsModel.subscriptions.value?.let {
96-
channels = it
97-
showChannels(it, null)
98-
return
99-
}
100-
lifecycleScope.launch(Dispatchers.IO) {
101-
channels = runCatching {
102-
SubscriptionHelper.getSubscriptions()
103-
}.getOrNull().orEmpty()
104-
withContext(Dispatchers.Main) {
105-
showChannels(channels, null)
106-
}
107-
}
108-
}
109-
110111
private fun showChannels(channels: List<Subscription>, query: String?) {
111112
binding.subscriptionsContainer.isVisible = true
112113
binding.progress.isVisible = false

app/src/main/java/com/github/libretube/ui/sheets/SubscriptionsBottomSheet.kt

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package com.github.libretube.ui.sheets
33
import android.annotation.SuppressLint
44
import android.os.Bundle
55
import android.view.View
6-
import androidx.core.content.ContextCompat
76
import androidx.core.view.isVisible
87
import androidx.core.widget.addTextChangedListener
98
import androidx.fragment.app.activityViewModels
9+
import androidx.lifecycle.Lifecycle
10+
import androidx.lifecycle.asFlow
11+
import androidx.lifecycle.lifecycleScope
12+
import androidx.lifecycle.repeatOnLifecycle
1013
import com.github.libretube.R
1114
import com.github.libretube.api.obj.Subscription
1215
import com.github.libretube.constants.PreferenceKeys
@@ -16,7 +19,11 @@ import com.github.libretube.helpers.PreferenceHelper
1619
import com.github.libretube.ui.adapters.SubscriptionChannelAdapter
1720
import com.github.libretube.ui.models.EditChannelGroupsModel
1821
import com.github.libretube.ui.models.SubscriptionsViewModel
19-
import java.util.Locale
22+
import kotlinx.coroutines.Dispatchers
23+
import kotlinx.coroutines.flow.collectLatest
24+
import kotlinx.coroutines.flow.combine
25+
import kotlinx.coroutines.flow.flowOn
26+
import kotlinx.coroutines.launch
2027

2128
class SubscriptionsBottomSheet : ExpandedBottomSheet(R.layout.sheet_subscriptions) {
2229
private var _binding: SheetSubscriptionsBinding? = null
@@ -38,14 +45,30 @@ class SubscriptionsBottomSheet : ExpandedBottomSheet(R.layout.sheet_subscription
3845

3946
binding.channelsRecycler.adapter = adapter
4047

41-
initHeaderLayout()
42-
4348
binding.subscriptionsSearchInput.addTextChangedListener { _ ->
4449
showFilteredSubscriptions()
4550
}
4651

47-
viewModel.subscriptions.observe(viewLifecycleOwner) {
48-
showFilteredSubscriptions()
52+
lifecycleScope.launch {
53+
repeatOnLifecycle(Lifecycle.State.STARTED) {
54+
launch(Dispatchers.IO) {
55+
viewModel.fetchSubscriptions(requireContext())
56+
}
57+
58+
launch {
59+
combine(
60+
viewModel.subscriptions.asFlow(),
61+
channelGroupsModel.groups.asFlow(),
62+
) { subscriptions, groups ->
63+
subscriptions to groups
64+
}
65+
.flowOn(Dispatchers.IO)
66+
.collectLatest {
67+
initHeaderLayout()
68+
showFilteredSubscriptions()
69+
}
70+
}
71+
}
4972
}
5073
}
5174

@@ -62,38 +85,36 @@ class SubscriptionsBottomSheet : ExpandedBottomSheet(R.layout.sheet_subscription
6285
showFilteredSubscriptions()
6386
}
6487

65-
channelGroupsModel.groups.observe(viewLifecycleOwner) { groups ->
66-
groups?.getOrNull(selectedChannelGroup - 1)?.let { channelGroup ->
67-
@SuppressLint("StringFormatInvalid")
68-
binding.allSubsBtn.text =
69-
"%s (%d)".format(
70-
requireContext().getString(R.string.all),
71-
viewModel.subscriptions.value?.size ?: 0
72-
)
73-
74-
binding.groupSubsBtn.isVisible = true
75-
binding.groupSubsBtn.isChecked = true
76-
binding.groupSubsBtn.text = "%s (%d)".format(
77-
channelGroup.name,
78-
channelGroup.channels.size
88+
channelGroupsModel.groups.value?.getOrNull(selectedChannelGroup - 1)?.let { channelGroup ->
89+
@SuppressLint("StringFormatInvalid")
90+
binding.allSubsBtn.text =
91+
"%s (%d)".format(
92+
requireContext().getString(R.string.all),
93+
viewModel.subscriptions.value?.size ?: 0
7994
)
80-
binding.groupSubsBtn.setOnClickListener {
81-
binding.groupEditBtn.isVisible = true
82-
83-
showFilteredSubscriptions()
84-
}
8595

96+
binding.groupSubsBtn.isVisible = true
97+
binding.groupSubsBtn.isChecked = true
98+
binding.groupSubsBtn.text = "%s (%d)".format(
99+
channelGroup.name,
100+
channelGroup.channels.size
101+
)
102+
binding.groupSubsBtn.setOnClickListener {
86103
binding.groupEditBtn.isVisible = true
87-
binding.groupEditBtn.setOnClickListener {
88-
channelGroupsModel.groupToEdit = channelGroup
89-
EditChannelGroupSheet()
90-
.show(parentFragmentManager, null)
91-
}
92104

93-
// refresh displayed list of channels when channel groups have been edited
94-
if (binding.groupSubsBtn.isChecked) {
95-
showFilteredSubscriptions()
96-
}
105+
showFilteredSubscriptions()
106+
}
107+
108+
binding.groupEditBtn.isVisible = true
109+
binding.groupEditBtn.setOnClickListener {
110+
channelGroupsModel.groupToEdit = channelGroup
111+
EditChannelGroupSheet()
112+
.show(parentFragmentManager, null)
113+
}
114+
115+
// refresh displayed list of channels when channel groups have been edited
116+
if (binding.groupSubsBtn.isChecked) {
117+
showFilteredSubscriptions()
97118
}
98119
}
99120
}

0 commit comments

Comments
 (0)