diff --git a/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessages.kt b/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessages.kt index f95ee9d3..9f40fa75 100644 --- a/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessages.kt +++ b/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessages.kt @@ -37,6 +37,7 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.viewmodel.compose.viewModel import com.skydoves.chatgpt.core.data.chat.chatGPTUser import com.skydoves.chatgpt.core.navigation.AppComposeNavigator +import com.skydoves.chatgpt.feature.chat.R import com.skydoves.chatgpt.feature.chat.theme.ChatGPTStreamTheme import io.getstream.chat.android.client.models.Channel import io.getstream.chat.android.common.state.DeletedMessageVisibility @@ -108,9 +109,7 @@ fun ChatGPTMessages( BackHandler(enabled = true, onBack = backAction) - LaunchedEffect(Unit) { - viewModel.sendHelloMessage(channelId) - } + HandleToastMessages(channelId = channelId) ChatGPTStreamTheme { Box(modifier = Modifier.fillMaxSize()) { @@ -214,3 +213,22 @@ fun ChatGPTMessages( } } } + +@Composable +private fun HandleToastMessages( + channelId: String, + viewModel: ChatGPTMessagesViewModel = hiltViewModel() +) { + val context = LocalContext.current + val isError by viewModel.isError.collectAsState() + + LaunchedEffect(key1 = Unit) { + viewModel.sendStreamChatMessage(channelId, context.getString(R.string.toast_hello)) + } + + LaunchedEffect(key1 = isError) { + if (isError) { + viewModel.sendStreamChatMessage(channelId, context.getString(R.string.toast_error_session)) + } + } +} diff --git a/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessagesViewModel.kt b/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessagesViewModel.kt index 37679946..6448d558 100644 --- a/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessagesViewModel.kt +++ b/feature-chat/src/main/kotlin/com/skydoves/chatgpt/feature/chat/messages/ChatGPTMessagesViewModel.kt @@ -25,6 +25,7 @@ import com.skydoves.chatgpt.core.model.GPTContent import com.skydoves.chatgpt.core.model.GPTMessage import com.skydoves.chatgpt.core.network.BuildConfig.CONVERSATION_ID import com.skydoves.chatgpt.core.network.BuildConfig.PARENT_MESSAGE_ID +import com.skydoves.sandwich.message import com.skydoves.sandwich.messageOrNull import com.skydoves.sandwich.onFailure import com.skydoves.sandwich.suspendOnSuccess @@ -37,6 +38,7 @@ import javax.inject.Inject import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch @@ -51,9 +53,14 @@ class ChatGPTMessagesViewModel @Inject constructor( it.isNotEmpty() }.stateIn(viewModelScope, WhileSubscribedOrRetained, false) - fun sendHelloMessage(channelId: String) { + private val mutableIsError: MutableStateFlow = MutableStateFlow("") + val isError: StateFlow = mutableIsError.mapLatest { + it.isNotEmpty() + }.stateIn(viewModelScope, WhileSubscribedOrRetained, false) + + fun sendStreamChatMessage(channelId: String, text: String) { viewModelScope.launch { - sendStreamMessage(channelId, "Hi, I'm OpenAI's ChatGPT. Ask me anything!") + sendStreamMessage(channelId, text) } } @@ -77,6 +84,8 @@ class ChatGPTMessagesViewModel @Inject constructor( messageItemSet.value -= text sendStreamMessage(channelId, data) }.onFailure { + messageItemSet.value -= messageItemSet.value + mutableIsError.value = message() streamLog { "Failure: $messageOrNull" } } } diff --git a/feature-chat/src/main/res/values/strings.xml b/feature-chat/src/main/res/values/strings.xml index d08e176e..355ff58d 100644 --- a/feature-chat/src/main/res/values/strings.xml +++ b/feature-chat/src/main/res/values/strings.xml @@ -18,5 +18,7 @@ Stream mgzfqwft2se8 A random ChatGPT channel is created! + Hi, I\'m OpenAI\'s ChatGPT. Ask me anything! Something went wrong + Something went wrong. Please check out your session and conversation id is valid.