class PlayerViewModel : ViewModel() {
var currentPlayer = mutableStateOf<SimpleExoPlayer?>(null)
var currentSong = mutableStateOf<Song?>(null)
var isPlaying = mutableStateOf(false)
private val _favorites = MutableStateFlow<List>(emptyList())
val favorites: StateFlow<List> = _favorites
code
fun createExoPlayer(context: Context): SimpleExoPlayer {
return SimpleExoPlayer.Builder(context).build()
}
fun playSong(context: Context, song: Song) {
currentPlayer.value?.let {
if (currentSong.value?.id == song.id) {
if (isPlaying.value) {
pauseSong()
} else {
resumeSong()
}
return
} else {
it.pause()
it.release()
}
}
currentSong.value = song
val player = createExoPlayer(context)
player.setMediaItem(MediaItem.fromUri(song.preview))
player.prepare()
player.play()
isPlaying.value = true
currentPlayer.value = player
}
fun pauseSong() {
currentPlayer.value?.pause()
isPlaying.value = false
}
fun resumeSong() {
currentPlayer.value?.play()
isPlaying.value = true
}
override fun onCleared() {
super.onCleared()
currentPlayer.value?.release()
}
fun addFavoriteSong(song: Song) {
_favorites.value = _favorites.value.toMutableList().apply { add(song) }
}
fun removeFavoriteSong(song: Song) {
_favorites.value = _favorites.value.toMutableList().apply { remove(song) }
}
fun isFavorite(song: Song): Boolean {
return favorites.value.contains(song)
}
// Remove this line as it seems like it’s trying to call an unavailable method
//suspend fun getAlbumDetails(albumId: Int): AlbumDetails {
// …
//}
@Composable
fun AlbumDetailScreen(albumId: Int, navController: NavController) {
val albumDetailsViewModel: AlbumDetailsViewModel = viewModel()
val playerViewModel: PlayerViewModel = viewModel()
albumDetailsViewModel.fetchAlbumDetails(albumId)
val albumDetails by albumDetailsViewModel.albumDetails.collectAsState()
@Composable
fun FavoritesScreen(navController: NavController) {
val playerViewModel: PlayerViewModel = viewModel()
val favorites by playerViewModel.favorites.collectAsState(initial = listOf())
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MusicAppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
MainScreen()
}
}
}
}
}
@Composable
fun FirstScreen(modifier: Modifier = Modifier) {
val categoriesViewModel: CategoriesViewModel = viewModel()
val categories by categoriesViewModel.categories.collectAsState()
Scaffold(
content = { padding ->
Column(
modifier = modifier.padding(padding)
) {
// Add your UI components here
MusicCategoriesScreen(categories = categories, topBar = { TopBar(title = "Music Categories") })
}
}
)
}
@Composable
fun SecondScreen(modifier: Modifier = Modifier) {
Scaffold(
topBar = {
// No top bar title
},
content = { padding ->
Column(
modifier = modifier.padding(padding)
) {
Text(text = "This is the second screen")
}
}
)
}
}-> these are some of my classes seperated with "," This was my ask -> What i want to do is adding a favorite songs functionality. I want to add a heart icon into album detail item composable. Whenever user clicks for empty heart icon for a song basically that song will add to the favorites. Favorites page will be similar to the album detail screen because. I want same structure. But every item in favorites will have loaded heart icon since they are already added to the favorite. If user reclick to the loaded heart than that means it is no longer a favorite song so it will be removed from favorites page and also if user go back to that song's album detail item the heart icon will also be empty since it is no longer favorite. And if a song is in favorites then when user opens also album detail item the heart icon in it will be a loaded heart. Also adding and removing from favorites by clicking heart feature will work on album detail item too. The song in the favorites page will also be ready to preview or pause just like in album detail item composable. For the favorites page i actually thought about making a bottom bar, you can see it in the main activity even if it is not on use right now. I wanted a bottom bar with 2 icons one for favorites page and other for category->artist->artist detail-> album detail page chains . Show me how can i add this functionality to the project as i described. Show me necessary classes and code changes.
But it is not working as i wanted right now. First of all when i tried to add a song as favorite from album detail item composable nothing happened. Heart image didn't changed or when i clicked to the favorites icon from bottom bar. There was no page changes. Can you fix it as i wanted
It seems like there may be some confusion in setting up the navigation and state management for your app. Please follow the steps below to set up your app according to your requirements:
MainScreen should contain a NavHost with all the navigation routes. You already have some of the routes, but you need to add the route for the "favorites" screen. Here's how your MainScreen should look like:
After these changes, the app should work as expected. If not, please let me know if you have any further questions or need any additional clarification.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MusicAppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
MainScreen()
}
}
}
}
}
@Composable
fun FirstScreen(modifier: Modifier = Modifier) {
val categoriesViewModel: CategoriesViewModel = viewModel()
val categories by categoriesViewModel.categories.collectAsState()
Scaffold(
content = { padding ->
Column(
modifier = modifier.padding(padding)
) {
// Add your UI components here
MusicCategoriesScreen(categories = categories, topBar = { TopBar(title = "Music Categories") })
}
}
)
}
@Composable
fun SecondScreen(modifier: Modifier = Modifier) {
Scaffold(
topBar = {
// No top bar title
},
content = { padding ->
Column(
modifier = modifier.padding(padding)
) {
Text(text = "This is the second screen")
}
}
)
}
@Composable
fun BottomNavigationBar(navController: NavController) {
val navBackStackEntry = navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry.value?.destination?.route
The other errors related to the startDestination are because it's not available within the navController.navigate scope. Instead, you can use the popUpTo method's inclusive parameter to reset your navigation stack correctly when using the bottom navigation bar.