Skip to content
Permalink
Browse files

Use Tmdb primary images

  • Loading branch information...
chrisbanes committed Jun 10, 2019
1 parent d4eae31 commit 97e8450304ad89a0ba57e704bd463ded40202461
@@ -57,9 +57,10 @@ fun loadBackdrop(
saturateOnLoad: Boolean?
) {
val image = if (path != null) ShowTmdbImage(path = path, type = ImageType.BACKDROP, showId = 0) else null
loadImage(view, image, urlProvider, saturateOnLoad)
loadImage(view, null, urlProvider, saturateOnLoad, image, urlProvider, saturateOnLoad)
}

@Suppress("UNUSED_PARAMETER")
@BindingAdapter(
"image",
"tmdbImageUrlProvider",
@@ -68,18 +69,21 @@ fun loadBackdrop(
)
fun loadImage(
view: ImageView,
previousImage: TmdbImageEntity?,
previousUrlProvider: TmdbImageUrlProvider?,
previousSaturateOnLoad: Boolean?,
image: TmdbImageEntity?,
urlProvider: TmdbImageUrlProvider?,
saturateOnLoad: Boolean?
) {
val requestKey = Objects.hash(image, urlProvider)
if (view.getTag(R.id.loading) == requestKey) {
// We're already loading this image, ignore the call
return
}
view.setTag(R.id.loading, requestKey)

if (urlProvider != null && image != null) {
if (previousUrlProvider == urlProvider && previousImage == image) {
return
}

view.setImageDrawable(null)

view.doOnLayout {
@@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 21,
"identityHash": "7432faa9533ec59c4c9d20967824dfa1",
"identityHash": "364dc66dca74ffa97cd348917bcb983a",
"entities": [
{
"tableName": "shows",
@@ -916,7 +916,7 @@
},
{
"tableName": "show_images",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `show_id` INTEGER NOT NULL, `path` TEXT NOT NULL, `type` TEXT NOT NULL, `lang` TEXT, `rating` REAL NOT NULL, FOREIGN KEY(`show_id`) REFERENCES `shows`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `show_id` INTEGER NOT NULL, `path` TEXT NOT NULL, `type` TEXT NOT NULL, `lang` TEXT, `rating` REAL NOT NULL, `is_primary` INTEGER NOT NULL, FOREIGN KEY(`show_id`) REFERENCES `shows`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
@@ -953,6 +953,12 @@
"columnName": "rating",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "isPrimary",
"columnName": "is_primary",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
@@ -1002,7 +1008,7 @@
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7432faa9533ec59c4c9d20967824dfa1')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '364dc66dca74ffa97cd348917bcb983a')"
]
}
}
@@ -39,5 +39,6 @@ data class ShowTmdbImage(
@ColumnInfo(name = "path") override val path: String,
@ColumnInfo(name = "type") override val type: ImageType,
@ColumnInfo(name = "lang") override val language: String? = null,
@ColumnInfo(name = "rating") override val rating: Float = 0f
@ColumnInfo(name = "rating") override val rating: Float = 0f,
@ColumnInfo(name = "is_primary") override val isPrimary: Boolean = false
) : TiviEntity, TmdbImageEntity
@@ -33,12 +33,19 @@ interface TmdbImageEntity : TiviEntity {
val type: ImageType
val language: String?
val rating: Float
val isPrimary: Boolean
}

enum class ImageType(val storageKey: String) {
BACKDROP("backdrop"),
POSTER("poster")
}

fun <T : TmdbImageEntity> Collection<T>.findHighestRatedPoster() = filter { it.type == ImageType.POSTER }.maxBy { it.rating }
fun <T : TmdbImageEntity> Collection<T>.findHighestRatedBackdrop() = filter { it.type == ImageType.BACKDROP }.maxBy { it.rating }
fun <T : TmdbImageEntity> Collection<T>.findHighestRatedPoster(): T? {
return filter { it.type == ImageType.POSTER }
.maxBy { it.rating + (if (it.isPrimary) 10f else 0f) }
}
fun <T : TmdbImageEntity> Collection<T>.findHighestRatedBackdrop(): T? {
return filter { it.type == ImageType.BACKDROP }
.maxBy { it.rating + (if (it.isPrimary) 10f else 0f) }
}
@@ -19,26 +19,30 @@ package app.tivi.data.mappers
import app.tivi.data.entities.ImageType
import app.tivi.data.entities.ShowTmdbImage
import com.uwetrottmann.tmdb2.entities.Image
import com.uwetrottmann.tmdb2.entities.Images
import com.uwetrottmann.tmdb2.entities.TvShow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TmdbImagesToShowImages @Inject constructor() : Mapper<Images, List<ShowTmdbImage>> {
override suspend fun map(from: Images): List<ShowTmdbImage> {
class TmdbImagesToShowImages @Inject constructor() : Mapper<TvShow, List<ShowTmdbImage>> {
override suspend fun map(from: TvShow): List<ShowTmdbImage> {
fun mapImage(image: Image, type: ImageType): ShowTmdbImage {
return ShowTmdbImage(
showId = 0,
path = image.file_path!!,
type = type,
language = image.iso_639_1,
rating = image.vote_average?.toFloat() ?: 0f
rating = image.vote_average?.toFloat() ?: 0f,
isPrimary = image.file_path!! == when (type) {
ImageType.BACKDROP -> from.backdrop_path
ImageType.POSTER -> from.poster_path
}
)
}

val result = mutableListOf<ShowTmdbImage>()
from.posters?.mapTo(result) { mapImage(it, ImageType.POSTER) }
from.backdrops?.mapTo(result) { mapImage(it, ImageType.BACKDROP) }
from.images?.posters?.mapTo(result) { mapImage(it, ImageType.POSTER) }
from.images?.backdrops?.mapTo(result) { mapImage(it, ImageType.BACKDROP) }
return result
}
}
@@ -24,6 +24,8 @@ import app.tivi.data.entities.TiviShow
import app.tivi.data.mappers.TmdbImagesToShowImages
import app.tivi.extensions.executeWithRetry
import com.uwetrottmann.tmdb2.Tmdb
import com.uwetrottmann.tmdb2.entities.AppendToResponse
import com.uwetrottmann.tmdb2.enumerations.AppendToResponseItem
import javax.inject.Inject

class TmdbShowImagesDataSource @Inject constructor(
@@ -35,8 +37,8 @@ class TmdbShowImagesDataSource @Inject constructor(
val tmdbId = show.tmdbId
return if (tmdbId != null) {
retrofitRunner.executeForResponse(mapper) {
// TODO Need to use devices locale!!
tmdb.tvService().images(tmdbId, "en,null").executeWithRetry()
tmdb.tvService().tv(tmdbId, null, AppendToResponse(AppendToResponseItem.IMAGES))
.executeWithRetry()
}
} else {
ErrorResult(IllegalArgumentException("TmdbId for show does not exist [$show]"))

0 comments on commit 97e8450

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