Skip to content

Commit

Permalink
Adding Bottom Navigation and reorganizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
Debanshu777 committed Mar 30, 2022
1 parent 07b7cf2 commit 8d2e33b
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
import io.ktor.client.request.*
import io.ktor.http.*
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import javax.inject.Inject
import javax.inject.Provider
import kotlin.io.use

class GitHubRepository @Inject constructor(){
class githubRepository @Inject constructor(){
@Inject lateinit var httpClient : Provider<HttpClient>
suspend fun getUserData(userId:String): Resource<GitHubUserResponse> {
return try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.debanshu777.compose_github.network.dataSource

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.debanshu777.compose_github.network.model.GitHubSearchResponse
import com.debanshu777.compose_github.network.model.GitHubSearchUserList
import com.debanshu777.compose_github.network.model.GitHubUserResponse
import com.debanshu777.compose_github.ui.feature_profile.state.ProfileState
import com.debanshu777.compose_github.ui.feature_search.state.SearchState
import com.debanshu777.compose_github.ui.feature_search.state.SearchWidgetState
Expand All @@ -19,7 +15,7 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GitHubViewModel @Inject constructor(private val gitHubRepository: GitHubRepository) :ViewModel() {
class githubViewModel @Inject constructor(private val gitHubRepository: githubRepository) :ViewModel() {
val userDataState = MutableStateFlow(ProfileState())
val searchState= MutableStateFlow(SearchState())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ package com.debanshu777.compose_github.ui
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.material.*
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel
import com.debanshu777.compose_github.ui.feature_search.SearchScreen
import com.debanshu777.compose_github.ui.navigation.Navigation
import com.debanshu777.compose_github.network.dataSource.githubViewModel
import com.debanshu777.compose_github.ui.base.MainScreen
import com.debanshu777.compose_github.ui.theme.ComposeGithubTheme
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -23,12 +16,12 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
ComposeGithubTheme {
val viewModel:GitHubViewModel= hiltViewModel()
val viewModel:githubViewModel= hiltViewModel()
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
Navigation(viewModel)
MainScreen(viewModel)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.debanshu777.compose_github.ui.base

import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.navigation.compose.rememberNavController
import com.debanshu777.compose_github.network.dataSource.githubViewModel
import com.debanshu777.compose_github.ui.base.components.BottomBar
import com.debanshu777.compose_github.ui.feature_search.components.MainAppBar
import com.debanshu777.compose_github.ui.feature_search.state.SearchState
import com.debanshu777.compose_github.ui.feature_search.state.SearchWidgetState

@Composable
fun MainScreen(viewModel: githubViewModel){
val navController = rememberNavController()
val searchWidgetState by viewModel.searchWidgetState
val searchTextState by viewModel.searchTextState
Scaffold(
topBar = {
MainAppBar(
searchWidgetState = searchWidgetState,
searchTextState = searchTextState,
onTextChange = {
viewModel.updateSearchTextState(it)
},
onCloseClicked = {
viewModel.updateSearchTextState("")
viewModel.updateSearchWidgetState(SearchWidgetState.CLOSED)
viewModel.searchState.value= SearchState(data= emptyList())
},
onSearchClick = {
viewModel.searchUser(it)
},
onSearchTriggered={
viewModel.updateSearchWidgetState(SearchWidgetState.OPENED)
}
)
},
bottomBar = { BottomBar(navController = navController)}
) {
Navigation(viewModel,navController)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.debanshu777.compose_github.ui.base

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.debanshu777.compose_github.network.dataSource.githubViewModel
import com.debanshu777.compose_github.ui.feature_follow.FollowScreen
import com.debanshu777.compose_github.ui.feature_profile.ProfileScreen
import com.debanshu777.compose_github.ui.feature_search.SearchScreen
import com.debanshu777.compose_github.ui.feature_trending.TrendingScreen

@Composable
fun Navigation(viewModel: githubViewModel, navController: NavHostController) {
NavHost(navController = navController, startDestination = Screen.TrendingScreen.route) {
composable(route = Screen.SearchScreen.route){
SearchScreen(viewModel,navController)
}
composable(route = Screen.ProfileScreen.route){
ProfileScreen(viewModel)
}
composable(route= Screen.TrendingScreen.route){
TrendingScreen()
}
composable(route = Screen.FollowScreen.route){
FollowScreen()
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/debanshu777/compose_github/ui/base/Screen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.debanshu777.compose_github.ui.base

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.ui.graphics.vector.ImageVector

sealed class Screen(val route:String, val title:String, val icon:ImageVector){
object SearchScreen: Screen("search_screen","Search", Icons.Default.Search)
object ProfileScreen: Screen("profile_screen","Profile", Icons.Default.Person)
object TrendingScreen: Screen("trending_screen","Trending", Icons.Default.DateRange)
object FollowScreen: Screen("follow_screen","Follow", Icons.Default.Favorite)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.debanshu777.compose_github.ui.base.components

import androidx.compose.foundation.layout.RowScope
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import com.debanshu777.compose_github.ui.base.Screen

@Composable
fun RowScope.AddItem(
screen: Screen,
currentDestination: NavDestination?,
navController: NavHostController
){
BottomNavigationItem(
label={
Text(text = screen.title)
},
icon = {
Icon(
imageVector = screen.icon,
contentDescription = "Navigation Icon"
)
},
selected = currentDestination?.hierarchy?.any{
it.route == screen.route
} == true,
unselectedContentColor = LocalContentColor.current.copy(alpha = ContentAlpha.disabled),
onClick = {
navController.navigate(screen.route){
popUpTo(navController.graph.findStartDestination().id)
launchSingleTop =true
}
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.debanshu777.compose_github.ui.base.components

import androidx.compose.material.BottomNavigation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.debanshu777.compose_github.ui.base.Screen

@Composable
fun BottomBar(navController: NavHostController){
val screens = listOf(
Screen.TrendingScreen,
Screen.FollowScreen
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination=navBackStackEntry?.destination
BottomNavigation {
screens.forEach{ screen ->
AddItem(screen = screen, currentDestination = currentDestination, navController = navController)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.debanshu777.compose_github.ui.base.components.SearchBar
import com.debanshu777.compose_github.ui.feature_search.state.SearchWidgetState

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.debanshu777.compose_github.ui.feature_search.components
package com.debanshu777.compose_github.ui.base.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.debanshu777.compose_github.ui.feature_follow

import androidx.compose.material.Text
import androidx.compose.runtime.Composable

@Composable
fun FollowScreen(){
Text("Follow Screen")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel
import com.debanshu777.compose_github.network.dataSource.githubViewModel

@Composable
fun ProfileScreen(viewModel:GitHubViewModel){
fun ProfileScreen(viewModel:githubViewModel){
val profileData by viewModel.userDataState.collectAsState()
Text(text = profileData.data.toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,18 @@ import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel
import com.debanshu777.compose_github.network.dataSource.githubViewModel
import com.debanshu777.compose_github.ui.feature_search.components.Card
import com.debanshu777.compose_github.ui.feature_search.components.MainAppBar
import com.debanshu777.compose_github.ui.feature_search.state.SearchState
import com.debanshu777.compose_github.ui.feature_search.state.SearchWidgetState
import com.debanshu777.compose_github.ui.navigation.Screen
import com.debanshu777.compose_github.ui.base.Screen

@OptIn(ExperimentalUnitApi::class)
@Composable
fun SearchScreen(viewModel: GitHubViewModel, navController: NavController){
val searchWidgetState by viewModel.searchWidgetState
val searchTextState by viewModel.searchTextState
fun SearchScreen(viewModel: githubViewModel, navController: NavController){
val searchData by viewModel.searchState.collectAsState()
Scaffold(
topBar = {
MainAppBar(
searchWidgetState = searchWidgetState,
searchTextState = searchTextState,
onTextChange = {
viewModel.updateSearchTextState(it)
},
onCloseClicked = {
viewModel.updateSearchTextState("")
viewModel.updateSearchWidgetState(SearchWidgetState.CLOSED)
viewModel.searchState.value= SearchState(data= emptyList())
},
onSearchClick = {
viewModel.searchUser(it)
},
onSearchTriggered={
viewModel.updateSearchWidgetState(SearchWidgetState.OPENED)
}
)
}

) {
LazyColumn(
modifier = Modifier.fillMaxWidth(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.debanshu777.compose_github.ui.feature_trending

import androidx.compose.material.Text
import androidx.compose.runtime.Composable

@Composable
fun TrendingScreen() {
Text("Trending Screen")
}

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ task clean(type: Delete) {

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += "-opt-in=org.mylibrary.OptInAnnotation"
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
}
}

0 comments on commit 8d2e33b

Please sign in to comment.