Skip to content

Commit

Permalink
Added Bottom Navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
gideonrotich committed Apr 28, 2023
1 parent 9d3d744 commit af8ea39
Show file tree
Hide file tree
Showing 37 changed files with 463 additions and 1,096 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {
implementation(project(":core"))
implementation(project(":compose-ui"))
implementation(project(":feature:home"))
implementation(project(":feature:favourite"))
implementation(project(":sync:work"))

implementation("io.github.raamcosta.compose-destinations:animations-core:1.7.32-beta")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Gideon Rotich
* Copyright 2022 Gideon Rotich.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swayy.home.domain.repository
package com.swayy.wezacare

import com.swayy.core.util.Resource
import com.swayy.home.domain.model.CharacterModel
import kotlinx.coroutines.flow.Flow
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.swayy.wezacare.navigation.NavGraphs

interface CharactersRepository {
sealed class BottomNavItem(var title: String, var icon: Int, var screen: NavGraphSpec) {
object Home : BottomNavItem(
title = "Home",
icon = com.swayy.core.R.drawable.baseline_home_24,
screen = NavGraphs.home
)

fun getCharacters(): Flow<Resource<List<CharacterModel>>>
}
object Favourite : BottomNavItem(
title = "Favourite",
icon = R.drawable.baseline_favorite_24,
screen = NavGraphs.favourite
)
}
36 changes: 31 additions & 5 deletions app/src/main/java/com/swayy/wezacare/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.scope.DestinationScope
import com.swayy.compose_ui.theme.Theme
import com.swayy.compose_ui.theme.WezaCareTheme
import com.swayy.favourite.presentation.destinations.FavouriteScreenDestination
import com.swayy.home.presentation.destinations.HomeScreenDestination
import com.swayy.wezacare.components.StandardScaffold
import com.swayy.wezacare.components.navGraph
import com.swayy.wezacare.navigation.CoreFeatureNavigator
import com.swayy.wezacare.navigation.NavGraphs
Expand Down Expand Up @@ -75,13 +78,22 @@ class MainActivity : ComponentActivity() {
val newBackStackEntry by navController.currentBackStackEntryAsState()
val route = newBackStackEntry?.destination?.route

Box(modifier = Modifier.padding(10.dp)) {
AppNavigation(
navController = navController,
modifier = Modifier
.fillMaxSize()
StandardScaffold(
navController = navController,
showBottomBar = route in listOf(
"home/${HomeScreenDestination.route}",
"favourite/${FavouriteScreenDestination.route}",
)
) { innerPadding ->
Box(modifier = Modifier.padding(10.dp)) {
AppNavigation(
navController = navController,
modifier = Modifier
.fillMaxSize()
)
}
}

}
}
}
Expand Down Expand Up @@ -112,6 +124,20 @@ internal fun AppNavigation(
popExitTransition = {
scaleOutPopExitTransition()
}
),
NavGraphs.favourite to NestedNavGraphDefaultAnimations(
enterTransition = {
scaleInEnterTransition()
},
exitTransition = {
scaleOutExitTransition()
},
popEnterTransition = {
scaleInPopEnterTransition()
},
popExitTransition = {
scaleOutPopExitTransition()
}
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,112 @@
*/
package com.swayy.wezacare.components

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Text
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavOptionsBuilder
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.swayy.wezacare.BottomNavItem
import com.swayy.wezacare.navigation.NavGraphs

/**
* Adds an [NavController.OnDestinationChangedListener] to this [NavController] and updates the
* returned [State] which is updated as the destination changes.
*/

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StandardScaffold(
navController: NavController,
showBottomBar: Boolean = true,
items: List<BottomNavItem> = listOf(
BottomNavItem.Home,
BottomNavItem.Favourite,
),
content: @Composable (paddingValues: PaddingValues) -> Unit
) {
Scaffold(
bottomBar = {
if (showBottomBar) {
val currentSelectedItem by navController.currentScreenAsState()

BottomNavigation(
backgroundColor = MaterialTheme.colorScheme.background,
// contentColor = MaterialTheme.colorScheme.onBackground,
elevation = 5.dp
) {
items.forEach { item ->
BottomNavigationItem(
icon = {
Icon(
painterResource(id = item.icon),
contentDescription = item.title,
tint = if (currentSelectedItem == item.screen) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurfaceVariant
}
)
},
label = {
Text(
text = item.title,
fontSize = 9.sp,
color = if (currentSelectedItem == item.screen) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
fontWeight = if (currentSelectedItem == item.screen) {
FontWeight.ExtraBold
} else {
FontWeight.Normal
}
)
},
alwaysShowLabel = true,
selected = currentSelectedItem == item.screen,
onClick = {
navController.navigate(item.screen, fun NavOptionsBuilder.() {
launchSingleTop = true
restoreState = true

popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
})
}
)
}
}
}
}
) { paddingValues ->
content(paddingValues)
}
}

@Stable
@Composable
fun NavController.currentScreenAsState(): State<NavGraphSpec> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ import androidx.navigation.NavController
import com.ramcosta.composedestinations.dynamic.within
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.swayy.favourite.presentation.FavouriteNavigator
import com.swayy.favourite.presentation.destinations.FavouriteScreenDestination
import com.swayy.home.presentation.destinations.DetailsScreenDestination
import com.swayy.home.presentation.destinations.HomeScreenDestination
import com.swayy.home.presentation.home.HomeNavigator

class CoreFeatureNavigator(
private val navGraph: NavGraphSpec,
private val navController: NavController
) : HomeNavigator {
) : HomeNavigator, FavouriteNavigator {

override fun openHome() {
navController.navigate(HomeScreenDestination within navGraph)
}

override fun openFavourite() {
navController.navigate(FavouriteScreenDestination within navGraph)
}

override fun popBackStack() {
navController.popBackStack()
}
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/swayy/wezacare/navigation/NavGraphs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.swayy.wezacare.navigation
import com.ramcosta.composedestinations.dynamic.routedIn
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.swayy.favourite.presentation.destinations.FavouriteScreenDestination
import com.swayy.home.presentation.destinations.DetailsScreenDestination
import com.swayy.home.presentation.destinations.HomeScreenDestination

Expand All @@ -35,12 +36,24 @@ object NavGraphs {
.associateBy { it.route }
}

val favourite = object : NavGraphSpec {
override val route = "favourite"

override val startRoute = FavouriteScreenDestination routedIn this

override val destinationsByRoute = listOf<DestinationSpec<*>>(
FavouriteScreenDestination,
).routedIn(this)
.associateBy { it.route }
}

val root = object : NavGraphSpec {
override val route = "root"
override val startRoute = home
override val destinationsByRoute = emptyMap<String, DestinationSpec<*>>()
override val nestedNavGraphs = listOf(
home
home,
favourite
)
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_favorite_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_home_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.swayy.core_database.dao

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.swayy.core_database.model.FavoriteEntity

@Dao
interface FavoritesDao {
@Insert
suspend fun insertAFavorite(favoriteEntity: FavoriteEntity)

@Query("SELECT * FROM favorites_table ORDER BY id DESC")
fun getFavorites():LiveData<List<FavoriteEntity>>



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.swayy.core_database.model

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "favorites_table")
data class FavoriteEntity(
@PrimaryKey(autoGenerate = true) val idCharacter: Int? = null,
val id: String,
val image: String?,
val name: String?,
)
Binary file not shown.
Binary file removed feature/build/intermediates/ktLint/reporters.bin
Binary file not shown.

This file was deleted.

Loading

0 comments on commit af8ea39

Please sign in to comment.