Skip to content

Commit

Permalink
Merge pull request #6 from Infomaniak/navigation
Browse files Browse the repository at this point in the history
feat: Adaptative NavigationBar for mobile and NavigationRail for Tablet
  • Loading branch information
sirambd authored Jul 25, 2024
2 parents 306cd4a + 52de92f commit c1fc96d
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler)
kotlin("plugin.serialization") version libs.versions.kotlin
}

val sharedMinSdk: Int by rootProject.extra
Expand Down Expand Up @@ -52,6 +53,7 @@ android {
}

dependencies {
implementation(kotlin("reflect"))

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand All @@ -62,11 +64,15 @@ dependencies {
implementation(libs.compose.ui)
implementation(libs.compose.ui.graphics)
implementation(libs.compose.material3)
implementation(libs.compose.material3.adaptative.navigation)
implementation(libs.navigation.compose)
// Compose preview tools
implementation(libs.compose.ui.tooling.preview)
debugImplementation(libs.compose.ui.tooling)

// Others
implementation(libs.kotlinx.serialization)

// Test
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
Expand Down
41 changes: 2 additions & 39 deletions app/src/main/java/com/infomaniak/swisstransfer/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@

package com.infomaniak.swisstransfer.ui

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.infomaniak.swisstransfer.ui.screen.main.MainScreen
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme

class MainActivity : ComponentActivity() {
Expand All @@ -42,35 +32,8 @@ class MainActivity : ComponentActivity() {
enableEdgeToEdge()
setContent {
SwissTransferTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
MainScreen()
}
}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Column {
Text(
text = "Hello $name!",
modifier = modifier,
style = SwissTransferTheme.typography.h1
)
}
}

@Preview(showBackground = true)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL)
@Composable
fun GreetingPreview() {
SwissTransferTheme {
Surface(color = MaterialTheme.colorScheme.background, modifier = Modifier.fillMaxSize()) {
Greeting("Android")
}
}
}
27 changes: 0 additions & 27 deletions app/src/main/java/com/infomaniak/swisstransfer/ui/MainScreen.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.navigation

import android.os.Bundle
import androidx.navigation.NavBackStackEntry
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass

/**
* Sealed class representing the navigation arguments for the main navigation flow.
*/
@Serializable
sealed class MainNavigation : NavigationDestination() {
@Serializable
data object SentDestination : MainNavigation()
@Serializable
data object ReceivedDestination : MainNavigation()
@Serializable
data class TransferDetailsDestination(val transferId: Int) : MainNavigation()

@Serializable
data object SettingsDestination : MainNavigation()
}

/**
* Sealed class representing the navigation arguments for the new transfer flow.
*/
@Serializable
sealed class NewTransferNavigation : NavigationDestination() {
@Serializable
data object ImportFilesDestination : NewTransferNavigation()
@Serializable
data object TransferTypeDestination : NewTransferNavigation()
@Serializable
data object TransferOptionsDestination : NewTransferNavigation()
@Serializable
data object ValidateUserEmailDestination : NewTransferNavigation()

@Serializable
data object UploadProgressDestination : NewTransferNavigation()
@Serializable
data object UploadSuccessDestination : NewTransferNavigation()
}

/**
* Sealed class representing navigation arguments with a title resource.
*/
@Serializable
sealed class NavigationDestination {

companion object {

inline fun <reified T : NavigationDestination> NavBackStackEntry.toDestination(): T? {
return toDestination(T::class, backStackEntry = this)
}

fun <T : NavigationDestination> toDestination(kClass: KClass<T>, backStackEntry: NavBackStackEntry?): T? {
fun kClassFromRoute(route: String) = kClass.sealedSubclasses.firstOrNull {
route.contains(it.qualifiedName.toString())
}

if (backStackEntry == null) return null

val route = backStackEntry.destination.route ?: ""
val args = backStackEntry.arguments
val subclass = kClassFromRoute(route) ?: return null

return createInstance(subclass, args)
}

private fun <T : NavigationDestination> createInstance(kClass: KClass<T>, bundle: Bundle?): T? {
val primaryConstructor = kClass.constructors.firstOrNull()
return primaryConstructor?.let {
val args = it.parameters.associateWith { parameter ->
bundle?.get(parameter.name)
}
it.callBy(args)
} ?: kClass.objectInstance
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.NavigationRail
import androidx.compose.ui.graphics.vector.ImageVector
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation.*

/**
* Enum class representing the different destinations in the app's [BottomAppBar] or [NavigationRail].
*
* @property label The resource ID of the string label for the destination.
* @property icon The icon to be displayed for the destination.
* @property contentDescription The resource ID of the content description for accessibility.
*/
enum class NavigationItem(
@StringRes val label: Int,
val icon: ImageVector,
@StringRes val contentDescription: Int,
val destination: MainNavigation,
) {
SENT(R.string.appName, Icons.AutoMirrored.Filled.Send, R.string.appName),
RECEIVED(R.string.appName, Icons.Default.Star, R.string.appName),
SETTINGS(R.string.appName, Icons.Default.Settings, R.string.appName),
SENT(R.string.appName, Icons.AutoMirrored.Filled.Send, SentDestination),
RECEIVED(R.string.appName, Icons.Default.Star, ReceivedDestination),
SETTINGS(R.string.appName, Icons.Default.Settings, SettingsDestination),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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

import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.material3.Text
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.*

@Composable
fun MainNavHost(navController: NavHostController, startDestination: SentDestination) {
NavHost(navController, startDestination, modifier = Modifier.safeDrawingPadding()) {
composable<SentDestination> {
Text("Sent")
}
composable<ReceivedDestination> {
Text("Received")
}
composable<TransferDetailsDestination> {
val transferDetails: TransferDetailsDestination = it.toRoute()
Text("TransferDetails for transfer ${transferDetails.transferId}")
}
composable<SettingsDestination> {
Text("Settings")
}
}
}
Loading

0 comments on commit c1fc96d

Please sign in to comment.