Skip to content

Commit

Permalink
Allow undoing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed Dec 13, 2024
1 parent b2b9883 commit 967ed5c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Box
Expand All @@ -28,7 +27,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
Expand All @@ -41,8 +39,8 @@ import org.jetbrains.kotlinconf.ui.theme.PreviewHelper

@Composable
fun FeedbackForm(
emotion: Emotion,
onSubmit: (comment: String) -> Unit,
emotion: Emotion?,
onSubmit: (emotion: Emotion, comment: String) -> Unit,
past: Boolean,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -139,14 +137,20 @@ fun FeedbackForm(
.align(Alignment.BottomCenter),
verticalAlignment = Alignment.Bottom
) {
KodeeEmotion(emotion = emotion)
if (emotion != null) {
KodeeEmotion(emotion)
}
Spacer(Modifier.weight(1f))
Action(
label = "Send",
icon = Res.drawable.arrow_right_24,
size = ActionSize.Large,
enabled = feedbackText.isNotEmpty(),
onClick = { onSubmit(feedbackText) },
onClick = {
if (emotion != null) {
onSubmit(emotion, feedbackText)
}
},
)
}
}
Expand All @@ -156,7 +160,7 @@ fun FeedbackForm(
@Composable
internal fun FeedbackFormPreview() {
PreviewHelper {
FeedbackForm(Emotion.Positive, { text -> println("Feedback: $text") }, true)
FeedbackForm(Emotion.Negative, { text -> println("Feedback: $text") }, false)
FeedbackForm(Emotion.Positive, { emotion, text -> println("Feedback: $text") }, true)
FeedbackForm(Emotion.Negative, { emotion, text -> println("Feedback: $text") }, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -45,14 +44,11 @@ import kotlinconfapp.ui_components.generated.resources.bookmark_24
import kotlinconfapp.ui_components.generated.resources.bookmark_24_fill
import kotlinconfapp.ui_components.generated.resources.lightning_16_fill
import kotlinconfapp.ui_components.generated.resources.up_24
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.jetbrains.kotlinconf.ui.theme.Brand
import org.jetbrains.kotlinconf.ui.theme.KotlinConfTheme
import org.jetbrains.kotlinconf.ui.theme.PreviewHelper
import kotlin.time.Duration.Companion.milliseconds

enum class TalkStatus {
Past, Now, Upcoming,
Expand All @@ -74,7 +70,8 @@ fun TalkCard(
time: String,
timeNote: String?,
status: TalkStatus,
onSubmitFeedback: (Emotion, String) -> Unit,
onSubmitFeedback: (Emotion?) -> Unit,
onSubmitFeedbackWithComment: (Emotion, String) -> Unit,
modifier: Modifier = Modifier,
) {
val backgroundColor by animateColorAsState(
Expand Down Expand Up @@ -125,6 +122,7 @@ fun TalkCard(
FeedbackBlock(
status = status,
onSubmitFeedback = onSubmitFeedback,
onSubmitFeedbackWithComment = onSubmitFeedbackWithComment,
)
}
}
Expand Down Expand Up @@ -269,7 +267,8 @@ private const val FeedbackAnimationDuration = 50
@Composable
private fun FeedbackBlock(
status: TalkStatus,
onSubmitFeedback: (Emotion, String) -> Unit,
onSubmitFeedback: (Emotion?) -> Unit,
onSubmitFeedbackWithComment: (Emotion, String) -> Unit,
) {
var selectedEmotion by remember { mutableStateOf<Emotion?>(null) }
var feedbackExpanded by remember { mutableStateOf(false) }
Expand All @@ -287,6 +286,7 @@ private fun FeedbackBlock(
fadeOut(tween(FeedbackAnimationDuration))
},
modifier = Modifier.fillMaxHeight(),
contentAlignment = Alignment.CenterStart,
) { emotionSelected ->
if (emotionSelected) {
val iconRotation by animateFloatAsState(if (feedbackExpanded) 0f else 180f)
Expand Down Expand Up @@ -316,8 +316,9 @@ private fun FeedbackBlock(
emotion = emotion,
selected = selectedEmotion == emotion,
onClick = {
selectedEmotion = emotion
feedbackExpanded = true
selectedEmotion = if (emotion == selectedEmotion) null else emotion
feedbackExpanded = selectedEmotion != null
onSubmitFeedback(selectedEmotion)
},
)
}
Expand All @@ -328,23 +329,15 @@ private fun FeedbackBlock(
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
selectedEmotion?.let { emotion ->
val scope = rememberCoroutineScope()
FeedbackForm(
emotion = emotion,
onSubmit = { comment ->
onSubmitFeedback(emotion, comment)

scope.launch {
feedbackExpanded = false
delay(FeedbackAnimationDuration.milliseconds)
selectedEmotion = null
}
},
past = status == TalkStatus.Past,
modifier = Modifier.padding(top = 14.dp).focusRequester(focusRequester)
)
}
FeedbackForm(
emotion = selectedEmotion,
onSubmit = { emotion, comment ->
onSubmitFeedbackWithComment(emotion, comment)
feedbackExpanded = false
},
past = status == TalkStatus.Past,
modifier = Modifier.padding(top = 14.dp).focusRequester(focusRequester)
)
}
}
}
Expand Down Expand Up @@ -375,7 +368,8 @@ internal fun TalkCardPreview() {
time = "9:00 – 10:00",
timeNote = null,
status = TalkStatus.Now,
onSubmitFeedback = { e, s -> println("Feedback: $e, $s") },
onSubmitFeedbackWithComment = { e, s -> println("Feedback, emotion + comment: $e, $s") },
onSubmitFeedback = { e -> println("Feedback, emotion only: $e") },
modifier = Modifier.weight(1f),
)
Spacer(Modifier.width(16.dp))
Expand All @@ -395,7 +389,8 @@ internal fun TalkCardPreview() {
time = "9:00 – 10:00",
timeNote = "In 10 min",
status = TalkStatus.Upcoming,
onSubmitFeedback = { e, s -> println("Feedback: $e, $s") },
onSubmitFeedbackWithComment = { e, s -> println("Feedback, emotion + comment: $e, $s") },
onSubmitFeedback = { e -> println("Feedback, emotion only: $e") },
modifier = Modifier.weight(1f),
)
}
Expand Down

0 comments on commit 967ed5c

Please sign in to comment.