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
About this episode

Leave a rating/review

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

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: 17. Connect LiveData to Compose UI

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

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