From 5c5a93167374c6b7bf68066d43d2ed9d5331f70f Mon Sep 17 00:00:00 2001 From: Owen LeJeune Date: Thu, 1 Jun 2023 16:11:11 -0400 Subject: [PATCH] convert session fields to mutablestate --- .../tvtime/ui/screens/main/AccountTab.kt | 7 +- .../tvtime/ui/screens/main/ListDetailView.kt | 4 +- .../tvtime/ui/screens/main/MediaDetailView.kt | 4 +- .../tvtime/utils/SessionManager.kt | 114 ++++++++++++------ 4 files changed, 83 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/AccountTab.kt b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/AccountTab.kt index f7d632b..6a92f88 100644 --- a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/AccountTab.kt +++ b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/AccountTab.kt @@ -64,10 +64,11 @@ fun AccountTab( } } else { if (currentSession?.isAuthorized == true) { + val accountDetails = remember { currentSession.accountDetails } appBarTitle.value = stringResource( id = R.string.account_header_title_formatted, - getAccountName(currentSession.accountDetails) + getAccountName(accountDetails.value) ) } else { appBarTitle.value = stringResource(id = R.string.account_not_logged_in) @@ -127,7 +128,7 @@ fun AccountTabContent( listFetchFun: ListFetchFun, clazz: KClass ) { - val contentItems = listFetchFun() + val contentItems = remember { listFetchFun() } if (contentItems.isEmpty()) { Column { @@ -342,7 +343,7 @@ private fun signInPart2() { @Composable private fun AuthorizedSessionIcon() { - val accountDetails = SessionManager.currentSession.value?.accountDetails + val accountDetails = SessionManager.currentSession.value?.accountDetails?.value val avatarUrl = accountDetails?.let { when { accountDetails.avatar.tmdb?.avatarPath?.isNotEmpty() == true -> { diff --git a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/ListDetailView.kt b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/ListDetailView.kt index 38aad0a..714629c 100644 --- a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/ListDetailView.kt +++ b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/ListDetailView.kt @@ -607,7 +607,7 @@ private fun addToWatchlist( onWatchlistChanged: (Boolean) -> Unit ) { val currentSession = SessionManager.currentSession.value - val accountId = currentSession!!.accountDetails!!.id + val accountId = currentSession!!.accountDetails.value!!.id CoroutineScope(Dispatchers.IO).launch { val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value)) if (response.isSuccessful) { @@ -632,7 +632,7 @@ private fun addToFavorite( onFavoriteChanged: (Boolean) -> Unit ) { val currentSession = SessionManager.currentSession.value - val accountId = currentSession!!.accountDetails!!.id + val accountId = currentSession!!.accountDetails.value!!.id CoroutineScope(Dispatchers.IO).launch { val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value)) if (response.isSuccessful) { diff --git a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/MediaDetailView.kt b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/MediaDetailView.kt index 46e628c..dd141ea 100644 --- a/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/MediaDetailView.kt +++ b/app/src/main/java/com/owenlejeune/tvtime/ui/screens/main/MediaDetailView.kt @@ -1268,7 +1268,7 @@ private fun addToWatchlist( onWatchlistChanged: (Boolean) -> Unit ) { val currentSession = SessionManager.currentSession.value - val accountId = currentSession!!.accountDetails!!.id + val accountId = currentSession!!.accountDetails.value!!.id CoroutineScope(Dispatchers.IO).launch { val response = AccountService().addToWatchlist(accountId, WatchlistBody(type, itemId, !itemIsWatchlisted.value)) if (response.isSuccessful) { @@ -1293,7 +1293,7 @@ private fun addToFavorite( onFavoriteChanged: (Boolean) -> Unit ) { val currentSession = SessionManager.currentSession.value - val accountId = currentSession!!.accountDetails!!.id + val accountId = currentSession!!.accountDetails.value!!.id CoroutineScope(Dispatchers.IO).launch { val response = AccountService().markAsFavorite(accountId, MarkAsFavoriteBody(type, itemId, !itemIsFavorited.value)) if (response.isSuccessful) { diff --git a/app/src/main/java/com/owenlejeune/tvtime/utils/SessionManager.kt b/app/src/main/java/com/owenlejeune/tvtime/utils/SessionManager.kt index 8f4f687..3f72800 100644 --- a/app/src/main/java/com/owenlejeune/tvtime/utils/SessionManager.kt +++ b/app/src/main/java/com/owenlejeune/tvtime/utils/SessionManager.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.widget.Toast +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import com.google.gson.annotations.SerializedName 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.V4AccountList import com.owenlejeune.tvtime.preferences.AppPreferences +import com.owenlejeune.tvtime.ui.navigation.AccountTabNavItem import com.owenlejeune.tvtime.ui.screens.main.MediaViewType import kotlinx.coroutines.CoroutineScope 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 = "") { - protected open var _ratedMovies: List = emptyList() - val ratedMovies: List - get() = _ratedMovies +// protected open var _ratedMovies: List = emptyList() +// val ratedMovies: List +// get() = _ratedMovies - protected open var _ratedTvShows: List = emptyList() - val ratedTvShows: List - get() = _ratedTvShows + val ratedMovies = mutableStateListOf() - protected open var _ratedTvEpisodes: List = emptyList() - val ratedTvEpisodes: List - get() = _ratedTvEpisodes +// protected open var _ratedTvShows: List = emptyList() +// val ratedTvShows: List +// get() = _ratedTvShows + val ratedTvShows = mutableStateListOf() - protected open var _accountDetails: AccountDetails? = null - val accountDetails: AccountDetails? - get() = _accountDetails +// protected open var _ratedTvEpisodes: List = emptyList() +// val ratedTvEpisodes: List +// get() = _ratedTvEpisodes + val ratedTvEpisodes = mutableStateListOf() - protected open var _accountLists: List = emptyList() - val accountLists: List - get() = _accountLists +// protected open var _accountDetails: AccountDetails? = null +// val accountDetails: AccountDetails? +// get() = _accountDetails + val accountDetails = mutableStateOf(null) - protected open var _favoriteMovies: List = emptyList() - val favoriteMovies: List - get() = _favoriteMovies +// protected open var _accountLists: List = emptyList() +// val accountLists: List +// get() = _accountLists + val accountLists = mutableStateListOf() - protected open var _favoriteTvShows: List = emptyList() - val favoriteTvShows: List - get() = _favoriteTvShows +// protected open var _favoriteMovies: List = emptyList() +// val favoriteMovies: List +// get() = _favoriteMovies + val favoriteMovies = mutableStateListOf() - protected open var _movieWatchlist: List = emptyList() - val movieWatchlist: List - get() = _movieWatchlist +// protected open var _favoriteTvShows: List = emptyList() +// val favoriteTvShows: List +// get() = _favoriteTvShows + val favoriteTvShows = mutableStateListOf() - protected open var _tvWatchlist: List = emptyList() - val tvWatchlist: List - get() = _tvWatchlist +// protected open var _movieWatchlist: List = emptyList() +// val movieWatchlist: List +// get() = _movieWatchlist + val movieWatchlist = mutableStateListOf() + +// protected open var _tvWatchlist: List = emptyList() +// val tvWatchlist: List +// get() = _tvWatchlist + val tvWatchlist = mutableStateListOf() fun hasRatedMovie(id: Int): Boolean { return ratedMovies.map { it.id }.contains(id) @@ -249,13 +261,13 @@ object SessionManager: KoinComponent { if (changed.contains(Changed.AccountDetails)) { val response = service.getAccountDetails() if (response.isSuccessful) { - _accountDetails = response.body() ?: _accountDetails - accountDetails?.let { + accountDetails.value = response.body() + accountDetails.value?.let { refreshWithAccountId(it.id, changed) } } - } else if (accountDetails != null) { - refreshWithAccountId(accountDetails!!.id, changed) + } else if (accountDetails.value != null) { + refreshWithAccountId(accountDetails.value!!.id, changed) } } @@ -264,7 +276,10 @@ object SessionManager: KoinComponent { serviceV4.getLists(preferences.authorizedSessionValues?.accountId ?: "").apply { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { 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 { if (isSuccessful) { withContext(Dispatchers.Main) { - _tvWatchlist = body()?.results ?: _tvWatchlist + body()?.results?.let { + tvWatchlist.clear() + tvWatchlist.addAll(it) + } } } }