Jetpack Compose

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

Part 3: Manage State with Compose

16. Move Operations to ViewModels

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: 15. Use the MVVM Pattern Next episode: 17. Connect LiveData to Compose 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

Now that you’ve added the ViewModel, it’s time to move all of the operations to it. Most of the VMs are prebuilt for you again, so you don’t have to write a lot of boilerplate code. But there’s one you’ll do for practice.

  fun addNewEntry(entry: String) {
    val data = _bookReviewDetailsState.value?.review ?: return

    val updatedReview = data.copy(
      entries = data.entries + ReadingEntry(comment = entry),
      lastUpdatedDate = Date()
    )

    updateReview(updatedReview)
    onDialogDismiss()
  }
  private fun updateReview(updatedReview: Review) {
    viewModelScope.launch {
      repository.updateReview(updatedReview)

      setReview(repository.getReviewById(updatedReview.id))
    }
  }

  fun onDialogDismiss() {
    _deleteEntryState.value = null
    _isShowingAddEntryState.value = false
  }

  fun removeReadingEntry(readingEntry: ReadingEntry) {
    val data = _bookReviewDetailsState.value?.review ?: return

    val updatedReview = data.copy(
      entries = data.entries - readingEntry,
      lastUpdatedDate = Date()
    )

    updateReview(updatedReview)
    onDialogDismiss()
  }

  
  fun onItemLongTapped(readingEntry: ReadingEntry) {
    _deleteEntryState.value = readingEntry
  }

  fun onAddEntryTapped() {
    _isShowingAddEntryState.value = true
  }
// onCreate
bookReviewDetailsViewModel.setReview(data)

// FAB
onClick = { bookReviewDetailsViewModel.onAddEntryTapped() })

// Reading Entries
ReadingEntries(bookReview.review.entries) {
  bookReviewDetailsViewModel.onItemLongTapped(it) // long item click
}
bookReviewDetailsViewModel.addNewEntry(it)
bookReviewDetailsViewModel.onDialogDismiss()

onDeleteItem = 
  {
    bookReviewDetailsViewModel.removeReadingEntry(it)
  },
onDismiss = { bookReviewDetailsViewModel.onDialogDismiss() }
val animationState by bookReviewDetailsViewModel.screenAnimationState.observeAsState(Initial)
val state = animateBookReviewDetails(animationState)

LaunchedEffect(Unit, block = {
  bookReviewDetailsViewModel.onFirstLoad()
})
fun onFirstLoad() {
  _screenAnimationState.value = Loaded
}
val reviewState by bookReviewDetailsViewModel.bookReviewDetailsState.observeAsState()
val bookName =
  reviewState?.book?.name ?: stringResource(id = R.string.book_review_details_title)
val bookReview by bookReviewDetailsViewModel.bookReviewDetailsState.observeAsState(
  EMPTY_BOOK_REVIEW
)

val genre by bookReviewDetailsViewModel.genreState.observeAsState(EMPTY_GENRE)
val deleteEntryState by bookReviewDetailsViewModel.deleteEntryState.observeAsState()
val isShowingAddEntry by bookReviewDetailsViewModel.isShowingAddEntryState.observeAsState(false)

val entryToDelete = deleteEntryState