Jetpack Compose

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

Part 1: Jetpack Compose Basics

6. Build Common UI Components - Part 1

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: 5. Decouple Composables Next episode: 7. Build Common UI Components - Part 2

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.

Intro

[Slide 1 - Material Design]


Demo

Let’s start off by making more of your components reusable. Open the AddBookActivity. Now let’s move the DropdownMenu from the class to a separate file in the composeUi package, to make it reusable:

@Composable
fun GenrePicker(
  genres: List<Genre>,
  selectedGenreId: String,
  onItemPicked: (Genre) -> Unit
) {
  ...
}
  DropdownMenu(expanded = isGenresPickerOpen.value,
    onDismissRequest = { isGenresPickerOpen.value = false }) {
    for (genre in genres) {
      DropdownMenuItem(onClick = {
        onItemPicked(genre)
        isGenresPickerOpen.value = false
      }) {
        Text(text = genre.name)
      }
    }
  }
GenrePicker(genres = genres, onItemPicked = {
  _addBookState.value = _addBookState.value.copy(genreId = it.id)
}, selectedGenreId = _addBookState.value.genreId ?: "")
private val _addBookState = mutableStateOf(AddBookState())
private val _genresState = mutableStateOf(emptyList<Genre>())	

@Composable
fun BooksContent() {
    val bookFilterDrawerState = rememberBottomDrawerState(initialValue = BottomDrawerValue.Closed)

  Scaffold(topBar = { BooksTopBar(bookFilterDrawerState) },
    floatingActionButton = { AddNewBook(bookFilterDrawerState) }) {
    BookFilterModalDrawer(bookFilterDrawerState = bookFilterDrawerState)
  }
}

@Composable
fun BooksTopBar(bookFilterDrawerState: BottomDrawerState) {
  TopBar(
    actions = { FilterButton(bookFilterDrawerState) })
}
@Composable
fun FilterButton(bookFilterDrawerState: BottomDrawerState) {
  val scope = rememberCoroutineScope()

  IconButton(onClick = {
    scope.launch {
      if (!bookFilterDrawerState.isClosed) {
        bookFilterDrawerState.close()
      } else {
        bookFilterDrawerState.expand()
      }
    }
  }) {
    Icon(Icons.Default.Edit, tint = Color.White, contentDescription = "Filter")
  }
}
               
@Composable
fun AddNewBook(bookFilterDrawerState: BottomDrawerState) {
  val scope = rememberCoroutineScope()

  FloatingActionButton(
    content = { Icon(Icons.Filled.Add, contentDescription = "Add Book") },
    onClick = {
      scope.launch {
        bookFilterDrawerState.close()
        showAddBook()
      }
    },
  )
}
  @ExperimentalMaterialApi
  @Composable
  fun BookFilterModalDrawer(bookFilterDrawerState: BottomDrawerState) {
    val books = _booksState.value ?: emptyList()

    BottomDrawer(
      drawerState = bookFilterDrawerState,
      drawerContent = {
        BookFilterModalDrawerContent(Modifier.align(CenterHorizontally), bookFilterDrawerState)
      },
      content = { BooksList(books) })
  }
@ExperimentalMaterialApi
@Composable
fun BookFilterModalDrawerContent(
  modifier: Modifier,
  bookFilterDrawerState: BottomDrawerState
) {
  val scope = rememberCoroutineScope()
  val genres = _genresState.value ?: emptyList()

  BookFilter(modifier, filter, genres, onFilterSelected = { newFilter ->
    scope.launch {
      bookFilterDrawerState.close()
      filter = newFilter
      loadBooks()
    }
  })
}
@Composable
fun BookFilter(
  modifier: Modifier,
  filter: Filter?,
  genres: List<Genre>,
  onFilterSelected: (Filter?) -> Unit
) {
  val currentFilter = remember {
    mutableStateOf(
      when (filter) {
        null -> 0
        is ByGenre -> 1
        is ByRating -> 2
      }
    )
  } // 0 - no filter, 1 - ByGenre, 2 - By Rating

  val currentGenreFilter = remember { mutableStateOf<Genre?>(null) }
  val currentRatingFilter = remember { mutableStateOf(0) }
Column(
  modifier = modifier,
  horizontalAlignment = Alignment.CenterHorizontally
) {

  Column {
  }
}
Row {
  RadioButton(
    selected = currentFilter.value == 0,
    onClick = { currentFilter.value = 0 },
    modifier = Modifier.padding(8.dp)
  )
  Text(
    text = stringResource(id = R.string.no_filter),
    modifier = Modifier.align(Alignment.CenterVertically)
  )
}
Row {
  RadioButton(
    selected = currentFilter.value == 1,
    onClick = { currentFilter.value = 1 },
    modifier = Modifier.padding(8.dp)
  )

  Text(
    text = stringResource(id = R.string.filter_by_genre),
    modifier = Modifier.align(Alignment.CenterVertically)
  )
}
Row {
  RadioButton(
    selected = currentFilter.value == 2,
    onClick = { currentFilter.value = 2 },
    modifier = Modifier.padding(8.dp)
  )

  Text(
    text = stringResource(id = R.string.filter_by_rating),
    modifier = Modifier.align(Alignment.CenterVertically)
  )
}
val currentlySelectedGenre = currentGenreFilter.value

if (currentFilter.value == 2) {
  RatingBar(
    range = 1..5,
    currentRating = currentRatingFilter.value,
    isLargeRating = true,
    onRatingChanged = { newRating -> currentRatingFilter.value = newRating })
}
if (currentFilter.value == 1) {
  GenrePicker(
    genres = genres,
    selectedGenreId = currentlySelectedGenre?.id ?: "",
    onItemPicked = {
      currentGenreFilter.value = it
    }
  )
}
ActionButton(
  modifier = Modifier.fillMaxWidth(),
  text = stringResource(id = R.string.confirm_filter),
  onClick = {
    val newFilter = when (currentFilter.value) {
      0 -> null
        ...
    }
  }
)
      1 -> ByGenre(currentGenreFilter.value?.id ?: "")
      2 -> ByRating(currentRatingFilter.value)
      else -> throw IllegalArgumentException("Unknown filter!")
    }

    onFilterSelected(newFilter)