Skip to content

Commit

Permalink
Fix account bug. Fixes #229
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Oct 7, 2022
1 parent 9da5433 commit 62a70a1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ fun getCurrentAccount(accountViewModel: AccountViewModel): Account? {
return getCurrentAccount(accounts)
}

fun getCurrentAccount(accounts: List<Account>?): Account? {
fun getCurrentAccountSync(accountViewModel: AccountViewModel): Account? {
val accounts = accountViewModel.allAccountSync
return getCurrentAccount(accounts)
}

private fun getCurrentAccount(accounts: List<Account>?): Account? {
return accounts?.firstOrNull { it.current }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/com/jerboa/ui/components/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +38,7 @@ import kotlinx.coroutines.launch
@Composable
fun Drawer(
navController: NavController = rememberNavController(),
accounts: List<Account>? = null,
accountViewModel: AccountViewModel,
onSwitchAccountClick: (account: Account) -> Unit,
onSignOutClick: () -> Unit,
onClickListingType: (ListingType) -> Unit,
Expand All @@ -59,7 +60,7 @@ fun Drawer(
Divider()
// Drawer items
DrawerContent(
accounts = accounts,
accountViewModel = accountViewModel,
unreadCounts = unreadCounts,
myUserInfo = myUserInfo,
showAccountAddMode = showAccountAddMode,
Expand All @@ -79,7 +80,7 @@ fun Drawer(
fun DrawerContent(
showAccountAddMode: Boolean,
navController: NavController,
accounts: List<Account>?,
accountViewModel: AccountViewModel,
onSwitchAccountClick: (account: Account) -> Unit,
onSignOutClick: () -> Unit,
onClickListingType: (ListingType) -> Unit,
Expand All @@ -97,7 +98,7 @@ fun DrawerContent(
exit = shrinkVertically()
) {
DrawerAddAccountMode(
accounts = accounts,
accountViewModel = accountViewModel,
navController = navController,
onSwitchAccountClick = onSwitchAccountClick,
onSignOutClick = onSignOutClick
Expand Down Expand Up @@ -248,12 +249,12 @@ fun DrawerItemsMainPreview() {
@Composable
fun DrawerAddAccountMode(
navController: NavController = rememberNavController(),
accounts: List<Account>? = 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 {
Expand All @@ -269,7 +270,7 @@ fun DrawerAddAccountMode(
onClick = { onSwitchAccountClick(it) }
)
}
accounts?.also {
currentAccount?.also {
IconAndTextDrawerItem(
text = "Sign Out",
icon = Icons.Default.Close,
Expand All @@ -284,7 +285,8 @@ fun DrawerAddAccountMode(
fun DrawerAddAccountModePreview() {
DrawerAddAccountMode(
onSignOutClick = {},
onSwitchAccountClick = {}
onSwitchAccountClick = {},
accountViewModel = null
)
}

Expand Down
20 changes: 8 additions & 12 deletions app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
)
},
Expand Down Expand Up @@ -246,19 +241,20 @@ fun MainPostListingsContent(
@Composable
fun MainDrawer(
siteViewModel: SiteViewModel,
accounts: List<Account>?,
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()
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 62a70a1

Please sign in to comment.