Jetpack Compose

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

Part 1: Jetpack Compose Basics

7. Build Common UI Components - Part 2

Episode complete

Play next episode

Next
Save for later
About this episode
See versions

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 6. Build Common UI Components - Part 1 Next episode: 8. Build Common UI Components - Part 3

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

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Demo


Next thing to build is the RatingBar. There currently isn’t a built-in control for rating bars in Compose, but it’s very easy to build them yourself!

@Composable
fun RatingBar(
  modifier: Modifier,
  range: IntRange,
  isLargeRating: Boolean = true,
  isSelectable: Boolean = true,
  currentRating: Int = 0,
  onRatingChanged: (Int) -> Unit = {}
) {
}
val selectedRating = remember { mutableStateOf(currentRating) }

LazyRow(modifier = modifier) {
    
}
items(range.toList()) { index ->
  RatingItem(
    isSelected = index <= selectedRating.value,
    isSelectable = isSelectable, index, isLargeRating
  ) { newRating ->
    selectedRating.value = newRating
    onRatingChanged(selectedRating.value)
  }
}

@Composable
fun RatingItem(
  isSelected: Boolean,
  isSelectable: Boolean,
  rating: Int,
  isLargeRating: Boolean,
  onRatingSelected: (Int) -> Unit
) {}
val padding = if (isLargeRating) 2.dp else 0.dp
val size = if (isLargeRating) 32.dp else 16.dp

val baseModifier = if (isSelectable) {
  Modifier.clickable(onClick = { onRatingSelected(rating) }, indication = null)
} else {
  Modifier
}
Icon(
  tint = colorResource(id = R.color.orange_200),
  modifier =
  baseModifier
    .size(size)
    .padding(padding),
  imageVector = if (isSelected) Icons.Default.Star else Icons.Default.StarOutline,
  contentDescription = rating.toString()
)
RatingBar(
  modifier = Modifier.align(CenterHorizontally), // new code + import
  range = 1..5,
  currentRating = currentRatingFilter.value,
  isLargeRating = true,
  onRatingChanged = { newRating -> currentRatingFilter.value = newRating })
@Composable
@Preview
fun BooksList(
  books: List<BookAndGenre> = emptyList()
) {
}
@Composable
@Preview
fun BooksList(
  books: List<BookAndGenre> = emptyList()
) {
  LazyColumn(modifier = Modifier.padding(top = 16.dp),
    verticalArrangement = Arrangement.spacedBy(2.dp)) { // here
    items(books) { bookAndGenre ->
      BookListItem(bookAndGenre)
    }
  }
}
@Composable
fun BookListItem(
  bookAndGenre: BookAndGenre
) {
  Card(
    modifier = Modifier
      .wrapContentHeight()
      .fillMaxWidth()
      .padding(start = 16.dp, end = 16.dp, bottom = 16.dp),
    elevation = 8.dp,
    border = BorderStroke(1.dp, MaterialTheme.colors.primary),
    shape = RoundedCornerShape(16.dp)
  ) {}
}
    Row(modifier = Modifier.fillMaxSize()) {
      Spacer(modifier = Modifier.width(16.dp))

      Column {
        Text(
          modifier = Modifier.padding(top = 16.dp),
          text = bookAndGenre.book.name,
          color = MaterialTheme.colors.primary,
          fontSize = 18.sp,
          fontWeight = FontWeight.Bold
        )

        Spacer(modifier = Modifier.height(2.dp))
      }
    }
        Text(
          fontSize = 16.sp,
          text = bookAndGenre.genre.name,
          fontStyle = FontStyle.Italic
        )

        Spacer(modifier = Modifier.height(8.dp))

        Text(
          fontSize = 12.sp,
          overflow = TextOverflow.Ellipsis,
          text = bookAndGenre.book.description,
          fontStyle = FontStyle.Italic,
          modifier = Modifier.fillMaxHeight().padding(end = 16.dp)
        )

        Spacer(modifier = Modifier.height(16.dp))
private val _booksState = mutableStateOf(emptyList<BookAndGenre>())
private val _genresState = mutableStateOf<List<Genre>>(emptyList())