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

MessagesAction Update to also handle non-actionable messages without failing #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -14,7 +14,7 @@ class MessageActionMapper @Inject constructor() {
fun map(action: String?): Result<MessagesAction> {
return runCatching {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is another error being thrown in the else case. I would propose changing the return type of this method to MessageAction? instead of result and update to runCatching { // exisiting logic }.getOrNull(). No need for NoAction imo as we can represent it with the null case

when {
action.isNullOrBlank() -> error("Invalid action type")
action.isNullOrBlank() -> MessagesAction.NoAction
videoSectionRegex.matches(action) -> {
mapVideoSectionAction(action).getOrThrow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ data class Message(
val description: String? = null,
val action: String?,
val isDismissible: Boolean = true,
val isLoading: Boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some logic in HomeViewModel that shows a loading indicator while a message is being processed, e.g. health summary pdf generation. I think we still need this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this out now, yes - but I believe this should actually be in some wrapper type or something, since it has nothing to do with the model itself. Like this, I would assume that the server sends us this information, which is not the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, in general there should be distinct types for domain and ui layer, e.g. Message and MessageUiModel and currently the message is serving for both layers

val isExpanded: Boolean = false,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
Expand Down Expand Up @@ -125,17 +124,14 @@ fun MessageItem(
)
}
Spacer(modifier = Modifier.weight(1f))
Button(
modifier = Modifier.testIdentifier(MessageItemTestIdentifiers.ACTION_BUTTON),
colors = ButtonDefaults.buttonColors(containerColor = primary),
enabled = !message.isLoading,
onClick = {
onAction(Action.MessageItemClicked(message))
},
) {
if (message.isLoading) {
CircularProgressIndicator(modifier = Modifier.size(Sizes.Content.small))
} else {
if (message.action != null || message.isDismissible) {
Button(
modifier = Modifier.testIdentifier(MessageItemTestIdentifiers.ACTION_BUTTON),
colors = ButtonDefaults.buttonColors(containerColor = primary),
onClick = {
onAction(Action.MessageItemClicked(message))
},
) {
Text(
text = stringResource(R.string.message_item_button_action_text),
color = Colors.onPrimary,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.stanford.bdh.engagehf.messages

sealed class MessagesAction {
data object NoAction : MessagesAction()
data class VideoSectionAction(val videoSectionVideo: VideoSectionVideo) : MessagesAction()
data object MedicationsAction : MessagesAction()
data object MeasurementsAction : MessagesAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MessagesHandler @Inject constructor(
val actionResult = messagesActionMapper.map(action = message.action)
var failure = actionResult.exceptionOrNull()
when (val messagesAction = actionResult.getOrNull()) {
is MessagesAction.NoAction -> Unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not be needed as it is covered in the else case (applies also in case you change the response to nullable instead of result)

Question: Should we complete the message below if we could not map any action for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure actually, would need to check iOS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked: iOS only calls dismissMessage, if an action has been performed and the message has isDismissible set to true.

is MessagesAction.HealthSummaryAction -> {
failure = healthSummaryService.generateHealthSummaryPdf().exceptionOrNull()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import edu.stanford.spezi.core.design.component.AsyncTextButton
import edu.stanford.spezi.core.design.component.VerticalSpacer
import edu.stanford.spezi.core.design.component.validated.outlinedtextfield.ValidatedOutlinedTextField
import edu.stanford.spezi.core.design.theme.Colors
import edu.stanford.spezi.core.design.theme.Spacings
Expand All @@ -56,8 +55,6 @@ import edu.stanford.spezi.core.design.theme.TextStyles.bodyLarge
import edu.stanford.spezi.core.design.theme.TextStyles.titleLarge
import edu.stanford.spezi.core.utils.extensions.testIdentifier
import edu.stanford.spezi.module.account.R
import edu.stanford.spezi.module.account.login.components.SignInWithGoogleButton
import edu.stanford.spezi.module.account.login.components.TextDivider
import edu.stanford.spezi.module.account.register.FieldState
import edu.stanford.spezi.module.account.register.IconLeadingContent
import edu.stanford.spezi.core.design.R as DesignR
Expand Down
Loading