Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
refactor: directly inject playerViewModel instead of using localViewM…
Browse files Browse the repository at this point in the history
…odelStoreOwner to prevent unwanted bugs
  • Loading branch information
SuhasDissa committed May 3, 2024
1 parent 5eed9c7 commit 35fd345
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 115 deletions.
18 changes: 12 additions & 6 deletions app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -68,7 +70,9 @@ class MainActivity : ComponentActivity() {
LaunchedEffect(Unit) {
(application as MellowMusicApplication).accentColor = primaryColor
}
MainAppContent()
MainAppContent(
playerViewModel, settingsModel
)
}
}

Expand Down Expand Up @@ -131,7 +135,10 @@ class MainActivity : ComponentActivity() {
}

@Composable
private fun MainAppContent() {
private fun MainAppContent(
playerViewModel: PlayerViewModel,
settingsModel: SettingsModel
) {
val navHostController = rememberNavController()
val homeNavHostController = rememberNavController()

Expand Down Expand Up @@ -163,9 +170,6 @@ private fun MainAppContent() {
)
}
) {

val playerViewModel: PlayerViewModel =
viewModel(factory = PlayerViewModel.Factory)
MiniPlayerScaffold(playerViewModel) { pV ->
AppNavHost(
modifier = Modifier
Expand All @@ -178,7 +182,9 @@ private fun MainAppContent() {
scope.launch {
drawerState.open()
}
}
},
playerViewModel,
settingsModel
)
}

Expand Down
68 changes: 22 additions & 46 deletions app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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<Destination.OnlineSearch>(
Expand All @@ -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<Destination.LocalSearch>(
enterTransition = {
Expand All @@ -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<Destination.Settings>(
Expand Down Expand Up @@ -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<Destination.LocalPlaylists>(
Expand All @@ -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<Destination.SavedPlaylists>(
Expand All @@ -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<Destination.OnlineArtist>(
Expand All @@ -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<Destination.LocalArtist>(
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ fun MiniPlayerScaffold(
isPlayerSheetVisible = false
}
}
}
},
playerViewModel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,8 +33,8 @@ import app.suhasdissa.vibeyou.utils.TimeUtil
@Composable
fun SongListView(
songs: List<Song>,
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<Song?>(null) }
Expand Down Expand Up @@ -112,7 +111,8 @@ fun SongListView(
selectedSong?.let {
SongSettingsSheet(
onDismissRequest = { showSongSettings = false },
song = selectedSong!!
song = selectedSong!!,
playerViewModel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -182,7 +182,8 @@ fun AlbumScreen(
selectedSong?.let {
SongSettingsSheetSearchPage(
onDismissRequest = { showSongSettings = false },
song = it
song = it,
playerViewModel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -129,7 +129,6 @@ fun HomeScreen(
}
}
}) { pV ->
val viewModelStoreOwner = LocalViewModelStoreOwner.current!!
NavHost(
navController,
startDestination = HomeDestination.LocalMusic,
Expand All @@ -140,9 +139,7 @@ fun HomeScreen(
exitTransition = { ExitTransition.None }
) {
composable<HomeDestination.OnlineMusic> {
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) {
MusicScreen(onNavigate)
}
MusicScreen(onNavigate, playerViewModel)
}
composable<HomeDestination.LocalMusic> {
LaunchedEffect(Unit) {
Expand All @@ -151,7 +148,7 @@ fun HomeScreen(
LocalMusicRepository.permissions
)
}
LocalMusicScreen(onNavigate)
LocalMusicScreen(onNavigate, playerViewModel)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -165,7 +165,8 @@ fun LocalMusicScreen(
}
SongListView(
songs = localSongViewModel.songs,
sortOrder = localSongViewModel.songsSortOrder
sortOrder = localSongViewModel.songsSortOrder,
playerViewModel = playerViewModel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -254,7 +254,8 @@ fun LocalSearchScreen(
selectedSong?.let {
SongSettingsSheetSearchPage(
onDismissRequest = { showSongSettings = false },
song = selectedSong!!
song = selectedSong!!,
playerViewModel
)
}
}
Expand Down
Loading

0 comments on commit 35fd345

Please sign in to comment.