Skip to content
Permalink
Browse files

Refactor TmdbManager

Since we no longer need to observe it's state
  • Loading branch information...
chrisbanes committed Sep 13, 2019
1 parent 995a13b commit 8be35ef2d5d6a5772018e3f840ff216286e08890
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.appinitializers

import android.app.Application
import app.tivi.domain.interactors.UpdateTmdbConfig
import javax.inject.Inject

class TmdbInitializer @Inject constructor(
private val updateTmdbConfig: UpdateTmdbConfig
) : AppInitializer {
override fun init(application: Application) {
updateTmdbConfig(Unit)
}
}
@@ -17,6 +17,7 @@
package app.tivi.inject

import app.tivi.TiviApplication
import app.tivi.common.imageloading.ImageLoadingModule
import app.tivi.data.DataModule
import app.tivi.data.DatabaseModule
import app.tivi.home.HomeBuilder
@@ -42,7 +43,8 @@ import javax.inject.Singleton
ShowDetailsBuilder::class,
TraktAuthModule::class,
TmdbModule::class,
NetworkModule::class
NetworkModule::class,
ImageLoadingModule::class
])
interface AppComponent : AndroidInjector<TiviApplication> {
@Component.Factory
@@ -22,12 +22,12 @@ import app.tivi.TiviAppNavigator
import app.tivi.TiviApplication
import app.tivi.appinitializers.AppInitializer
import app.tivi.appinitializers.ArchTaskExecutorInitializer
import app.tivi.appinitializers.CoilInitializer
import app.tivi.appinitializers.EmojiInitializer
import app.tivi.appinitializers.EpoxyInitializer
import app.tivi.appinitializers.PreferencesInitializer
import app.tivi.appinitializers.ThreeTenBpInitializer
import app.tivi.appinitializers.TimberInitializer
import app.tivi.appinitializers.TmdbInitializer
import app.tivi.settings.TiviPreferences
import app.tivi.settings.TiviPreferencesImpl
import app.tivi.util.Logger
@@ -82,5 +82,5 @@ abstract class AppModuleBinds {

@Binds
@IntoSet
abstract fun provideCoilInitializer(bind: CoilInitializer): AppInitializer
abstract fun provideTmdbInitializer(bind: TmdbInitializer): AppInitializer
}
@@ -34,7 +34,6 @@ import app.tivi.data.entities.TiviShow
import app.tivi.data.resultentities.EntryWithShow
import app.tivi.domain.PagingInteractor
import app.tivi.domain.interactors.ChangeShowFollowStatus
import app.tivi.tmdb.TmdbManager
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
@@ -50,7 +49,6 @@ abstract class EntryViewModel<LI : EntryWithShow<out Entry>, PI : PagingInteract
) : ViewModel() {
protected abstract val dispatchers: AppCoroutineDispatchers
protected abstract val pagingInteractor: PI
protected abstract val tmdbManager: TmdbManager
protected abstract val logger: Logger
protected abstract val changeShowFollowStatus: ChangeShowFollowStatus

@@ -46,8 +46,10 @@ android {

dependencies {
implementation project(":base")
implementation project(":base-android")
implementation project(":data")
implementation project(":common-ui")
implementation project(":tmdb")

implementation Libs.AndroidX.coreKtx

@@ -14,15 +14,15 @@
* limitations under the License.
*/

package app.tivi.appinitializers
package app.tivi.common.imageloading

import android.app.Application
import app.tivi.ui.imageloading.TmdbImageEntityCoilMapper
import app.tivi.appinitializers.AppInitializer
import coil.Coil
import coil.ImageLoader
import javax.inject.Inject

class CoilInitializer @Inject constructor(
class CoilAppInitializer @Inject constructor(
private val mapper: TmdbImageEntityCoilMapper
) : AppInitializer {
override fun init(application: Application) {
@@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.ui.imageloading
package app.tivi.common.imageloading

import android.widget.ImageView
import androidx.databinding.BindingAdapter
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.common.imageloading

import app.tivi.appinitializers.AppInitializer

import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoSet

@Module
abstract class ImageLoadingModule {
@Binds
@IntoSet
abstract fun provideCoilInitializer(bind: CoilAppInitializer): AppInitializer
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.ui.imageloading
package app.tivi.common.imageloading

import app.tivi.data.entities.ImageType
import app.tivi.data.entities.TmdbImageEntity

This file was deleted.

@@ -28,7 +28,7 @@ kapt {
dependencies {
api project(":base")
api project(":trakt")
api project(":tmdb")
implementation project(":tmdb")

api Libs.AndroidX.Room.common
api Libs.AndroidX.Room.ktx
@@ -45,6 +45,7 @@ dependencies {
api project(":data")
implementation project(":data-android")
implementation project(":trakt-auth")
implementation project(":tmdb")

kapt Libs.Dagger.compiler

@@ -18,10 +18,10 @@ package app.tivi.domain

import androidx.paging.PagedList
import app.tivi.base.InvokeError
import app.tivi.base.InvokeSuccess
import app.tivi.base.InvokeIdle
import app.tivi.base.InvokeStarted
import app.tivi.base.InvokeStatus
import app.tivi.base.InvokeSuccess
import app.tivi.base.InvokeTimeout
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
@@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.concurrent.TimeUnit

@@ -59,9 +60,7 @@ abstract class Interactor<in P> {
return channel.asFlow()
}

suspend fun executeSync(params: P) {
scope.launch { doWork(params) }.join()
}
suspend fun executeSync(params: P) = withContext(scope.coroutineContext) { doWork(params) }

protected abstract suspend fun doWork(params: P)

@@ -0,0 +1,37 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.domain.interactors

import app.tivi.domain.Interactor
import app.tivi.inject.ProcessLifetime
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.plus
import javax.inject.Inject

class UpdateTmdbConfig @Inject constructor(
private val tmdbManager: TmdbManager,
dispatchers: AppCoroutineDispatchers,
@ProcessLifetime val processScope: CoroutineScope
) : Interactor<Unit>() {
override val scope: CoroutineScope = processScope + dispatchers.io

override suspend fun doWork(params: Unit) {
tmdbManager.refreshConfiguration()
}
}
@@ -41,11 +41,7 @@ class TmdbManager @Inject constructor(

fun getLatestImageProvider() = imageProviderSubject.value

init {
refreshConfiguration()
}

private fun refreshConfiguration() {
fun refreshConfiguration() {
processScope.launch {
try {
val config = withContext(dispatchers.io) {
@@ -16,12 +16,11 @@

package app.tivi.home.popular

import app.tivi.data.resultentities.PopularEntryWithShow
import app.tivi.base.InvokeStatus
import app.tivi.data.resultentities.PopularEntryWithShow
import app.tivi.domain.interactors.ChangeShowFollowStatus
import app.tivi.domain.interactors.UpdatePopularShows
import app.tivi.domain.observers.ObservePagedPopularShows
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.EntryViewModel
import app.tivi.util.Logger
@@ -32,7 +31,6 @@ class PopularShowsViewModel @Inject constructor(
override val dispatchers: AppCoroutineDispatchers,
override val pagingInteractor: ObservePagedPopularShows,
private val interactor: UpdatePopularShows,
override val tmdbManager: TmdbManager,
override val logger: Logger,
override val changeShowFollowStatus: ChangeShowFollowStatus
) : EntryViewModel<PopularEntryWithShow, ObservePagedPopularShows>() {
@@ -22,7 +22,6 @@ import app.tivi.domain.interactors.UpdateRecommendedShows
import app.tivi.domain.interactors.UpdateRecommendedShows.Page.NEXT_PAGE
import app.tivi.domain.interactors.UpdateRecommendedShows.Page.REFRESH
import app.tivi.domain.observers.ObservePagedRecommendedShows
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.EntryViewModel
import app.tivi.util.Logger
@@ -32,7 +31,6 @@ class RecommendedShowsViewModel @Inject constructor(
override val dispatchers: AppCoroutineDispatchers,
override val pagingInteractor: ObservePagedRecommendedShows,
private val interactor: UpdateRecommendedShows,
override val tmdbManager: TmdbManager,
override val logger: Logger,
override val changeShowFollowStatus: ChangeShowFollowStatus
) : EntryViewModel<RecommendedEntryWithShow, ObservePagedRecommendedShows>() {
@@ -22,7 +22,6 @@ import app.tivi.domain.interactors.UpdateTrendingShows
import app.tivi.domain.interactors.UpdateTrendingShows.Page.NEXT_PAGE
import app.tivi.domain.interactors.UpdateTrendingShows.Page.REFRESH
import app.tivi.domain.observers.ObservePagedTrendingShows
import app.tivi.tmdb.TmdbManager
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.EntryViewModel
import app.tivi.util.Logger
@@ -32,7 +31,6 @@ class TrendingShowsViewModel @Inject constructor(
override val dispatchers: AppCoroutineDispatchers,
override val pagingInteractor: ObservePagedTrendingShows,
private val interactor: UpdateTrendingShows,
override val tmdbManager: TmdbManager,
override val logger: Logger,
override val changeShowFollowStatus: ChangeShowFollowStatus
) : EntryViewModel<TrendingEntryWithShow, ObservePagedTrendingShows>() {

0 comments on commit 8be35ef

Please sign in to comment.
You can’t perform that action at this time.