Skip to content

Commit fb90359

Browse files
authored
fix(home): hide empty sections when video songs are hidden
Closes #2676 When the 'Hide video songs' setting is enabled, sections like 'Music videos for you' and 'Live performances' would appear empty on the homepage. This fix removes those sections entirely if all their items are filtered out. Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
1 parent 02f8cbb commit fb90359

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

app/src/main/kotlin/com/metrolist/music/viewmodels/HomeViewModel.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ class HomeViewModel @Inject constructor(
115115
QuickPicks.QUICK_PICKS -> {
116116
val relatedSongs = database.quickPicks().first().filterVideoSongs(hideVideoSongs)
117117
val forgotten = database.forgottenFavorites().first().filterVideoSongs(hideVideoSongs).take(8)
118-
118+
119119
// Get similar songs from YouTube based on recent listening
120120
val recentSong = database.events().first().firstOrNull()?.song
121121
val ytSimilarSongs = mutableListOf<Song>()
122-
122+
123123
if (recentSong != null) {
124124
val endpoint = YouTube.next(WatchEndpoint(videoId = recentSong.id)).getOrNull()?.relatedEndpoint
125125
if (endpoint != null) {
@@ -135,13 +135,13 @@ class HomeViewModel @Inject constructor(
135135
}
136136
}
137137
}
138-
138+
139139
// Combine all sources and remove duplicates
140140
val combined = (relatedSongs + forgotten + ytSimilarSongs)
141141
.distinctBy { it.id }
142142
.shuffled()
143143
.take(20)
144-
144+
145145
quickPicks.value = combined.ifEmpty { relatedSongs.shuffled().take(20) }
146146
}
147147
QuickPicks.LAST_LISTEN -> {
@@ -216,7 +216,7 @@ class HomeViewModel @Inject constructor(
216216
.ifEmpty { return@mapNotNull null }
217217
)
218218
}
219-
219+
220220
// Get recommendations from most played albums
221221
val albumRecommendations = database.mostPlayedAlbums(fromTimeStamp, limit = 10).first()
222222
.filter { it.album.thumbnailUrl != null }
@@ -244,13 +244,14 @@ class HomeViewModel @Inject constructor(
244244
.ifEmpty { return@mapNotNull null }
245245
)
246246
}
247-
247+
248248
similarRecommendations.value = (artistRecommendations + songRecommendations + albumRecommendations).shuffled()
249249

250250
YouTube.home().onSuccess { page ->
251251
homePage.value = page.copy(
252-
sections = page.sections.map { section ->
253-
section.copy(items = section.items.filterExplicit(hideExplicit).filterVideoSongs(hideVideoSongs).filterYoutubeShorts(hideYoutubeShorts))
252+
sections = page.sections.mapNotNull { section ->
253+
val filteredItems = section.items.filterExplicit(hideExplicit).filterVideoSongs(hideVideoSongs).filterYoutubeShorts(hideYoutubeShorts)
254+
if (filteredItems.isEmpty()) null else section.copy(items = filteredItems)
254255
}
255256
)
256257
}.onFailure {
@@ -289,8 +290,9 @@ class HomeViewModel @Inject constructor(
289290

290291
homePage.value = nextSections.copy(
291292
chips = homePage.value?.chips,
292-
sections = (homePage.value?.sections.orEmpty() + nextSections.sections).map { section ->
293-
section.copy(items = section.items.filterExplicit(hideExplicit).filterVideoSongs(hideVideoSongs).filterYoutubeShorts(hideYoutubeShorts))
293+
sections = (homePage.value?.sections.orEmpty() + nextSections.sections).mapNotNull { section ->
294+
val filteredItems = section.items.filterExplicit(hideExplicit).filterVideoSongs(hideVideoSongs).filterYoutubeShorts(hideYoutubeShorts)
295+
if (filteredItems.isEmpty()) null else section.copy(items = filteredItems)
294296
}
295297
)
296298
_isLoadingMore.value = false
@@ -359,7 +361,7 @@ class HomeViewModel @Inject constructor(
359361

360362
load()
361363
}
362-
364+
363365
// Run sync in separate coroutine with cooldown to avoid blocking UI
364366
viewModelScope.launch(Dispatchers.IO) {
365367
syncUtils.tryAutoSync()
@@ -391,17 +393,17 @@ class HomeViewModel @Inject constructor(
391393
.collect { cookie ->
392394
// Avoid processing if already processing
393395
if (isProcessingAccountData) return@collect
394-
396+
395397
// Always process cookie changes, even if same value (for logout/login scenarios)
396398
lastProcessedCookie = cookie
397399
isProcessingAccountData = true
398-
400+
399401
try {
400402
if (cookie != null && cookie.isNotEmpty()) {
401-
403+
402404
// Update YouTube.cookie manually to ensure it's set
403405
YouTube.cookie = cookie
404-
406+
405407
// Fetch new account data
406408
YouTube.accountInfo().onSuccess { info ->
407409
accountName.value = info.name

0 commit comments

Comments
 (0)