Skip to content

Commit

Permalink
Split remaining Interactors apart
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed May 29, 2019
1 parent c3d5530 commit 7e1fa6d
Show file tree
Hide file tree
Showing 43 changed files with 650 additions and 376 deletions.
12 changes: 7 additions & 5 deletions app/src/main/java/app/tivi/home/HomeActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package app.tivi.home

import androidx.lifecycle.viewModelScope
import app.tivi.home.main.HomeNavigationViewState
import app.tivi.interactors.ObserveUserDetails
import app.tivi.interactors.UpdateUserDetails
import app.tivi.interactors.launchInteractor
import app.tivi.trakt.TraktAuthState
Expand All @@ -34,19 +35,20 @@ import net.openid.appauth.AuthorizationService
class HomeActivityViewModel @AssistedInject constructor(
@Assisted initialState: HomeNavigationViewState,
private val traktManager: TraktManager,
private val updateUserDetails: UpdateUserDetails
private val updateUserDetails: UpdateUserDetails,
observeUserDetails: ObserveUserDetails
) : TiviMvRxViewModel<HomeNavigationViewState>(initialState) {
init {
updateUserDetails.setParams(UpdateUserDetails.Params("me"))
updateUserDetails.observe()
.toObservable()
observeUserDetails.observe()
.execute { copy(user = it()) }
observeUserDetails(ObserveUserDetails.Params("me"))

traktManager.state
.distinctUntilChanged()
.doOnNext {
if (it == TraktAuthState.LOGGED_IN) {
viewModelScope.launchInteractor(updateUserDetails, UpdateUserDetails.ExecuteParams(false))
viewModelScope.launchInteractor(updateUserDetails,
UpdateUserDetails.Params("me", false))
}
}.execute {
copy(authState = it() ?: TraktAuthState.LOGGED_OUT)
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/app/tivi/home/discover/DiscoverViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package app.tivi.home.discover

import androidx.lifecycle.viewModelScope
import app.tivi.interactors.ObservePopularShows
import app.tivi.interactors.ObserveTrendingShows
import app.tivi.interactors.UpdatePopularShows
import app.tivi.interactors.UpdateTrendingShows
import app.tivi.interactors.launchInteractor
Expand All @@ -34,7 +36,9 @@ class DiscoverViewModel @AssistedInject constructor(
@Assisted initialState: DiscoverViewState,
schedulers: AppRxSchedulers,
private val updatePopularShows: UpdatePopularShows,
observePopularShows: ObservePopularShows,
private val updateTrendingShows: UpdateTrendingShows,
observeTrendingShows: ObserveTrendingShows,
tmdbManager: TmdbManager
) : TiviMvRxViewModel<DiscoverViewState>(initialState) {
private val loadingState = RxLoadingCounter()
Expand All @@ -46,15 +50,15 @@ class DiscoverViewModel @AssistedInject constructor(
loadingState.observable
.execute { copy(isLoading = it() ?: false) }

updateTrendingShows.observe()
.toObservable()
observeTrendingShows.observe()
.subscribeOn(schedulers.io)
.execute { copy(trendingItems = it() ?: emptyList()) }
observeTrendingShows(Unit)

updatePopularShows.observe()
.toObservable()
observePopularShows.observe()
.subscribeOn(schedulers.io)
.execute { copy(popularItems = it() ?: emptyList()) }
observePopularShows(Unit)

refresh()
}
Expand All @@ -63,13 +67,13 @@ class DiscoverViewModel @AssistedInject constructor(
loadingState.addLoader()
viewModelScope.launchInteractor(
updatePopularShows,
UpdatePopularShows.ExecuteParams(UpdatePopularShows.Page.REFRESH)
UpdatePopularShows.Params(UpdatePopularShows.Page.REFRESH)
).invokeOnCompletion { loadingState.removeLoader() }

loadingState.addLoader()
viewModelScope.launchInteractor(
updateTrendingShows,
UpdateTrendingShows.ExecuteParams(UpdateTrendingShows.Page.REFRESH)
UpdateTrendingShows.Params(UpdateTrendingShows.Page.REFRESH)
).invokeOnCompletion { loadingState.removeLoader() }
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/app/tivi/home/followed/FollowedViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.paging.PagedList
import app.tivi.data.entities.SortOption
import app.tivi.data.resultentities.FollowedShowEntryWithShow
import app.tivi.interactors.ObserveFollowedShows
import app.tivi.interactors.SyncFollowedShows
import app.tivi.interactors.UpdateFollowedShows
import app.tivi.interactors.launchInteractor
import app.tivi.tmdb.TmdbManager
import app.tivi.trakt.TraktAuthState
Expand All @@ -41,8 +41,8 @@ import java.util.concurrent.TimeUnit

class FollowedViewModel @AssistedInject constructor(
@Assisted initialState: FollowedViewState,
private val schedulers: AppRxSchedulers,
private val syncFollowedShows: SyncFollowedShows,
schedulers: AppRxSchedulers,
private val updateFollowedShows: UpdateFollowedShows,
private val observeFollowedShows: ObserveFollowedShows,
private val traktManager: TraktManager,
tmdbManager: TmdbManager,
Expand Down Expand Up @@ -122,7 +122,7 @@ class FollowedViewModel @AssistedInject constructor(

private fun refreshFollowed() {
loadingState.addLoader()
viewModelScope.launchInteractor(syncFollowedShows, SyncFollowedShows.ExecuteParams(false))
viewModelScope.launchInteractor(updateFollowedShows, UpdateFollowedShows.ExecuteParams(false))
.invokeOnCompletion { loadingState.removeLoader() }
}

Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/app/tivi/home/popular/PopularShowsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@
package app.tivi.home.popular

import app.tivi.data.resultentities.PopularEntryWithShow
import app.tivi.interactors.ObservePagedPopularShows
import app.tivi.interactors.UpdatePopularShows
import app.tivi.interactors.UpdatePopularShows.Page.NEXT_PAGE
import app.tivi.interactors.UpdatePopularShows.Page.REFRESH
import app.tivi.interactors.execute
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.AppRxSchedulers
import app.tivi.util.EntryViewModel
import app.tivi.util.Logger
import javax.inject.Inject

class PopularShowsViewModel @Inject constructor(
schedulers: AppRxSchedulers,
dispatchers: AppCoroutineDispatchers,
private val interactor: UpdatePopularShows,
pagingInteractor: ObservePagedPopularShows,
tmdbManager: TmdbManager,
logger: Logger
) : EntryViewModel<PopularEntryWithShow>(
schedulers,
) : EntryViewModel<PopularEntryWithShow, ObservePagedPopularShows>(
dispatchers,
interactor.dataSourceFactory(),
pagingInteractor,
tmdbManager,
logger
) {
override suspend fun callLoadMore() = interactor.execute(UpdatePopularShows.ExecuteParams(NEXT_PAGE))
init {
pagingInteractor(ObservePagedPopularShows.Params(pageListConfig, boundaryCallback))
}

override suspend fun callRefresh() = interactor.execute(UpdatePopularShows.ExecuteParams(REFRESH))
override suspend fun callLoadMore() = interactor.execute(UpdatePopularShows.Params(NEXT_PAGE))

override suspend fun callRefresh() = interactor.execute(UpdatePopularShows.Params(REFRESH))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@
package app.tivi.home.trending

import app.tivi.data.resultentities.TrendingEntryWithShow
import app.tivi.interactors.ObservePagedTrendingShows
import app.tivi.interactors.UpdateTrendingShows
import app.tivi.interactors.UpdateTrendingShows.Page.NEXT_PAGE
import app.tivi.interactors.UpdateTrendingShows.Page.REFRESH
import app.tivi.interactors.execute
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.AppRxSchedulers
import app.tivi.util.EntryViewModel
import app.tivi.util.Logger
import javax.inject.Inject

class TrendingShowsViewModel @Inject constructor(
schedulers: AppRxSchedulers,
dispatchers: AppCoroutineDispatchers,
private val interactor: UpdateTrendingShows,
pagingInteractor: ObservePagedTrendingShows,
tmdbManager: TmdbManager,
logger: Logger
) : EntryViewModel<TrendingEntryWithShow>(
schedulers,
) : EntryViewModel<TrendingEntryWithShow, ObservePagedTrendingShows>(
dispatchers,
interactor.dataSourceFactory(),
pagingInteractor,
tmdbManager,
logger
) {
override suspend fun callLoadMore() = interactor.execute(UpdateTrendingShows.ExecuteParams(NEXT_PAGE))
override suspend fun callLoadMore() = interactor.execute(UpdateTrendingShows.Params(NEXT_PAGE))

override suspend fun callRefresh() = interactor.execute(UpdateTrendingShows.ExecuteParams(REFRESH))
override suspend fun callRefresh() = interactor.execute(UpdateTrendingShows.Params(REFRESH))
}
32 changes: 13 additions & 19 deletions app/src/main/java/app/tivi/home/watched/WatchedViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package app.tivi.home.watched

import androidx.lifecycle.viewModelScope
import androidx.paging.DataSource
import androidx.paging.PagedList
import androidx.paging.RxPagedListBuilder
import app.tivi.data.resultentities.EntryWithShow
import app.tivi.data.resultentities.WatchedShowEntryWithShow
import app.tivi.interactors.ObserveWatchedShows
import app.tivi.interactors.UpdateWatchedShows
import app.tivi.interactors.launchInteractor
import app.tivi.tmdb.TmdbManager
Expand All @@ -35,16 +34,16 @@ import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.BehaviorSubject
import java.util.concurrent.TimeUnit

class WatchedViewModel @AssistedInject constructor(
@Assisted initialState: WatchedViewState,
private val schedulers: AppRxSchedulers,
schedulers: AppRxSchedulers,
private val updateWatchedShows: UpdateWatchedShows,
observeWatchedShows: ObserveWatchedShows,
private val traktManager: TraktManager,
tmdbManager: TmdbManager,
private val logger: Logger
Expand All @@ -55,6 +54,12 @@ class WatchedViewModel @AssistedInject constructor(

private val filterObservable = BehaviorSubject.create<CharSequence>()

private val boundaryCallback = object : PagedList.BoundaryCallback<WatchedShowEntryWithShow>() {
override fun onZeroItemsLoaded() = setState { copy(isEmpty = true) }
override fun onItemAtEndLoaded(itemAtEnd: WatchedShowEntryWithShow) = setState { copy(isEmpty = false) }
override fun onItemAtFrontLoaded(itemAtFront: WatchedShowEntryWithShow) = setState { copy(isEmpty = false) }
}

init {
loadingState.observable.execute {
copy(isLoading = it() ?: false)
Expand All @@ -64,8 +69,9 @@ class WatchedViewModel @AssistedInject constructor(
.delay(50, TimeUnit.MILLISECONDS, schedulers.io)
.execute { copy(tmdbImageUrlProvider = it() ?: tmdbImageUrlProvider) }

dataSourceToObservable(updateWatchedShows.dataSourceFactory())
observeWatchedShows.observe()
.execute { copy(watchedShows = it()) }
observeWatchedShows(ObserveWatchedShows.Params(PAGING_CONFIG, boundaryCallback))

filterObservable.distinctUntilChanged()
.debounce(500, TimeUnit.MILLISECONDS, schedulers.main)
Expand All @@ -74,18 +80,6 @@ class WatchedViewModel @AssistedInject constructor(
refresh()
}

private fun <T : EntryWithShow<*>> dataSourceToObservable(f: DataSource.Factory<Int, T>): Observable<PagedList<T>> {
return RxPagedListBuilder(f, PAGING_CONFIG)
.setBoundaryCallback(object : PagedList.BoundaryCallback<T>() {
override fun onZeroItemsLoaded() = setState { copy(isEmpty = true) }
override fun onItemAtEndLoaded(itemAtEnd: T) = setState { copy(isEmpty = false) }
override fun onItemAtFrontLoaded(itemAtFront: T) = setState { copy(isEmpty = false) }
})
.setFetchScheduler(schedulers.io)
.setNotifyScheduler(schedulers.main)
.buildObservable()
}

fun refresh() {
refreshDisposable?.let {
it.dispose()
Expand All @@ -106,7 +100,7 @@ class WatchedViewModel @AssistedInject constructor(

private fun refreshWatched() {
loadingState.addLoader()
viewModelScope.launchInteractor(updateWatchedShows, UpdateWatchedShows.ExecuteParams(false))
viewModelScope.launchInteractor(updateWatchedShows, UpdateWatchedShows.Params(false))
.invokeOnCompletion { loadingState.removeLoader() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import app.tivi.interactors.ChangeSeasonWatchedStatus.Action
import app.tivi.interactors.ChangeSeasonWatchedStatus.Params
import app.tivi.interactors.ChangeShowFollowStatus
import app.tivi.interactors.ChangeShowFollowStatus.Action.TOGGLE
import app.tivi.interactors.ObserveFollowedShowSeasonData
import app.tivi.interactors.ObserveRelatedShows
import app.tivi.interactors.ObserveShowDetails
import app.tivi.interactors.ObserveShowFollowStatus
import app.tivi.interactors.UpdateFollowedShowSeasonData
import app.tivi.interactors.UpdateRelatedShows
import app.tivi.interactors.UpdateShowDetails
Expand All @@ -49,15 +53,18 @@ class ShowDetailsFragmentViewModel @AssistedInject constructor(
@Assisted initialState: ShowDetailsViewState,
schedulers: AppRxSchedulers,
private val updateShowDetails: UpdateShowDetails,
observeShowDetails: ObserveShowDetails,
private val updateRelatedShows: UpdateRelatedShows,
observeRelatedShows: ObserveRelatedShows,
private val updateShowSeasons: UpdateFollowedShowSeasonData,
observeShowSeasons: ObserveFollowedShowSeasonData,
private val changeSeasonWatchedStatus: ChangeSeasonWatchedStatus,
observeShowFollowStatus: ObserveShowFollowStatus,
tmdbManager: TmdbManager,
private val changeShowFollowStatus: ChangeShowFollowStatus
) : TiviMvRxViewModel<ShowDetailsViewState>(initialState) {
init {
changeShowFollowStatus.observe()
.toObservable()
observeShowFollowStatus.observe()
.subscribeOn(schedulers.io)
.execute {
when (it) {
Expand All @@ -66,13 +73,11 @@ class ShowDetailsFragmentViewModel @AssistedInject constructor(
}
}

updateShowDetails.observe()
.toObservable()
observeShowDetails.observe()
.subscribeOn(schedulers.io)
.execute { copy(show = it) }

updateRelatedShows.observe()
.toObservable()
observeRelatedShows.observe()
.subscribeOn(schedulers.io)
.execute { copy(relatedShows = it) }

Expand All @@ -81,36 +86,30 @@ class ShowDetailsFragmentViewModel @AssistedInject constructor(
.subscribeOn(schedulers.io)
.execute { copy(tmdbImageUrlProvider = it) }

updateShowSeasons.observe()
.toObservable()
observeShowSeasons.observe()
.subscribeOn(schedulers.io)
.execute { copy(seasons = it) }

withState {
updateShowDetails.setParams(UpdateShowDetails.Params(it.showId))
updateRelatedShows.setParams(UpdateRelatedShows.Params(it.showId))
updateShowSeasons.setParams(UpdateFollowedShowSeasonData.Params(it.showId))
changeShowFollowStatus.setParams(ChangeShowFollowStatus.Params(it.showId))

refresh()
observeShowFollowStatus(ObserveShowFollowStatus.Params(it.showId))
observeShowDetails(ObserveShowDetails.Params(it.showId))
observeRelatedShows(ObserveRelatedShows.Params(it.showId))
observeShowSeasons(ObserveFollowedShowSeasonData.Params(it.showId))
}
}

private fun refresh() {
viewModelScope.launchInteractor(updateShowDetails, UpdateShowDetails.ExecuteParams(true))
viewModelScope.launchInteractor(updateRelatedShows, UpdateRelatedShows.ExecuteParams(true))
viewModelScope.launchInteractor(updateShowSeasons, UpdateFollowedShowSeasonData.ExecuteParams(true))
refresh()
}

override fun onCleared() {
super.onCleared()
updateShowDetails.clear()
private fun refresh() = withState {
viewModelScope.launchInteractor(updateShowDetails, UpdateShowDetails.Params(it.showId, true))
viewModelScope.launchInteractor(updateRelatedShows, UpdateRelatedShows.Params(it.showId, true))
viewModelScope.launchInteractor(updateShowSeasons, UpdateFollowedShowSeasonData.Params(it.showId, true))
}

fun onToggleMyShowsButtonClicked() {
fun onToggleMyShowsButtonClicked() = withState {
viewModelScope.launch {
changeShowFollowStatus.execute(ChangeShowFollowStatus.ExecuteParams(TOGGLE))
updateShowSeasons.execute(UpdateFollowedShowSeasonData.ExecuteParams(false))
changeShowFollowStatus.execute(ChangeShowFollowStatus.Params(it.showId, TOGGLE))
updateShowSeasons.execute(UpdateFollowedShowSeasonData.Params(it.showId, false))
}
}

Expand All @@ -126,8 +125,7 @@ class ShowDetailsFragmentViewModel @AssistedInject constructor(
) = showDetailsNavigator.showEpisodeDetails(episode)

fun onMarkSeasonWatched(season: Season, onlyAired: Boolean, date: ActionDate) {
viewModelScope.launchInteractor(changeSeasonWatchedStatus,
Params(season.id, ChangeSeasonWatchedStatus.Action.WATCHED, onlyAired, date))
viewModelScope.launchInteractor(changeSeasonWatchedStatus, Params(season.id, Action.WATCHED, onlyAired, date))
}

fun onMarkSeasonUnwatched(season: Season) {
Expand Down
Loading

0 comments on commit 7e1fa6d

Please sign in to comment.