Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contenttypes to lazy columns #984

Merged
merged 9 commits into from
Jul 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into feat/add_contenttypes
# Conflicts:
#	app/src/main/java/com/jerboa/ui/components/common/Dialogs.kt
#	app/src/main/java/com/jerboa/ui/components/home/Home.kt
#	app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt
#	benchmarks/src/main/java/com/jerboa/actions/JerboaActions.kt
MV-GH committed Jul 19, 2023
commit ea9b08436294a536b55690b7a12e606e3b147b3d
Original file line number Diff line number Diff line change
@@ -163,6 +163,12 @@ fun SortOptionsDialog(
onClick = { onClickSortType(SortType.New) },
highlight = (selectedSortType == SortType.New),
)
IconAndTextDrawerItem(
text = stringResource(R.string.dialogs_old),
icon = Icons.Outlined.BrightnessHigh,
onClick = { onClickSortType(SortType.Old) },
highlight = (selectedSortType == SortType.Old),
)
IconAndTextDrawerItem(
modifier = Modifier.testTag("jerboa:sortoption_mostcomments"),
text = stringResource(R.string.dialogs_most_comments),
375 changes: 0 additions & 375 deletions app/src/main/java/com/jerboa/ui/components/home/Home.kt
Original file line number Diff line number Diff line change
@@ -46,381 +46,6 @@ import com.jerboa.ui.components.common.PostViewModeDialog
import com.jerboa.ui.components.common.SortOptionsDialog
import com.jerboa.ui.components.common.SortTopOptionsDialog
import com.jerboa.ui.theme.LARGE_PADDING
import com.jerboa.ui.theme.SMALL_PADDING
import com.jerboa.ui.theme.XL_PADDING
import com.jerboa.ui.theme.muted
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Composable
fun Drawer(
siteRes: ApiState<GetSiteResponse>,
unreadCount: Int,
navController: NavController = rememberNavController(),
accountViewModel: AccountViewModel,
onSwitchAccountClick: (account: Account) -> Unit,
onSignOutClick: () -> Unit,
onClickListingType: (ListingType) -> Unit,
onCommunityClick: (community: Community) -> Unit,
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSaved: () -> Unit,
onClickSettings: () -> Unit,
onClickCommunities: () -> Unit,
isOpen: Boolean,
blurNSFW: Boolean,
) {
var showAccountAddMode by rememberSaveable { mutableStateOf(false) }

if (!isOpen) showAccountAddMode = false

val myUserInfo = when (siteRes) {
is ApiState.Success -> siteRes.data.my_user
else -> null
}

DrawerHeader(
myPerson = myUserInfo?.local_user_view?.person,
showAccountAddMode = showAccountAddMode,
onClickShowAccountAddMode = { showAccountAddMode = !showAccountAddMode },
showAvatar = myUserInfo?.local_user_view?.local_user?.show_avatars ?: true,
)
// Drawer items
DrawerContent(
accountViewModel = accountViewModel,
unreadCount = unreadCount,
myUserInfo = myUserInfo,
showAccountAddMode = showAccountAddMode,
navController = navController,
onSwitchAccountClick = onSwitchAccountClick,
onSignOutClick = onSignOutClick,
onClickListingType = onClickListingType,
onCommunityClick = onCommunityClick,
onClickProfile = onClickProfile,
onClickInbox = onClickInbox,
onClickSaved = onClickSaved,
onClickSettings = onClickSettings,
onClickCommunities = onClickCommunities,
blurNSFW = blurNSFW,
)
}

@Composable
fun DrawerContent(
showAccountAddMode: Boolean,
navController: NavController,
accountViewModel: AccountViewModel,
onSwitchAccountClick: (account: Account) -> Unit,
onSignOutClick: () -> Unit,
onClickListingType: (ListingType) -> Unit,
onCommunityClick: (community: Community) -> Unit,
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSaved: () -> Unit,
onClickSettings: () -> Unit,
onClickCommunities: () -> Unit,
myUserInfo: MyUserInfo?,
unreadCount: Int,
blurNSFW: Boolean,
) {
AnimatedVisibility(
visible = showAccountAddMode,
enter = expandVertically(),
exit = shrinkVertically(),
) {
Divider()
DrawerAddAccountMode(
accountViewModel = accountViewModel,
navController = navController,
onSwitchAccountClick = onSwitchAccountClick,
onSignOutClick = onSignOutClick,
)
}

Divider()
DrawerItemsMain(
myUserInfo = myUserInfo,
onClickListingType = onClickListingType,
onCommunityClick = onCommunityClick,
onClickProfile = onClickProfile,
onClickInbox = onClickInbox,
onClickSaved = onClickSaved,
unreadCount = unreadCount,
onClickSettings = onClickSettings,
onClickCommunities = onClickCommunities,
blurNSFW = blurNSFW,
)
}

@Composable
fun DrawerItemsMain(
myUserInfo: MyUserInfo?,
onClickSaved: () -> Unit,
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSettings: () -> Unit,
onClickCommunities: () -> Unit,
onClickListingType: (ListingType) -> Unit,
onCommunityClick: (community: Community) -> Unit,
unreadCount: Int,
blurNSFW: Boolean,
) {
val listState = rememberLazyListState()

val follows = myUserInfo?.follows

LazyColumn(
state = listState,
modifier = Modifier.simpleVerticalScrollbar(listState),
) {
if (!follows.isNullOrEmpty()) {
item {
IconAndTextDrawerItem(
text = stringResource(R.string.home_subscribed),
icon = Icons.Outlined.Bookmarks,
onClick = { onClickListingType(ListingType.Subscribed) },
)
}
}
item {
IconAndTextDrawerItem(
text = stringResource(R.string.home_local),
icon = Icons.Outlined.LocationCity,
onClick = { onClickListingType(ListingType.Local) },
)
}
item {
IconAndTextDrawerItem(
text = stringResource(R.string.home_all),
icon = Icons.Outlined.Public,
onClick = { onClickListingType(ListingType.All) },
)
}
item {
myUserInfo?.also {
IconAndTextDrawerItem(
text = stringResource(R.string.home_saved),
icon = Icons.Outlined.Bookmarks,
onClick = onClickSaved,
)
}
}
item {
IconAndTextDrawerItem(
text = stringResource(R.string.home_communities),
icon = Icons.Outlined.List,
onClick = onClickCommunities,
)
}
item {
Divider()
}
item {
myUserInfo?.also {
IconAndTextDrawerItem(
text = stringResource(R.string.home_profile),
icon = Icons.Outlined.Person,
onClick = onClickProfile,
)
}
}
item {
myUserInfo?.also {
IconAndTextDrawerItem(
text = stringResource(R.string.home_inbox),
icon = Icons.Outlined.Email,
onClick = onClickInbox,
iconBadgeCount = unreadCount,
)
}
}
item {
IconAndTextDrawerItem(
text = stringResource(R.string.home_settings),
icon = Icons.Outlined.Settings,
onClick = onClickSettings,
)
}
item {
myUserInfo?.also {
Divider()
}
}

follows?.also { follows ->
item {
Text(
text = stringResource(R.string.home_subscriptions),
modifier = Modifier.padding(LARGE_PADDING),
color = MaterialTheme.colorScheme.onBackground.muted,
)
}
items(
follows,
key = { follow -> follow.community.id },
contentType = { "communitylink" },
) { follow ->
CommunityLinkLarger(
community = follow.community,
onClick = onCommunityClick,
showDefaultIcon = true,
blurNSFW = blurNSFW,
)
}
}
}
}

@Preview
@Composable
fun DrawerItemsMainPreview() {
DrawerItemsMain(
myUserInfo = null,
onClickListingType = {},
onClickProfile = {},
onClickInbox = {},
onCommunityClick = {},
onClickSaved = {},
onClickSettings = {},
onClickCommunities = {},
unreadCount = 2,
blurNSFW = true,
)
}

@Composable
fun DrawerAddAccountMode(
navController: NavController = rememberNavController(),
accountViewModel: AccountViewModel?,
onSwitchAccountClick: (account: Account) -> Unit,
onSignOutClick: () -> Unit,
) {
val accountsWithoutCurrent = accountViewModel?.allAccounts?.value?.toMutableList()
val currentAccount = accountsWithoutCurrent?.firstOrNull { it.current }
accountsWithoutCurrent?.remove(currentAccount)

Column {
IconAndTextDrawerItem(
text = stringResource(R.string.home_add_account),
icon = Icons.Outlined.Add,
onClick = { navController.toLogin() },
)
accountsWithoutCurrent?.forEach {
IconAndTextDrawerItem(
text = stringResource(R.string.home_switch_to, it.name, it.instance),
icon = Icons.Outlined.Login,
onClick = { onSwitchAccountClick(it) },
)
}
currentAccount?.also {
IconAndTextDrawerItem(
text = stringResource(R.string.home_sign_out),
icon = Icons.Outlined.Close,
onClick = onSignOutClick,
)
}
}
}

@Preview
@Composable
fun DrawerAddAccountModePreview() {
DrawerAddAccountMode(
onSignOutClick = {},
onSwitchAccountClick = {},
accountViewModel = null,
)
}

@Composable
fun DrawerHeader(
myPerson: Person?,
onClickShowAccountAddMode: () -> Unit,
showAccountAddMode: Boolean = false,
showAvatar: Boolean,
) {
val sizeMod = Modifier
.fillMaxWidth()
.height(DRAWER_BANNER_SIZE)

Box(
modifier = sizeMod
.clickable(onClick = onClickShowAccountAddMode),
) {
myPerson?.banner?.also {
PictrsBannerImage(
url = it,
)
}
// banner
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = sizeMod
.padding(XL_PADDING),
) {
Box(modifier = Modifier.weight(0.9f)) {
AvatarAndAccountName(myPerson, showAvatar)
}
Icon(
modifier = Modifier.weight(0.1f),
imageVector = if (showAccountAddMode) {
Icons.Outlined.ExpandLess
} else {
Icons.Outlined.ExpandMore
},
contentDescription = if (showAccountAddMode) {
stringResource(R.string.moreOptions)
} else {
stringResource(R.string.lessOptions)
},
)
}
}
}

@Composable
fun AvatarAndAccountName(myPerson: Person?, showAvatar: Boolean) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(SMALL_PADDING),
) {
if (showAvatar) {
myPerson?.avatar?.also {
LargerCircularIcon(icon = it)
}
}
Column() {
PersonName(
person = myPerson,
color = MaterialTheme.colorScheme.onSurface,
)
Text(
text = myPerson?.let { federatedNameShown(it) } ?: "",
color = MaterialTheme.colorScheme.tertiary,
style = MaterialTheme.typography.labelSmall,
overflow = TextOverflow.Ellipsis,
maxLines = 2,
)
}
}
}

@Preview
@Composable
fun AvatarAndAccountNamePreview() {
AvatarAndAccountName(myPerson = samplePerson, showAvatar = true)
}

@Preview
@Composable
fun DrawerHeaderPreview() {
DrawerHeader(
myPerson = samplePerson,
onClickShowAccountAddMode = {},
showAvatar = true,
)
}

@Composable
fun HomeHeaderTitle(
12 changes: 10 additions & 2 deletions benchmarks/src/main/java/com/jerboa/actions/JerboaActions.kt
Original file line number Diff line number Diff line change
@@ -94,13 +94,21 @@ fun MacrobenchmarkScope.waitUntilPostsActuallyVisible(retry: Boolean = true, tim
if (retry && !device.hasObject(By.res("jerboa:posts").hasDescendant(By.res("jerboa:post")))) {
openMoreOptions()
clickRefresh()
waitUntilPostsActuallyVisible(timeout = timeout)
waitUntilPostsActuallyVisible(timeout = timeout, depth = depth + 1)
}
}

fun MacrobenchmarkScope.openMoreOptions() {
device.findOrFailTimeout("jerboa:options", "Options not found", timeout = 2_000).click()
var options = device.findTimeout("jerboa:options", timeout = 2_000)

if (options == null) {
val feed = device.findOrFailTimeout("jerboa:posts", "Posts not found", 2_000)
feed.setGestureMargin(device.displayWidth / 5)
feed.fling(Direction.UP)
options = device.findOrFailTimeout("jerboa:options")
}

options.click()
}

fun MacrobenchmarkScope.clickRefresh() {
You are viewing a condensed version of this merge commit. You can view the full changes here.