Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit error message from sending messages #24

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ val chatGPTUser = User(
"206655413-fb7c70f6-703e-476b-9ee9-861bfb8bf6f7.jpeg"
)

val commonChannelId: String = "messaging:4d7cd1e8-e6d6-4df3-bfad-babbb9411cce"
const val commonChannelId: String = "messaging:4d7cd1e8-e6d6-4df3-bfad-babbb9411cce"
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,17 @@ private fun HandleToastMessages(
) {
val context = LocalContext.current
val isMessageEmpty by viewModel.isMessageEmpty.collectAsState()
val isError by viewModel.isError.collectAsState()
val error by viewModel.error.collectAsState()

LaunchedEffect(key1 = isMessageEmpty) {
if (isMessageEmpty) {
viewModel.sendStreamChatMessage(context.getString(R.string.toast_hello))
}
}

LaunchedEffect(key1 = isError) {
if (isError) {
viewModel.sendStreamChatMessage(context.getString(R.string.toast_error_session))
LaunchedEffect(key1 = error) {
if (error.isNotEmpty()) {
viewModel.sendStreamChatMessage(error)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.skydoves.chatgpt.core.model.GPTChatRequest
import com.skydoves.chatgpt.core.model.GPTContent
import com.skydoves.chatgpt.core.model.GPTMessage
import com.skydoves.chatgpt.core.navigation.ChatGPTScreens.Companion.argument_channel_id
import com.skydoves.chatgpt.core.preferences.Empty
import com.skydoves.sandwich.message
import com.skydoves.sandwich.messageOrNull
import com.skydoves.sandwich.onFailure
Expand All @@ -39,8 +40,8 @@ import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

Expand All @@ -58,10 +59,10 @@ class ChatGPTMessagesViewModel @Inject constructor(
it.isNotEmpty()
}.stateIn(viewModelScope, WhileSubscribedOrRetained, false)

private val mutableIsError: MutableStateFlow<String> = MutableStateFlow("")
val isError: StateFlow<Boolean> = mutableIsError.mapLatest {
it.isNotEmpty()
}.stateIn(viewModelScope, WhileSubscribedOrRetained, false)
private val mutableError: MutableStateFlow<String> = MutableStateFlow("")
val error: StateFlow<String> = mutableError
.filter { it.isNotEmpty() }
.stateIn(viewModelScope, WhileSubscribedOrRetained, String.Empty)

val isMessageEmpty: StateFlow<Boolean> =
GPTMessageRepository.watchIsChannelMessageEmpty(channelId)
Expand Down Expand Up @@ -94,7 +95,7 @@ class ChatGPTMessagesViewModel @Inject constructor(
sendStreamMessage(data)
}.onFailure {
messageItemSet.value -= messageItemSet.value
mutableIsError.value = message()
mutableError.value = message()
streamLog { "Failure: $messageOrNull" }
}
}
Expand Down