From b4fdc4b5b2a551db234c2f5b6b805e07500105b7 Mon Sep 17 00:00:00 2001 From: Ranbir Singh Date: Wed, 18 Oct 2023 20:56:45 +0530 Subject: [PATCH 1/2] removed hardcoded compose navigator from composable. --- .../navigation/WhatsAppNavHost.kt | 7 ++--- .../navigation/WhatsAppNavigation.kt | 12 +++------ .../navigation/WhatsAppPagerContent.kt | 13 +++------ .../whatsappclone/ui/WhatsAppCloneMain.kt | 3 +-- .../whatsappclone/ui/WhatsAppTabPager.kt | 6 ++--- .../whatsappclone/calls/WhatsAppCalls.kt | 17 ++++++------ .../calls/WhatsAppCallsViewModel.kt | 12 +++++++-- .../calls/info/WhatsAppCallHistoryInfo.kt | 5 ++-- .../calls/info/WhatsAppCallHistoryInfoBody.kt | 4 ++- .../calls/info/WhatsAppCallHistoryTopBar.kt | 17 +++++------- .../info/WhatsAppCallHistoryViewModel.kt | 17 ++++++++++++ .../chats/{ => channels}/WhatsAppChannels.kt | 9 +++---- .../chats/channels/WhatsChannelsViewModel.kt | 20 ++++++++++++++ .../chats/messages/WhatsAppMessageTopBar.kt | 21 ++++++--------- .../chats/messages/WhatsAppMessages.kt | 27 +++++++------------ .../messages/WhatsAppMessagesViewModel.kt | 20 +++++++++++--- 16 files changed, 119 insertions(+), 91 deletions(-) create mode 100644 features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt rename features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/{ => channels}/WhatsAppChannels.kt (87%) create mode 100644 features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavHost.kt b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavHost.kt index 0019739..f3a56b5 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavHost.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavHost.kt @@ -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() } } diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt index c402ef7..4d4ee1f 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt @@ -29,13 +29,11 @@ import io.getstream.whatsappclone.ui.WhatsAppTabPager import io.getstream.whatsappclone.ui.WhatsAppTopBar fun NavGraphBuilder.whatsAppHomeNavigation( - composeNavigator: AppComposeNavigator ) { composable(route = WhatsAppScreens.Home.name) { Scaffold(topBar = { WhatsAppTopBar() }) { padding -> WhatsAppTabPager( - modifier = Modifier.padding(padding), - composeNavigator = composeNavigator + modifier = Modifier.padding(padding) ) } } @@ -47,9 +45,8 @@ fun NavGraphBuilder.whatsAppHomeNavigation( val channelId = it.arguments?.getString("channelId") ?: return@composable WhatsAppMessages( channelId = channelId, - composeNavigator = composeNavigator, - whatsAppMessagesViewModel = hiltViewModel() - ) + + ) } composable( @@ -61,7 +58,6 @@ fun NavGraphBuilder.whatsAppHomeNavigation( WhatsAppCallHistoryInfo( whatsAppUser = whatsAppUser, - composeNavigator = composeNavigator - ) + ) } } diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppPagerContent.kt b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppPagerContent.kt index 404808f..7acaac6 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppPagerContent.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppPagerContent.kt @@ -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 { @@ -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() } } diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppCloneMain.kt b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppCloneMain.kt index 285160c..c6e9c22 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppCloneMain.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppCloneMain.kt @@ -37,8 +37,7 @@ fun WhatsAppCloneMain( WhatsAppCloneBackground { WhatsAppNavHost( - navHostController = navHostController, - composeNavigator = composeNavigator + navHostController = navHostController ) } } diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt index 2d24f29..b9056b2 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt @@ -46,8 +46,7 @@ import kotlinx.coroutines.launch @Composable fun WhatsAppTabPager( - modifier: Modifier = Modifier, - composeNavigator: AppComposeNavigator + modifier: Modifier = Modifier ) { val coroutineScope = rememberCoroutineScope() val pagerState = rememberPagerState() @@ -110,8 +109,7 @@ fun WhatsAppTabPager( ) { page -> WhatsAppPagerContent( - page = page, - composeNavigator = composeNavigator + page = page ) } } diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt index f490878..9ce1774 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt @@ -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() @@ -55,7 +54,7 @@ private fun WhatsAppCallsScreen( key = { it.name } ) { WhatsAppCallHistory(whatsAppUser = it) { - composeNavigator.navigate(WhatsAppScreens.CallInfo.createRoute(whatsAppUser = it)) + onHistoryItemClick(it) } } } diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt index 228f5a2..b1b7a89 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt @@ -21,17 +21,21 @@ 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 import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.stateIn +import javax.inject.Inject @HiltViewModel class WhatsAppCallsViewModel @Inject constructor( - callHistoryRepository: CallHistoryRepository + callHistoryRepository: CallHistoryRepository, + private val composeNavigator: AppComposeNavigator ) : ViewModel() { val whatsAppUserState: StateFlow = @@ -52,4 +56,8 @@ class WhatsAppCallsViewModel @Inject constructor( started = WhileSubscribedOrRetained, initialValue = WhatsAppUserUiState.Loading ) + + fun navigateToCallInfo(whatsAppUser: WhatsAppUser) { + composeNavigator.navigate(WhatsAppScreens.CallInfo.createRoute(whatsAppUser = whatsAppUser)) + } } diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt index 9acae97..7af44b0 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt @@ -21,17 +21,18 @@ 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), diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt index 1f96281..5f07616 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt @@ -28,6 +28,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.unit.dp import androidx.constraintlayout.compose.ConstraintLayout +import androidx.hilt.navigation.compose.hiltViewModel import com.skydoves.landscapist.glide.GlideImage import io.getstream.whatsappclone.designsystem.R import io.getstream.whatsappclone.designsystem.icon.WhatsAppIcons @@ -40,7 +41,8 @@ import java.util.Date @Composable fun WhatsAppCallHistoryInfoBody( modifier: Modifier, - whatsAppUser: WhatsAppUser + whatsAppUser: WhatsAppUser, + ) { ConstraintLayout(modifier = modifier.padding(12.dp)) { val (image, name, call, divider, location, date) = createRefs() diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt index 4ebc70d..b87f8fa 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt @@ -37,17 +37,17 @@ import io.getstream.whatsappclone.navigation.WhatsAppCloneComposeNavigator @Composable fun WhatsAppCallHistoryTopBar( - composeNavigator: AppComposeNavigator + onBackClick: () -> Unit ) { TopAppBar( modifier = Modifier.fillMaxWidth(), navigationIcon = { Icon( modifier = Modifier - .size(26.dp) - .clickable { - composeNavigator.navigateUp() - }, + .size(26.dp) + .clickable { + onBackClick() + }, imageVector = WhatsAppIcons.ArrowBack, tint = MaterialTheme.colorScheme.tertiary, contentDescription = null @@ -85,9 +85,7 @@ fun WhatsAppCallHistoryTopBar( @Composable private fun WhatsAppCallHistoryTopBarPreview() { WhatsAppCloneComposeTheme { - WhatsAppCallHistoryTopBar( - composeNavigator = WhatsAppCloneComposeNavigator() - ) + WhatsAppCallHistoryTopBar(onBackClick = {}) } } @@ -95,8 +93,7 @@ private fun WhatsAppCallHistoryTopBarPreview() { @Composable private fun WhatsAppCallHistoryTopBarDarkPreview() { WhatsAppCloneComposeTheme(darkTheme = true) { - WhatsAppCallHistoryTopBar( - composeNavigator = WhatsAppCloneComposeNavigator() + WhatsAppCallHistoryTopBar(onBackClick = {} ) } } diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt new file mode 100644 index 0000000..074bcee --- /dev/null +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt @@ -0,0 +1,17 @@ +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() + } +} diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/WhatsAppChannels.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsAppChannels.kt similarity index 87% rename from features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/WhatsAppChannels.kt rename to features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsAppChannels.kt index 9115c25..1b56c54 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/WhatsAppChannels.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsAppChannels.kt @@ -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 @@ -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) } ) diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt new file mode 100644 index 0000000..0f21916 --- /dev/null +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt @@ -0,0 +1,20 @@ +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)) + } + + +} diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt index 3eb3a3f..79f2215 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt @@ -29,29 +29,24 @@ import androidx.compose.material3.SmallTopAppBar import androidx.compose.material3.Text import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.skydoves.landscapist.animation.crossfade.CrossfadePlugin import com.skydoves.landscapist.components.rememberImageComponent import com.skydoves.landscapist.glide.GlideImage -import io.getstream.chat.android.client.ChatClient import io.getstream.whatsappclone.designsystem.component.WhatsAppLoadingIndicator 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 import io.getstream.whatsappclone.uistate.WhatsAppMessageUiState @Composable fun WhatsAppMessageTopBar( - viewModel: WhatsAppMessagesViewModel, - composeNavigator: AppComposeNavigator + messageUiState: WhatsAppMessageUiState, + onBackClick: () -> Unit ) { - val messageUiState by viewModel.messageUiSate.collectAsStateWithLifecycle() + SmallTopAppBar( modifier = Modifier.fillMaxWidth(), @@ -60,7 +55,7 @@ fun WhatsAppMessageTopBar( modifier = Modifier .size(26.dp) .clickable { - composeNavigator.navigateUp() + onBackClick() }, imageVector = WhatsAppIcons.ArrowBack, tint = MaterialTheme.colorScheme.tertiary, @@ -141,8 +136,8 @@ private fun WhatsAppMessageUserInfo( private fun WhatsAppTopBarPreview() { WhatsAppCloneComposeTheme { WhatsAppMessageTopBar( - WhatsAppMessagesViewModel(ChatClient.instance()), - WhatsAppCloneComposeNavigator() + messageUiState = WhatsAppMessageUiState.Loading, + onBackClick = {} ) } } @@ -152,8 +147,8 @@ private fun WhatsAppTopBarPreview() { private fun WhatsAppTopBarDarkPreview() { WhatsAppCloneComposeTheme(darkTheme = true) { WhatsAppMessageTopBar( - WhatsAppMessagesViewModel(ChatClient.instance()), - WhatsAppCloneComposeNavigator() + messageUiState = WhatsAppMessageUiState.Loading, + onBackClick = {} ) } } diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt index 654da70..22d3d6e 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt @@ -19,36 +19,27 @@ package io.getstream.whatsappclone.chats.messages import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier +import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import io.getstream.chat.android.compose.ui.messages.MessagesScreen import io.getstream.whatsappclone.chats.theme.WhatsAppChatTheme -import io.getstream.whatsappclone.navigation.AppComposeNavigator @Composable fun WhatsAppMessages( - channelId: String, - composeNavigator: AppComposeNavigator, - whatsAppMessagesViewModel: WhatsAppMessagesViewModel + channelId: String, whatsAppMessagesViewModel: WhatsAppMessagesViewModel = hiltViewModel() ) { - LaunchedEffect(key1 = channelId) { - whatsAppMessagesViewModel.handleEvents( - WhatsAppMessageEvent.FetchChannel(channelId) - ) - } + val messageUiState by whatsAppMessagesViewModel.messageUiSate.collectAsStateWithLifecycle() WhatsAppChatTheme { Column(Modifier.fillMaxSize()) { - WhatsAppMessageTopBar( - viewModel = whatsAppMessagesViewModel, - composeNavigator = composeNavigator - ) + WhatsAppMessageTopBar(messageUiState = messageUiState, + onBackClick = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) }) - MessagesScreen( - channelId = channelId, + MessagesScreen(channelId = channelId, showHeader = false, - onBackPressed = { composeNavigator.navigateUp() } - ) + onBackPressed = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) }) } } } diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt index cd332b4..c75c147 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt @@ -16,12 +16,14 @@ package io.getstream.whatsappclone.chats.messages +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import io.getstream.chat.android.client.ChatClient import io.getstream.chat.android.client.utils.onError import io.getstream.chat.android.client.utils.onSuccess +import io.getstream.whatsappclone.navigation.AppComposeNavigator import io.getstream.whatsappclone.uistate.WhatsAppMessageUiState import javax.inject.Inject import kotlinx.coroutines.flow.MutableStateFlow @@ -30,15 +32,25 @@ import kotlinx.coroutines.launch @HiltViewModel class WhatsAppMessagesViewModel @Inject constructor( - private val chatClient: ChatClient + private val chatClient: ChatClient, + private val composeNavigator: AppComposeNavigator, + savedStateHandle: SavedStateHandle ) : ViewModel() { private val messageMutableUiState = MutableStateFlow(WhatsAppMessageUiState.Loading) val messageUiSate: StateFlow = messageMutableUiState + private val channelId = savedStateHandle.get("channelId") + + init { + if (channelId != null) { + fetchChannel(channelId = channelId) + } + } + fun handleEvents(whatsAppMessageEvent: WhatsAppMessageEvent) { when (whatsAppMessageEvent) { - is WhatsAppMessageEvent.FetchChannel -> fetchChannel(whatsAppMessageEvent.channelId) + is WhatsAppMessageEvent.NavigateUp -> composeNavigator.navigateUp() } } @@ -52,8 +64,10 @@ class WhatsAppMessagesViewModel @Inject constructor( } } } + + } sealed interface WhatsAppMessageEvent { - class FetchChannel(val channelId: String) : WhatsAppMessageEvent + data object NavigateUp : WhatsAppMessageEvent } From 11c649409978536cc2d2da94f278952b36cef269 Mon Sep 17 00:00:00 2001 From: Ranbir Singh Date: Wed, 18 Oct 2023 20:58:27 +0530 Subject: [PATCH 2/2] removed hardcoded compose navigator from composable. --- .../navigation/WhatsAppNavigation.kt | 12 +++++------ .../whatsappclone/ui/WhatsAppTabPager.kt | 1 - .../whatsappclone/calls/WhatsAppCalls.kt | 2 +- .../calls/WhatsAppCallsViewModel.kt | 2 +- .../calls/info/WhatsAppCallHistoryInfo.kt | 1 - .../calls/info/WhatsAppCallHistoryInfoBody.kt | 3 +-- .../calls/info/WhatsAppCallHistoryTopBar.kt | 13 ++++++------ .../info/WhatsAppCallHistoryViewModel.kt | 17 ++++++++++++++- .../chats/channels/WhatsChannelsViewModel.kt | 21 +++++++++++++++---- .../chats/messages/WhatsAppMessageTopBar.kt | 2 -- .../chats/messages/WhatsAppMessages.kt | 15 ++++++++----- .../messages/WhatsAppMessagesViewModel.kt | 2 -- 12 files changed, 57 insertions(+), 34 deletions(-) diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt index 4d4ee1f..d23eb91 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/navigation/WhatsAppNavigation.kt @@ -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 @@ -28,8 +27,7 @@ import io.getstream.whatsappclone.model.WhatsAppUser import io.getstream.whatsappclone.ui.WhatsAppTabPager import io.getstream.whatsappclone.ui.WhatsAppTopBar -fun NavGraphBuilder.whatsAppHomeNavigation( -) { +fun NavGraphBuilder.whatsAppHomeNavigation() { composable(route = WhatsAppScreens.Home.name) { Scaffold(topBar = { WhatsAppTopBar() }) { padding -> WhatsAppTabPager( @@ -44,9 +42,9 @@ fun NavGraphBuilder.whatsAppHomeNavigation( ) { val channelId = it.arguments?.getString("channelId") ?: return@composable WhatsAppMessages( - channelId = channelId, + channelId = channelId - ) + ) } composable( @@ -57,7 +55,7 @@ fun NavGraphBuilder.whatsAppHomeNavigation( ?: return@composable WhatsAppCallHistoryInfo( - whatsAppUser = whatsAppUser, - ) + whatsAppUser = whatsAppUser + ) } } diff --git a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt index b9056b2..84914b5 100644 --- a/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt +++ b/app/src/main/kotlin/io/getstream/whatsappclone/ui/WhatsAppTabPager.kt @@ -38,7 +38,6 @@ 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 diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt index 9ce1774..c7dd0fa 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCalls.kt @@ -42,7 +42,7 @@ fun WhatsAppCalls( @Composable private fun WhatsAppCallsScreen( whatsAppUsersUiState: WhatsAppUserUiState, - onHistoryItemClick: (WhatsAppUser) -> Unit, + onHistoryItemClick: (WhatsAppUser) -> Unit ) { when (whatsAppUsersUiState) { WhatsAppUserUiState.Loading -> WhatsAppLoadingColumn() diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt index b1b7a89..b8158eb 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/WhatsAppCallsViewModel.kt @@ -26,11 +26,11 @@ 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 import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.stateIn -import javax.inject.Inject @HiltViewModel class WhatsAppCallsViewModel @Inject constructor( diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt index 7af44b0..a654188 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfo.kt @@ -23,7 +23,6 @@ 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( diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt index 5f07616..41f8216 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryInfoBody.kt @@ -28,7 +28,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.unit.dp import androidx.constraintlayout.compose.ConstraintLayout -import androidx.hilt.navigation.compose.hiltViewModel import com.skydoves.landscapist.glide.GlideImage import io.getstream.whatsappclone.designsystem.R import io.getstream.whatsappclone.designsystem.icon.WhatsAppIcons @@ -41,7 +40,7 @@ import java.util.Date @Composable fun WhatsAppCallHistoryInfoBody( modifier: Modifier, - whatsAppUser: WhatsAppUser, + whatsAppUser: WhatsAppUser ) { ConstraintLayout(modifier = modifier.padding(12.dp)) { diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt index b87f8fa..ae08146 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryTopBar.kt @@ -32,8 +32,6 @@ 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( @@ -44,10 +42,10 @@ fun WhatsAppCallHistoryTopBar( navigationIcon = { Icon( modifier = Modifier - .size(26.dp) - .clickable { - onBackClick() - }, + .size(26.dp) + .clickable { + onBackClick() + }, imageVector = WhatsAppIcons.ArrowBack, tint = MaterialTheme.colorScheme.tertiary, contentDescription = null @@ -93,7 +91,8 @@ private fun WhatsAppCallHistoryTopBarPreview() { @Composable private fun WhatsAppCallHistoryTopBarDarkPreview() { WhatsAppCloneComposeTheme(darkTheme = true) { - WhatsAppCallHistoryTopBar(onBackClick = {} + WhatsAppCallHistoryTopBar( + onBackClick = {} ) } } diff --git a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt index 074bcee..0723dc1 100644 --- a/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt @@ -1,3 +1,19 @@ +/* + * 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 @@ -10,7 +26,6 @@ class WhatsAppCallHistoryViewModel @Inject constructor( private val composeNavigator: AppComposeNavigator ) : ViewModel() { - fun navigateUp() { composeNavigator.navigateUp() } diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt index 0f21916..1a74667 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt @@ -1,3 +1,19 @@ +/* + * 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 @@ -8,13 +24,10 @@ import javax.inject.Inject @HiltViewModel class WhatsChannelsViewModel @Inject constructor( - private val composeNavigator: AppComposeNavigator, + private val composeNavigator: AppComposeNavigator ) : ViewModel() { - fun navigateToMessages(channelId: String) { composeNavigator.navigate(WhatsAppScreens.Messages.createRoute(channelId)) } - - } diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt index 79f2215..e5dbbc4 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessageTopBar.kt @@ -46,8 +46,6 @@ fun WhatsAppMessageTopBar( messageUiState: WhatsAppMessageUiState, onBackClick: () -> Unit ) { - - SmallTopAppBar( modifier = Modifier.fillMaxWidth(), navigationIcon = { diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt index 22d3d6e..d06eb62 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessages.kt @@ -28,18 +28,23 @@ import io.getstream.whatsappclone.chats.theme.WhatsAppChatTheme @Composable fun WhatsAppMessages( - channelId: String, whatsAppMessagesViewModel: WhatsAppMessagesViewModel = hiltViewModel() + channelId: String, + whatsAppMessagesViewModel: WhatsAppMessagesViewModel = hiltViewModel() ) { val messageUiState by whatsAppMessagesViewModel.messageUiSate.collectAsStateWithLifecycle() WhatsAppChatTheme { Column(Modifier.fillMaxSize()) { - WhatsAppMessageTopBar(messageUiState = messageUiState, - onBackClick = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) }) + WhatsAppMessageTopBar( + messageUiState = messageUiState, + onBackClick = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) } + ) - MessagesScreen(channelId = channelId, + MessagesScreen( + channelId = channelId, showHeader = false, - onBackPressed = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) }) + onBackPressed = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) } + ) } } } diff --git a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt index c75c147..d1f2536 100644 --- a/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/messages/WhatsAppMessagesViewModel.kt @@ -64,8 +64,6 @@ class WhatsAppMessagesViewModel @Inject constructor( } } } - - } sealed interface WhatsAppMessageEvent {