Skip to content

Commit

Permalink
Merge pull request #103 from AndroidPoet/removed_hardcoded_navigator
Browse files Browse the repository at this point in the history
Removed hardcoded navigator
  • Loading branch information
skydoves authored Oct 20, 2023
2 parents 0e4d24e + 11c6494 commit 19bf96b
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ import androidx.navigation.compose.NavHost

@Composable
fun WhatsAppNavHost(
navHostController: NavHostController,
composeNavigator: AppComposeNavigator
navHostController: NavHostController
) {
NavHost(
navController = navHostController,
startDestination = WhatsAppScreens.Home.route
) {
whatsAppHomeNavigation(
composeNavigator = composeNavigator
)
whatsAppHomeNavigation()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package io.getstream.whatsappclone.navigation
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import io.getstream.whatsappclone.calls.info.WhatsAppCallHistoryInfo
Expand All @@ -28,14 +27,11 @@ import io.getstream.whatsappclone.model.WhatsAppUser
import io.getstream.whatsappclone.ui.WhatsAppTabPager
import io.getstream.whatsappclone.ui.WhatsAppTopBar

fun NavGraphBuilder.whatsAppHomeNavigation(
composeNavigator: AppComposeNavigator
) {
fun NavGraphBuilder.whatsAppHomeNavigation() {
composable(route = WhatsAppScreens.Home.name) {
Scaffold(topBar = { WhatsAppTopBar() }) { padding ->
WhatsAppTabPager(
modifier = Modifier.padding(padding),
composeNavigator = composeNavigator
modifier = Modifier.padding(padding)
)
}
}
Expand All @@ -46,9 +42,8 @@ fun NavGraphBuilder.whatsAppHomeNavigation(
) {
val channelId = it.arguments?.getString("channelId") ?: return@composable
WhatsAppMessages(
channelId = channelId,
composeNavigator = composeNavigator,
whatsAppMessagesViewModel = hiltViewModel()
channelId = channelId

)
}

Expand All @@ -60,8 +55,7 @@ fun NavGraphBuilder.whatsAppHomeNavigation(
?: return@composable

WhatsAppCallHistoryInfo(
whatsAppUser = whatsAppUser,
composeNavigator = composeNavigator
whatsAppUser = whatsAppUser
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ import android.app.Activity
import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import io.getstream.whatsappclone.calls.WhatsAppCalls
import io.getstream.whatsappclone.camera.WhatsAppCamera
import io.getstream.whatsappclone.chats.WhatsAppChannels
import io.getstream.whatsappclone.chats.channels.WhatsAppChannels
import io.getstream.whatsappclone.status.WhatsAppStatus

@Composable
fun WhatsAppPagerContent(
page: Int,
composeNavigator: AppComposeNavigator
page: Int
) {
val activity = (LocalContext.current as? Activity)
BackHandler {
Expand All @@ -38,11 +36,8 @@ fun WhatsAppPagerContent(

when (page) {
WhatsAppPage.Camera.index -> WhatsAppCamera()
WhatsAppPage.Chats.index -> WhatsAppChannels(composeNavigator = composeNavigator)
WhatsAppPage.Chats.index -> WhatsAppChannels()
WhatsAppPage.Status.index -> WhatsAppStatus()
WhatsAppPage.Calls.index -> WhatsAppCalls(
composeNavigator = composeNavigator,
whatsAppCallsViewModel = hiltViewModel()
)
WhatsAppPage.Calls.index -> WhatsAppCalls()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ fun WhatsAppCloneMain(

WhatsAppCloneBackground {
WhatsAppNavHost(
navHostController = navHostController,
composeNavigator = composeNavigator
navHostController = navHostController
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ import io.getstream.whatsappclone.designsystem.component.WhatsAppCloneBackground
import io.getstream.whatsappclone.designsystem.icon.WhatsAppIcons
import io.getstream.whatsappclone.designsystem.theme.WHITE200
import io.getstream.whatsappclone.designsystem.theme.getTabPrimaryColor
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.TOP_LEVEL_DESTINATIONS
import io.getstream.whatsappclone.navigation.WhatsAppPage
import io.getstream.whatsappclone.navigation.WhatsAppPagerContent
import kotlinx.coroutines.launch

@Composable
fun WhatsAppTabPager(
modifier: Modifier = Modifier,
composeNavigator: AppComposeNavigator
modifier: Modifier = Modifier
) {
val coroutineScope = rememberCoroutineScope()
val pagerState = rememberPagerState()
Expand Down Expand Up @@ -110,8 +108,7 @@ fun WhatsAppTabPager(
) { page ->

WhatsAppPagerContent(
page = page,
composeNavigator = composeNavigator
page = page
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,29 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.getstream.whatsappclone.designsystem.component.WhatsAppError
import io.getstream.whatsappclone.designsystem.component.WhatsAppLoadingColumn
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.WhatsAppScreens
import io.getstream.whatsappclone.model.WhatsAppUser
import io.getstream.whatsappclone.uistate.WhatsAppUserUiState

@Composable
fun WhatsAppCalls(
composeNavigator: AppComposeNavigator,
whatsAppCallsViewModel: WhatsAppCallsViewModel
whatsAppCallsViewModel: WhatsAppCallsViewModel = hiltViewModel()
) {
val whatsAppUsersUiState by whatsAppCallsViewModel.whatsAppUserState.collectAsStateWithLifecycle()

WhatsAppCallsScreen(
composeNavigator = composeNavigator,
whatsAppUsersUiState = whatsAppUsersUiState
whatsAppUsersUiState = whatsAppUsersUiState,
onHistoryItemClick = whatsAppCallsViewModel::navigateToCallInfo
)
}

@Composable
private fun WhatsAppCallsScreen(
composeNavigator: AppComposeNavigator,
whatsAppUsersUiState: WhatsAppUserUiState
whatsAppUsersUiState: WhatsAppUserUiState,
onHistoryItemClick: (WhatsAppUser) -> Unit
) {
when (whatsAppUsersUiState) {
WhatsAppUserUiState.Loading -> WhatsAppLoadingColumn()
Expand All @@ -55,7 +54,7 @@ private fun WhatsAppCallsScreen(
key = { it.name }
) {
WhatsAppCallHistory(whatsAppUser = it) {
composeNavigator.navigate(WhatsAppScreens.CallInfo.createRoute(whatsAppUser = it))
onHistoryItemClick(it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.getstream.whatsappclone.data.coroutines.WhileSubscribedOrRetained
import io.getstream.whatsappclone.data.repository.CallHistoryRepository
import io.getstream.whatsappclone.model.WhatsAppUser
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.WhatsAppScreens
import io.getstream.whatsappclone.uistate.WhatsAppUserExtensive
import io.getstream.whatsappclone.uistate.WhatsAppUserUiState
import javax.inject.Inject
Expand All @@ -31,7 +34,8 @@ import kotlinx.coroutines.flow.stateIn

@HiltViewModel
class WhatsAppCallsViewModel @Inject constructor(
callHistoryRepository: CallHistoryRepository
callHistoryRepository: CallHistoryRepository,
private val composeNavigator: AppComposeNavigator
) : ViewModel() {

val whatsAppUserState: StateFlow<WhatsAppUserUiState> =
Expand All @@ -52,4 +56,8 @@ class WhatsAppCallsViewModel @Inject constructor(
started = WhileSubscribedOrRetained,
initialValue = WhatsAppUserUiState.Loading
)

fun navigateToCallInfo(whatsAppUser: WhatsAppUser) {
composeNavigator.navigate(WhatsAppScreens.CallInfo.createRoute(whatsAppUser = whatsAppUser))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import io.getstream.whatsappclone.model.WhatsAppUser
import io.getstream.whatsappclone.navigation.AppComposeNavigator

@Composable
fun WhatsAppCallHistoryInfo(
whatsAppUser: WhatsAppUser,
composeNavigator: AppComposeNavigator
whatsAppCallHistoryViewModel: WhatsAppCallHistoryViewModel = hiltViewModel()
) {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = { WhatsAppCallHistoryTopBar(composeNavigator = composeNavigator) }
topBar = { WhatsAppCallHistoryTopBar(onBackClick = whatsAppCallHistoryViewModel::navigateUp) }
) {
WhatsAppCallHistoryInfoBody(
modifier = Modifier.padding(it),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import java.util.Date
fun WhatsAppCallHistoryInfoBody(
modifier: Modifier,
whatsAppUser: WhatsAppUser

) {
ConstraintLayout(modifier = modifier.padding(12.dp)) {
val (image, name, call, divider, location, date) = createRefs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import androidx.compose.ui.unit.dp
import io.getstream.whatsappclone.calls.R
import io.getstream.whatsappclone.designsystem.icon.WhatsAppIcons
import io.getstream.whatsappclone.designsystem.theme.WhatsAppCloneComposeTheme
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.WhatsAppCloneComposeNavigator

@Composable
fun WhatsAppCallHistoryTopBar(
composeNavigator: AppComposeNavigator
onBackClick: () -> Unit
) {
TopAppBar(
modifier = Modifier.fillMaxWidth(),
Expand All @@ -46,7 +44,7 @@ fun WhatsAppCallHistoryTopBar(
modifier = Modifier
.size(26.dp)
.clickable {
composeNavigator.navigateUp()
onBackClick()
},
imageVector = WhatsAppIcons.ArrowBack,
tint = MaterialTheme.colorScheme.tertiary,
Expand Down Expand Up @@ -85,9 +83,7 @@ fun WhatsAppCallHistoryTopBar(
@Composable
private fun WhatsAppCallHistoryTopBarPreview() {
WhatsAppCloneComposeTheme {
WhatsAppCallHistoryTopBar(
composeNavigator = WhatsAppCloneComposeNavigator()
)
WhatsAppCallHistoryTopBar(onBackClick = {})
}
}

Expand All @@ -96,7 +92,7 @@ private fun WhatsAppCallHistoryTopBarPreview() {
private fun WhatsAppCallHistoryTopBarDarkPreview() {
WhatsAppCloneComposeTheme(darkTheme = true) {
WhatsAppCallHistoryTopBar(
composeNavigator = WhatsAppCloneComposeNavigator()
onBackClick = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 Stream.IO, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.whatsappclone.calls.info

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import javax.inject.Inject

@HiltViewModel
class WhatsAppCallHistoryViewModel @Inject constructor(
private val composeNavigator: AppComposeNavigator
) : ViewModel() {

fun navigateUp() {
composeNavigator.navigateUp()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.getstream.whatsappclone.chats
package io.getstream.whatsappclone.chats.channels

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -28,23 +28,22 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import io.getstream.chat.android.compose.ui.channels.ChannelsScreen
import io.getstream.whatsappclone.chats.theme.WhatsAppChatTheme
import io.getstream.whatsappclone.designsystem.icon.WhatsAppIcons
import io.getstream.whatsappclone.designsystem.theme.GREEN500
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.WhatsAppScreens

@Composable
fun WhatsAppChannels(
composeNavigator: AppComposeNavigator
whatsChannelsViewModel: WhatsChannelsViewModel = hiltViewModel()
) {
WhatsAppChatTheme {
Box(modifier = Modifier.fillMaxSize()) {
ChannelsScreen(
isShowingHeader = false,
onItemClick = { channel ->
composeNavigator.navigate(WhatsAppScreens.Messages.createRoute(channel.cid))
whatsChannelsViewModel.navigateToMessages(channel.cid)
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Stream.IO, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.whatsappclone.chats.channels

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.getstream.whatsappclone.navigation.AppComposeNavigator
import io.getstream.whatsappclone.navigation.WhatsAppScreens
import javax.inject.Inject

@HiltViewModel
class WhatsChannelsViewModel @Inject constructor(
private val composeNavigator: AppComposeNavigator
) : ViewModel() {

fun navigateToMessages(channelId: String) {
composeNavigator.navigate(WhatsAppScreens.Messages.createRoute(channelId))
}
}
Loading

0 comments on commit 19bf96b

Please sign in to comment.