Jetpack Compose

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

Part 3: Manage State with Compose

17. Connect LiveData to Compose UI

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: 16. Move Operations to ViewModels Next episode: 18. Apply the CompositionLocal Pattern to UI

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

You’ll wrap up the reactive and declarative approach to your UI and state by changing all the other screens’ logic to use just the state from the ViewModel.

// remove state

//AddBookFormContent()
val genres by addBookViewModel.genresState.observeAsState(emptyList())
val addBookState by addBookViewModel.addBookState.observeAsState(AddBookState())
value = addBookState.name
isInputValid = addBookState.name.isNotEmpty()

value = addBookState.description
isInputValid = addBookState.description.isNotEmpty()

onItemPicked = { genre -> addBookViewModel.genrePicked(genre) })

isEnabled = addBookState.description.isNotEmpty() && addBookState.name.isNotEmpty() && addBookState.genreId.isNotEmpty()
// remove state

//AddBookReviewForm

val books by addBookReviewViewModel.booksState.observeAsState(emptyList())
val reviewState by addBookReviewViewModel.bookReviewState.observeAsState(AddBookReviewState())
items = books

preselectedItem = reviewState.bookAndGenre

value = reviewState.bookImageUrl
isInputValid = reviewState.bookImageUrl.isNotEmpty()

currentRating = reviewState.rating
onRatingChanged = { newRating -> addBookReviewViewModel.onRatingSelected(newRating) })

value = reviewState.notes
isInputValid = reviewState.notes.isNotEmpty()

isEnabled = reviewState.bookImageUrl.isNotEmpty() &&
            reviewState.notes.isNotEmpty() &&
            reviewState.bookAndGenre != EMPTY_BOOK_AND_GENRE