From 32963808f442be1a2b37caad1f582238cc042d85 Mon Sep 17 00:00:00 2001 From: "maarten.vercruysse" Date: Thu, 10 Aug 2023 19:07:18 +0200 Subject: [PATCH 1/5] Refactor sort type handling, and fetching initData/Posts --- app/build.gradle.kts | 1 + app/src/main/java/com/jerboa/MainActivity.kt | 13 +--- app/src/main/java/com/jerboa/Utils.kt | 52 ++------------ .../main/java/com/jerboa/db/entity/Account.kt | 6 +- .../jerboa/feat/AccountVerificationState.kt | 4 +- .../jerboa/model/AccountSettingsViewModel.kt | 4 +- .../java/com/jerboa/model/AccountViewModel.kt | 15 +--- .../java/com/jerboa/model/HomeViewModel.kt | 29 ++------ .../java/com/jerboa/model/PostViewModel.kt | 11 +-- .../java/com/jerboa/model/SiteViewModel.kt | 69 ++++++++++++++----- .../components/community/CommunityActivity.kt | 26 ++++--- .../community/list/CommunityListActivity.kt | 3 +- .../ui/components/drawer/DrawerActivity.kt | 27 +------- .../ui/components/home/BottomNavActivity.kt | 11 --- .../jerboa/ui/components/home/HomeActivity.kt | 5 ++ .../person/PersonProfileActivity.kt | 11 +-- .../settings/crashlogs/CrashLogsActivity.kt | 17 ++--- 17 files changed, 115 insertions(+), 189 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5ce2925a7..c21bee2bb 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -123,6 +123,7 @@ dependencies { // LiveData implementation("androidx.compose.runtime:runtime-livedata:1.4.3") implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1") + implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1") // Images implementation("io.coil-kt:coil-compose:2.4.0") diff --git a/app/src/main/java/com/jerboa/MainActivity.kt b/app/src/main/java/com/jerboa/MainActivity.kt index 211a46aed..b0fd8e244 100644 --- a/app/src/main/java/com/jerboa/MainActivity.kt +++ b/app/src/main/java/com/jerboa/MainActivity.kt @@ -17,7 +17,6 @@ import androidx.compose.animation.slideOutHorizontally import androidx.compose.material3.DrawerValue import androidx.compose.material3.rememberDrawerState import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf @@ -55,9 +54,7 @@ import com.jerboa.ui.components.common.MarkdownHelper import com.jerboa.ui.components.common.Route import com.jerboa.ui.components.common.ShowChangelog import com.jerboa.ui.components.common.ShowOutdatedServerDialog -import com.jerboa.ui.components.common.SpecialAccount import com.jerboa.ui.components.common.SwipeToNavigateBack -import com.jerboa.ui.components.common.getSpecialCurrentAccount import com.jerboa.ui.components.community.CommunityActivity import com.jerboa.ui.components.community.list.CommunityListActivity import com.jerboa.ui.components.community.sidebar.CommunitySidebarActivity @@ -82,7 +79,7 @@ import com.jerboa.ui.components.settings.lookandfeel.LookAndFeelActivity import com.jerboa.ui.theme.JerboaTheme class MainActivity : AppCompatActivity() { - val siteViewModel by viewModels() + val siteViewModel by viewModels(factoryProducer = { SiteViewModel.Factory }) val accountViewModel by viewModels(factoryProducer = { AccountViewModelFactory.Factory }) private val appSettingsViewModel by viewModels(factoryProducer = { AppSettingsViewModelFactory.Factory }) private val accountSettingsViewModel by viewModels(factoryProducer = { AccountSettingsViewModelFactory.Factory }) @@ -105,14 +102,6 @@ class MainActivity : AppCompatActivity() { null } - val account = getSpecialCurrentAccount(accountViewModel) - - LaunchedEffect(account) { - if (account !== SpecialAccount) { - fetchInitialData(account, siteViewModel) - } - } - val appSettings by appSettingsViewModel.appSettings.observeAsState(APP_SETTINGS_DEFAULT) @Suppress("SENSELESS_COMPARISON") diff --git a/app/src/main/java/com/jerboa/Utils.kt b/app/src/main/java/com/jerboa/Utils.kt index 952c9fb46..9adea58fc 100644 --- a/app/src/main/java/com/jerboa/Utils.kt +++ b/app/src/main/java/com/jerboa/Utils.kt @@ -67,14 +67,9 @@ import com.google.gson.reflect.TypeToken import com.jerboa.api.API import com.jerboa.api.API.Companion.checkIfLemmyInstance import com.jerboa.api.ApiState -import com.jerboa.api.DEFAULT_INSTANCE import com.jerboa.datatypes.types.* import com.jerboa.db.APP_SETTINGS_DEFAULT -import com.jerboa.db.entity.Account import com.jerboa.db.entity.AppSettings -import com.jerboa.db.entity.isAnon -import com.jerboa.model.HomeViewModel -import com.jerboa.model.SiteViewModel import com.jerboa.ui.components.common.Route import com.jerboa.ui.components.inbox.InboxTab import com.jerboa.ui.components.person.UserTab @@ -753,48 +748,6 @@ fun siFormat(num: Int): String { formattedNumber } } - -fun fetchInitialData( - account: Account, - siteViewModel: SiteViewModel, -) { - if (!account.isAnon()) { - API.changeLemmyInstance(account.instance) - siteViewModel.fetchUnreadCounts(GetUnreadCount(auth = account.jwt)) - } else { - API.changeLemmyInstance(DEFAULT_INSTANCE) - } - - siteViewModel.getSite( - GetSite( - auth = account.jwt.ifEmpty { null }, - ), - ) -} - -fun fetchHomePosts(account: Account, homeViewModel: HomeViewModel) { - if (!account.isAnon()) { - homeViewModel.updateFromAccount(account) - homeViewModel.resetPage() - homeViewModel.getPosts( - GetPosts( - type_ = homeViewModel.listingType, - sort = homeViewModel.sortType, - auth = account.jwt, - ), - ) - } else { - Log.d("jerboa", "Fetching posts for anonymous user") - homeViewModel.resetPage() - homeViewModel.getPosts( - GetPosts( - type_ = ListingType.Local, - sort = SortType.Active, - ), - ) - } -} - fun imageInputStreamFromUri(ctx: Context, uri: Uri): InputStream { return ctx.contentResolver.openInputStream(uri)!! } @@ -1505,6 +1458,11 @@ inline fun > Int.toEnum(): T { return enumValues()[this] } +inline fun > Int.toEnumSafe(): T { + val vals = enumValues() + return if (vals.size >= this) vals[this] else vals[0] +} + fun matchLoginErrorMsgToStringRes(ctx: Context, e: Throwable): String { return when (e.message) { "incorrect_login" -> ctx.getString(R.string.login_view_model_incorrect_login) diff --git a/app/src/main/java/com/jerboa/db/entity/Account.kt b/app/src/main/java/com/jerboa/db/entity/Account.kt index eee3eee86..7baf83e92 100644 --- a/app/src/main/java/com/jerboa/db/entity/Account.kt +++ b/app/src/main/java/com/jerboa/db/entity/Account.kt @@ -35,7 +35,7 @@ val AnonAccount = Account( "", "Anonymous", "", - 0, + 1, 0, verificationState = 0, ) @@ -47,3 +47,7 @@ fun Account.isAnon(): Boolean { fun Account.isReady(): Boolean { return this.verificationState == AccountVerificationState.CHECKS_COMPLETE.ordinal } + +fun Account.getJWT(): String? { + return if (isAnon()) null else this.jwt +} diff --git a/app/src/main/java/com/jerboa/feat/AccountVerificationState.kt b/app/src/main/java/com/jerboa/feat/AccountVerificationState.kt index 62fe40a92..8fe686d3d 100644 --- a/app/src/main/java/com/jerboa/feat/AccountVerificationState.kt +++ b/app/src/main/java/com/jerboa/feat/AccountVerificationState.kt @@ -425,12 +425,12 @@ suspend fun Account.isReadyAndIfNotDisplayInfo( } AccountVerificationState.ACCOUNT_DELETED to CheckState.Failed -> { - accountVM.deleteAccountAndSwapCurrent(this, siteVM, swapToAnon = true) + accountVM.deleteAccountAndSwapCurrent(this, swapToAnon = true) appState.toHome() } AccountVerificationState.JWT_VERIFIED to CheckState.Failed -> { - accountVM.deleteAccountAndSwapCurrent(this, siteVM, swapToAnon = true) + accountVM.deleteAccountAndSwapCurrent(this, swapToAnon = true) appState.toLogin() } diff --git a/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt b/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt index e8478b545..ae9280d18 100644 --- a/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt +++ b/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt @@ -39,9 +39,7 @@ class AccountSettingsViewModel( GetSite(auth = account.jwt), ) - val newAccount = maybeUpdateAccountSettings(account, form) - - siteViewModel.updateFromAccount(newAccount) + maybeUpdateAccountSettings(account, form) } } diff --git a/app/src/main/java/com/jerboa/model/AccountViewModel.kt b/app/src/main/java/com/jerboa/model/AccountViewModel.kt index 042207d55..442030255 100644 --- a/app/src/main/java/com/jerboa/model/AccountViewModel.kt +++ b/app/src/main/java/com/jerboa/model/AccountViewModel.kt @@ -6,13 +6,10 @@ import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import com.jerboa.db.entity.Account -import com.jerboa.db.entity.AnonAccount import com.jerboa.db.entity.isAnon import com.jerboa.db.entity.isReady import com.jerboa.db.repository.AccountRepository import com.jerboa.feat.AccountVerificationState -import com.jerboa.fetchHomePosts -import com.jerboa.fetchInitialData import com.jerboa.jerboaApplication import kotlinx.coroutines.launch @@ -54,8 +51,6 @@ class AccountViewModel(private val repository: AccountRepository) : ViewModel() fun deleteAccountAndSwapCurrent( account: Account, - siteViewModel: SiteViewModel, - homeViewModel: HomeViewModel? = null, swapToAnon: Boolean = false, ) = viewModelScope.launch { if (account.isAnon()) return@launch @@ -63,18 +58,10 @@ class AccountViewModel(private val repository: AccountRepository) : ViewModel() repository.delete(account) val accounts = repository.allAccounts.value - var nextAcc = accounts?.firstOrNull { it.id != account.id } + val nextAcc = accounts?.firstOrNull { it.id != account.id } if (!swapToAnon && nextAcc != null) { repository.setCurrent(nextAcc.id) - } else { - nextAcc = AnonAccount - } - - fetchInitialData(nextAcc, siteViewModel) - - if (homeViewModel != null) { - fetchHomePosts(nextAcc, homeViewModel) } } } diff --git a/app/src/main/java/com/jerboa/model/HomeViewModel.kt b/app/src/main/java/com/jerboa/model/HomeViewModel.kt index f05391000..d8a8775f7 100644 --- a/app/src/main/java/com/jerboa/model/HomeViewModel.kt +++ b/app/src/main/java/com/jerboa/model/HomeViewModel.kt @@ -18,7 +18,6 @@ import com.jerboa.datatypes.types.BlockPerson import com.jerboa.datatypes.types.BlockPersonResponse import com.jerboa.datatypes.types.CreatePostLike import com.jerboa.datatypes.types.DeletePost -import com.jerboa.datatypes.types.GetPostResponse import com.jerboa.datatypes.types.GetPosts import com.jerboa.datatypes.types.GetPostsResponse import com.jerboa.datatypes.types.ListingType @@ -28,6 +27,7 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType import com.jerboa.db.entity.Account +import com.jerboa.db.entity.getJWT import com.jerboa.findAndUpdatePost import com.jerboa.mergePosts import com.jerboa.serializeToMap @@ -42,14 +42,11 @@ class HomeViewModel : ViewModel(), Initializable { var postsRes: ApiState by mutableStateOf(ApiState.Empty) private set - private var postRes: ApiState by mutableStateOf(ApiState.Empty) - private var likePostRes: ApiState by mutableStateOf(ApiState.Empty) private var savePostRes: ApiState by mutableStateOf(ApiState.Empty) private var deletePostRes: ApiState by mutableStateOf(ApiState.Empty) private var blockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) private var blockPersonRes: ApiState by mutableStateOf(ApiState.Empty) - private var markPostRes: ApiState by mutableStateOf(ApiState.Empty) val lazyListState = LazyListState() @@ -83,10 +80,9 @@ class HomeViewModel : ViewModel(), Initializable { fun getPosts(form: GetPosts, state: ApiState = ApiState.Loading) { viewModelScope.launch { postsRes = state - postsRes = - apiWrapper( - API.getInstance().getPosts(form.serializeToMap()), - ) + postsRes = apiWrapper( + API.getInstance().getPosts(form.serializeToMap()), + ) } } @@ -184,13 +180,6 @@ class HomeViewModel : ViewModel(), Initializable { } } - fun updateFromAccount(account: Account) { - updateSortType(SortType.values().getOrElse(account.defaultSortType) { sortType }) - updateListingType( - ListingType.values().getOrElse(account.defaultListingType) { listingType }, - ) - } - fun updatePost(postView: PostView) { when (val existing = postsRes) { is ApiState.Success -> { @@ -207,10 +196,9 @@ class HomeViewModel : ViewModel(), Initializable { resetPage() getPosts( GetPosts( - page = page, sort = sortType, type_ = listingType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) } @@ -222,7 +210,7 @@ class HomeViewModel : ViewModel(), Initializable { page = page, sort = sortType, type_ = listingType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ApiState.Refreshing, ) @@ -242,10 +230,7 @@ class HomeViewModel : ViewModel(), Initializable { appState: JerboaAppState, ) { appState.coroutineScope.launch { - markPostRes = ApiState.Loading - markPostRes = apiWrapper(API.getInstance().markAsRead(form)) - - when (val markRes = markPostRes) { + when (val markRes = apiWrapper(API.getInstance().markAsRead(form))) { is ApiState.Success -> { updatePost(markRes.data.post_view) } diff --git a/app/src/main/java/com/jerboa/model/PostViewModel.kt b/app/src/main/java/com/jerboa/model/PostViewModel.kt index 6fa5834c5..ddc8b9a4a 100644 --- a/app/src/main/java/com/jerboa/model/PostViewModel.kt +++ b/app/src/main/java/com/jerboa/model/PostViewModel.kt @@ -35,6 +35,7 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost import com.jerboa.db.entity.Account +import com.jerboa.db.entity.getJWT import com.jerboa.findAndUpdateComment import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast @@ -92,9 +93,9 @@ class PostViewModel : ViewModel(), Initializable { id?.also { id -> val postForm = id.fold({ - GetPost(id = it, auth = account.jwt.ifEmpty { null }) + GetPost(id = it, auth = account.getJWT()) }, { - GetPost(comment_id = it, auth = account.jwt.ifEmpty { null }) + GetPost(comment_id = it, auth = account.getJWT()) }) postRes = state @@ -105,7 +106,7 @@ class PostViewModel : ViewModel(), Initializable { max_depth = COMMENTS_DEPTH_MAX, type_ = ListingType.All, post_id = it, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), sort = sortType, ) }, { @@ -113,7 +114,7 @@ class PostViewModel : ViewModel(), Initializable { max_depth = COMMENTS_DEPTH_MAX, type_ = ListingType.All, parent_id = it, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), sort = sortType, ) }) @@ -144,7 +145,7 @@ class PostViewModel : ViewModel(), Initializable { parent_id = commentView.comment.id, max_depth = COMMENTS_DEPTH_MAX, type_ = ListingType.All, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ) val moreComments = diff --git a/app/src/main/java/com/jerboa/model/SiteViewModel.kt b/app/src/main/java/com/jerboa/model/SiteViewModel.kt index 65ee5c577..6b75f4a82 100644 --- a/app/src/main/java/com/jerboa/model/SiteViewModel.kt +++ b/app/src/main/java/com/jerboa/model/SiteViewModel.kt @@ -1,26 +1,32 @@ package com.jerboa.model +import android.util.Log import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel +import androidx.lifecycle.asFlow import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory import com.jerboa.api.API import com.jerboa.api.ApiState +import com.jerboa.api.DEFAULT_INSTANCE import com.jerboa.api.apiWrapper import com.jerboa.datatypes.types.GetSite import com.jerboa.datatypes.types.GetSiteResponse import com.jerboa.datatypes.types.GetUnreadCount import com.jerboa.datatypes.types.GetUnreadCountResponse -import com.jerboa.datatypes.types.ListingType -import com.jerboa.datatypes.types.SortType -import com.jerboa.db.entity.Account +import com.jerboa.db.entity.AnonAccount +import com.jerboa.db.entity.isAnon +import com.jerboa.db.repository.AccountRepository +import com.jerboa.jerboaApplication import com.jerboa.serializeToMap import kotlinx.coroutines.Job import kotlinx.coroutines.launch -class SiteViewModel : ViewModel() { +class SiteViewModel(private val accountRepository: AccountRepository) : ViewModel() { // Can't be private, because it needs to be set by the login viewmodel var siteRes: ApiState by mutableStateOf(ApiState.Empty) @@ -29,22 +35,30 @@ class SiteViewModel : ViewModel() { val unreadCount by derivedStateOf { getUnreadCountTotal(unreadCountRes) } - var sortType by mutableStateOf(SortType.Active) - private set - var listingType by mutableStateOf(ListingType.Local) - private set + init { + viewModelScope.launch { + accountRepository.currentAccount.asFlow().collect { + Log.d("currAcc", "$it") - fun updateSortType(sortType: SortType) { - this.sortType = sortType - } + val acc = it ?: AnonAccount - fun updateListingType(listingType: ListingType) { - this.listingType = listingType - } + if (acc.isAnon()) { + API.changeLemmyInstance(DEFAULT_INSTANCE) + } else { + API.changeLemmyInstance(acc.instance) + } + + getSite( + GetSite( + auth = it?.jwt, + ), + ) - fun updateFromAccount(account: Account) { - updateSortType(SortType.values().getOrElse(account.defaultSortType) { sortType }) - updateListingType(ListingType.values().getOrElse(account.defaultListingType) { listingType }) + if (it != null) { + fetchUnreadCounts(GetUnreadCount(auth = it.jwt)) + } + } + } } fun getSite( @@ -57,8 +71,17 @@ class SiteViewModel : ViewModel() { when (val res = siteRes) { is ApiState.Success -> { res.data.my_user?.local_user_view?.local_user?.let { - updateSortType(it.default_sort_type) - updateListingType(it.default_listing_type) + val currAcc = accountRepository.currentAccount.value + if (currAcc != null) { + val newAccount = currAcc.copy( + defaultListingType = it.default_listing_type.ordinal, + defaultSortType = it.default_sort_type.ordinal, + ) + + if (currAcc != newAccount) { + accountRepository.update(newAccount) + } + } } } else -> {} @@ -123,4 +146,12 @@ class SiteViewModel : ViewModel() { else -> true } } + + companion object { + val Factory = viewModelFactory { + initializer { + SiteViewModel(jerboaApplication().container.accountRepository) + } + } + } } diff --git a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt index 2fd8d774f..965bdd511 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt @@ -19,6 +19,7 @@ import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.rememberTopAppBarState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment @@ -48,8 +49,8 @@ import com.jerboa.datatypes.types.GetSite import com.jerboa.datatypes.types.MarkPostAsRead import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost -import com.jerboa.datatypes.types.SortType import com.jerboa.datatypes.types.SubscribedType +import com.jerboa.db.entity.getJWT import com.jerboa.db.entity.isAnon import com.jerboa.feat.doIfReadyElseDisplayInfo import com.jerboa.hostName @@ -61,6 +62,7 @@ import com.jerboa.newVote import com.jerboa.rootChannel import com.jerboa.scrollToTop import com.jerboa.shareLink +import com.jerboa.toEnumSafe import com.jerboa.ui.components.common.ApiEmptyText import com.jerboa.ui.components.common.ApiErrorText import com.jerboa.ui.components.common.JerboaSnackbarHost @@ -111,21 +113,23 @@ fun CommunityActivity( if (communityViewModel.initialized) communityViewModel.updatePost(pv) } + LaunchedEffect(account) { + if (!account.isAnon()) { + communityViewModel.updateSortType(account.defaultSortType.toEnumSafe()) + } + } + InitializeRoute(communityViewModel) { val communityId = communityArg.fold({ it }, { null }) val communityName = communityArg.fold({ null }, { it }) communityViewModel.resetPage() - if (account.isAnon()) { - communityViewModel.updateSortType(SortType.values().getOrElse(account.defaultSortType) { siteViewModel.sortType }) - } - communityViewModel.getCommunity( form = GetCommunity( id = communityId, name = communityName, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) communityViewModel.getPosts( @@ -135,7 +139,7 @@ fun CommunityActivity( community_name = communityName, page = communityViewModel.page, sort = communityViewModel.sortType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) } @@ -152,7 +156,7 @@ fun CommunityActivity( community_id = communityRes.data.community_view.community.id, page = communityViewModel.page, sort = communityViewModel.sortType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ApiState.Refreshing, ) @@ -194,7 +198,7 @@ fun CommunityActivity( community_id = communityId, page = communityViewModel.page, sort = communityViewModel.sortType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) }, @@ -210,7 +214,7 @@ fun CommunityActivity( community_id = communityId, page = communityViewModel.page, sort = communityViewModel.sortType, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) }, @@ -447,7 +451,7 @@ fun CommunityActivity( is ApiState.Success -> { communityViewModel.appendPosts( communityRes.data.community_view.community.id, - account.jwt.ifEmpty { null }, + account.getJWT(), ) } diff --git a/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListActivity.kt b/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListActivity.kt index f7918b30a..f929d648c 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListActivity.kt @@ -21,6 +21,7 @@ import com.jerboa.api.ApiState import com.jerboa.datatypes.types.Search import com.jerboa.datatypes.types.SearchType import com.jerboa.datatypes.types.SortType +import com.jerboa.db.entity.getJWT import com.jerboa.model.AccountViewModel import com.jerboa.model.CommunityListViewModel import com.jerboa.model.SiteViewModel @@ -82,7 +83,7 @@ fun CommunityListActivity( q = search, type_ = SearchType.Communities, sort = SortType.TopAll, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) } diff --git a/app/src/main/java/com/jerboa/ui/components/drawer/DrawerActivity.kt b/app/src/main/java/com/jerboa/ui/components/drawer/DrawerActivity.kt index f62a9cdf8..9ea2fab8c 100644 --- a/app/src/main/java/com/jerboa/ui/components/drawer/DrawerActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/drawer/DrawerActivity.kt @@ -10,11 +10,8 @@ import androidx.compose.runtime.setValue import com.jerboa.api.ApiState import com.jerboa.closeDrawer import com.jerboa.datatypes.types.CommunityFollowerView -import com.jerboa.db.entity.AnonAccount import com.jerboa.db.entity.isAnon import com.jerboa.db.entity.isReady -import com.jerboa.fetchHomePosts -import com.jerboa.fetchInitialData import com.jerboa.model.AccountViewModel import com.jerboa.model.HomeViewModel import com.jerboa.model.SiteViewModel @@ -74,24 +71,11 @@ fun MainDrawer( accountViewModel.removeCurrent() accountViewModel.setCurrent(acct.id) - fetchInitialData( - account = acct, - siteViewModel = siteViewModel, - ) - fetchHomePosts( - account = acct, - homeViewModel = homeViewModel, - ) - onSelectTab(NavTab.Home) closeDrawer(scope, drawerState) }, onSignOutClick = { - accountViewModel.deleteAccountAndSwapCurrent( - account, - siteViewModel, - homeViewModel, - ) + accountViewModel.deleteAccountAndSwapCurrent(account) onSelectTab(NavTab.Home) closeDrawer(scope, drawerState) @@ -100,15 +84,6 @@ fun MainDrawer( if (!account.isAnon()) { accountViewModel.removeCurrent() - fetchInitialData( - account = AnonAccount, - siteViewModel = siteViewModel, - ) - fetchHomePosts( - account = AnonAccount, - homeViewModel = homeViewModel, - ) - onSelectTab(NavTab.Home) closeDrawer(scope, drawerState) } diff --git a/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt index d7bcb99dd..f6f6aacd8 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt @@ -41,10 +41,8 @@ import androidx.navigation.compose.rememberNavController import arrow.core.Either import com.jerboa.JerboaAppState import com.jerboa.R -import com.jerboa.api.ApiState import com.jerboa.db.entity.AppSettings import com.jerboa.feat.doIfReadyElseDisplayInfo -import com.jerboa.fetchHomePosts import com.jerboa.model.AccountViewModel import com.jerboa.model.AppSettingsViewModel import com.jerboa.model.HomeViewModel @@ -56,7 +54,6 @@ import com.jerboa.ui.components.community.list.CommunityListActivity import com.jerboa.ui.components.drawer.MainDrawer import com.jerboa.ui.components.inbox.InboxActivity import com.jerboa.ui.components.person.PersonProfileActivity -import com.jerboa.util.InitializeRoute import kotlinx.coroutines.launch enum class NavTab( @@ -155,14 +152,6 @@ fun BottomNavActivity( } } - if (siteViewModel.siteRes is ApiState.Success) { - InitializeRoute(homeViewModel) { - homeViewModel.updateSortType(siteViewModel.sortType) - homeViewModel.updateListingType(siteViewModel.listingType) - fetchHomePosts(account, homeViewModel) - } - } - ModalNavigationDrawer( gesturesEnabled = true, drawerState = drawerState, diff --git a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt index 0f3a4c0bb..b2eaf727c 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt @@ -63,6 +63,7 @@ import com.jerboa.newVote import com.jerboa.rootChannel import com.jerboa.scrollToTop import com.jerboa.shareLink +import com.jerboa.toEnumSafe import com.jerboa.ui.components.common.ApiEmptyText import com.jerboa.ui.components.common.ApiErrorText import com.jerboa.ui.components.common.JerboaSnackbarHost @@ -119,6 +120,10 @@ fun HomeActivity( if (!account.isAnon() && !account.isReady()) { account.doIfReadyElseDisplayInfo(appState, ctx, snackbarHostState, scope, siteViewModel, accountViewModel) {} } + homeViewModel.updateSortType(account.defaultSortType.toEnumSafe()) + homeViewModel.updateListingType(account.defaultListingType.toEnumSafe()) + Log.d("jerboa", "Fetching posts") + homeViewModel.resetPosts(account) } Scaffold( diff --git a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt index 4b40c345c..bf5532b58 100644 --- a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt @@ -69,6 +69,7 @@ import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType import com.jerboa.db.entity.Account +import com.jerboa.db.entity.getJWT import com.jerboa.db.entity.isAnon import com.jerboa.feat.doIfReadyElseDisplayInfo import com.jerboa.getLocalizedStringForUserTab @@ -174,7 +175,7 @@ fun PersonProfileActivity( person_id = personId, username = personName, sort = SortType.New, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), saved_only = savedMode, ), ) @@ -223,7 +224,7 @@ fun PersonProfileActivity( sort = personProfileViewModel.sortType, page = personProfileViewModel.page, saved_only = personProfileViewModel.savedOnly, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ) }, @@ -358,7 +359,7 @@ fun UserTabs( sort = personProfileViewModel.sortType, page = personProfileViewModel.page, saved_only = personProfileViewModel.savedOnly, - auth = account.jwt.ifEmpty { null }, + auth = account.getJWT(), ), ApiState.Refreshing, ) @@ -614,7 +615,7 @@ fun UserTabs( isScrolledToEnd = { personProfileViewModel.appendData( profileRes.data.person_view.person.id, - account.jwt.ifEmpty { null }, + account.getJWT(), ) }, account = account, @@ -697,7 +698,7 @@ fun UserTabs( LaunchedEffect(Unit) { personProfileViewModel.appendData( profileRes.data.person_view.person.id, - account.jwt.ifEmpty { null }, + account.getJWT(), ) } } diff --git a/app/src/main/java/com/jerboa/ui/components/settings/crashlogs/CrashLogsActivity.kt b/app/src/main/java/com/jerboa/ui/components/settings/crashlogs/CrashLogsActivity.kt index 317fb14a4..6336c97ee 100644 --- a/app/src/main/java/com/jerboa/ui/components/settings/crashlogs/CrashLogsActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/settings/crashlogs/CrashLogsActivity.kt @@ -1,6 +1,5 @@ package com.jerboa.ui.components.settings.crashlogs -import android.content.Context import android.util.Log import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column @@ -18,7 +17,6 @@ import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarDuration -import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -38,6 +36,7 @@ import com.crazylegend.crashyreporter.CrashyReporter import com.jerboa.R import com.jerboa.copyToClipboard import com.jerboa.showSnackbar +import com.jerboa.ui.components.common.JerboaSnackbarHost import com.jerboa.ui.components.common.SimpleTopAppBar import com.jerboa.ui.theme.MEDIUM_PADDING import com.jerboa.ui.theme.SMALL_PADDING @@ -48,23 +47,21 @@ fun CrashLogsActivity( ) { Log.d("jerboa", "Got to Crash log activity") - val ctx = LocalContext.current - val crashes = CrashyReporter.getLogsAsStrings()?.toMutableStateList() ?: run { mutableStateListOf() } - CrashLogs(ctx = ctx, onClickBack = onClickBack, crashes = crashes) + CrashLogs(onClickBack = onClickBack, crashes = crashes) } @OptIn(ExperimentalMaterial3Api::class) @Composable -fun CrashLogs(ctx: Context, onClickBack: () -> Unit, crashes: MutableList) { +fun CrashLogs(onClickBack: () -> Unit, crashes: MutableList) { val scope = rememberCoroutineScope() val snackbarHostState = remember { SnackbarHostState() } val deleteMessage = stringResource(R.string.crash_logs_all_deleted) Scaffold( - snackbarHost = { SnackbarHost(snackbarHostState) }, + snackbarHost = { JerboaSnackbarHost(snackbarHostState) }, topBar = { SimpleTopAppBar( text = stringResource(R.string.crash_logs), @@ -99,7 +96,7 @@ fun CrashLogs(ctx: Context, onClickBack: () -> Unit, crashes: MutableList - CrashLog(ctx = ctx, crash = crash) + CrashLog(crash = crash) } } }, @@ -107,9 +104,10 @@ fun CrashLogs(ctx: Context, onClickBack: () -> Unit, crashes: MutableList Date: Thu, 10 Aug 2023 19:58:25 +0200 Subject: [PATCH 2/5] Handle save usersetting failure --- .../jerboa/model/AccountSettingsViewModel.kt | 21 +++++++++++++++---- .../account/AccountSettingsActivity.kt | 4 +++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt b/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt index ae9280d18..7b98185d2 100644 --- a/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt +++ b/app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt @@ -1,5 +1,6 @@ package com.jerboa.model +import android.content.Context import androidx.compose.runtime.Stable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -17,6 +18,7 @@ import com.jerboa.datatypes.types.SaveUserSettings import com.jerboa.db.entity.Account import com.jerboa.db.repository.AccountRepository import com.jerboa.jerboaApplication +import com.jerboa.ui.components.common.apiErrorToast import kotlinx.coroutines.launch @Stable @@ -30,16 +32,27 @@ class AccountSettingsViewModel( form: SaveUserSettings, siteViewModel: SiteViewModel, account: Account, + ctx: Context, ) { viewModelScope.launch { saveUserSettingsRes = ApiState.Loading saveUserSettingsRes = apiWrapper(API.getInstance().saveUserSettings(form)) - siteViewModel.getSite( - GetSite(auth = account.jwt), - ) + when (val res = saveUserSettingsRes) { + is ApiState.Success -> { + siteViewModel.getSite( + GetSite(auth = account.jwt), + ) - maybeUpdateAccountSettings(account, form) + maybeUpdateAccountSettings(account, form) + } + + is ApiState.Failure -> { + apiErrorToast(ctx, res.msg) + } + + else -> {} + } } } diff --git a/app/src/main/java/com/jerboa/ui/components/settings/account/AccountSettingsActivity.kt b/app/src/main/java/com/jerboa/ui/components/settings/account/AccountSettingsActivity.kt index 83bcb6064..9b50318fb 100644 --- a/app/src/main/java/com/jerboa/ui/components/settings/account/AccountSettingsActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/settings/account/AccountSettingsActivity.kt @@ -8,6 +8,7 @@ import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHostState import androidx.compose.runtime.Composable import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import com.jerboa.R import com.jerboa.model.AccountSettingsViewModel @@ -25,7 +26,7 @@ fun AccountSettingsActivity( onBack: () -> Unit, ) { Log.d("jerboa", "Got to settings activity") - + val ctx = LocalContext.current val account = getCurrentAccount(accountViewModel = accountViewModel) val snackbarHostState = remember { SnackbarHostState() } @@ -42,6 +43,7 @@ fun AccountSettingsActivity( form, siteViewModel = siteViewModel, account = account, + ctx, ) }, siteViewModel = siteViewModel, From 4b846b8dbc4864cb081a3d463f9e98ca2ef0df9b Mon Sep 17 00:00:00 2001 From: "maarten.vercruysse" Date: Thu, 10 Aug 2023 20:18:19 +0200 Subject: [PATCH 3/5] Update log --- app/src/main/java/com/jerboa/model/SiteViewModel.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/com/jerboa/model/SiteViewModel.kt b/app/src/main/java/com/jerboa/model/SiteViewModel.kt index 6b75f4a82..47ffe9f7e 100644 --- a/app/src/main/java/com/jerboa/model/SiteViewModel.kt +++ b/app/src/main/java/com/jerboa/model/SiteViewModel.kt @@ -38,9 +38,8 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode init { viewModelScope.launch { accountRepository.currentAccount.asFlow().collect { - Log.d("currAcc", "$it") - val acc = it ?: AnonAccount + Log.d("Jerboa", "acc init for id: ${acc.id}") if (acc.isAnon()) { API.changeLemmyInstance(DEFAULT_INSTANCE) From 055488317e3a5164192f36454708f9aefa97167a Mon Sep 17 00:00:00 2001 From: "maarten.vercruysse" Date: Fri, 11 Aug 2023 18:02:57 +0200 Subject: [PATCH 4/5] Fix fetching posts too much --- .../java/com/jerboa/model/HomeViewModel.kt | 33 +++++++++++++++- .../java/com/jerboa/model/SiteViewModel.kt | 38 ++++++++++--------- .../ui/components/home/BottomNavActivity.kt | 2 +- .../jerboa/ui/components/home/HomeActivity.kt | 5 --- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/jerboa/model/HomeViewModel.kt b/app/src/main/java/com/jerboa/model/HomeViewModel.kt index d8a8775f7..edb323556 100644 --- a/app/src/main/java/com/jerboa/model/HomeViewModel.kt +++ b/app/src/main/java/com/jerboa/model/HomeViewModel.kt @@ -1,13 +1,17 @@ package com.jerboa.model import android.content.Context +import android.util.Log import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel +import androidx.lifecycle.asFlow import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory import com.jerboa.JerboaAppState import com.jerboa.api.API import com.jerboa.api.ApiState @@ -27,16 +31,21 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType import com.jerboa.db.entity.Account +import com.jerboa.db.entity.AnonAccount import com.jerboa.db.entity.getJWT +import com.jerboa.db.repository.AccountRepository import com.jerboa.findAndUpdatePost +import com.jerboa.jerboaApplication import com.jerboa.mergePosts import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast import com.jerboa.showBlockPersonToast +import com.jerboa.toEnumSafe import com.jerboa.util.Initializable +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch -class HomeViewModel : ViewModel(), Initializable { +class HomeViewModel(private val accountRepository: AccountRepository) : ViewModel(), Initializable { override var initialized by mutableStateOf(false) var postsRes: ApiState by mutableStateOf(ApiState.Empty) @@ -57,6 +66,20 @@ class HomeViewModel : ViewModel(), Initializable { var page by mutableIntStateOf(1) private set + init { + viewModelScope.launch { + accountRepository.currentAccount + .asFlow() + .map { it ?: AnonAccount } + .collect { account -> + updateSortType(account.defaultSortType.toEnumSafe()) + updateListingType(account.defaultListingType.toEnumSafe()) + Log.d("jerboa", "Fetching posts") + resetPosts(account) + } + } + } + fun updateSortType(sortType: SortType) { this.sortType = sortType } @@ -239,4 +262,12 @@ class HomeViewModel : ViewModel(), Initializable { } } } + + companion object { + val Factory = viewModelFactory { + initializer { + HomeViewModel(jerboaApplication().container.accountRepository) + } + } + } } diff --git a/app/src/main/java/com/jerboa/model/SiteViewModel.kt b/app/src/main/java/com/jerboa/model/SiteViewModel.kt index 47ffe9f7e..433278167 100644 --- a/app/src/main/java/com/jerboa/model/SiteViewModel.kt +++ b/app/src/main/java/com/jerboa/model/SiteViewModel.kt @@ -19,11 +19,13 @@ import com.jerboa.datatypes.types.GetSiteResponse import com.jerboa.datatypes.types.GetUnreadCount import com.jerboa.datatypes.types.GetUnreadCountResponse import com.jerboa.db.entity.AnonAccount +import com.jerboa.db.entity.getJWT import com.jerboa.db.entity.isAnon import com.jerboa.db.repository.AccountRepository import com.jerboa.jerboaApplication import com.jerboa.serializeToMap import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch class SiteViewModel(private val accountRepository: AccountRepository) : ViewModel() { @@ -37,26 +39,28 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode init { viewModelScope.launch { - accountRepository.currentAccount.asFlow().collect { - val acc = it ?: AnonAccount - Log.d("Jerboa", "acc init for id: ${acc.id}") - - if (acc.isAnon()) { - API.changeLemmyInstance(DEFAULT_INSTANCE) - } else { - API.changeLemmyInstance(acc.instance) - } + accountRepository.currentAccount + .asFlow() + .map { it ?: AnonAccount } + .collect { + Log.d("Jerboa", "acc init for id: ${it.id}") + + if (it.isAnon()) { + API.changeLemmyInstance(DEFAULT_INSTANCE) + } else { + API.changeLemmyInstance(it.instance) + } - getSite( - GetSite( - auth = it?.jwt, - ), - ) + getSite( + GetSite( + auth = it.getJWT(), + ), + ) - if (it != null) { - fetchUnreadCounts(GetUnreadCount(auth = it.jwt)) + if (!it.isAnon()) { + fetchUnreadCounts(GetUnreadCount(auth = it.jwt)) + } } - } } } diff --git a/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt index f6f6aacd8..053ba95e6 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/BottomNavActivity.kt @@ -113,7 +113,7 @@ fun BottomNavActivity( val account = getCurrentAccount(accountViewModel) val ctx = LocalContext.current val scope = rememberCoroutineScope() - val homeViewModel: HomeViewModel = viewModel() + val homeViewModel: HomeViewModel = viewModel(factory = HomeViewModel.Factory) val bottomNavController = rememberNavController() val snackbarHostState = remember(account) { SnackbarHostState() } diff --git a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt index b2eaf727c..0f3a4c0bb 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt @@ -63,7 +63,6 @@ import com.jerboa.newVote import com.jerboa.rootChannel import com.jerboa.scrollToTop import com.jerboa.shareLink -import com.jerboa.toEnumSafe import com.jerboa.ui.components.common.ApiEmptyText import com.jerboa.ui.components.common.ApiErrorText import com.jerboa.ui.components.common.JerboaSnackbarHost @@ -120,10 +119,6 @@ fun HomeActivity( if (!account.isAnon() && !account.isReady()) { account.doIfReadyElseDisplayInfo(appState, ctx, snackbarHostState, scope, siteViewModel, accountViewModel) {} } - homeViewModel.updateSortType(account.defaultSortType.toEnumSafe()) - homeViewModel.updateListingType(account.defaultListingType.toEnumSafe()) - Log.d("jerboa", "Fetching posts") - homeViewModel.resetPosts(account) } Scaffold( From 28c9522f690939eb81d71b0952d1ea422b32d89f Mon Sep 17 00:00:00 2001 From: "maarten.vercruysse" Date: Sun, 13 Aug 2023 23:26:56 +0200 Subject: [PATCH 5/5] Trigger woodpecker