Skip to content

Commit

Permalink
Introduce Generic BaseMoleculeViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3y-the-programmer committed Mar 13, 2024
1 parent 11296a7 commit dd558fc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mr3y.podcaster.ui.presenter

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow

abstract class BaseMoleculeViewModel<Event : Any> : ViewModel() {

protected val events = MutableSharedFlow<Event>(extraBufferCapacity = 20)

protected val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
Expand All @@ -25,9 +22,7 @@ import javax.inject.Inject
@HiltViewModel
class DownloadsViewModel @Inject constructor(
private val podcastsRepository: PodcastsRepository,
) : ViewModel() {

private val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)
) : BaseMoleculeViewModel<Nothing>() {

val state = moleculeScope.launchMolecule(mode = RecompositionMode.ContextClock) {
DownloadsPresenter(repository = podcastsRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,23 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import com.kiwi.navigationcompose.typed.decodeArguments
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.core.model.Episode
import com.mr3y.podcaster.ui.navigation.Destinations
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import com.mr3y.podcaster.ui.presenter.RefreshResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import javax.inject.Inject

@HiltViewModel
class EpisodeDetailsViewModel @Inject constructor(
private val podcastsRepository: PodcastsRepository,
private val savedStateHandle: SavedStateHandle,
) : ViewModel() {

private val events = MutableSharedFlow<EpisodeDetailsUIEvent>(extraBufferCapacity = 20)

private val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)
) : BaseMoleculeViewModel<EpisodeDetailsUIEvent>() {

private val navArguments = savedStateHandle.decodeArguments<Destinations.EpisodeDetails>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import com.github.michaelbull.result.mapBoth
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import javax.inject.Inject

@HiltViewModel
class ExploreViewModel @Inject constructor(
private val podcastsRepository: PodcastsRepository,
) : ViewModel() {

private val events = MutableSharedFlow<ExploreUIEvent>(extraBufferCapacity = 20)

private val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)
) : BaseMoleculeViewModel<ExploreUIEvent>() {

val state = moleculeScope.launchMolecule(mode = RecompositionMode.ContextClock) {
ExplorePresenter(repository = podcastsRepository, events = events)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import com.kiwi.navigationcompose.typed.decodeArguments
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.core.model.Episode
import com.mr3y.podcaster.core.model.Podcast
import com.mr3y.podcaster.ui.navigation.Destinations
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import com.mr3y.podcaster.ui.presenter.RefreshResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -32,11 +28,7 @@ import javax.inject.Inject
class PodcastDetailsViewModel @Inject constructor(
private val podcastsRepository: PodcastsRepository,
private val savedStateHandle: SavedStateHandle,
) : ViewModel() {

private val events = MutableSharedFlow<PodcastDetailsUIEvent>(extraBufferCapacity = 20)

private val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)
) : BaseMoleculeViewModel<PodcastDetailsUIEvent>() {

private val navArguments = savedStateHandle.decodeArguments<Destinations.PodcastDetails>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import com.mr3y.podcaster.ui.presenter.RefreshResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
Expand All @@ -30,11 +26,7 @@ import javax.inject.Inject
@HiltViewModel
class SubscriptionsViewModel @Inject constructor(
private val podcastsRepository: PodcastsRepository,
) : ViewModel() {

private val events = MutableSharedFlow<SubscriptionsUIEvent>(extraBufferCapacity = 20)

private val moleculeScope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)
) : BaseMoleculeViewModel<SubscriptionsUIEvent>() {

val state = moleculeScope.launchMolecule(mode = RecompositionMode.ContextClock) {
SubscriptionsPresenter(repository = podcastsRepository, events = events)
Expand Down

0 comments on commit dd558fc

Please sign in to comment.