Modern Apps git

← Modern-Apps.git

youpipe: show loading skeleton and move subscription feed transforms off the main thread

Commit
69156aa24221032446bdcc6152a629a0e381f807
Author
Vayun Mathur <[email protected]>
When
2026-08-09 16:01 UTC (3 months ago)
Parents
dd03402

Diff

youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionVideosPage.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionVideosPage.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionVideosPage.ktindex 5166af5752..3a05d1b7df 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionVideosPage.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionVideosPage.kt@@ -1,11 +1,19 @@ package com.vayunmathur.youpipe.ui +import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items+import androidx.compose.foundation.shape.RoundedCornerShape import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Scaffold import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState@@ -13,6 +21,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import com.vayunmathur.library.ui.invisibleClickable@@ -37,6 +46,7 @@ fun SubscriptionVideosPage(     val videos by remember(category) { youPipeViewModel.subscriptionVideosFor(category) }         .collectAsState(initial = emptyList())     val fetchProgress by youPipeViewModel.fetchProgress.collectAsState()+    val hasLoaded by youPipeViewModel.hasLoadedSubscriptionVideos.collectAsState()      val history by youPipeViewModel.historyVideos.collectAsState()     val deArrowEnabled by youPipeViewModel.deArrowEnabled.collectAsState()@@ -44,22 +54,29 @@ fun SubscriptionVideosPage(     val progressById = remember(history) { history.associate { it.id to it.progress } }     val context = LocalContext.current +    // Map the raw feed to render-ready rows only when an input actually changes,+    // rather than on every recomposition.+    val rows = remember(videos, deArrowEnabled, deArrowCache, progressById) {+        videos.map { video ->+            val deArrow = if (deArrowEnabled) deArrowCache[video.videoID] else null+            val watched = progressById[video.videoID] ?: 0L+            videoRowState(+                context = context,+                videoInfo = video,+                showAuthor = true,+                percentWatched = if (video.duration > 0) (watched.toDouble() / video.duration).toFloat() else 0f,+                deArrowTitle = deArrow?.title,+                deArrowThumbnailURL = deArrow?.thumbnailUrl,+            )+        }+    }+     SubscriptionVideosScreen(         backStack = backStack,         state = SubscriptionFeedUiState(-            videos = videos.map { video ->-                val deArrow = if (deArrowEnabled) deArrowCache[video.videoID] else null-                val watched = progressById[video.videoID] ?: 0L-                videoRowState(-                    context = context,-                    videoInfo = video,-                    showAuthor = true,-                    percentWatched = if (video.duration > 0) (watched.toDouble() / video.duration).toFloat() else 0f,-                    deArrowTitle = deArrow?.title,-                    deArrowThumbnailURL = deArrow?.thumbnailUrl,-                )-            },+            videos = rows,             fetchProgress = fetchProgress,+            isLoading = !hasLoaded,         ),         actions = object : SubscriptionFeedActions {             override fun openVideo(videoID: Long) {@@ -88,12 +105,60 @@ fun SubscriptionVideosScreen(                     }                 }             }-            items(state.videos, key = { it.videoID }) { row ->-                VideoRow(-                    row = row,-                    modifier = Modifier.invisibleClickable { actions.openVideo(row.videoID) },-                )+            if (state.isLoading) {+                // Skeleton placeholders shown before the first DB emission arrives,+                // so the screen never looks frozen while the feed is loading.+                items(SKELETON_ROW_COUNT) {+                    VideoRowSkeleton()+                }+            } else {+                items(state.videos, key = { it.videoID }) { row ->+                    VideoRow(+                        row = row,+                        modifier = Modifier.invisibleClickable { actions.openVideo(row.videoID) },+                    )+                }             }         }     } }++private const val SKELETON_ROW_COUNT = 8++/**+ * A single placeholder row matching [VideoRow]'s layout: a 16:9 block on the left+ * and a few stacked gray bars on the right. Uses the same static `surfaceVariant`+ * gray-box idiom the real rows use for image placeholders — no shimmer library.+ */+@Composable+fun VideoRowSkeleton(modifier: Modifier = Modifier) {+    Row(modifier) {+        Box(Modifier.weight(1f)) {+            Box(+                Modifier.padding(start = 8.dp, top = 8.dp, bottom = 8.dp)+                    .clip(RoundedCornerShape(12.dp))+                    .fillMaxWidth()+                    .aspectRatio(16f / 9f)+                    .background(MaterialTheme.colorScheme.surfaceVariant)+            )+        }+        Box(Modifier.weight(1.5f)) {+            Column(Modifier.padding(16.dp)) {+                SkeletonBar(Modifier.fillMaxWidth().height(16.dp))+                Spacer(Modifier.height(8.dp))+                SkeletonBar(Modifier.fillMaxWidth(0.6f).height(12.dp))+                Spacer(Modifier.height(6.dp))+                SkeletonBar(Modifier.fillMaxWidth(0.4f).height(12.dp))+            }+        }+    }+}++@Composable+private fun SkeletonBar(modifier: Modifier) {+    Box(+        modifier+            .clip(RoundedCornerShape(4.dp))+            .background(MaterialTheme.colorScheme.surfaceVariant)+    )+}
youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeUiContract.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeUiContract.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeUiContract.ktindex 3073df1296..603daf3339 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeUiContract.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeUiContract.kt@@ -101,6 +101,8 @@ data class SubscriptionFeedUiState(     val videos: List<VideoRowState> = emptyList(),     /** Refresh progress. Anything outside 0f..1f means "no refresh running". */     val fetchProgress: Float = -1f,+    /** True until the first DB emission arrives; drives the loading skeleton. */+    val isLoading: Boolean = false, )  /** Subscription-feed callbacks. Same no-op-default arrangement as [SearchActions]. */
youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.ktindex c3243b1a66..87533be664 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt@@ -58,7 +58,9 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine+import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map+import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch@@ -125,7 +127,13 @@ class YouPipeViewModel(     val subscriptionCategories: StateFlow<List<SubscriptionCategory>> = subscriptionCategoryDao.getAllFlow()         .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) +    private val _hasLoadedSubscriptionVideos = MutableStateFlow(false)++    /** Flips true once the subscription-video DAO emits at least once, so the UI can distinguish "still loading" from "loaded and empty". */+    val hasLoadedSubscriptionVideos: StateFlow<Boolean> = _hasLoadedSubscriptionVideos.asStateFlow()+     val subscriptionVideos: StateFlow<List<SubscriptionVideo>> = subscriptionVideoDao.getAllFlow()+        .onEach { _hasLoadedSubscriptionVideos.value = true }         .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())      val historyVideos: StateFlow<List<HistoryVideo>> = historyVideoDao.getAllFlow()@@ -179,7 +187,7 @@ class YouPipeViewModel(             visible                 .map { VideoInfo(it.name, it.id, it.duration, it.views, it.uploadDate, it.thumbnailURL, it.author) }                 .sortedByDescending { it.uploadDate }-        }+        }.flowOn(Dispatchers.Default)      /**      * The subscriptions currently assigned to [category]. A category row that