From 62a70a15f8bfca4e2a5065f1a5a048f36e8f7f00 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 7 Oct 2022 12:09:03 -0400 Subject: [PATCH] Fix account bug. Fixes #229 --- app/src/main/java/com/jerboa/MainActivity.kt | 7 ++++--- .../ui/components/common/AccountHelpers.kt | 7 ++++++- .../components/community/CommunityActivity.kt | 4 +--- .../com/jerboa/ui/components/home/Home.kt | 20 ++++++++++--------- .../jerboa/ui/components/home/HomeActivity.kt | 20 ++++++++----------- .../ui/components/inbox/InboxActivity.kt | 4 +--- .../person/PersonProfileActivity.kt | 4 +--- 7 files changed, 32 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/jerboa/MainActivity.kt b/app/src/main/java/com/jerboa/MainActivity.kt index 8097baaf6..d55658106 100644 --- a/app/src/main/java/com/jerboa/MainActivity.kt +++ b/app/src/main/java/com/jerboa/MainActivity.kt @@ -27,6 +27,7 @@ import com.jerboa.ui.components.comment.edit.CommentEditViewModel import com.jerboa.ui.components.comment.reply.CommentReplyActivity import com.jerboa.ui.components.comment.reply.CommentReplyViewModel import com.jerboa.ui.components.common.getCurrentAccount +import com.jerboa.ui.components.common.getCurrentAccountSync import com.jerboa.ui.components.community.CommunityActivity import com.jerboa.ui.components.community.CommunityViewModel import com.jerboa.ui.components.community.list.CommunityListActivity @@ -82,14 +83,14 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - // TODO this is bad - val account = getCurrentAccount(accountViewModel.allAccountSync) - fetchInitialData(account, siteViewModel, homeViewModel) + val accountSync = getCurrentAccountSync(accountViewModel) + fetchInitialData(accountSync, siteViewModel, homeViewModel) setContent { JerboaTheme { val navController = rememberNavController() val ctx = LocalContext.current + val account = getCurrentAccount(accountViewModel) NavHost( navController = navController, diff --git a/app/src/main/java/com/jerboa/ui/components/common/AccountHelpers.kt b/app/src/main/java/com/jerboa/ui/components/common/AccountHelpers.kt index 12ecc6383..3d5b2afc9 100644 --- a/app/src/main/java/com/jerboa/ui/components/common/AccountHelpers.kt +++ b/app/src/main/java/com/jerboa/ui/components/common/AccountHelpers.kt @@ -12,6 +12,11 @@ fun getCurrentAccount(accountViewModel: AccountViewModel): Account? { return getCurrentAccount(accounts) } -fun getCurrentAccount(accounts: List?): Account? { +fun getCurrentAccountSync(accountViewModel: AccountViewModel): Account? { + val accounts = accountViewModel.allAccountSync + return getCurrentAccount(accounts) +} + +private fun getCurrentAccount(accounts: List?): Account? { return accounts?.firstOrNull { it.current } } 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 2d017acbd..8f8b405d9 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 @@ -9,7 +9,6 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -42,8 +41,7 @@ fun CommunityActivity( val postListState = rememberLazyListState() val scaffoldState = rememberScaffoldState() val ctx = LocalContext.current - val accounts by accountViewModel.allAccounts.observeAsState() - val account = getCurrentAccount(accounts = accounts) + val account = getCurrentAccount(accountViewModel) Surface(color = MaterialTheme.colors.background) { Scaffold( diff --git a/app/src/main/java/com/jerboa/ui/components/home/Home.kt b/app/src/main/java/com/jerboa/ui/components/home/Home.kt index 2fa74fa53..70277952d 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/Home.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/Home.kt @@ -26,6 +26,7 @@ import com.jerboa.datatypes.* import com.jerboa.datatypes.api.GetUnreadCountResponse import com.jerboa.datatypes.api.MyUserInfo import com.jerboa.db.Account +import com.jerboa.db.AccountViewModel import com.jerboa.ui.components.common.* import com.jerboa.ui.components.community.CommunityLinkLarger import com.jerboa.ui.components.person.PersonName @@ -37,7 +38,7 @@ import kotlinx.coroutines.launch @Composable fun Drawer( navController: NavController = rememberNavController(), - accounts: List? = null, + accountViewModel: AccountViewModel, onSwitchAccountClick: (account: Account) -> Unit, onSignOutClick: () -> Unit, onClickListingType: (ListingType) -> Unit, @@ -59,7 +60,7 @@ fun Drawer( Divider() // Drawer items DrawerContent( - accounts = accounts, + accountViewModel = accountViewModel, unreadCounts = unreadCounts, myUserInfo = myUserInfo, showAccountAddMode = showAccountAddMode, @@ -79,7 +80,7 @@ fun Drawer( fun DrawerContent( showAccountAddMode: Boolean, navController: NavController, - accounts: List?, + accountViewModel: AccountViewModel, onSwitchAccountClick: (account: Account) -> Unit, onSignOutClick: () -> Unit, onClickListingType: (ListingType) -> Unit, @@ -97,7 +98,7 @@ fun DrawerContent( exit = shrinkVertically() ) { DrawerAddAccountMode( - accounts = accounts, + accountViewModel = accountViewModel, navController = navController, onSwitchAccountClick = onSwitchAccountClick, onSignOutClick = onSignOutClick @@ -248,12 +249,12 @@ fun DrawerItemsMainPreview() { @Composable fun DrawerAddAccountMode( navController: NavController = rememberNavController(), - accounts: List? = null, + accountViewModel: AccountViewModel?, onSwitchAccountClick: (account: Account) -> Unit, onSignOutClick: () -> Unit ) { - val accountsWithoutCurrent = accounts?.toMutableList() - val currentAccount = getCurrentAccount(accounts) + val accountsWithoutCurrent = accountViewModel?.allAccounts?.value?.toMutableList() + val currentAccount = accountsWithoutCurrent?.firstOrNull { it.current } accountsWithoutCurrent?.remove(currentAccount) Column { @@ -269,7 +270,7 @@ fun DrawerAddAccountMode( onClick = { onSwitchAccountClick(it) } ) } - accounts?.also { + currentAccount?.also { IconAndTextDrawerItem( text = "Sign Out", icon = Icons.Default.Close, @@ -284,7 +285,8 @@ fun DrawerAddAccountMode( fun DrawerAddAccountModePreview() { DrawerAddAccountMode( onSignOutClick = {}, - onSwitchAccountClick = {} + onSwitchAccountClick = {}, + accountViewModel = null ) } 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 55fc43a9b..4eda4ce43 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 @@ -11,8 +11,6 @@ import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -46,8 +44,7 @@ fun HomeActivity( val postListState = rememberLazyListState() val scaffoldState = rememberScaffoldState() val ctx = LocalContext.current - val accounts by accountViewModel.allAccounts.observeAsState() - val account = getCurrentAccount(accounts = accounts) + val account = getCurrentAccount(accountViewModel) Surface(color = MaterialTheme.colors.background) { Scaffold( @@ -68,13 +65,11 @@ fun HomeActivity( drawerContent = { MainDrawer( siteViewModel = siteViewModel, - accounts = accounts, navController = navController, accountViewModel = accountViewModel, homeViewModel = homeViewModel, scope = scope, scaffoldState = scaffoldState, - account = account, ctx = ctx ) }, @@ -246,19 +241,20 @@ fun MainPostListingsContent( @Composable fun MainDrawer( siteViewModel: SiteViewModel, - accounts: List?, navController: NavController, accountViewModel: AccountViewModel, homeViewModel: HomeViewModel, scope: CoroutineScope, scaffoldState: ScaffoldState, - account: Account?, ctx: Context ) { + val accounts = accountViewModel.allAccounts.value + val account = getCurrentAccount(accountViewModel) + Drawer( myUserInfo = siteViewModel.siteRes?.my_user, unreadCounts = homeViewModel.unreadCountResponse, - accounts = accounts, + accountViewModel = accountViewModel, navController = navController, onSwitchAccountClick = { acct -> accountViewModel.removeCurrent() @@ -273,10 +269,10 @@ fun MainDrawer( closeDrawer(scope, scaffoldState) }, onSignOutClick = { - accounts?.also { accounts -> - getCurrentAccount(accounts)?.also { + accounts?.also { accts -> + account?.also { accountViewModel.delete(it) - val updatedList = accounts.toMutableList() + val updatedList = accts.toMutableList() updatedList.remove(it) if (updatedList.isNotEmpty()) { diff --git a/app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt b/app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt index 4cf7392b9..55e21520c 100644 --- a/app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt @@ -8,7 +8,6 @@ import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.* import androidx.compose.runtime.* -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -48,8 +47,7 @@ fun InboxActivity( val scope = rememberCoroutineScope() val scaffoldState = rememberScaffoldState() val ctx = LocalContext.current - val accounts by accountViewModel.allAccounts.observeAsState() - val account = getCurrentAccount(accounts = accounts) + val account = getCurrentAccount(accountViewModel) val unreadCount = homeViewModel.unreadCountResponse?.let { unreadCountTotal(it) } Surface(color = MaterialTheme.colors.background) { 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 43348ffb8..f0b299bff 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 @@ -9,7 +9,6 @@ import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.* import androidx.compose.runtime.* -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -60,8 +59,7 @@ fun PersonProfileActivity( val postListState = rememberLazyListState() val scaffoldState = rememberScaffoldState() val ctx = LocalContext.current - val accounts by accountViewModel.allAccounts.observeAsState() - val account = getCurrentAccount(accounts = accounts) + val account = getCurrentAccount(accountViewModel) val bottomAppBarScreen = if (savedMode) { "saved" } else { "profile" } Surface(color = MaterialTheme.colors.background) {