convert session fields to mutablestate

This commit is contained in:
Owen LeJeune
2023-06-01 16:11:11 -04:00
parent 5fdb23ce42
commit 5c5a931673
4 changed files with 83 additions and 46 deletions

View File

@@ -64,10 +64,11 @@ fun AccountTab(
} }
} else { } else {
if (currentSession?.isAuthorized == true) { if (currentSession?.isAuthorized == true) {
val accountDetails = remember { currentSession.accountDetails }
appBarTitle.value = appBarTitle.value =
stringResource( stringResource(
id = R.string.account_header_title_formatted, id = R.string.account_header_title_formatted,
getAccountName(currentSession.accountDetails) getAccountName(accountDetails.value)
) )
} else { } else {
appBarTitle.value = stringResource(id = R.string.account_not_logged_in) appBarTitle.value = stringResource(id = R.string.account_not_logged_in)
@@ -127,7 +128,7 @@ fun <T: Any> AccountTabContent(
listFetchFun: ListFetchFun, listFetchFun: ListFetchFun,
clazz: KClass<T> clazz: KClass<T>
) { ) {
val contentItems = listFetchFun() val contentItems = remember { listFetchFun() }
if (contentItems.isEmpty()) { if (contentItems.isEmpty()) {
Column { Column {
@@ -342,7 +343,7 @@ private fun signInPart2() {
@Composable @Composable
private fun AuthorizedSessionIcon() { private fun AuthorizedSessionIcon() {
val accountDetails = SessionManager.currentSession.value?.accountDetails val accountDetails = SessionManager.currentSession.value?.accountDetails?.value
val avatarUrl = accountDetails?.let { val avatarUrl = accountDetails?.let {
when { when {
accountDetails.avatar.tmdb?.avatarPath?.isNotEmpty() == true -> { accountDetails.avatar.tmdb?.avatarPath?.isNotEmpty() == true -> {

View File

@@ -607,7 +607,7 @@ private fun addToWatchlist(
onWatchlistChanged: (Boolean) -> Unit onWatchlistChanged: (Boolean) -> Unit
) { ) {
val currentSession = SessionManager.currentSession.value val currentSession = SessionManager.currentSession.value
val accountId = currentSession!!.accountDetails!!.id val accountId = currentSession!!.accountDetails.value!!.id
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value)) val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value))
if (response.isSuccessful) { if (response.isSuccessful) {
@@ -632,7 +632,7 @@ private fun addToFavorite(
onFavoriteChanged: (Boolean) -> Unit onFavoriteChanged: (Boolean) -> Unit
) { ) {
val currentSession = SessionManager.currentSession.value val currentSession = SessionManager.currentSession.value
val accountId = currentSession!!.accountDetails!!.id val accountId = currentSession!!.accountDetails.value!!.id
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value)) val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value))
if (response.isSuccessful) { if (response.isSuccessful) {

View File

@@ -1268,7 +1268,7 @@ private fun addToWatchlist(
onWatchlistChanged: (Boolean) -> Unit onWatchlistChanged: (Boolean) -> Unit
) { ) {
val currentSession = SessionManager.currentSession.value val currentSession = SessionManager.currentSession.value
val accountId = currentSession!!.accountDetails!!.id val accountId = currentSession!!.accountDetails.value!!.id
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value)) val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value))
if (response.isSuccessful) { if (response.isSuccessful) {
@@ -1293,7 +1293,7 @@ private fun addToFavorite(
onFavoriteChanged: (Boolean) -> Unit onFavoriteChanged: (Boolean) -> Unit
) { ) {
val currentSession = SessionManager.currentSession.value val currentSession = SessionManager.currentSession.value
val accountId = currentSession!!.accountDetails!!.id val accountId = currentSession!!.accountDetails.value!!.id
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value)) val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value))
if (response.isSuccessful) { if (response.isSuccessful) {

View File

@@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.widget.Toast import android.widget.Toast
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
import com.owenlejeune.tvtime.R import com.owenlejeune.tvtime.R
@@ -17,6 +18,7 @@ import com.owenlejeune.tvtime.api.tmdb.api.v4.model.AuthDeleteBody
import com.owenlejeune.tvtime.api.tmdb.api.v4.model.AuthRequestBody import com.owenlejeune.tvtime.api.tmdb.api.v4.model.AuthRequestBody
import com.owenlejeune.tvtime.api.tmdb.api.v4.model.V4AccountList import com.owenlejeune.tvtime.api.tmdb.api.v4.model.V4AccountList
import com.owenlejeune.tvtime.preferences.AppPreferences import com.owenlejeune.tvtime.preferences.AppPreferences
import com.owenlejeune.tvtime.ui.navigation.AccountTabNavItem
import com.owenlejeune.tvtime.ui.screens.main.MediaViewType import com.owenlejeune.tvtime.ui.screens.main.MediaViewType
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -124,41 +126,51 @@ object SessionManager: KoinComponent {
} }
abstract class Session(val sessionId: String, val isAuthorized: Boolean, val accessToken: String = "", val accountId: String = "") { abstract class Session(val sessionId: String, val isAuthorized: Boolean, val accessToken: String = "", val accountId: String = "") {
protected open var _ratedMovies: List<RatedMovie> = emptyList() // protected open var _ratedMovies: List<RatedMovie> = emptyList()
val ratedMovies: List<RatedMovie> // val ratedMovies: List<RatedMovie>
get() = _ratedMovies // get() = _ratedMovies
protected open var _ratedTvShows: List<RatedTv> = emptyList() val ratedMovies = mutableStateListOf<RatedMovie>()
val ratedTvShows: List<RatedTv>
get() = _ratedTvShows
protected open var _ratedTvEpisodes: List<RatedEpisode> = emptyList() // protected open var _ratedTvShows: List<RatedTv> = emptyList()
val ratedTvEpisodes: List<RatedEpisode> // val ratedTvShows: List<RatedTv>
get() = _ratedTvEpisodes // get() = _ratedTvShows
val ratedTvShows = mutableStateListOf<RatedTv>()
protected open var _accountDetails: AccountDetails? = null // protected open var _ratedTvEpisodes: List<RatedEpisode> = emptyList()
val accountDetails: AccountDetails? // val ratedTvEpisodes: List<RatedEpisode>
get() = _accountDetails // get() = _ratedTvEpisodes
val ratedTvEpisodes = mutableStateListOf<RatedEpisode>()
protected open var _accountLists: List<V4AccountList> = emptyList() // protected open var _accountDetails: AccountDetails? = null
val accountLists: List<V4AccountList> // val accountDetails: AccountDetails?
get() = _accountLists // get() = _accountDetails
val accountDetails = mutableStateOf<AccountDetails?>(null)
protected open var _favoriteMovies: List<FavoriteMovie> = emptyList() // protected open var _accountLists: List<V4AccountList> = emptyList()
val favoriteMovies: List<FavoriteMovie> // val accountLists: List<V4AccountList>
get() = _favoriteMovies // get() = _accountLists
val accountLists = mutableStateListOf<V4AccountList>()
protected open var _favoriteTvShows: List<FavoriteTvSeries> = emptyList() // protected open var _favoriteMovies: List<FavoriteMovie> = emptyList()
val favoriteTvShows: List<FavoriteTvSeries> // val favoriteMovies: List<FavoriteMovie>
get() = _favoriteTvShows // get() = _favoriteMovies
val favoriteMovies = mutableStateListOf<FavoriteMovie>()
protected open var _movieWatchlist: List<WatchlistMovie> = emptyList() // protected open var _favoriteTvShows: List<FavoriteTvSeries> = emptyList()
val movieWatchlist: List<WatchlistMovie> // val favoriteTvShows: List<FavoriteTvSeries>
get() = _movieWatchlist // get() = _favoriteTvShows
val favoriteTvShows = mutableStateListOf<FavoriteTvSeries>()
protected open var _tvWatchlist: List<WatchlistTvSeries> = emptyList() // protected open var _movieWatchlist: List<WatchlistMovie> = emptyList()
val tvWatchlist: List<WatchlistTvSeries> // val movieWatchlist: List<WatchlistMovie>
get() = _tvWatchlist // get() = _movieWatchlist
val movieWatchlist = mutableStateListOf<WatchlistMovie>()
// protected open var _tvWatchlist: List<WatchlistTvSeries> = emptyList()
// val tvWatchlist: List<WatchlistTvSeries>
// get() = _tvWatchlist
val tvWatchlist = mutableStateListOf<WatchlistTvSeries>()
fun hasRatedMovie(id: Int): Boolean { fun hasRatedMovie(id: Int): Boolean {
return ratedMovies.map { it.id }.contains(id) return ratedMovies.map { it.id }.contains(id)
@@ -249,13 +261,13 @@ object SessionManager: KoinComponent {
if (changed.contains(Changed.AccountDetails)) { if (changed.contains(Changed.AccountDetails)) {
val response = service.getAccountDetails() val response = service.getAccountDetails()
if (response.isSuccessful) { if (response.isSuccessful) {
_accountDetails = response.body() ?: _accountDetails accountDetails.value = response.body()
accountDetails?.let { accountDetails.value?.let {
refreshWithAccountId(it.id, changed) refreshWithAccountId(it.id, changed)
} }
} }
} else if (accountDetails != null) { } else if (accountDetails.value != null) {
refreshWithAccountId(accountDetails!!.id, changed) refreshWithAccountId(accountDetails.value!!.id, changed)
} }
} }
@@ -264,7 +276,10 @@ object SessionManager: KoinComponent {
serviceV4.getLists(preferences.authorizedSessionValues?.accountId ?: "").apply { serviceV4.getLists(preferences.authorizedSessionValues?.accountId ?: "").apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_accountLists = body()?.results ?: _accountLists body()?.results?.let {
accountLists.clear()
accountLists.addAll(it)
}
} }
} }
} }
@@ -273,7 +288,10 @@ object SessionManager: KoinComponent {
service.getFavoriteMovies(accountId).apply { service.getFavoriteMovies(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_favoriteMovies = body()?.results ?: _favoriteMovies body()?.results?.let {
favoriteMovies.clear()
favoriteMovies.addAll(it)
}
} }
} }
} }
@@ -282,7 +300,10 @@ object SessionManager: KoinComponent {
service.getFavoriteTvShows(accountId).apply { service.getFavoriteTvShows(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_favoriteTvShows = body()?.results ?: _favoriteTvShows body()?.results?.let {
favoriteTvShows.clear()
favoriteTvShows.addAll(it)
}
} }
} }
} }
@@ -291,7 +312,10 @@ object SessionManager: KoinComponent {
service.getRatedMovies(accountId).apply { service.getRatedMovies(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_ratedMovies = body()?.results ?: _ratedMovies body()?.results?.let {
ratedMovies.clear()
ratedMovies.addAll(it)
}
} }
} }
} }
@@ -300,7 +324,10 @@ object SessionManager: KoinComponent {
service.getRatedTvShows(accountId).apply { service.getRatedTvShows(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_ratedTvShows = body()?.results ?: _ratedTvShows body()?.results?.let {
ratedTvShows.clear()
ratedTvShows.addAll(it)
}
} }
} }
} }
@@ -309,7 +336,10 @@ object SessionManager: KoinComponent {
service.getRatedTvEpisodes(accountId).apply { service.getRatedTvEpisodes(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_ratedTvEpisodes = body()?.results ?: _ratedTvEpisodes body()?.results?.let {
ratedTvEpisodes.clear()
ratedTvEpisodes.addAll(it)
}
} }
} }
} }
@@ -318,7 +348,10 @@ object SessionManager: KoinComponent {
service.getMovieWatchlist(accountId).apply { service.getMovieWatchlist(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_movieWatchlist = body()?.results ?: _movieWatchlist body()?.results?.let {
movieWatchlist.clear()
movieWatchlist.addAll(it)
}
} }
} }
} }
@@ -327,7 +360,10 @@ object SessionManager: KoinComponent {
service.getTvWatchlist(accountId).apply { service.getTvWatchlist(accountId).apply {
if (isSuccessful) { if (isSuccessful) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
_tvWatchlist = body()?.results ?: _tvWatchlist body()?.results?.let {
tvWatchlist.clear()
tvWatchlist.addAll(it)
}
} }
} }
} }