Jetpack Compose

Oct 11 2022 · Kotlin 1.7.10, Android 13, Android Studio Chipmunk

Part 2: Build Complex UI with Compose

10. Add Actions & Handlers

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Apply Error & Data Handling to the UI Next episode: 11. Build Custom Dialogs

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 10. Add Actions & Handlers

The student materials have been reviewed and are updated as of September 2022.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Intro

[Slide 1 - Dialogs and Actions]

Demo

Let’s start off with the BookReviews feature. Open the BookReviewsFragment, and add the following code at the top:

private val _deleteReviewState = mutableStateOf<BookReview?>(null)
Box(
  contentAlignment = Alignment.Center,
  modifier = Modifier.fillMaxSize()
) {
    
    val reviewToDelete = _deleteReviewState.value

      if (reviewToDelete != null) {
        DeleteReviewDialog(
          item = reviewToDelete,
          message = stringResource(id = R.string.delete_review_message, reviewToDelete.book.name),
			...
        )
      }
...
          onDeleteItem = { bookReview ->
            deleteReview(bookReview)
            _deleteReviewState.value = null
          },
          onDismiss = {
            _deleteReviewState.value = null
          }

...   
  fun deleteReview(bookReview: BookReview) {
    lifecycleScope.launch {
      repository.removeReview(bookReview.review)

      bookReviewsState.value = repository.getReviews()
    }
  }
onItemLongTap = {
  _deleteReviewState.value = it
}
onItemLongTap: (BookReview) -> Unit

LazyColumn(Modifier.fillMaxSize()) // new

// BookReviewItem
.combinedClickable(
  interactionSource = remember { MutableInteractionSource() },
  onClick = { onItemClick(bookReview) },
        onLongClick = { onItemLongTap(bookReview) },
        indication = null)
@Composable
fun DeleteReviewDialog(
  item: BookReview,
  message: String,
  onDeleteItem: (BookReview) -> Unit,
  onDismiss: () -> Unit
) {

}
  AlertDialog(
    title = { Text(text = stringResource(id = R.string.delete_title)) },
    text = { Text(text = message) },
    onDismissRequest = onDismiss,
    buttons = {
      Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.End
      ) {

      }
    })
        DialogButton(
          text = R.string.yes,
          onClickAction = { onDeleteItem(item) }
        )

        DialogButton(text = R.string.cancel, onClickAction = onDismiss)
@Composable
fun DialogButton(
  modifier: Modifier = Modifier,
  @StringRes text: Int,
  onClickAction: () -> Unit
) {
  TextButton(
    modifier = modifier.padding(start = 8.dp, end = 8.dp, top = 8.dp, bottom = 8.dp),
    colors = buttonColors(
      backgroundColor = colorResource(id = R.color.colorPrimary),
      contentColor = Color.White),
    onClick = onClickAction
  ) {
    Text(text = stringResource(id = text))
  }
}