Skip to content

Commit c255d1b

Browse files
committed
fix: don't block ui thread while filtering watched videos
1 parent 4445df6 commit c255d1b

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.libretube.api
22

3-
import android.content.Context
4-
import com.github.libretube.R
53
import com.github.libretube.constants.PreferenceKeys
64
import com.github.libretube.db.obj.SubscriptionsFeedItem
75
import com.github.libretube.helpers.PreferenceHelper
@@ -14,8 +12,6 @@ import com.github.libretube.repo.PipedAccountFeedRepository
1412
import com.github.libretube.repo.PipedLocalSubscriptionsRepository
1513
import com.github.libretube.repo.PipedNoAccountFeedRepository
1614
import com.github.libretube.repo.SubscriptionsRepository
17-
import com.google.android.material.dialog.MaterialAlertDialogBuilder
18-
import kotlinx.coroutines.runBlocking
1915

2016
object SubscriptionHelper {
2117
/**

app/src/main/java/com/github/libretube/db/DatabaseHelper.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object DatabaseHelper {
102102
return unfinished xor isVideoWatched(watchHistoryItem.videoId, watchHistoryItem.duration ?: 0)
103103
}
104104

105-
fun filterByStatusAndWatchPosition(
105+
suspend fun filterByStatusAndWatchPosition(
106106
streams: List<StreamItem>,
107107
hideWatched: Boolean
108108
): List<StreamItem> {
@@ -116,13 +116,8 @@ object DatabaseHelper {
116116
else -> true
117117
}
118118
}
119+
if (!hideWatched) return streamItems
119120

120-
return if (hideWatched) {
121-
runBlocking {
122-
filterUnwatched(streamItems)
123-
}
124-
} else {
125-
streamItems
126-
}
121+
return filterUnwatched(streamItems)
127122
}
128123
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.github.libretube.ui.extensions.setupFragmentAnimation
2828
import com.github.libretube.ui.models.HomeViewModel
2929
import com.github.libretube.ui.models.SubscriptionsViewModel
3030
import com.google.android.material.snackbar.Snackbar
31+
import kotlinx.coroutines.runBlocking
3132

3233

3334
class HomeFragment : Fragment(R.layout.fragment_home) {
@@ -150,7 +151,7 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
150151
makeVisible(binding.featuredRV, binding.featuredTV)
151152
val hideWatched = PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false)
152153
val feedVideos = streamItems
153-
.let { DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched) }
154+
.let { runBlocking { DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched) } }
154155
.take(20)
155156

156157
feedAdapter.submitList(feedVideos)

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
142142
var alreadyShowedFeedOnce = false
143143
viewModel.videoFeed.observe(viewLifecycleOwner) {
144144
if (!viewModel.isCurrentTabSubChannels && it != null) {
145-
showFeed(!alreadyShowedFeedOnce)
145+
lifecycleScope.launch {
146+
showFeed(!alreadyShowedFeedOnce)
147+
}
146148
alreadyShowedFeedOnce = true
147149
}
148150
}
@@ -179,7 +181,9 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
179181
binding.subRefresh.isRefreshing = true
180182
viewModel.isCurrentTabSubChannels = !viewModel.isCurrentTabSubChannels
181183

182-
if (viewModel.isCurrentTabSubChannels) showSubscriptions() else showFeed()
184+
lifecycleScope.launch {
185+
if (viewModel.isCurrentTabSubChannels) showSubscriptions() else showFeed()
186+
}
183187

184188
binding.subChannels.isVisible = viewModel.isCurrentTabSubChannels
185189
binding.subFeed.isGone = viewModel.isCurrentTabSubChannels
@@ -204,7 +208,10 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
204208

205209
binding.channelGroups.setOnCheckedStateChangeListener { group, _ ->
206210
selectedFilterGroup = group.children.indexOfFirst { it.id == group.checkedChipId }
207-
if (viewModel.isCurrentTabSubChannels) showSubscriptions() else showFeed()
211+
212+
lifecycleScope.launch {
213+
if (viewModel.isCurrentTabSubChannels) showSubscriptions() else showFeed()
214+
}
208215
}
209216

210217
channelGroupsModel.groups.observe(viewLifecycleOwner) {
@@ -256,7 +263,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
256263
selectedSortOrder = resultBundle.getInt(IntentData.sortOptions)
257264
hideWatched = resultBundle.getBoolean(IntentData.hideWatched)
258265
showUpcoming = resultBundle.getBoolean(IntentData.showUpcoming)
259-
showFeed()
266+
lifecycleScope.launch { showFeed() }
260267
}
261268

262269
FilterSortBottomSheet()
@@ -283,7 +290,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
283290
_binding = null
284291
}
285292

286-
private fun playByGroup(groupIndex: Int) {
293+
private suspend fun playByGroup(groupIndex: Int) {
287294
val streams = viewModel.videoFeed.value.orEmpty()
288295
.filterByGroup(groupIndex)
289296
.let { DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched) }
@@ -306,7 +313,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
306313

307314
binding.chipAll.isChecked = selectedFilterGroup == 0
308315
binding.chipAll.setOnLongClickListener {
309-
playByGroup(0)
316+
lifecycleScope.launch { playByGroup(0) }
310317
true
311318
}
312319

@@ -321,7 +328,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
321328
text = group.name
322329
setOnLongClickListener {
323330
// the index must be increased by one to skip the "all channels" group button
324-
playByGroup(index + 1)
331+
lifecycleScope.launch { playByGroup(index + 1) }
325332
true
326333
}
327334
}
@@ -360,7 +367,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment(R.layout.fragment_sub
360367
else -> this
361368
}
362369

363-
private fun showFeed(restoreScrollState: Boolean = true) {
370+
private suspend fun showFeed(restoreScrollState: Boolean = true) {
364371
val binding = _binding ?: return
365372
val videoFeed = viewModel.videoFeed.value ?: return
366373

0 commit comments

Comments
 (0)