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

Commit

Permalink
refactor: drawer sheet and mini player scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed May 3, 2024
1 parent 5799953 commit 12e1a6e
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 647 deletions.
94 changes: 63 additions & 31 deletions app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Surface
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -28,6 +31,8 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import app.suhasdissa.vibeyou.navigation.AppNavHost
import app.suhasdissa.vibeyou.navigation.Destination
import app.suhasdissa.vibeyou.navigation.HomeDestination
import app.suhasdissa.vibeyou.presentation.components.MiniPlayerScaffold
import app.suhasdissa.vibeyou.presentation.components.NavDrawerContent
import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel
import app.suhasdissa.vibeyou.presentation.screens.settings.model.SettingsModel
Expand All @@ -40,6 +45,7 @@ class MainActivity : ComponentActivity() {
private val playerViewModel: PlayerViewModel by viewModels { PlayerViewModel.Factory }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val settingsModel: SettingsModel = viewModel(factory = SettingsModel.Factory)

Expand All @@ -57,41 +63,12 @@ class MainActivity : ComponentActivity() {
dynamicColor = settingsModel.colorTheme == SettingsModel.ColorTheme.SYSTEM,
amoledDark = settingsModel.themeMode == SettingsModel.Theme.AMOLED
) {
val navHostController = rememberNavController()
val primaryColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f).toArgb()

LaunchedEffect(Unit) {
(application as MellowMusicApplication).accentColor = primaryColor
}

Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.surface
) {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
var currentDestination by remember {
mutableStateOf<Destination>(Destination.Home)
}
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerState.isOpen,
drawerContent = {
NavDrawerContent(
currentDestination = currentDestination,
onDestinationSelected = {
scope.launch {
drawerState.close()
}
navHostController.navigate(it)
currentDestination = it
}
)
}
) {
AppNavHost(navHostController = navHostController)
}
}
MainAppContent()
}
}

Expand Down Expand Up @@ -152,3 +129,58 @@ class MainActivity : ComponentActivity() {
playerViewModel.tryToPlayId(vidId)
}
}

@Composable
private fun MainAppContent() {
val navHostController = rememberNavController()
val homeNavHostController = rememberNavController()

val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
var currentDestination by remember {
mutableStateOf<HomeDestination>(HomeDestination.LocalMusic)
}

ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerState.isOpen,
drawerContent = {
NavDrawerContent(
currentDestination = currentDestination,
onDestinationSelected = {
scope.launch {
drawerState.close()
}
when (it) {
is Destination -> navHostController.navigate(it)
is HomeDestination -> {
currentDestination = it
homeNavHostController.popBackStack()
homeNavHostController.navigate(it)
}
}
}
)
}
) {

val playerViewModel: PlayerViewModel =
viewModel(factory = PlayerViewModel.Factory)
MiniPlayerScaffold(playerViewModel) { pV ->
AppNavHost(
modifier = Modifier
.fillMaxSize()
.consumeWindowInsets(pV)
.padding(pV),
navHostController = navHostController,
homeNavHostController = homeNavHostController,
onDrawerOpen = {
scope.launch {
drawerState.open()
}
}
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ sealed class Destination {
@Serializable
object Home : Destination()

@Serializable
object LocalMusic : Destination()

@Serializable
object OnlineSearch : Destination()

Expand Down Expand Up @@ -48,3 +45,12 @@ sealed class Destination {
@Serializable
data class LocalArtist(val artist: Artist) : Destination()
}

@Serializable
sealed class HomeDestination {
@Serializable
object LocalMusic : HomeDestination()

@Serializable
object OnlineMusic : HomeDestination()
}
11 changes: 9 additions & 2 deletions app/src/main/java/app/suhasdissa/vibeyou/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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
Expand Down Expand Up @@ -36,11 +37,17 @@ import app.suhasdissa.vibeyou.presentation.screens.settings.model.SettingsModel
import kotlin.reflect.typeOf

@Composable
fun AppNavHost(navHostController: NavHostController) {
fun AppNavHost(
modifier: Modifier = Modifier,
navHostController: NavHostController,
homeNavHostController: NavHostController,
onDrawerOpen: () -> Unit
) {
val viewModelStoreOwner = LocalViewModelStoreOwner.current!!

val settingsModel: SettingsModel = viewModel(factory = SettingsModel.Factory)
NavHost(
modifier = modifier,
navController = navHostController,
startDestination = Destination.Home
) {
Expand All @@ -61,7 +68,7 @@ fun AppNavHost(navHostController: NavHostController) {
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) {
HomeScreen(onNavigate = { destination ->
navHostController.navigate(destination)
})
}, onDrawerOpen = onDrawerOpen, homeNavHostController)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package app.suhasdissa.vibeyou.presentation.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
Expand All @@ -16,7 +17,6 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import app.suhasdissa.vibeyou.presentation.screens.player.FullScreenPlayer
import app.suhasdissa.vibeyou.presentation.screens.player.MiniPlayer
import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel
Expand All @@ -26,11 +26,9 @@ import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MiniPlayerScaffold(
fab: @Composable () -> Unit = {},
topBar: @Composable () -> Unit = {},
content: @Composable () -> Unit
playerViewModel: PlayerViewModel,
content: @Composable (PaddingValues) -> Unit
) {
val playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory)
var isPlayerSheetVisible by remember { mutableStateOf(false) }
val playerSheetState = rememberModalBottomSheetState(
skipPartiallyExpanded = true
Expand Down Expand Up @@ -59,8 +57,6 @@ fun MiniPlayerScaffold(
}
Scaffold(
modifier = Modifier.fillMaxSize(),
floatingActionButton = fab,
topBar = topBar,
bottomBar = {
playerViewModel.controller?.let { controller ->
val mediaItem by controller.mediaItemState()
Expand All @@ -73,14 +69,11 @@ fun MiniPlayerScaffold(
}
},
controller,
it
it,
playerViewModel
)
}
}
}
) { innerPadding ->
Column(Modifier.padding(innerPadding)) {
content()
}
}
}, content = content, contentWindowInsets = WindowInsets.systemBars
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.suhasdissa.vibeyou.R
import app.suhasdissa.vibeyou.navigation.Destination
import app.suhasdissa.vibeyou.navigation.HomeDestination

@Composable
fun NavDrawerContent(
currentDestination: Destination,
onDestinationSelected: (Destination) -> Unit
currentDestination: HomeDestination,
onDestinationSelected: (Any) -> Unit,
) {
val view = LocalView.current
ModalDrawerSheet(modifier = Modifier.width(250.dp)) {
Expand Down Expand Up @@ -58,10 +59,10 @@ fun NavDrawerContent(
)
},
label = { Text(text = stringResource(id = R.string.local_music)) },
selected = currentDestination == Destination.LocalMusic,
selected = currentDestination is HomeDestination.LocalMusic,
onClick = {
view.playSoundEffect(SoundEffectConstants.CLICK)
onDestinationSelected(Destination.LocalMusic)
onDestinationSelected(HomeDestination.LocalMusic)
}
)
Spacer(Modifier.height(16.dp))
Expand All @@ -73,10 +74,10 @@ fun NavDrawerContent(
)
},
label = { Text(text = stringResource(id = R.string.piped_music)) },
selected = currentDestination == Destination.Home,
selected = currentDestination is HomeDestination.OnlineMusic,
onClick = {
view.playSoundEffect(SoundEffectConstants.CLICK)
onDestinationSelected(Destination.Home)
onDestinationSelected(HomeDestination.OnlineMusic)
}
)
Spacer(Modifier.height(16.dp))
Expand Down
Loading

0 comments on commit 12e1a6e

Please sign in to comment.