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

Commit

Permalink
fix: simplify navigation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed May 9, 2024
1 parent 1f98f66 commit 138c83a
Show file tree
Hide file tree
Showing 12 changed files with 658 additions and 511 deletions.
269 changes: 1 addition & 268 deletions app/src/main/java/app/suhasdissa/vibeyou/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,18 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.PermanentNavigationDrawer
import androidx.compose.material3.Surface
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
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
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import androidx.window.core.layout.WindowHeightSizeClass
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass
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.ModalNavDrawerContent
import app.suhasdissa.vibeyou.presentation.components.PermanentNavDrawerContent
import app.suhasdissa.vibeyou.presentation.screens.player.FullScreenPlayer
import app.suhasdissa.vibeyou.presentation.screens.player.components.EqualizerSheet
import app.suhasdissa.vibeyou.presentation.screens.player.components.QueueSheet
import app.suhasdissa.vibeyou.presentation.screens.player.components.SongOptionsSheet
import app.suhasdissa.vibeyou.presentation.layout.MainAppContent
import app.suhasdissa.vibeyou.presentation.screens.player.model.PlayerViewModel
import app.suhasdissa.vibeyou.presentation.screens.settings.model.SettingsModel
import app.suhasdissa.vibeyou.ui.theme.VibeYouTheme
import app.suhasdissa.vibeyou.utils.ThemeUtil
import app.suhasdissa.vibeyou.utils.mediaItemState
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -154,233 +116,4 @@ class MainActivity : ComponentActivity() {
Toast.makeText(this, vidId, Toast.LENGTH_SHORT).show()
playerViewModel.tryToPlayId(vidId)
}
}

@Composable
private fun MainAppContent(
playerViewModel: PlayerViewModel,
settingsModel: SettingsModel,
windowSizeClass: WindowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
) {
val navHostController = rememberNavController()
if (windowSizeClass.windowHeightSizeClass != WindowHeightSizeClass.COMPACT) {
when (windowSizeClass.windowWidthSizeClass) {
WindowWidthSizeClass.COMPACT -> {
ModelNavDrawerLayout(
navHostController,
playerViewModel,
settingsModel
)
}

WindowWidthSizeClass.MEDIUM -> {
PermanentNavDrawerLayout(
navHostController,
playerViewModel,
settingsModel
)
}

WindowWidthSizeClass.EXPANDED -> {
PermanentNavDrawerWithPlayerLayout(
navHostController,
playerViewModel,
settingsModel
)
}
}
} else {
PermanentNavDrawerLayout(
navHostController,
playerViewModel,
settingsModel,
horizontalPlayer = true
)
}
}

@Composable
private fun ModelNavDrawerLayout(
navHostController: NavHostController,
playerViewModel: PlayerViewModel,
settingsModel: SettingsModel
) {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
var currentDestination by remember {
mutableStateOf<HomeDestination>(HomeDestination.LocalMusic)
}

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

is HomeDestination -> {
navHostController.popBackStack()
navHostController.navigate(Destination.Home(it.destination))
}
}
}
)
}
) {
MiniPlayerScaffold(playerViewModel) { pV ->
AppNavHost(
modifier = Modifier
.fillMaxSize()
.consumeWindowInsets(pV)
.padding(pV),
navHostController = navHostController,
onDrawerOpen = {
scope.launch {
drawerState.open()
}
},
playerViewModel,
settingsModel
)
}

}
}

@Composable
private fun PermanentNavDrawerLayout(
navHostController: NavHostController,
playerViewModel: PlayerViewModel,
settingsModel: SettingsModel,
horizontalPlayer: Boolean = false
) {
var currentDestination by remember {
mutableStateOf<Any>(HomeDestination.LocalMusic)
}

PermanentNavigationDrawer(drawerContent = {
PermanentNavDrawerContent(
currentDestination = currentDestination,
onDestinationSelected = {
currentDestination = it
when (it) {
is Destination -> {
navHostController.popBackStack()
navHostController.navigate(it)
}

is HomeDestination -> {
navHostController.popBackStack()
navHostController.navigate(Destination.Home(it.destination))
}
}
}
)
}) {
MiniPlayerScaffold(playerViewModel, horizontalPlayer = horizontalPlayer) { pV ->
AppNavHost(
modifier = Modifier
.fillMaxSize()
.consumeWindowInsets(pV)
.padding(pV),
navHostController = navHostController,
onDrawerOpen = null,
playerViewModel,
settingsModel
)
}
}
}

@Composable
private fun PermanentNavDrawerWithPlayerLayout(
navHostController: NavHostController,
playerViewModel: PlayerViewModel,
settingsModel: SettingsModel
) {
var currentDestination by remember {
mutableStateOf<Any>(HomeDestination.LocalMusic)
}

PermanentNavigationDrawer(drawerContent = {
PermanentNavDrawerContent(
currentDestination = currentDestination,
onDestinationSelected = {
currentDestination = it
when (it) {
is Destination -> {
navHostController.popBackStack()
navHostController.navigate(it)
}

is HomeDestination -> {
navHostController.popBackStack()
navHostController.navigate(Destination.Home(it.destination))
}
}
}
)
}) {
Row(Modifier.fillMaxSize()) {
AppNavHost(
modifier = Modifier
.fillMaxHeight()
.weight(1f),
navHostController = navHostController,
onDrawerOpen = null,
playerViewModel,
settingsModel
)

playerViewModel.controller?.let { controller ->
val mediaItem by controller.mediaItemState()
AnimatedVisibility(visible = mediaItem != null) {
Column(
Modifier
.fillMaxHeight()
.width(400.dp)
) {
var showQueueSheet by remember { mutableStateOf(false) }
var showSongOptions by remember { mutableStateOf(false) }
var showEqualizerSheet by remember {
mutableStateOf(false)
}
FullScreenPlayer(
controller,
onCollapse = null,
playerViewModel,
onClickShowQueueSheet = { showQueueSheet = true },
onClickShowSongOptions = { showSongOptions = true }
)
if (showQueueSheet) QueueSheet(onDismissRequest = {
showQueueSheet = false
}, playerViewModel)
if (showSongOptions) SongOptionsSheet(
onDismissRequest = { showSongOptions = false },
playerViewModel,
onClickShowEqualizer = { showEqualizerSheet = true }
)
if (showEqualizerSheet) {
val app =
LocalContext.current.applicationContext as MellowMusicApplication
EqualizerSheet(
equalizerData = app.supportedEqualizerData!!,
onDismissRequest = { showEqualizerSheet = false },
playerViewModel = playerViewModel
)
}
}
}
}
}
}
}
14 changes: 4 additions & 10 deletions app/src/main/java/app/suhasdissa/vibeyou/navigation/Destination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import kotlinx.serialization.Serializable
@Serializable
sealed class Destination {
@Serializable
data class Home(val destination: Int) : Destination()
object OnlineMusic : Destination()

@Serializable
object LocalMusic : Destination()

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

@Serializable
sealed class HomeDestination(val destination: Int) {
@Serializable
object LocalMusic : HomeDestination(destination = 0)

@Serializable
object OnlineMusic : HomeDestination(destination = 1)
}
Loading

0 comments on commit 138c83a

Please sign in to comment.