Skip to content

Commit

Permalink
Merge pull request #55 from KevinSchildhorn/ks/UpdatingNavigation
Browse files Browse the repository at this point in the history
Updating navigation
  • Loading branch information
KevinSchildhorn authored Oct 11, 2024
2 parents de7e0f2 + 93ee04c commit 2359022
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 46 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ktlint = "0.0.26"
ktlint-plugin = "12.1.1"
multiplatform-settings = "1.1.1"
multiplatform-settings-test = "1.0.0"
navigationCompose = "2.7.0-alpha07"
resources-version = "0.23.0"
security-crypto = "1.1.0-alpha06"
smbj = "0.11.5"
Expand Down Expand Up @@ -57,6 +58,7 @@ kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serializa
ktlint = { module = "com.twitter.compose.rules:ktlint", version.ref = "ktlint" }
multiplatform-settings = { module = "com.russhwolf:multiplatform-settings", version.ref = "multiplatform-settings" }
multiplatform-settings-test = { module = "com.russhwolf:multiplatform-settings-test", version.ref = "multiplatform-settings-test" }
navigation-compose = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
security-crypto = { module = "androidx.security:security-crypto", version.ref = "security-crypto" }
smbj = { module = "com.hierynomus:smbj", version.ref = "smbj" }
sqlite-driver = { module = "app.cash.sqldelight:sqlite-driver", version.ref = "sqlite-driver" }
Expand Down
1 change: 1 addition & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ kotlin {
implementation(compose.material)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
implementation(libs.navigation.compose)
implementation(libs.eva.icons)
implementation(libs.cache4k)
implementation(libs.kotlinx.serialization.json)
Expand Down
87 changes: 53 additions & 34 deletions shared/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.kevinschildhorn.fotopresenter.ui.atoms.FotoTypography
import com.kevinschildhorn.fotopresenter.ui.atoms.fotoColors
import com.kevinschildhorn.fotopresenter.ui.atoms.fotoShapes
import com.kevinschildhorn.fotopresenter.ui.screens.common.Screen
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryScreen
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
Expand All @@ -12,49 +21,59 @@ import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowScreen
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel

// The ViewModels need to be passed in here because koin doesn't support Koin
@Composable
fun App(
loginViewModel: LoginViewModel,
directoryViewModel: DirectoryViewModel,
slideshowViewModel: SlideshowViewModel,
playlistViewModel: PlaylistViewModel,
navController: NavHostController = rememberNavController()
) {
val currentScreen = remember { mutableStateOf(Screen.LOGIN) }
MaterialTheme(
colors = fotoColors,
typography = FotoTypography(),
shapes = fotoShapes,
) {

MaterialTheme {
when (currentScreen.value) {
Screen.LOGIN ->
LoginScreen(loginViewModel) {
directoryViewModel.setLoggedIn()
currentScreen.value = Screen.DIRECTORY
Scaffold { innerPadding ->
NavHost(
navController = navController,
startDestination = Screen.LOGIN.name,
modifier = Modifier.fillMaxSize().padding(innerPadding)
) {
composable(route = Screen.LOGIN.name) {
LoginScreen(loginViewModel) {
navController.navigate(Screen.DIRECTORY.name)
}
}

Screen.DIRECTORY ->
DirectoryScreen(
directoryViewModel,
onLogout = {
loginViewModel.setLoggedOut()
currentScreen.value = Screen.LOGIN
},
onStartSlideshow = {
slideshowViewModel.setSlideshow(it)
directoryViewModel.clearSlideshow()
currentScreen.value = Screen.SLIDESHOW
},
onShowPlaylists = {
currentScreen.value = Screen.PLAYLIST
composable(route = Screen.DIRECTORY.name) {
DirectoryScreen(
directoryViewModel,
onLogout = {
loginViewModel.setLoggedOut()
navController.navigate(Screen.LOGIN.name)
},
onStartSlideshow = {
slideshowViewModel.setSlideshow(it)
directoryViewModel.clearSlideshow()
navController.navigate(Screen.SLIDESHOW.name)
},
onShowPlaylists = {
navController.navigate(Screen.PLAYLIST.name)
}
)
}
composable(route = Screen.SLIDESHOW.name) {
SlideshowScreen(slideshowViewModel) {
navController.navigate(Screen.DIRECTORY.name)
}
)

Screen.SLIDESHOW ->
SlideshowScreen(slideshowViewModel) {
currentScreen.value = Screen.DIRECTORY
}

Screen.PLAYLIST -> {
PlaylistScreen(playlistViewModel, overlaid = false) {
slideshowViewModel.setSlideshowFromPlaylist(it)
currentScreen.value = Screen.SLIDESHOW
composable(route = Screen.PLAYLIST.name) {
PlaylistScreen(playlistViewModel, overlaid = false) {
slideshowViewModel.setSlideshowFromPlaylist(it)
navController.navigate(Screen.SLIDESHOW.name)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ fun DirectoryScreen(
var overlayVisible by remember { mutableStateOf(DirectoryOverlay.NONE) }

// Navigation
if (!uiState.loggedIn) onLogout()
uiState.slideshowDetails?.let {
onStartSlideshow(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ data class DirectoryScreenState(
val currentPath: String = "",
var directoryGridState: DirectoryGridState = DirectoryGridState(emptyList(), mutableListOf()),
val slideshowDetails: ImageSlideshowDetails? = null,
val loggedIn: Boolean = true,
val selectedDirectory: DirectoryGridCellState? = null,
val currentImageCount: Int = 0,
val totalImageCount: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,12 @@ class DirectoryViewModel(

//region Connection

fun setLoggedIn() {
_uiState.update { it.copy(loggedIn = true) }
}

fun logout() {
cancelJobs()
viewModelScope.launch(Dispatchers.Default) {
logger.i { "Logging Out" }
val logoutUseCase = UseCaseFactory.disconnectFromServerUseCase
logoutUseCase()
logger.d { "Setting loggedIn state to false" }
_uiState.update { it.copy(loggedIn = false) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class DirectoryViewModelTest : KoinTest {
fun `Refresh Screen`() =
runTest(testDispatcher) {
val viewModel: DirectoryViewModel by inject()
assertTrue(viewModel.uiState.value.loggedIn)

viewModel.uiState.test {
viewModel.refreshScreen()
Expand All @@ -68,7 +67,6 @@ class DirectoryViewModelTest : KoinTest {
state = awaitItem()
assertEquals(UiState.SUCCESS, state.state)
assertEquals("", state.currentPath)
assertTrue(state.loggedIn)
assertEquals(2, state.directoryGridState.imageStates.count())
assertEquals(2, state.directoryGridState.folderStates.count())
cancelAndIgnoreRemainingEvents()
Expand All @@ -79,12 +77,10 @@ class DirectoryViewModelTest : KoinTest {
fun logout() =
runTest(testDispatcher) {
val viewModel: DirectoryViewModel by inject()
assertTrue(viewModel.uiState.value.loggedIn)
viewModel.uiState.test {
var state = awaitItem()
viewModel.logout()
state = awaitItem()
assertFalse(state.loggedIn)
cancelAndIgnoreRemainingEvents()
}
}
Expand Down

0 comments on commit 2359022

Please sign in to comment.