Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Migrate to moviebaseapp/tmdb-api #1135

Merged
merged 13 commits into from
Mar 8, 2023
1 change: 1 addition & 0 deletions api/tmdb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
api("org.threeten:threetenbp:${libs.versions.threetenbp.get()}:no-tzdb")

implementation(libs.okhttp.okhttp)
implementation("io.ktor:ktor-client-okhttp:2.2.4")

implementation(libs.kotlininject.runtime)
ksp(libs.kotlininject.compiler)
Expand Down
10 changes: 2 additions & 8 deletions api/tmdb/src/main/java/app/tivi/tmdb/TmdbComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package app.tivi.tmdb

import app.moviebase.tmdb.Tmdb3
import app.tivi.inject.ApplicationScope
import com.uwetrottmann.tmdb2.Tmdb
import me.tatarka.inject.annotations.Provides
import okhttp3.OkHttpClient

interface TmdbComponent {
@ApplicationScope
Expand All @@ -31,11 +30,6 @@ interface TmdbComponent {
@ApplicationScope
@Provides
fun provideTmdb(
client: OkHttpClient,
tmdbOAuthInfo: TmdbOAuthInfo,
): Tmdb = object : Tmdb(tmdbOAuthInfo.apiKey) {
override fun okHttpClient(): OkHttpClient = client.newBuilder()
.apply { setOkHttpClientDefaults(this) }
.build()
}
): Tmdb3 = Tmdb3(tmdbApiKey = tmdbOAuthInfo.apiKey)
}
47 changes: 23 additions & 24 deletions api/tmdb/src/main/java/app/tivi/tmdb/TmdbManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,46 @@
* limitations under the License.
*/

@file:Suppress("unused")

package app.tivi.tmdb

import app.moviebase.tmdb.Tmdb3
import app.tivi.inject.ApplicationScope
import app.tivi.util.AppCoroutineDispatchers
import com.uwetrottmann.tmdb2.Tmdb
import com.uwetrottmann.tmdb2.entities.Configuration
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.withContext
import me.tatarka.inject.annotations.Inject
import retrofit2.awaitResponse

@ApplicationScope
@Inject
class TmdbManager(
private val dispatchers: AppCoroutineDispatchers,
private val tmdbClient: Tmdb,
private val tmdbClient: Tmdb3,
) {
private val imageProvider = MutableStateFlow(TmdbImageUrlProvider())

fun getLatestImageProvider() = imageProvider.value

suspend fun refreshConfiguration() {
try {
val response = withContext(dispatchers.io) {
tmdbClient.configurationService().configuration().awaitResponse()
}
onConfigurationLoaded(response.body()!!)
} catch (t: Throwable) {
// TODO
}
// try {
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved
// val response = withContext(dispatchers.io) {
// tmdbClient.configurationService().configuration().awaitResponse()
// }
// onConfigurationLoaded(response.body()!!)
// } catch (t: Throwable) {
// // TODO
// }
}

private fun onConfigurationLoaded(configuration: Configuration) {
configuration.images?.also { images ->
val newProvider = TmdbImageUrlProvider(
images.secure_base_url!!,
images.poster_sizes ?: emptyList(),
images.backdrop_sizes ?: emptyList(),
images.logo_sizes ?: emptyList(),
)
imageProvider.value = newProvider
}
}
// private fun onConfigurationLoaded(configuration: Configuration) {
// configuration.images?.also { images ->
// val newProvider = TmdbImageUrlProvider(
// images.secure_base_url!!,
// images.poster_sizes ?: emptyList(),
// images.backdrop_sizes ?: emptyList(),
// images.logo_sizes ?: emptyList(),
// )
// imageProvider.value = newProvider
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@

package app.tivi.data.episodes

import app.moviebase.tmdb.Tmdb3
import app.tivi.data.mappers.ShowIdToTmdbIdMapper
import app.tivi.data.mappers.TmdbEpisodeToEpisode
import app.tivi.data.mappers.TmdbEpisodeDetailToEpisode
import app.tivi.data.models.Episode
import app.tivi.data.util.bodyOrThrow
import app.tivi.data.util.withRetry
import com.uwetrottmann.tmdb2.Tmdb
import me.tatarka.inject.annotations.Inject
import retrofit2.awaitResponse

@Inject
class TmdbEpisodeDataSourceImpl(
private val tmdbIdMapper: ShowIdToTmdbIdMapper,
private val tmdb: Tmdb,
private val episodeMapper: TmdbEpisodeToEpisode,
private val tmdb: Tmdb3,
private val episodeMapper: TmdbEpisodeDetailToEpisode,
) : EpisodeDataSource {
override suspend fun getEpisode(
showId: Long,
seasonNumber: Int,
episodeNumber: Int,
): Episode = withRetry {
tmdb.tvEpisodesService()
.episode(tmdbIdMapper.map(showId), seasonNumber, episodeNumber, null)
.awaitResponse()
.let { episodeMapper.map(it.bodyOrThrow()) }
): Episode {
return tmdb.showEpisodes
.getDetails(
showId = tmdbIdMapper.map(showId),
seasonNumber = seasonNumber,
episodeNumber = episodeNumber,
)
.let { episodeMapper.map(it) }
}
}
9 changes: 0 additions & 9 deletions data/legacy/src/main/java/app/tivi/data/mappers/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

package app.tivi.data.mappers

import com.uwetrottmann.tmdb2.entities.BaseTvShow
import com.uwetrottmann.tmdb2.entities.TvShowResultsPage

suspend inline fun <F, T> Mapper<F, T>.map(collection: Collection<F>) = collection.map { map(it) }

inline fun <F, T1, T2> pairMapperOf(
Expand All @@ -40,9 +37,3 @@ inline fun <F, T1, T2> pairMapperOf(
firstMapper.map(value) to secondMapper.map(index, value)
}
}

inline fun <T> unwrapTmdbShowResults(
crossinline f: suspend (List<BaseTvShow>) -> List<T>,
): suspend (TvShowResultsPage) -> List<T> = {
f(it.results ?: emptyList())
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package app.tivi.data.mappers

import app.moviebase.tmdb.model.TmdbEpisodeDetail
import app.tivi.data.models.Episode
import com.uwetrottmann.tmdb2.entities.TvEpisode
import me.tatarka.inject.annotations.Inject

@Inject
class TmdbEpisodeToEpisode : Mapper<TvEpisode, Episode> {
override suspend fun map(from: TvEpisode) = Episode(
class TmdbEpisodeDetailToEpisode : Mapper<TmdbEpisodeDetail, Episode> {
override suspend fun map(from: TmdbEpisodeDetail): Episode = Episode(
seasonId = 0,
tmdbId = from.id,
title = from.name,
number = from.episode_number,
number = from.episodeNumber,
summary = from.overview,
tmdbBackdropPath = from.still_path,
tmdbBackdropPath = from.stillPath,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,57 @@

package app.tivi.data.mappers

import app.moviebase.tmdb.model.TmdbShowDetail
import app.tivi.data.models.ImageType
import app.tivi.data.models.ShowTmdbImage
import com.uwetrottmann.tmdb2.entities.Image
import com.uwetrottmann.tmdb2.entities.TvShow
import me.tatarka.inject.annotations.Inject

@Inject
class TmdbImagesToShowImages() : Mapper<TvShow, List<ShowTmdbImage>> {
override suspend fun map(from: TvShow): List<ShowTmdbImage> {
class TmdbShowDetailToShowImages : Mapper<TmdbShowDetail, List<ShowTmdbImage>> {
override suspend fun map(from: TmdbShowDetail): List<ShowTmdbImage> {
val results = ArrayList<ShowTmdbImage>()
from.images?.posters?.mapTo(results) { image ->
from.mapImage(image, ImageType.POSTER)
}
from.images?.backdrops?.mapTo(results) { image ->
from.mapImage(image, ImageType.BACKDROP)
}
// FIXME
// from.images?.posters?.mapTo(results) { image ->
// from.mapImage(image, ImageType.POSTER)
// }
// from.images?.backdrops?.mapTo(results) { image ->
// from.mapImage(image, ImageType.BACKDROP)
// }

if (results.isEmpty()) {
// If we have no images, we haven't been passed a result which has images. We'll
// synthesize some from the TvShow properties
from.poster_path?.also { path ->
from.posterPath?.let { path ->
results += ShowTmdbImage(
showId = 0,
path = path,
type = ImageType.POSTER,
isPrimary = true,
type = ImageType.POSTER,
)
}
from.backdrop_path?.also { path ->
from.backdropPath?.let { path ->
results += ShowTmdbImage(
showId = 0,
path = path,
type = ImageType.BACKDROP,
isPrimary = true,
type = ImageType.BACKDROP,
)
}
}

return results
}

private fun TvShow.mapImage(image: Image, type: ImageType) = ShowTmdbImage(
showId = 0,
path = image.file_path!!,
type = type,
language = image.iso_639_1,
rating = image.vote_average?.toFloat() ?: 0f,
isPrimary = when (type) {
ImageType.BACKDROP -> image.file_path == backdrop_path
ImageType.POSTER -> image.file_path == poster_path
else -> false
},
)
// private fun TvShow.mapImage(image: Image, type: ImageType) = ShowTmdbImage(
// showId = 0,
// path = image.file_path!!,
// type = type,
// language = image.iso_639_1,
// rating = image.vote_average?.toFloat() ?: 0f,
// isPrimary = when (type) {
// ImageType.BACKDROP -> image.file_path == backdrop_path
// ImageType.POSTER -> image.file_path == poster_path
// else -> false
// },
// )
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package app.tivi.data.mappers

import app.moviebase.tmdb.model.TmdbShowDetail
import app.tivi.data.models.TiviShow
import com.uwetrottmann.tmdb2.entities.BaseTvShow
import me.tatarka.inject.annotations.Inject

@Inject
class TmdbBaseShowToTiviShow() : Mapper<BaseTvShow, TiviShow> {
override suspend fun map(from: BaseTvShow) = TiviShow(
class TmdbShowDetailToTiviShow : Mapper<TmdbShowDetail, TiviShow> {
override suspend fun map(from: TmdbShowDetail) = TiviShow(
tmdbId = from.id,
imdbId = from.externalIds?.imdbId,
title = from.name,
summary = from.overview,
homepage = from.homepage,
network = from.networks.firstOrNull()?.name,
networkLogoPath = null, // FIXME from.networks.firstOrNull()?.logo_path,
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@

package app.tivi.data.mappers

import app.moviebase.tmdb.model.TmdbShowPageResult
import app.tivi.data.models.ImageType
import app.tivi.data.models.ShowTmdbImage
import app.tivi.data.models.TiviShow
import com.uwetrottmann.tmdb2.entities.TvShowResultsPage
import me.tatarka.inject.annotations.Inject

@Inject
class TmdbShowResultsPageToTiviShows(
private val tmdbShowMapper: TmdbBaseShowToTiviShow,
) : Mapper<TvShowResultsPage, List<Pair<TiviShow, List<ShowTmdbImage>>>> {
override suspend fun map(from: TvShowResultsPage): List<Pair<TiviShow, List<ShowTmdbImage>>> {
return from.results.map {
val show = tmdbShowMapper.map(it)
class TmdbShowPageResultToTiviShows(
private val tmdbShowMapper: TmdbShowToTiviShow,
) : Mapper<TmdbShowPageResult, List<Pair<TiviShow, List<ShowTmdbImage>>>> {
override suspend fun map(from: TmdbShowPageResult): List<Pair<TiviShow, List<ShowTmdbImage>>> {
return from.results.map { result ->
val show = tmdbShowMapper.map(result)

val images = ArrayList<ShowTmdbImage>()
if (it.poster_path != null) {
result.posterPath?.let { path ->
images += ShowTmdbImage(
showId = 0,
path = it.poster_path,
path = path,
isPrimary = true,
type = ImageType.POSTER,
)
}
if (it.backdrop_path != null) {
result.backdropPath?.let { path ->
images += ShowTmdbImage(
showId = 0,
path = it.backdrop_path,
path = path,
isPrimary = true,
type = ImageType.BACKDROP,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package app.tivi.data.mappers

import app.moviebase.tmdb.model.TmdbShow
import app.tivi.data.models.TiviShow
import com.uwetrottmann.tmdb2.entities.TvShow
import me.tatarka.inject.annotations.Inject

@Inject
class TmdbShowToTiviShow() : Mapper<TvShow, TiviShow> {
override suspend fun map(from: TvShow) = TiviShow(
class TmdbShowToTiviShow : Mapper<TmdbShow, TiviShow> {
override suspend fun map(from: TmdbShow) = TiviShow(
tmdbId = from.id,
imdbId = from.external_ids?.imdb_id,
title = from.name,
summary = from.overview,
homepage = from.homepage,
network = from.networks?.firstOrNull()?.name,
networkLogoPath = from.networks?.firstOrNull()?.logo_path,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

interface RelatedShowsBinds {
@ApplicationScope
@Provides
fun provideTmdbRelatedShowsDataSource(
bind: TmdbRelatedShowsDataSourceImpl,
): TmdbRelatedShowsDataSource = bind

@ApplicationScope
@Provides
fun provideTraktRelatedShowsDataSource(
bind: TraktRelatedShowsDataSourceImpl,
): TraktRelatedShowsDataSource = bind
}

typealias TmdbRelatedShowsDataSource = RelatedShowsDataSource
typealias TraktRelatedShowsDataSource = RelatedShowsDataSource
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.mobilenativefoundation.store.store5.StoreBuilder
@ApplicationScope
@Inject
class RelatedShowsStore(
dataSource: TmdbRelatedShowsDataSource,
dataSource: TraktRelatedShowsDataSource,
relatedShowsDao: RelatedShowsDao,
showDao: TiviShowDao,
lastRequestStore: RelatedShowsLastRequestStore,
Expand Down
Loading