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

Enhancing FeedbackScreen with Performance Metrics #283

Merged
merged 12 commits into from
Dec 18, 2024
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
@@ -1,6 +1,7 @@
package com.github.se.orator.ui.overview

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
Expand Down Expand Up @@ -145,6 +146,45 @@ class FeedbackScreenTest {
.assertIsDisplayed()
}

@Test
fun performanceMetricsTitlesAreDisplayed() = runTest {
// Mock chatGPTService to return a ChatResponse with feedback message
`when`(chatGPTService.getChatCompletion(any()))
.thenReturn(
ChatResponse(
"id",
"object",
0,
"model",
listOf(Choice(0, Message("assistant", "Great job! Keep it up."), null)),
Usage(0, 0, 0)))

// Initialize ChatViewModel
chatViewModel = ChatViewModel(chatGPTService, apiLinkViewModel)

// Generate feedback with the mocked response
chatViewModel.generateFeedback()

// Advance coroutine until idle
advanceUntilIdle()

// Set the content with FeedbackScreen
composeTestRule.setContent {
FeedbackScreen(
chatViewModel = chatViewModel,
userProfileViewModel = userProfileViewModel,
apiLinkViewModel = apiLinkViewModel,
navigationActions = navigationActions)
}

// Assert that the performance metrics title is displayed
composeTestRule
.onNodeWithTag("performanceMetricsTitle")
.assertExists()
.assertIsDisplayed()
.assertTextContains("Your Performance Metrics:")
}

@Test
fun clickingTheTryAgainButtonRedirectsToHomeScreen() = runTest {
chatViewModel = ChatViewModel(chatGPTService, apiLinkViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ class ChatViewModel(
}

val systemMessage = Message(role = "system", content = systemMessageContent)

val userStartMessage = Message(role = "user", content = "I'm ready to begin the session.")

_chatMessages.value = listOf(systemMessage, userStartMessage)

getNextGPTResponse()
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/github/se/orator/ui/overview/ChatScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ fun ChatScreen(
// Collect the loading state from the view model as a state.
val isLoading by chatViewModel.isLoading.collectAsState()

// Filter out system messages for display purposes only
val displayMessages = chatMessages.filter { it.role != "system" }

// Remember the list state for the LazyColumn to manage scrolling position.
val listState = rememberLazyListState()

// Side effect to auto-scroll to the last message when a new message is added.
LaunchedEffect(chatMessages.size) {
if (chatMessages.isNotEmpty()) {
listState.animateScrollToItem(chatMessages.size - 1)
LaunchedEffect(displayMessages.size) {
if (displayMessages.isNotEmpty()) {
listState.animateScrollToItem(displayMessages.size - 1)
}
}

Expand Down Expand Up @@ -135,7 +138,7 @@ fun ChatScreen(
.testTag(
"chat_messages_list")) // Dynamically add chat message items.
{
items(chatMessages) { message -> ChatMessageItem(message) }
items(displayMessages) { message -> ChatMessageItem(message) }
}

// Display a loading indicator when a message is being processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ import com.github.se.orator.ui.network.Message
import com.github.se.orator.ui.theme.AppDimensions
import com.github.se.orator.ui.theme.AppTypography

/**
* Composable function to display the feedback screen.
*
* @param chatViewModel The view model for chat interactions.
* @param userProfileViewModel The view model for user profiles.
* @param apiLinkViewModel The view model for API links.
* @param navigationActions The actions used for navigation.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FeedbackScreen(
Expand All @@ -42,14 +34,15 @@ fun FeedbackScreen(
apiLinkViewModel: ApiLinkViewModel,
navigationActions: NavigationActions
) {
// State variables for feedback message, decision, loading status, and error message.
var feedbackMessage by remember { mutableStateOf<String?>(null) }
var decisionResult by remember { mutableStateOf<ChatViewModel.DecisionResult?>(null) }
var isLoading by remember { mutableStateOf(true) }
var errorMessage by remember { mutableStateOf<String?>(null) }

val practiceContext by apiLinkViewModel.practiceContext.collectAsState()
val userProfile by userProfileViewModel.userProfile.collectAsState()
val paceMean = userProfile?.statistics?.paceMean
val talkTimeSecondsMean = userProfile?.statistics?.talkTimeSecMean

val sessionType =
when (practiceContext) {
Expand All @@ -59,7 +52,6 @@ fun FeedbackScreen(
else -> null
}

// Retrieve the number of successful sessions
val successfulSessionsCount =
sessionType?.let { userProfile?.statistics?.successfulSessions?.get(it.name) ?: 0 } ?: 0

Expand All @@ -68,8 +60,6 @@ fun FeedbackScreen(
feedbackMessage = chatViewModel.generateFeedback()
feedbackMessage?.let {
decisionResult = parseDecisionFromFeedback(it, sessionType)

// Update user statistics based on the decision and session type
if (decisionResult != null && sessionType != null) {
userProfileViewModel.updateSessionResult(
isSuccess = decisionResult!!.isSuccess, sessionType = sessionType)
Expand Down Expand Up @@ -148,7 +138,6 @@ fun FeedbackScreen(
.weight(1f)
.verticalScroll(rememberScrollState())
.testTag("feedbackMessage")) {
// Display the decision prominently.
decisionResult?.message?.let { decisionText ->
Text(
text = decisionText,
Expand All @@ -163,29 +152,48 @@ fun FeedbackScreen(
Text(
text =
"You have successfully completed $successfulSessionsCount ${
when (sessionType) {
SessionType.SPEECH -> "speeches"
SessionType.INTERVIEW -> "interviews"
SessionType.NEGOTIATION -> "negotiations"
else -> "sessions"
}
} so far!",
when (sessionType) {
SessionType.SPEECH -> "speeches"
SessionType.INTERVIEW -> "interviews"
SessionType.NEGOTIATION -> "negotiations"
else -> "sessions"
}
} so far!",
style = AppTypography.bodyLargeStyle,
modifier =
Modifier.align(Alignment.CenterHorizontally)
.padding(AppDimensions.paddingMedium)
.testTag("successfulSessionsText"),
color = MaterialTheme.colorScheme.tertiary)
// Display the detailed feedback message.
ChatMessageItem(
message = Message(content = feedbackMessage!!, role = "assistant"))

Text(
text = "Your Performance Metrics:",
style = AppTypography.mediumTitleStyle,
modifier =
Modifier.padding(AppDimensions.paddingMedium)
.testTag("performanceMetricsTitle"))

// Display Talk Time Seconds
MetricDisplayItem(
title = "Talk Time (Seconds)",
value = talkTimeSecondsMean?.let { "%.2f".format(it) } ?: "No data",
testTag = "talkTimeSeconds")

// Display Pace
MetricDisplayItem(
title = "Pace",
value = paceMean?.let { "%.2f".format(it) } ?: "No data",
testTag = "pace")
}
}
else -> {
Text(
text = "No feedback available.",
style = AppTypography.bodyLargeStyle,
color = MaterialTheme.colorScheme.secondary)
color = MaterialTheme.colorScheme.secondary,
modifier = Modifier.testTag("feedbackNoMessage"))
Copy link
Contributor

@ymarc11 ymarc11 Dec 18, 2024

Choose a reason for hiding this comment

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

Good adaptation for the dark mode. Well done.

}
}

Expand Down Expand Up @@ -217,10 +225,41 @@ fun FeedbackScreen(
}

/**
* Function to parse the decision from the feedback message.
* A reusable composable function to display a performance metric.
*
* @param title The title of the metric, typically describing what the metric represents. Example:
* "Talk Time (Seconds)" or "Talk Time (Percentage)".
* @param value The value of the metric, formatted as a string for display. Example: "120.00" or
* "75.50%".
* @param testTag A test tag assigned to the composable for UI testing purposes.
*
* Functionality:
* - Displays a metric with its title and value in a column layout.
* - Applies consistent padding and styling to ensure visual alignment with other UI components.
* - Includes a test tag for accessibility and testing frameworks.
*
* Usage: Call this function with appropriate parameters to render a performance metric anywhere in
* the UI.
*/
@Composable
fun MetricDisplayItem(title: String, value: String, testTag: String) {
Column(modifier = Modifier.fillMaxWidth().padding(AppDimensions.paddingSmall).testTag(testTag)) {
Text(
text = title,
style = AppTypography.smallTitleStyle,
modifier = Modifier.padding(bottom = AppDimensions.paddingSmall))
Text(
text = value,
style = AppTypography.smallTitleStyle,
modifier = Modifier.padding(bottom = AppDimensions.paddingSmall))
}
}

/**
* Parses the decision from the feedback message.
*
* @param feedback The feedback message.
* @param sessionType The type of session.
* @param sessionType The session type.
* @return The decision result.
*/
private fun parseDecisionFromFeedback(
Expand Down
Loading