Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import com.github.damontecres.wholphin.api.seerr.model.SearchGet200ResponseResul
import com.github.damontecres.wholphin.api.seerr.model.ServiceProfile
import com.github.damontecres.wholphin.api.seerr.model.TvDetails
import com.github.damontecres.wholphin.api.seerr.model.TvResult
import com.github.damontecres.wholphin.api.seerr.model.WatchProviders
import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.DiscoverItem
import com.github.damontecres.wholphin.data.model.SeerrAvailability
import com.github.damontecres.wholphin.data.model.SeerrItemType
import com.github.damontecres.wholphin.data.model.SeerrPermission
import com.github.damontecres.wholphin.data.model.hasPermission
import com.github.damontecres.wholphin.ui.components.WatchProviderDisplay
import com.github.damontecres.wholphin.ui.detail.discover.SeerrProfile
import com.github.damontecres.wholphin.ui.detail.discover.SeerrRequestData
import com.github.damontecres.wholphin.ui.detail.discover.SeerrRootFolder
Expand All @@ -28,6 +30,7 @@ import kotlinx.coroutines.flow.firstOrNull
import org.jellyfin.sdk.model.api.BaseItemKind
import org.jellyfin.sdk.model.api.ImageType
import org.jellyfin.sdk.model.serializer.toUUIDOrNull
import java.util.Locale
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -227,6 +230,18 @@ class SeerrService
}
}

suspend fun createWatchProviderDisplays(providers: List<WatchProviders>?): List<WatchProviderDisplay> {
val current = seerrServerRepository.current.firstOrNull() ?: return emptyList()
val base =
if (current.serverConfig.cacheImages == true) {
current.server.url.removeSuffix("/") + "/imageproxy/tmdb"
} else {
"https://image.tmdb.org"
}

return mapWatchProviderDisplays(providers, base)
}

suspend fun getProfilesAndFolders(type: SeerrItemType): SeerrRequestData {
var result = SeerrRequestData()
val current = seerrServerRepository.current.firstOrNull()
Expand Down Expand Up @@ -499,6 +514,39 @@ class SeerrService
)
}

/**
* Select the most appropriate watch-provider region for the supplied locale.
*
* The locale's country is preferred, followed by the US region and then the first available
* region. Returns null when no watch-provider regions are available.
*/
internal fun mapWatchProviderDisplays(
providers: List<WatchProviders>?,
imageBaseUrl: String,
): List<WatchProviderDisplay> =
providers
.selectWatchProviderRegion()
?.flatrate
.orEmpty()
.mapNotNull { provider ->
val logoPath =
provider.logoPath?.takeIf { it.isNotBlank() }
?: return@mapNotNull null
WatchProviderDisplay(
name = provider.name,
logoUrl = "$imageBaseUrl/t/p/w92$logoPath",
)
}.distinctBy { it.logoUrl }

fun List<WatchProviders>?.selectWatchProviderRegion(locale: Locale = Locale.getDefault()): WatchProviders? {
val regions = orEmpty()
val country = locale.country.takeIf { it.isNotBlank() }

return regions.firstOrNull { it.iso31661.equals(country, ignoreCase = true) }
?: regions.firstOrNull { it.iso31661.equals(Locale.US.country, ignoreCase = true) }
?: regions.firstOrNull()
}

val MediaInfo.jellyfinId: UUID?
get() = jellyfinMediaId4k?.toUUIDOrNull() ?: jellyfinMediaId?.toUUIDOrNull()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.github.damontecres.wholphin.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.Text
import coil3.compose.AsyncImage
import coil3.request.ImageRequest
import com.github.damontecres.wholphin.R

data class WatchProviderDisplay(
val name: String?,
val logoUrl: String,
)

@Composable
fun WatchProvidersRow(
providers: List<WatchProviderDisplay>,
modifier: Modifier = Modifier,
) {
if (providers.isEmpty()) return

Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = modifier,
) {
Text(
text = stringResource(R.string.currently_streaming_on),
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onSurface,
)
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
providers.forEach { provider ->
WatchProviderLogo(provider)
}
}
}
}

@Composable
private fun WatchProviderLogo(
provider: WatchProviderDisplay,
modifier: Modifier = Modifier,
) {
AsyncImage(
model =
ImageRequest
.Builder(LocalContext.current)
.data(provider.logoUrl)
.build(),
contentDescription = provider.name,
contentScale = ContentScale.Crop,
modifier =
modifier
.size(30.dp)
.clip(RoundedCornerShape(10.dp))
.background(MaterialTheme.colorScheme.surfaceVariant),
)
}

@Preview(showBackground = true, backgroundColor = 11119017)
@Composable
private fun WatchProvidersRowPreview() {
WatchProvidersRow(
providers =
listOf(
WatchProviderDisplay(
name = "Netflix",
logoUrl = "https://upload.wikimedia.org/wikipedia/commons/0/08/Netflix_2015_logo.svg",
),
WatchProviderDisplay(
name = "HBO Max",
logoUrl = "https://upload.wikimedia.org/wikipedia/commons/1/17/HBO_Max_Logo.svg",
),
WatchProviderDisplay(
name = "Disney+",
logoUrl = "https://upload.wikimedia.org/wikipedia/commons/3/3e/Disney%2B_logo.svg",
),
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.github.damontecres.wholphin.R
import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.studioNames
import com.github.damontecres.wholphin.ui.components.ScrollableDialog
import com.github.damontecres.wholphin.ui.components.WatchProviderDisplay
import com.github.damontecres.wholphin.ui.components.WatchProvidersRow
import com.github.damontecres.wholphin.ui.formatBitrate
import com.github.damontecres.wholphin.ui.formatBytes
import com.github.damontecres.wholphin.ui.formatDate
Expand All @@ -47,6 +49,7 @@ data class ItemDetailsDialogInfo(
val files: List<MediaSourceInfo>,
val studios: List<String> = emptyList(),
val lastPlayed: LocalDateTime? = null,
val watchProviders: List<WatchProviderDisplay> = emptyList(),
) {
constructor(item: BaseItem) : this(
title = item.name ?: "",
Expand Down Expand Up @@ -109,6 +112,9 @@ fun ItemDetailsDialog(
style = MaterialTheme.typography.bodyMedium,
)
}
if (info.watchProviders.isNotEmpty()) {
WatchProvidersRow(providers = info.watchProviders)
}
val lastPlayed =
remember(info.lastPlayed) { info.lastPlayed?.let { formatDate(it) } }
if (lastPlayed != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fun DiscoverMovieDetails(
overview = movie.overview,
genres = movie.genres?.mapNotNull { it.name }.orEmpty(),
files = listOf(),
watchProviders = state.watchProviders,
)
},
goToOnClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.relocation.BringIntoViewRequester
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.github.damontecres.wholphin.services.NavigationManager
import com.github.damontecres.wholphin.services.SeerrServerRepository
import com.github.damontecres.wholphin.services.SeerrService
import com.github.damontecres.wholphin.services.SeerrUserConfig
import com.github.damontecres.wholphin.ui.components.WatchProviderDisplay
import com.github.damontecres.wholphin.ui.equalsNotNull
import com.github.damontecres.wholphin.ui.isNotNullOrBlank
import com.github.damontecres.wholphin.ui.launchDefault
Expand Down Expand Up @@ -85,7 +86,14 @@ class DiscoverMovieViewModel
viewModelScope.async(WholphinDispatchers.IO) {
try {
val movie = seerrService.api.moviesApi.movieMovieIdGet(movieId = item.id)
_state.update { it.copy(movie = DataLoadingState.Success(movie)) }
val watchProviders =
seerrService.createWatchProviderDisplays(movie.watchProviders)
_state.update {
it.copy(
movie = DataLoadingState.Success(movie),
watchProviders = watchProviders,
)
}
movie
} catch (ex: CancellationException) {
throw ex
Expand All @@ -100,7 +108,14 @@ class DiscoverMovieViewModel
Timber.v("Init for movie %s", item.id)
try {
val movie = seerrService.api.moviesApi.movieMovieIdGet(movieId = item.id)
_state.update { it.copy(movie = DataLoadingState.Success(movie)) }
val watchProviders =
seerrService.createWatchProviderDisplays(movie.watchProviders)
_state.update {
it.copy(
movie = DataLoadingState.Success(movie),
watchProviders = watchProviders,
)
}
val discoveredItem = seerrService.createDiscoverItem(movie)
backdropService.submit(discoveredItem)

Expand Down Expand Up @@ -279,6 +294,7 @@ fun canUserCancelRequest(

data class DiscoverMovieState(
val movie: DataLoadingState<MovieDetails> = DataLoadingState.Pending,
val watchProviders: List<WatchProviderDisplay> = emptyList(),
val rating: DiscoverRating? = null,
val seasons: List<RequestSeason> = emptyList(),
val trailers: List<Trailer> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fun DiscoverSeriesDetails(
overview = item.overview,
genres = item.genres?.mapNotNull { it.name }.orEmpty(),
files = listOf(),
watchProviders = state.watchProviders,
)
},
trailerOnClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.github.damontecres.wholphin.services.BackdropService
import com.github.damontecres.wholphin.services.NavigationManager
import com.github.damontecres.wholphin.services.SeerrServerRepository
import com.github.damontecres.wholphin.services.SeerrService
import com.github.damontecres.wholphin.ui.components.WatchProviderDisplay
import com.github.damontecres.wholphin.ui.isNotNullOrBlank
import com.github.damontecres.wholphin.ui.launchDefault
import com.github.damontecres.wholphin.ui.launchIO
Expand Down Expand Up @@ -89,7 +90,14 @@ class DiscoverSeriesViewModel
viewModelScope.async(WholphinDispatchers.IO) {
try {
val tv = seerrService.api.tvApi.tvTvIdGet(tvId = item.id)
_state.update { it.copy(tvSeries = DataLoadingState.Success(tv)) }
val watchProviders =
seerrService.createWatchProviderDisplays(tv.watchProviders)
_state.update {
it.copy(
tvSeries = DataLoadingState.Success(tv),
watchProviders = watchProviders,
)
}
tv
} catch (ex: CancellationException) {
throw ex
Expand All @@ -104,7 +112,14 @@ class DiscoverSeriesViewModel
Timber.v("Init for tv %s", item.id)
try {
val tv = seerrService.api.tvApi.tvTvIdGet(tvId = item.id)
_state.update { it.copy(tvSeries = DataLoadingState.Success(tv)) }
val watchProviders =
seerrService.createWatchProviderDisplays(tv.watchProviders)
_state.update {
it.copy(
tvSeries = DataLoadingState.Success(tv),
watchProviders = watchProviders,
)
}
val discoveredItem = seerrService.createDiscoverItem(tv)
backdropService.submit(discoveredItem)

Expand Down Expand Up @@ -449,6 +464,7 @@ class DiscoverSeriesViewModel

data class DiscoverSeriesState(
val tvSeries: DataLoadingState<TvDetails> = DataLoadingState.Pending,
val watchProviders: List<WatchProviderDisplay> = emptyList(),
val rating: DiscoverRating? = null,
val seasons: List<RequestSeason> = emptyList(),
val seasons4k: List<RequestSeason> = emptyList(),
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
<string name="show_details">Show details</string>
<string name="image_type">Image type</string>
<string name="cast_and_crew"><![CDATA[Cast & Crew]]></string>
<string name="currently_streaming_on">Currently Streaming On</string>
<string name="guest_stars">Guest stars</string>
<string name="edge_size">Edge size</string>
<string name="refresh_rate_switching">Refresh rate switching</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/seerr/seerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ components:
items:
$ref: '#/components/schemas/WatchProviderDetails'
flatrate:
type: array
items:
$ref: '#/components/schemas/WatchProviderDetails'
WatchProviderDetails:
Expand Down
Loading
Loading