Jetpack Compose

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

Part 1: Jetpack Compose Basics

07. Build Common UI Components - Part 2

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: 06. Build Common UI Components - Part 1 Next episode: 08. Build Common UI Components - Part 3

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: 07. Build Common UI Components - Part 2

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

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())