Skip to content

Commit

Permalink
feat: (TwoPane) Add tablet support for SettingsScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Jul 26, 2024
1 parent f2fcf92 commit 58e3e27
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,44 @@
package com.infomaniak.swisstransfer.ui.screen.main

import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation.*
import com.infomaniak.swisstransfer.ui.screen.main.received.ReceivedScreen
import com.infomaniak.swisstransfer.ui.screen.main.sent.SentScreen
import com.infomaniak.swisstransfer.ui.screen.main.settings.SettingsScreenWrapper
import com.infomaniak.swisstransfer.ui.screen.main.transferdetails.TransferDetailsScreen

@Composable
fun MainNavHost(navController: NavHostController, startDestination: SentDestination) {
fun MainNavHost(
navController: NavHostController,
startDestination: SentDestination,
windowAdaptiveInfo: WindowAdaptiveInfo,
) {
NavHost(navController, startDestination, modifier = Modifier.safeDrawingPadding()) {
composable<SentDestination> {
Text("Sent")
SentScreen(
navigateToDetails = { navController.navigate(TransferDetailsDestination(it)) },
)
}
composable<ReceivedDestination> {
Text("Received")
ReceivedScreen(
navigateToDetails = { navController.navigate(TransferDetailsDestination(it)) },
)
}
composable<TransferDetailsDestination> {
val transferDetails: TransferDetailsDestination = it.toRoute()
Text("TransferDetails for transfer ${transferDetails.transferId}")
TransferDetailsScreen(
transferId = transferDetails.transferId,
)
}
composable<SettingsDestination> {
Text("Settings")
SettingsScreenWrapper(windowAdaptiveInfo)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ package com.infomaniak.swisstransfer.ui.screen.main

import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
Expand All @@ -35,21 +38,20 @@ import com.infomaniak.swisstransfer.ui.navigation.NavigationItem
fun MainScaffold(
navController: NavHostController,
currentDestination: MainNavigation,
windowAdaptiveInfo: WindowAdaptiveInfo,
content: @Composable () -> Unit = {},
) {
val adaptiveInfo by rememberUpdatedState(currentWindowAdaptiveInfo())

val showNavigation by remember(currentDestination) {
derivedStateOf {
if (currentDestination == MainNavigation.SettingsDestination) false else
NavigationItem.entries.any { it.destination == currentDestination }
NavigationItem.entries.any { it.destination == currentDestination }
}
}

val navType by remember(showNavigation, adaptiveInfo) {
val navType by remember(showNavigation, windowAdaptiveInfo) {
derivedStateOf {
if (showNavigation) {
NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(adaptiveInfo)
NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(windowAdaptiveInfo)
} else {
NavigationSuiteType.None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.infomaniak.swisstransfer.ui.screen.main

import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand All @@ -35,6 +36,7 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewTablet
fun MainScreen() {
val navController = rememberNavController()
val startDestination = SentDestination
val windowAdaptiveInfo = currentWindowAdaptiveInfo()

val navBackStackEntry by navController.currentBackStackEntryAsState()

Expand All @@ -44,8 +46,8 @@ fun MainScreen() {
}
}

MainScaffold(navController, currentDestination) {
MainNavHost(navController, startDestination)
MainScaffold(navController, currentDestination, windowAdaptiveInfo) {
MainNavHost(navController, startDestination, windowAdaptiveInfo)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Infomaniak SwissTransfer - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.infomaniak.swisstransfer.ui.screen.main.settings

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.runtime.Composable
import com.infomaniak.swisstransfer.ui.components.TwoPaneScaffold
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewMobile
import com.infomaniak.swisstransfer.ui.utils.PreviewTablet


@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun SettingsScreenWrapper(
windowAdaptiveInfo: WindowAdaptiveInfo = currentWindowAdaptiveInfo(),
) {
TwoPaneScaffold<Any>( // TODO: Replace Any with item type
windowAdaptiveInfo,
listPane = {
SettingsScreen(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
navigateTo(ListDetailPaneScaffoldRole.Detail, item)
},
)
},
detailPane = {
// Show the detail pane content if selected item is available
if (currentDestination?.content == null) {
Text("Empty state")
} else {
Text("Show selected item")
}
}
)
}

@Composable
private fun SettingsScreen(onItemClick: (Any) -> Unit) {
Text("Settings screen")
}

@PreviewMobile
@PreviewTablet
@Composable
private fun SettingsScreenPreview() {
SwissTransferTheme {
Surface(color = MaterialTheme.colorScheme.background) {
SettingsScreenWrapper()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ annotation class PreviewMobile
@Preview(
name = "(1) Tablet portrait light",
group = "Tablet",
device = "spec:id=reference_tablet,shape=Normal,width=1280,height=800,unit=dp,dpi=240",
device = "spec:width=1280dp,height=800dp,dpi=240,orientation=portrait",
)
@Preview(
name = "(2) Tablet portrait dark",
group = "Tablet",
uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL,
device = "spec:id=reference_tablet,shape=Normal,width=1280,height=800,unit=dp,dpi=240",
device = "spec:width=1280dp,height=800dp,dpi=240,orientation=portrait",
)
@Preview(
name = "(3) Tablet landscape light",
Expand Down

0 comments on commit 58e3e27

Please sign in to comment.