KatInterview

Star Feedback Widget with Compose and a ViewModel

Data Structure DesignModel answer
MediumModel answerData Structure Designkotlinjetpack-composeandroid

Last updated

Build a Compose feedback form with a 1-5 star selector, a capped comment field and an async submit, driven by a single ViewModel state stream that survives rotation and double taps.

Task

Build a small feedback widget for an Android app using Kotlin and Jetpack Compose. Users pick a star score from 1 to 5, optionally type a comment, and send both to a backend. Structure it with MVVM and unidirectional data flow: the UI renders state and reports events, and a ViewModel owns the state.

What to build

1. StarRatingBar (stateless)

  • Inputs: rating: Int (0 means nothing chosen), onRatingChanged: (Int) -> Unit, modifier, and maxStars: Int = 5.
  • Draws maxStars tappable icons: filled up to the current rating, outlined after it.
  • Tapping star k reports k through the callback. The component holds no state of its own.

2. RatingCardScreen

  • Shows the star bar and a comment field limited to 280 characters, with a live counter in the form "{current}/{max}".
  • Shows a Submit button, disabled when the rating is 0, the comment is over 280 characters, or a submission is already running.
  • Reflects the submission lifecycle:
    • Loading: a CircularProgressIndicator replaces or sits inside the button.
    • Success: the card is replaced by "Thank you for your feedback!" plus a Reset action.
    • Error: an inline banner or Snackbar shows the message with a Retry option.

3. RatingViewModel

  • Exposes exactly one read-only StateFlow of UI state.
  • Supports setRating(Int), updateComment(String), submit() and reset().
  • Fakes the backend with a suspend function (for example delay(1000), optionally failing at random).

Required types

data class RatingUiState(
    val rating: Int = 0,
    val comment: String = "",
    val submissionStatus: SubmissionStatus = SubmissionStatus.Idle,
    val errorMessage: String? = null
) {
    val isValid: Boolean
        get() = rating in 1..5 && comment.length <= 280
}

sealed interface SubmissionStatus {
    object Idle : SubmissionStatus
    object Loading : SubmissionStatus
    object Success : SubmissionStatus
    object Error : SubmissionStatus
}

@Composable
fun StarRatingBar(
    rating: Int,
    onRatingChanged: (Int) -> Unit,
    modifier: Modifier = Modifier,
    maxStars: Int = 5
)

@Composable
fun RatingCardScreen(
    viewModel: RatingViewModel = viewModel(),
    modifier: Modifier = Modifier
)

Cases your design must handle

  • Several fast taps on Submit before the first call finishes must not send duplicate requests.
  • Rotating the device must keep the draft rating and comment and must not fire a new request.
  • Pasting more than 280 characters must be either truncated or clearly flagged as invalid.

Write the code and explain why your design handles each case.

Hints

Hint 1

Keep all business state in the ViewModel's single immutable state object. Make the star bar and a `RatingCardContent(state, callbacks)` composable fully stateless, so the screen composable is only a thin wrapper that collects the flow.

Hint 2

Disabling the button is not enough to stop double submits, because two taps can arrive before recomposition. Do the check and the move to `Loading` in one atomic `MutableStateFlow.update {}` and only launch the coroutine if that transition succeeded.

Hint 3

Launch the request from the click handler inside `viewModelScope`, never from composition, so rotation cannot restart it. Also decide what happens on `reset()` during loading (cancel the job), whether edits are allowed while loading, and never surface a `CancellationException` as an error.