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

Leave a rating/review

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

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: 16. Move Operations to ViewModels

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

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