From 35fd3456b6cd1c633fd875eef8f9b423aaf4adad Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Fri, 3 May 2024 22:32:38 +0530 Subject: [PATCH] refactor: directly inject playerViewModel instead of using localViewModelStoreOwner to prevent unwanted bugs --- .../app/suhasdissa/vibeyou/MainActivity.kt | 18 +++-- .../suhasdissa/vibeyou/navigation/NavHost.kt | 68 ++++++------------- .../components/MiniPlayerScaffold.kt | 3 +- .../presentation/components/SongListView.kt | 8 +-- .../components/SongSettingsSheet.kt | 8 +-- .../presentation/screens/album/AlbumScreen.kt | 5 +- .../presentation/screens/home/HomeScreen.kt | 13 ++-- .../screens/localmusic/LocalMusicScreen.kt | 5 +- .../screens/localsearch/LocalSearchScreen.kt | 7 +- .../screens/onlinemusic/MusicScreen.kt | 10 +-- .../onlinemusic/components/SongsScreen.kt | 4 +- .../onlinemusic/model/SongOptionsViewModel.kt | 42 ++++++++++++ .../onlinemusic/model/SongViewModel.kt | 19 +----- .../screens/onlinesearch/SearchScreen.kt | 7 +- .../screens/player/FullScreenPlayer.kt | 9 ++- .../player/components/EqualizerSheet.kt | 3 +- .../screens/player/components/QueueSheet.kt | 3 +- .../player/components/SongOptionsSheet.kt | 9 +-- 18 files changed, 126 insertions(+), 115 deletions(-) create mode 100644 app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongOptionsViewModel.kt diff --git a/app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt b/app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt index 2292c15..f6f34ce 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt @@ -48,6 +48,8 @@ class MainActivity : ComponentActivity() { enableEdgeToEdge() setContent { val settingsModel: SettingsModel = viewModel(factory = SettingsModel.Factory) + val playerViewModel: PlayerViewModel = + viewModel(factory = PlayerViewModel.Factory) val darkTheme = when (settingsModel.themeMode) { SettingsModel.Theme.SYSTEM -> isSystemInDarkTheme() @@ -68,7 +70,9 @@ class MainActivity : ComponentActivity() { LaunchedEffect(Unit) { (application as MellowMusicApplication).accentColor = primaryColor } - MainAppContent() + MainAppContent( + playerViewModel, settingsModel + ) } } @@ -131,7 +135,10 @@ class MainActivity : ComponentActivity() { } @Composable -private fun MainAppContent() { +private fun MainAppContent( + playerViewModel: PlayerViewModel, + settingsModel: SettingsModel +) { val navHostController = rememberNavController() val homeNavHostController = rememberNavController() @@ -163,9 +170,6 @@ private fun MainAppContent() { ) } ) { - - val playerViewModel: PlayerViewModel = - viewModel(factory = PlayerViewModel.Factory) MiniPlayerScaffold(playerViewModel) { pV -> AppNavHost( modifier = Modifier @@ -178,7 +182,9 @@ private fun MainAppContent() { scope.launch { drawerState.open() } - } + }, + playerViewModel, + settingsModel ) } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt b/app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt index dc50b26..6fa6fee 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt @@ -6,9 +6,7 @@ import androidx.compose.animation.fadeOut import androidx.compose.animation.scaleIn import androidx.compose.animation.scaleOut import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Modifier -import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavDestination.Companion.hasRoute import androidx.navigation.NavHostController @@ -24,9 +22,8 @@ import app.suhasdissa.vibeyou.presentation.screens.artist.model.LocalArtistViewM import app.suhasdissa.vibeyou.presentation.screens.artist.model.OnlineArtistViewModel import app.suhasdissa.vibeyou.presentation.screens.home.HomeScreen import app.suhasdissa.vibeyou.presentation.screens.localsearch.LocalSearchScreen -import app.suhasdissa.vibeyou.presentation.screens.localsearch.model.LocalSearchViewModel import app.suhasdissa.vibeyou.presentation.screens.onlinesearch.SearchScreen -import app.suhasdissa.vibeyou.presentation.screens.onlinesearch.model.PipedSearchViewModel +import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel import app.suhasdissa.vibeyou.presentation.screens.playlists.model.PlaylistInfoViewModel import app.suhasdissa.vibeyou.presentation.screens.settings.AboutScreen import app.suhasdissa.vibeyou.presentation.screens.settings.AppearanceSettingsScreen @@ -41,11 +38,10 @@ fun AppNavHost( modifier: Modifier = Modifier, navHostController: NavHostController, homeNavHostController: NavHostController, - onDrawerOpen: () -> Unit + onDrawerOpen: () -> Unit, + playerViewModel: PlayerViewModel, + settingsModel: SettingsModel ) { - val viewModelStoreOwner = LocalViewModelStoreOwner.current!! - - val settingsModel: SettingsModel = viewModel(factory = SettingsModel.Factory) NavHost( modifier = modifier, navController = navHostController, @@ -65,11 +61,9 @@ fun AppNavHost( } } ) { - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - HomeScreen(onNavigate = { destination -> - navHostController.navigate(destination) - }, onDrawerOpen = onDrawerOpen, homeNavHostController) - } + HomeScreen(onNavigate = { destination -> + navHostController.navigate(destination) + }, onDrawerOpen = onDrawerOpen, homeNavHostController, playerViewModel) } composable( @@ -82,13 +76,9 @@ fun AppNavHost( targetOffset = { it }) + fadeOut() } ) { - val pipedSearchViewModel: PipedSearchViewModel = - viewModel(factory = PipedSearchViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - SearchScreen(onNavigate = { - navHostController.navigate(it) - }, pipedSearchViewModel) - } + SearchScreen(onNavigate = { + navHostController.navigate(it) + }, playerViewModel) } composable( enterTransition = { @@ -100,13 +90,9 @@ fun AppNavHost( targetOffset = { it }) + fadeOut() } ) { - val localSearchViewModel: LocalSearchViewModel = - viewModel(factory = LocalSearchViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - LocalSearchScreen(onNavigate = { - navHostController.navigate(it) - }, localSearchViewModel) - } + LocalSearchScreen(onNavigate = { + navHostController.navigate(it) + }, playerViewModel) } composable( @@ -225,9 +211,7 @@ fun AppNavHost( ) { val onlinePlaylistViewModel: OnlinePlaylistViewModel = viewModel(factory = OnlinePlaylistViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - AlbumScreen(onlinePlaylistViewModel.albumInfoState) - } + AlbumScreen(onlinePlaylistViewModel.albumInfoState, playerViewModel) } composable( @@ -245,9 +229,7 @@ fun AppNavHost( ) { val localPlaylistViewModel: LocalPlaylistViewModel = viewModel(factory = LocalPlaylistViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - AlbumScreen(localPlaylistViewModel.albumInfoState) - } + AlbumScreen(localPlaylistViewModel.albumInfoState, playerViewModel) } composable( @@ -265,9 +247,7 @@ fun AppNavHost( ) { val playlistInfoViewModel: PlaylistInfoViewModel = viewModel(factory = PlaylistInfoViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - AlbumScreen(playlistInfoViewModel.albumInfoState) - } + AlbumScreen(playlistInfoViewModel.albumInfoState, playerViewModel) } composable( @@ -285,11 +265,9 @@ fun AppNavHost( ) { val onlineArtistViewModel: OnlineArtistViewModel = viewModel(factory = OnlineArtistViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - ArtistScreen(onClickAlbum = { - navHostController.navigate(Destination.Playlists(it)) - }, onlineArtistViewModel.artistInfoState) - } + ArtistScreen(onClickAlbum = { + navHostController.navigate(Destination.Playlists(it)) + }, onlineArtistViewModel.artistInfoState) } composable( @@ -307,11 +285,9 @@ fun AppNavHost( ) { val localArtistViewModel: LocalArtistViewModel = viewModel(factory = LocalArtistViewModel.Factory) - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - ArtistScreen(onClickAlbum = { - navHostController.navigate(Destination.LocalPlaylists(it)) - }, localArtistViewModel.artistInfoState) - } + ArtistScreen(onClickAlbum = { + navHostController.navigate(Destination.LocalPlaylists(it)) + }, localArtistViewModel.artistInfoState) } } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/MiniPlayerScaffold.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/MiniPlayerScaffold.kt index 413b224..a1ebbc4 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/MiniPlayerScaffold.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/MiniPlayerScaffold.kt @@ -50,7 +50,8 @@ fun MiniPlayerScaffold( isPlayerSheetVisible = false } } - } + }, + playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongListView.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongListView.kt index fc9eff3..ebedded 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongListView.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongListView.kt @@ -23,7 +23,6 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.unit.dp -import androidx.lifecycle.viewmodel.compose.viewModel import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.domain.models.primary.Song import app.suhasdissa.vibeyou.presentation.screens.localmusic.components.SortOrder @@ -34,8 +33,8 @@ import app.suhasdissa.vibeyou.utils.TimeUtil @Composable fun SongListView( songs: List, - sortOrder: SortOrder = SortOrder.Alphabetic, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel, + sortOrder: SortOrder = SortOrder.Alphabetic ) { var showSongSettings by remember { mutableStateOf(false) } var selectedSong by remember { mutableStateOf(null) } @@ -112,7 +111,8 @@ fun SongListView( selectedSong?.let { SongSettingsSheet( onDismissRequest = { showSongSettings = false }, - song = selectedSong!! + song = selectedSong!!, + playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongSettingsSheet.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongSettingsSheet.kt index c835c58..d82b89c 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongSettingsSheet.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/components/SongSettingsSheet.kt @@ -44,7 +44,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.domain.models.primary.Song -import app.suhasdissa.vibeyou.presentation.screens.onlinemusic.model.SongViewModel +import app.suhasdissa.vibeyou.presentation.screens.onlinemusic.model.SongOptionsViewModel import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel import coil.compose.AsyncImage @@ -53,8 +53,8 @@ import coil.compose.AsyncImage fun SongSettingsSheet( onDismissRequest: () -> Unit, song: Song, - songViewModel: SongViewModel = viewModel(factory = SongViewModel.Factory), - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel, + songViewModel: SongOptionsViewModel = viewModel(factory = SongOptionsViewModel.Factory), ) { val songSettingsSheetState = rememberModalBottomSheetState( skipPartiallyExpanded = true @@ -166,7 +166,7 @@ fun SongSettingsSheet( fun SongSettingsSheetSearchPage( onDismissRequest: () -> Unit, song: Song, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel ) { val songSettingsSheetState = rememberModalBottomSheetState( skipPartiallyExpanded = true diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/album/AlbumScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/album/AlbumScreen.kt index e4df3b2..bfabfa2 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/album/AlbumScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/album/AlbumScreen.kt @@ -51,7 +51,7 @@ import coil.compose.AsyncImage @Composable fun AlbumScreen( state: AlbumInfoState, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory), + playerViewModel: PlayerViewModel, playlistViewModel: NewPlaylistViewModel = viewModel(factory = NewPlaylistViewModel.Factory) ) { when (state) { @@ -182,7 +182,8 @@ fun AlbumScreen( selectedSong?.let { SongSettingsSheetSearchPage( onDismissRequest = { showSongSettings = false }, - song = it + song = it, + playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/home/HomeScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/home/HomeScreen.kt index 69eca28..93d294a 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/home/HomeScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/home/HomeScreen.kt @@ -20,7 +20,6 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -34,7 +33,6 @@ import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner import androidx.navigation.NavController import androidx.navigation.NavDestination.Companion.hasRoute import androidx.navigation.NavHostController @@ -47,13 +45,15 @@ import app.suhasdissa.vibeyou.navigation.Destination import app.suhasdissa.vibeyou.navigation.HomeDestination import app.suhasdissa.vibeyou.presentation.screens.localmusic.LocalMusicScreen import app.suhasdissa.vibeyou.presentation.screens.onlinemusic.MusicScreen +import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel import app.suhasdissa.vibeyou.utils.PermissionHelper @Composable fun HomeScreen( onNavigate: (Destination) -> Unit, onDrawerOpen: () -> Unit, - navController: NavHostController + navController: NavHostController, + playerViewModel: PlayerViewModel ) { val view = LocalView.current val mainActivity = (LocalContext.current as MainActivity) @@ -129,7 +129,6 @@ fun HomeScreen( } } }) { pV -> - val viewModelStoreOwner = LocalViewModelStoreOwner.current!! NavHost( navController, startDestination = HomeDestination.LocalMusic, @@ -140,9 +139,7 @@ fun HomeScreen( exitTransition = { ExitTransition.None } ) { composable { - CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { - MusicScreen(onNavigate) - } + MusicScreen(onNavigate, playerViewModel) } composable { LaunchedEffect(Unit) { @@ -151,7 +148,7 @@ fun HomeScreen( LocalMusicRepository.permissions ) } - LocalMusicScreen(onNavigate) + LocalMusicScreen(onNavigate, playerViewModel) } } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localmusic/LocalMusicScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localmusic/LocalMusicScreen.kt index 8dc7eee..0b0734d 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localmusic/LocalMusicScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localmusic/LocalMusicScreen.kt @@ -53,8 +53,8 @@ import kotlinx.coroutines.launch @Composable fun LocalMusicScreen( onNavigate: (Destination) -> Unit, + playerViewModel: PlayerViewModel, localSongViewModel: LocalSongViewModel = viewModel(factory = LocalSongViewModel.Factory), - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) ) { val pagerState = rememberPagerState { 3 } val scope = rememberCoroutineScope() @@ -165,7 +165,8 @@ fun LocalMusicScreen( } SongListView( songs = localSongViewModel.songs, - sortOrder = localSongViewModel.songsSortOrder + sortOrder = localSongViewModel.songsSortOrder, + playerViewModel = playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localsearch/LocalSearchScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localsearch/LocalSearchScreen.kt index 15af7e8..fa5476f 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localsearch/LocalSearchScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/localsearch/LocalSearchScreen.kt @@ -56,8 +56,8 @@ import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel @Composable fun LocalSearchScreen( onNavigate: (Destination) -> Unit, - localSearchViewModel: LocalSearchViewModel, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel, + localSearchViewModel: LocalSearchViewModel = viewModel(factory = LocalSearchViewModel.Factory) ) { var isPopupOpen by remember { mutableStateOf(localSearchViewModel.state !is SearchState.Success) @@ -254,7 +254,8 @@ fun LocalSearchScreen( selectedSong?.let { SongSettingsSheetSearchPage( onDismissRequest = { showSongSettings = false }, - song = selectedSong!! + song = selectedSong!!, + playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/MusicScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/MusicScreen.kt index 2d8ff1d..d54a92e 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/MusicScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/MusicScreen.kt @@ -1,7 +1,6 @@ package app.suhasdissa.vibeyou.presentation.screens.onlinemusic import android.view.SoundEffectConstants -import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth @@ -21,13 +20,14 @@ import androidx.compose.ui.unit.dp import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.navigation.Destination import app.suhasdissa.vibeyou.presentation.screens.onlinemusic.components.SongsScreen +import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel import app.suhasdissa.vibeyou.presentation.screens.playlists.PlaylistsScreen import kotlinx.coroutines.launch -@OptIn(ExperimentalFoundationApi::class) @Composable fun MusicScreen( - onNavigate: (Destination) -> Unit + onNavigate: (Destination) -> Unit, + playerViewModel: PlayerViewModel ) { val pagerState = rememberPagerState { 3 } val scope = rememberCoroutineScope() @@ -82,8 +82,8 @@ fun MusicScreen( modifier = Modifier.fillMaxSize() ) { index -> when (index) { - 0 -> SongsScreen(showFavourites = false) - 1 -> SongsScreen(showFavourites = true) + 0 -> SongsScreen(showFavourites = false, playerViewModel) + 1 -> SongsScreen(showFavourites = true, playerViewModel) 2 -> PlaylistsScreen(onNavigate = onNavigate) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/components/SongsScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/components/SongsScreen.kt index 0b7ef51..83b1293 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/components/SongsScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/components/SongsScreen.kt @@ -29,7 +29,7 @@ import app.suhasdissa.vibeyou.utils.asSong @Composable fun SongsScreen( showFavourites: Boolean, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory), + playerViewModel: PlayerViewModel, songViewModel: SongViewModel = viewModel(factory = SongViewModel.Factory) ) { val view = LocalView.current @@ -67,7 +67,7 @@ fun SongsScreen( .fillMaxSize() .padding(innerPadding) ) { - SongListView(songs) + SongListView(songs, playerViewModel) } } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongOptionsViewModel.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongOptionsViewModel.kt new file mode 100644 index 0000000..85f6969 --- /dev/null +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongOptionsViewModel.kt @@ -0,0 +1,42 @@ +package app.suhasdissa.vibeyou.presentation.screens.onlinemusic.model + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory +import app.suhasdissa.vibeyou.MellowMusicApplication +import app.suhasdissa.vibeyou.backend.repository.SongDatabaseRepository +import app.suhasdissa.vibeyou.domain.models.primary.Song +import kotlinx.coroutines.launch + +class SongOptionsViewModel(private val songDatabaseRepository: SongDatabaseRepository) : + ViewModel() { + fun removeSong(song: Song) { + viewModelScope.launch { + songDatabaseRepository.removeSong(song) + } + } + + fun toggleFavourite(id: String) { + viewModelScope.launch { + val song = songDatabaseRepository.getSongById(id) + song ?: return@launch + song.let { + songDatabaseRepository.addSong(it.toggleLike()) + } + } + } + + companion object { + val Factory: ViewModelProvider.Factory = viewModelFactory { + initializer { + val application = + (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MellowMusicApplication) + SongOptionsViewModel( + application.container.songDatabaseRepository + ) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongViewModel.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongViewModel.kt index 26cf2c7..dac7012 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongViewModel.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinemusic/model/SongViewModel.kt @@ -7,10 +7,8 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import app.suhasdissa.vibeyou.MellowMusicApplication import app.suhasdissa.vibeyou.backend.repository.SongDatabaseRepository -import app.suhasdissa.vibeyou.domain.models.primary.Song import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.stateIn -import kotlinx.coroutines.launch class SongViewModel(private val songDatabaseRepository: SongDatabaseRepository) : ViewModel() { val songs = songDatabaseRepository.getAllSongsStream().stateIn( @@ -25,22 +23,6 @@ class SongViewModel(private val songDatabaseRepository: SongDatabaseRepository) initialValue = listOf() ) - fun removeSong(song: Song) { - viewModelScope.launch { - songDatabaseRepository.removeSong(song) - } - } - - fun toggleFavourite(id: String) { - viewModelScope.launch { - val song = songDatabaseRepository.getSongById(id) - song ?: return@launch - song.let { - songDatabaseRepository.addSong(it.toggleLike()) - } - } - } - companion object { val Factory: ViewModelProvider.Factory = viewModelFactory { initializer { @@ -53,3 +35,4 @@ class SongViewModel(private val songDatabaseRepository: SongDatabaseRepository) } } } + diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinesearch/SearchScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinesearch/SearchScreen.kt index 9a3a1bd..a0810fa 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinesearch/SearchScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/onlinesearch/SearchScreen.kt @@ -56,8 +56,8 @@ import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel @Composable fun SearchScreen( onNavigate: (Destination) -> Unit, - pipedSearchViewModel: PipedSearchViewModel, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel, + pipedSearchViewModel: PipedSearchViewModel = viewModel(factory = PipedSearchViewModel.Factory), ) { var isPopupOpen by remember { mutableStateOf(pipedSearchViewModel.state !is SearchState.Success) @@ -277,7 +277,8 @@ fun SearchScreen( selectedSong?.let { SongSettingsSheetSearchPage( onDismissRequest = { showSongSettings = false }, - song = selectedSong!! + song = selectedSong!!, + playerViewModel ) } } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/FullScreenPlayer.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/FullScreenPlayer.kt index 2f3679d..8c2b9c5 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/FullScreenPlayer.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/FullScreenPlayer.kt @@ -76,7 +76,7 @@ import coil.compose.SubcomposeAsyncImageContent fun FullScreenPlayer( controller: MediaController, onCollapse: () -> Unit, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel ) { var showQueueSheet by remember { mutableStateOf(false) } var showSongOptions by remember { mutableStateOf(false) } @@ -181,8 +181,11 @@ fun FullScreenPlayer( } } } - if (showQueueSheet) QueueSheet(onDismissRequest = { showQueueSheet = false }) - if (showSongOptions) SongOptionsSheet(onDismissRequest = { showSongOptions = false }) + if (showQueueSheet) QueueSheet(onDismissRequest = { showQueueSheet = false }, playerViewModel) + if (showSongOptions) SongOptionsSheet( + onDismissRequest = { showSongOptions = false }, + playerViewModel + ) } @Composable diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/EqualizerSheet.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/EqualizerSheet.kt index 7affe69..2802834 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/EqualizerSheet.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/EqualizerSheet.kt @@ -38,7 +38,6 @@ import androidx.compose.ui.text.intl.Locale import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.content.edit -import androidx.lifecycle.viewmodel.compose.viewModel import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.domain.models.primary.EqualizerData import app.suhasdissa.vibeyou.presentation.components.VerticalSlider @@ -50,7 +49,7 @@ import app.suhasdissa.vibeyou.utils.rememberPreference @Composable fun EqualizerSheet( equalizerData: EqualizerData, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory), + playerViewModel: PlayerViewModel, onDismissRequest: () -> Unit ) { ModalBottomSheet(onDismissRequest = onDismissRequest) { diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/QueueSheet.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/QueueSheet.kt index a64fd87..298271e 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/QueueSheet.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/QueueSheet.kt @@ -26,7 +26,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.lifecycle.viewmodel.compose.viewModel import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel import kotlinx.coroutines.launch @@ -35,7 +34,7 @@ import kotlinx.coroutines.launch @Composable fun QueueSheet( onDismissRequest: () -> Unit, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel ) { val playerSheetState = rememberModalBottomSheetState( skipPartiallyExpanded = true diff --git a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/SongOptionsSheet.kt b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/SongOptionsSheet.kt index 7733642..53584f6 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/SongOptionsSheet.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/presentation/screens/player/components/SongOptionsSheet.kt @@ -36,7 +36,6 @@ import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.lifecycle.viewmodel.compose.viewModel import app.suhasdissa.vibeyou.MellowMusicApplication import app.suhasdissa.vibeyou.R import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel @@ -46,7 +45,7 @@ import kotlinx.coroutines.launch @Composable fun SongOptionsSheet( onDismissRequest: () -> Unit, - playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory) + playerViewModel: PlayerViewModel ) { val playerSheetState = rememberModalBottomSheetState( skipPartiallyExpanded = true @@ -132,7 +131,9 @@ fun SongOptionsSheet( } if (showEqualizerSheet) { - EqualizerSheet(equalizerData = app.supportedEqualizerData!!, - onDismissRequest = { showEqualizerSheet = false }) + EqualizerSheet( + equalizerData = app.supportedEqualizerData!!, + onDismissRequest = { showEqualizerSheet = false }, playerViewModel = playerViewModel + ) } }