mirror of
https://github.com/owenlejeune/TVTime.git
synced 2025-11-12 06:42:45 -05:00
add authorized session type to account manager
This commit is contained in:
@@ -11,6 +11,10 @@ class AccountService {
|
||||
return accountService.getAccountDetails()
|
||||
}
|
||||
|
||||
suspend fun getLists(accountId: Int, page: Int = 1): Response<AccountListResponse> {
|
||||
return accountService.getLists(accountId, page)
|
||||
}
|
||||
|
||||
suspend fun getFavoriteMovies(accountId: Int, page: Int = 1): Response<FavoriteMediaResponse<FavoriteMovie>> {
|
||||
return accountService.getFavoriteMovies(accountId, page)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.owenlejeune.tvtime.utils
|
||||
|
||||
import com.owenlejeune.tvtime.api.tmdb.AccountService
|
||||
import com.owenlejeune.tvtime.api.tmdb.GuestSessionService
|
||||
import com.owenlejeune.tvtime.api.tmdb.TmdbClient
|
||||
import com.owenlejeune.tvtime.api.tmdb.model.*
|
||||
import com.owenlejeune.tvtime.preferences.AppPreferences
|
||||
@@ -32,6 +34,7 @@ object SessionManager: KoinComponent {
|
||||
if (deleteResponse.isSuccessful) {
|
||||
_currentSession = null
|
||||
preferences.guestSessionId = ""
|
||||
preferences.authorizedSessionId = ""
|
||||
}
|
||||
onResponse(deleteResponse.isSuccessful)
|
||||
}
|
||||
@@ -81,18 +84,42 @@ object SessionManager: KoinComponent {
|
||||
}
|
||||
|
||||
abstract class Session(val sessionId: String, val isAuthorized: Boolean) {
|
||||
protected abstract var _ratedMovies: List<RatedMovie>
|
||||
protected open var _ratedMovies: List<RatedMovie> = emptyList()
|
||||
val ratedMovies: List<RatedMovie>
|
||||
get() = _ratedMovies
|
||||
|
||||
protected abstract var _ratedTvShows: List<RatedTv>
|
||||
protected open var _ratedTvShows: List<RatedTv> = emptyList()
|
||||
val ratedTvShows: List<RatedTv>
|
||||
get() = _ratedTvShows
|
||||
|
||||
protected abstract var _ratedTvEpisodes: List<RatedEpisode>
|
||||
protected open var _ratedTvEpisodes: List<RatedEpisode> = emptyList()
|
||||
val ratedTvEpisodes: List<RatedEpisode>
|
||||
get() = _ratedTvEpisodes
|
||||
|
||||
protected open var _accountDetails: AccountDetails? = null
|
||||
val accountDetails: AccountDetails?
|
||||
get() = _accountDetails
|
||||
|
||||
protected open var _accountLists: List<AccountList> = emptyList()
|
||||
val accountLists: List<AccountList>
|
||||
get() = _accountLists
|
||||
|
||||
protected open var _favoriteMovies: List<FavoriteMovie> = emptyList()
|
||||
val favoriteMovies: List<FavoriteMovie>
|
||||
get() = _favoriteMovies
|
||||
|
||||
protected open var _favoriteTvShows: List<FavoriteTvSeries> = emptyList()
|
||||
val favoriteTvShows: List<FavoriteTvSeries>
|
||||
get() = _favoriteTvShows
|
||||
|
||||
protected open var _movieWatchlist: List<WatchlistMovie> = emptyList()
|
||||
val movieWatchlist: List<WatchlistMovie>
|
||||
get() = _movieWatchlist
|
||||
|
||||
protected open var _tvWatchlist: List<WatchlistTvSeries> = emptyList()
|
||||
val tvWatchlist: List<WatchlistTvSeries>
|
||||
get() = _tvWatchlist
|
||||
|
||||
fun hasRatedMovie(id: Int): Boolean {
|
||||
return ratedMovies.map { it.id }.contains(id)
|
||||
}
|
||||
@@ -105,31 +132,111 @@ object SessionManager: KoinComponent {
|
||||
return ratedTvEpisodes.map { it.id }.contains(id)
|
||||
}
|
||||
|
||||
fun hasFavoritedMovie(id: Int): Boolean {
|
||||
return favoriteMovies.map { it.id }.contains(id)
|
||||
}
|
||||
|
||||
fun hasFavoritedTvShow(id: Int): Boolean {
|
||||
return favoriteTvShows.map { it.id }.contains(id)
|
||||
}
|
||||
|
||||
fun hasWatchlistedMovie(id: Int): Boolean {
|
||||
return movieWatchlist.map { it.id }.contains(id)
|
||||
}
|
||||
|
||||
fun hasWatchlistedTvShow(id: Int): Boolean {
|
||||
return tvWatchlist.map { it.id }.contains(id)
|
||||
}
|
||||
|
||||
abstract suspend fun initialize()
|
||||
|
||||
abstract suspend fun refresh()
|
||||
}
|
||||
|
||||
private class AuthorizedSession: Session(preferences.authorizedSessionId, true) {
|
||||
override var _ratedMovies: List<RatedMovie> = emptyList()
|
||||
override var _ratedTvShows: List<RatedTv> = emptyList()
|
||||
override var _ratedTvEpisodes: List<RatedEpisode> = emptyList()
|
||||
private val service by lazy { AccountService() }
|
||||
|
||||
override suspend fun initialize() {
|
||||
refresh()
|
||||
}
|
||||
|
||||
override suspend fun refresh() {
|
||||
service.getAccountDetails().apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_accountDetails = body() ?: _accountDetails
|
||||
accountDetails?.let {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
refreshWithAccountId(it.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun refreshWithAccountId(accountId: Int) {
|
||||
service.getLists(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_accountLists = body()?.results ?: _accountLists
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getFavoriteMovies(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_favoriteMovies = body()?.results ?: _favoriteMovies
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getFavoriteTvShows(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_favoriteTvShows = body()?.results ?: _favoriteTvShows
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getRatedMovies(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_ratedMovies = body()?.results ?: _ratedMovies
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getRatedTvShows(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_ratedTvShows = body()?.results ?: _ratedTvShows
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getRatedTvEpisodes(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_ratedTvEpisodes = body()?.results ?: _ratedTvEpisodes
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getMovieWatchlist(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_movieWatchlist = body()?.results ?: _movieWatchlist
|
||||
}
|
||||
}
|
||||
}
|
||||
service.getTvWatchlist(accountId).apply {
|
||||
if (isSuccessful) {
|
||||
withContext(Dispatchers.Main) {
|
||||
_tvWatchlist = body()?.results ?: _tvWatchlist
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class GuestSession: Session(preferences.guestSessionId, false) {
|
||||
override var _ratedMovies: List<RatedMovie> = emptyList()
|
||||
override var _ratedTvShows: List<RatedTv> = emptyList()
|
||||
override var _ratedTvEpisodes: List<RatedEpisode> = emptyList()
|
||||
|
||||
private val service by lazy { TmdbClient().createGuestSessionService() }
|
||||
private val service by lazy { GuestSessionService() }
|
||||
|
||||
override suspend fun initialize() {
|
||||
refresh()
|
||||
|
||||
Reference in New Issue
Block a user