From 453cfc8023d8e1002136aee10300e4fe35d68cc4 Mon Sep 17 00:00:00 2001 From: Malopieds Date: Wed, 17 Apr 2024 09:04:23 +0200 Subject: [PATCH] feat(home_screen): ytm account to homescreen closes #55 --- .../zionhuang/music/ui/screens/HomeScreen.kt | 41 +++++++++++++++++++ .../music/viewmodels/HomeViewModel.kt | 11 +++++ 2 files changed, 52 insertions(+) diff --git a/app/src/main/java/com/zionhuang/music/ui/screens/HomeScreen.kt b/app/src/main/java/com/zionhuang/music/ui/screens/HomeScreen.kt index 686360001..4b12e8c3c 100644 --- a/app/src/main/java/com/zionhuang/music/ui/screens/HomeScreen.kt +++ b/app/src/main/java/com/zionhuang/music/ui/screens/HomeScreen.kt @@ -95,6 +95,8 @@ fun HomeScreen( val homeSecondContinuation by viewModel.homeSecondContinuation.collectAsState() val homeThirdContinuation by viewModel.homeThirdContinuation.collectAsState() + val youtubePlaylists by viewModel.youtubePlaylists.collectAsState() + val isRefreshing by viewModel.isRefreshing.collectAsState() val mostPlayedLazyGridState = rememberLazyGridState() @@ -264,6 +266,45 @@ fun HomeScreen( } } + if (youtubePlaylists?.isNotEmpty() == true) { + NavigationTitle( + title = "Your Youtube playlists", + onClick = { + navController.navigate("account") + } + ) + LazyRow( + contentPadding = WindowInsets.systemBars + .only(WindowInsetsSides.Horizontal) + .asPaddingValues() + ) { + items( + items = youtubePlaylists.orEmpty(), + key = { it.id } + ) { item -> + YouTubeGridItem( + item = item, + modifier = Modifier + .combinedClickable( + onClick = { + navController.navigate("online_playlist/${item.id}") + }, + onLongClick = { + haptic.performHapticFeedback(HapticFeedbackType.LongPress) + menuState.show { + YouTubePlaylistMenu( + playlist = item, + coroutineScope = coroutineScope, + onDismiss = menuState::dismiss + ) + } + } + ) + ) + } + } + } + if (keepListening?.isNotEmpty() == true) { keepListening?.let { NavigationTitle( diff --git a/app/src/main/java/com/zionhuang/music/viewmodels/HomeViewModel.kt b/app/src/main/java/com/zionhuang/music/viewmodels/HomeViewModel.kt index fcb5e1d52..f541e80f7 100644 --- a/app/src/main/java/com/zionhuang/music/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/zionhuang/music/viewmodels/HomeViewModel.kt @@ -3,6 +3,7 @@ package com.zionhuang.music.viewmodels import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.zionhuang.innertube.YouTube +import com.zionhuang.innertube.models.PlaylistItem import com.zionhuang.innertube.models.WatchEndpoint import com.zionhuang.innertube.models.YTItem import com.zionhuang.innertube.pages.AlbumUtils @@ -52,6 +53,8 @@ class HomeViewModel @Inject constructor( val homeSecondArtistRecommendation = MutableStateFlow(null) val homeThirdArtistRecommendation = MutableStateFlow(null) + val youtubePlaylists = MutableStateFlow?>(null) + private suspend fun load() { quickPicks.value = database.quickPicks().first().shuffled().take(20) val artists = database.mostPlayedArtists(System.currentTimeMillis() - 86400000 * 7 * 2).first().shuffled().take(5) @@ -84,6 +87,14 @@ class HomeViewModel @Inject constructor( songsAlbumRecommendation.value = database.getRecommendationAlbum(limit = 10).first().shuffled().take(2) artistRecommendation.value = database.mostPlayedArtists(System.currentTimeMillis() - 86400000 * 7, limit = 10).first().shuffled().take(3) + + viewModelScope.launch { + YouTube.likedPlaylists().onSuccess { + youtubePlaylists.value = it + }.onFailure { + reportException(it) + } + } } private suspend fun homeLoad() {