mirror of
https://github.com/owenlejeune/TVTime.git
synced 2025-11-18 09:40:53 -05:00
fix some bugs with tv seasons loading
This commit is contained in:
@@ -86,6 +86,10 @@ class TmdbClient: KoinComponent {
|
|||||||
return clientV4.create(ListV4Api::class.java)
|
return clientV4.create(ListV4Api::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ignoreErrorResponseCode(urlSegments: List<String>): Boolean {
|
||||||
|
return (urlSegments.last() == "0" && urlSegments[urlSegments.size-2] == "season")
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("AutoboxingStateValueProperty")
|
@SuppressLint("AutoboxingStateValueProperty")
|
||||||
private fun handleResponseCode(response: Response) {
|
private fun handleResponseCode(response: Response) {
|
||||||
when (response.code) {
|
when (response.code) {
|
||||||
@@ -106,10 +110,9 @@ class TmdbClient: KoinComponent {
|
|||||||
val apiParam = QueryParam("api_key", BuildConfig.TMDB_ApiKey)
|
val apiParam = QueryParam("api_key", BuildConfig.TMDB_ApiKey)
|
||||||
|
|
||||||
val segments = chain.request().url.encodedPathSegments
|
val segments = chain.request().url.encodedPathSegments
|
||||||
val sessionIdParam: QueryParam? = sessionIdParam(segments)
|
|
||||||
|
|
||||||
val builder = chain.request().url.newBuilder()
|
val builder = chain.request().url.newBuilder()
|
||||||
builder.addQueryParams(apiParam, sessionIdParam)
|
builder.addQueryParams(apiParam)
|
||||||
|
|
||||||
if (shouldIncludeLanguageParam(segments)) {
|
if (shouldIncludeLanguageParam(segments)) {
|
||||||
val locale = Locale.current
|
val locale = Locale.current
|
||||||
@@ -130,22 +133,13 @@ class TmdbClient: KoinComponent {
|
|||||||
val request = requestBuilder.build()
|
val request = requestBuilder.build()
|
||||||
val response = chain.proceed(request)
|
val response = chain.proceed(request)
|
||||||
|
|
||||||
|
if (!ignoreErrorResponseCode(segments)) {
|
||||||
handleResponseCode(response)
|
handleResponseCode(response)
|
||||||
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sessionIdParam(urlSegments: List<String>): QueryParam? {
|
|
||||||
var sessionIdParam: QueryParam? = null
|
|
||||||
if (urlSegments.size > 1 && urlSegments[1] == "account") {
|
|
||||||
val currentSession = SessionManager.currentSession.value
|
|
||||||
if (!currentSession?.sessionId.isNullOrEmpty()) {
|
|
||||||
sessionIdParam = QueryParam("session_id", currentSession!!.sessionId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sessionIdParam
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun shouldIncludeLanguageParam(urlSegments: List<String>): Boolean {
|
private fun shouldIncludeLanguageParam(urlSegments: List<String>): Boolean {
|
||||||
val ignoredRoutes = listOf("images", "account")
|
val ignoredRoutes = listOf("images", "account")
|
||||||
for (route in ignoredRoutes) {
|
for (route in ignoredRoutes) {
|
||||||
@@ -157,7 +151,7 @@ class TmdbClient: KoinComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldIncludeSessionIdParam(urlSegments: List<String>): Boolean {
|
private fun shouldIncludeSessionIdParam(urlSegments: List<String>): Boolean {
|
||||||
val includedRoutes = listOf("account_states")
|
val includedRoutes = listOf("account_states", "account")
|
||||||
for (route in includedRoutes) {
|
for (route in includedRoutes) {
|
||||||
if (urlSegments.contains(route)) {
|
if (urlSegments.contains(route)) {
|
||||||
return true
|
return true
|
||||||
@@ -191,7 +185,9 @@ class TmdbClient: KoinComponent {
|
|||||||
|
|
||||||
val response = chain.proceed(builder.build())
|
val response = chain.proceed(builder.build())
|
||||||
|
|
||||||
|
if (!ignoreErrorResponseCode(chain.request().url.encodedPathSegments)) {
|
||||||
handleResponseCode(response)
|
handleResponseCode(response)
|
||||||
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,9 +172,13 @@ class TvService: KoinComponent, DetailService, HomePageService {
|
|||||||
suspend fun getSeason(seriesId: Int, seasonId: Int, refreshing: Boolean) {
|
suspend fun getSeason(seriesId: Int, seasonId: Int, refreshing: Boolean) {
|
||||||
loadRemoteData(
|
loadRemoteData(
|
||||||
{ service.getSeason(seriesId, seasonId) },
|
{ service.getSeason(seriesId, seasonId) },
|
||||||
{
|
{ season ->
|
||||||
_seasons[seriesId]?.add(it) ?: run {
|
_seasons[seriesId]?.let { seasonSet ->
|
||||||
_seasons[seriesId] = mutableSetOf(it)
|
if (seasonSet.find { it.seasonNumber == seasonId } == null) {
|
||||||
|
seasonSet.add(season)
|
||||||
|
}
|
||||||
|
} ?: run {
|
||||||
|
_seasons[seriesId] = mutableSetOf(season)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
seasonsLoadingState,
|
seasonsLoadingState,
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ fun SeasonListScreen(
|
|||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
val numSeasons = mainViewModel.detailedTv[id]!!.numberOfSeasons
|
val numSeasons = mainViewModel.detailedTv[id]!!.numberOfSeasons
|
||||||
for (i in 0..numSeasons) {
|
for (i in 0..numSeasons) {
|
||||||
mainViewModel.getSeason(id, i)
|
mainViewModel.getSeason(id, i, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,8 +112,8 @@ fun SeasonListScreen(
|
|||||||
val seasonsMap = remember { mainViewModel.tvSeasons }
|
val seasonsMap = remember { mainViewModel.tvSeasons }
|
||||||
val seasons = seasonsMap[id] ?: emptySet()
|
val seasons = seasonsMap[id] ?: emptySet()
|
||||||
|
|
||||||
seasons.sortedBy { it.seasonNumber }.forEach { season ->
|
seasons.sortedBy { it.seasonNumber }.forEachIndexed { index, season ->
|
||||||
SeasonSection(season = season, singleSeason = isSingleSeason(seasons))
|
SeasonSection(season = season, expandedByDefault = index == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(6.dp))
|
Spacer(modifier = Modifier.height(6.dp))
|
||||||
@@ -123,8 +123,8 @@ fun SeasonListScreen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun SeasonSection(season: Season, singleSeason: Boolean) {
|
private fun SeasonSection(season: Season, expandedByDefault: Boolean) {
|
||||||
var isExpanded by remember { mutableStateOf(singleSeason) }
|
var isExpanded by remember { mutableStateOf(expandedByDefault) }
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.padding(horizontal = 12.dp),
|
modifier = Modifier.padding(horizontal = 12.dp),
|
||||||
@@ -257,7 +257,3 @@ private fun EpisodeItem(episode: Episode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isSingleSeason(seasons: Set<Season>): Boolean {
|
|
||||||
return seasons.size == 1 || (seasons.size == 2 && seasons.any { it.seasonNumber == 0 })
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user