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..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,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) ) } } @@ -46,9 +42,8 @@ fun NavGraphBuilder.whatsAppHomeNavigation( ) { val channelId = it.arguments?.getString("channelId") ?: return@composable WhatsAppMessages( - channelId = channelId, - composeNavigator = composeNavigator, - whatsAppMessagesViewModel = hiltViewModel() + channelId = channelId + ) } @@ -60,8 +55,7 @@ fun NavGraphBuilder.whatsAppHomeNavigation( ?: return@composable WhatsAppCallHistoryInfo( - whatsAppUser = whatsAppUser, - composeNavigator = composeNavigator + whatsAppUser = whatsAppUser ) } } 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..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 @@ -46,8 +45,7 @@ import kotlinx.coroutines.launch @Composable fun WhatsAppTabPager( - modifier: Modifier = Modifier, - composeNavigator: AppComposeNavigator + modifier: Modifier = Modifier ) { val coroutineScope = rememberCoroutineScope() val pagerState = rememberPagerState() @@ -110,8 +108,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..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 @@ -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..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 @@ -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 @@ -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 = @@ -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..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 @@ -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), 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..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 @@ -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() 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..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,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(), @@ -46,7 +44,7 @@ fun WhatsAppCallHistoryTopBar( modifier = Modifier .size(26.dp) .clickable { - composeNavigator.navigateUp() + onBackClick() }, imageVector = WhatsAppIcons.ArrowBack, tint = MaterialTheme.colorScheme.tertiary, @@ -85,9 +83,7 @@ fun WhatsAppCallHistoryTopBar( @Composable private fun WhatsAppCallHistoryTopBarPreview() { WhatsAppCloneComposeTheme { - WhatsAppCallHistoryTopBar( - composeNavigator = WhatsAppCloneComposeNavigator() - ) + WhatsAppCallHistoryTopBar(onBackClick = {}) } } @@ -96,7 +92,7 @@ private fun WhatsAppCallHistoryTopBarPreview() { private fun WhatsAppCallHistoryTopBarDarkPreview() { WhatsAppCloneComposeTheme(darkTheme = true) { WhatsAppCallHistoryTopBar( - composeNavigator = WhatsAppCloneComposeNavigator() + 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..0723dc1 --- /dev/null +++ b/features/calls/src/main/kotlin/io/getstream/whatsappclone/calls/info/WhatsAppCallHistoryViewModel.kt @@ -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() + } +} 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..1a74667 --- /dev/null +++ b/features/chats/src/main/kotlin/io/getstream/whatsappclone/chats/channels/WhatsChannelsViewModel.kt @@ -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)) + } +} 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..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 @@ -29,30 +29,23 @@ 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(), navigationIcon = { @@ -60,7 +53,7 @@ fun WhatsAppMessageTopBar( modifier = Modifier .size(26.dp) .clickable { - composeNavigator.navigateUp() + onBackClick() }, imageVector = WhatsAppIcons.ArrowBack, tint = MaterialTheme.colorScheme.tertiary, @@ -141,8 +134,8 @@ private fun WhatsAppMessageUserInfo( private fun WhatsAppTopBarPreview() { WhatsAppCloneComposeTheme { WhatsAppMessageTopBar( - WhatsAppMessagesViewModel(ChatClient.instance()), - WhatsAppCloneComposeNavigator() + messageUiState = WhatsAppMessageUiState.Loading, + onBackClick = {} ) } } @@ -152,8 +145,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..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 @@ -19,35 +19,31 @@ 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 + 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 + messageUiState = messageUiState, + onBackClick = { whatsAppMessagesViewModel.handleEvents(WhatsAppMessageEvent.NavigateUp) } ) 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..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 @@ -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() } } @@ -55,5 +67,5 @@ class WhatsAppMessagesViewModel @Inject constructor( } sealed interface WhatsAppMessageEvent { - class FetchChannel(val channelId: String) : WhatsAppMessageEvent + data object NavigateUp : WhatsAppMessageEvent }