Skip to content

Commit

Permalink
Adding site sidebar.
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Jan 18, 2022
1 parent 1026cb9 commit 7427620
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 271 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _ | _
---|---
![img_1](https://i.imgur.com/W3lT4nO.jpg)|![img_2](https://i.imgur.com/MrAkd4d.jpg)

Jerboa is a native-android client for Lemmy, built using the newest.
Jerboa is a native-android client for Lemmy, built using the native Android Toolkit, Jetpack Compose.

**Warning**: You can submit issues, but between Lemmy and lemmy-ui, I probably won't have too much time to work on them. Learn jetpack compose like I did if you want to help make this app better.

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import com.jerboa.ui.components.community.CommunityActivity
import com.jerboa.ui.components.community.CommunityViewModel
import com.jerboa.ui.components.community.list.CommunityListActivity
import com.jerboa.ui.components.community.list.CommunityListViewModel
import com.jerboa.ui.components.home.HomeActivity
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.home.SiteViewModel
import com.jerboa.ui.components.home.SplashScreenActivity
import com.jerboa.ui.components.home.*
import com.jerboa.ui.components.inbox.InboxActivity
import com.jerboa.ui.components.login.LoginActivity
import com.jerboa.ui.components.login.LoginViewModel
Expand Down Expand Up @@ -194,6 +191,14 @@ class MainActivity : ComponentActivity() {
navController = navController,
)
}
composable(
route = "sidebar",
) {
SidebarActivity(
siteViewModel = siteViewModel,
navController = navController,
)
}
composable(
route = "commentEdit",
) {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.ocpsoft.prettytime.PrettyTime
import java.net.URL
import java.text.DecimalFormat
import java.util.*

val prettyTime = PrettyTime(Locale.getDefault())
Expand Down Expand Up @@ -1050,3 +1051,14 @@ fun validateUrl(
)
}
}

private val suffix = arrayOf("", "K", "M", "B", "T")

fun siFormat(number: Int): String {
var r: String = DecimalFormat("##0E0").format(number)
r = r.replace("E[0-9]".toRegex(), suffix[Character.getNumericValue(r[r.length - 1]) / 3])
while (r.length > 4 || r.matches(Regex("[0-9]+\\.[a-z]"))) {
r = r.substring(0, r.length - 2) + r.substring(r.length - 1)
}
return r
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/jerboa/datatypes/SampleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,38 @@ val samplePrivateMessageView = PrivateMessageView(
creator = samplePersonSafe,
recipient = samplePersonSafe2,
)

val sampleSite = Site(
id = 23,
name = "Lemmy.ml",
sidebar = "# Hello!\n\n**This is** lemmy's sidebar",
description = "A general purpose instance for lemmy",
creator_id = 82,
published = "2022-01-07T04:12:26.398434",
updated = "2022-01-07T03:15:37.360888",
enable_downvotes = true,
open_registration = true,
enable_nsfw = true,
community_creation_admin_only = true,
icon = "https://lemmy.ml/pictrs/image/LqURxPzFNW.jpg",
banner = "https://lemmy.ml/pictrs/image/386rk5OYWS.jpg"
)

val sampleSiteAggregates = SiteAggregates(
id = 23,
site_id = 84,
users = 8092,
posts = 888929,
comments = 9882,
communities = 89,
users_active_day = 21,
users_active_week = 82,
users_active_month = 208,
users_active_half_year = 689,
)

val sampleSiteView = SiteView(
site = sampleSite,
creator = samplePersonSafe,
counts = sampleSiteAggregates,
)
51 changes: 43 additions & 8 deletions app/src/main/java/com/jerboa/ui/components/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ fun DrawerItemsMain(
onClick = { onClickListingType(ListingType.All) },
)
}
item {
IconAndTextDrawerItem(
text = "Saved",
icon = Icons.Default.Star,
onClick = onClickSaved,
)
}
// TODO add saved
// item {
// IconAndTextDrawerItem(
// text = "Saved",
// icon = Icons.Default.Star,
// onClick = onClickSaved,
// )
// }
item {
Divider()
}
Expand Down Expand Up @@ -440,11 +441,13 @@ fun HomeHeader(
onClickListingType: (ListingType) -> Unit = {},
selectedSortType: SortType,
selectedListingType: ListingType,
navController: NavController,
) {

var showSortOptions by remember { mutableStateOf(false) }
var showTopOptions by remember { mutableStateOf(false) }
var showListingTypeOptions by remember { mutableStateOf(false) }
var showMoreOptions by remember { mutableStateOf(false) }

if (showSortOptions) {
SortOptionsDialog(
Expand Down Expand Up @@ -483,6 +486,13 @@ fun HomeHeader(
)
}

if (showMoreOptions) {
HomeMoreDialog(
onDismissRequest = { showMoreOptions = false },
navController = navController,
)
}

TopAppBar(
title = {
HomeHeaderTitle(
Expand Down Expand Up @@ -523,6 +533,7 @@ fun HomeHeader(
)
}
IconButton(onClick = {
showMoreOptions = !showMoreOptions
}) {
Icon(
Icons.Default.MoreVert,
Expand All @@ -544,7 +555,8 @@ fun HomeHeaderPreview() {
scope,
scaffoldState,
selectedSortType = SortType.Hot,
selectedListingType = ListingType.All
selectedListingType = ListingType.All,
navController = rememberNavController(),
)
}

Expand Down Expand Up @@ -636,3 +648,26 @@ fun BottomAppBarAll(
fun BottomAppBarAllPreview() {
BottomAppBarAll()
}

@Composable
fun HomeMoreDialog(
onDismissRequest: () -> Unit = {},
navController: NavController,
) {
AlertDialog(
onDismissRequest = onDismissRequest,
text = {
Column {
IconAndTextDrawerItem(
text = "View Sidebar",
icon = Icons.Default.Info,
onClick = {
navController.navigate("sidebar")
onDismissRequest()
},
)
}
},
buttons = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fun HomeActivity(
homeViewModel = homeViewModel,
account = account,
ctx = ctx,
navController = navController,
)
},
drawerShape = MaterialTheme.shapes.small,
Expand Down Expand Up @@ -331,10 +332,13 @@ fun MainTopBar(
homeViewModel: HomeViewModel,
account: Account?,
ctx: Context,
navController: NavController,
) {
Column {
HomeHeader(
scope, scaffoldState,
scope = scope,
scaffoldState = scaffoldState,
navController = navController,
selectedSortType = homeViewModel.sortType.value,
selectedListingType = homeViewModel.listingType.value,
onClickSortType = { sortType ->
Expand Down
89 changes: 89 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/home/Sidebar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.jerboa.ui.components.home

import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.jerboa.DotSpacer
import com.jerboa.MyMarkdownText
import com.jerboa.datatypes.SiteView
import com.jerboa.datatypes.sampleSiteView
import com.jerboa.siFormat
import com.jerboa.ui.components.common.LargerCircularIcon
import com.jerboa.ui.components.common.PictrsBannerImage
import com.jerboa.ui.components.common.TimeAgo
import com.jerboa.ui.theme.MEDIUM_PADDING
import com.jerboa.ui.theme.Muted
import com.jerboa.ui.theme.PROFILE_BANNER_SIZE

@Composable
fun Sidebar(siteView: SiteView) {
val site = siteView.site
Column(
modifier = Modifier.padding(MEDIUM_PADDING),
verticalArrangement = Arrangement.spacedBy(MEDIUM_PADDING)
) {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.BottomStart
) {
site.banner?.also {
PictrsBannerImage(
url = it, modifier = Modifier.height(PROFILE_BANNER_SIZE)
)
}
Box(modifier = Modifier.padding(MEDIUM_PADDING)) {
site.icon?.also {
LargerCircularIcon(icon = it)
}
}
}
site.description?.also {
Text(
text = it,
style = MaterialTheme.typography.subtitle1
)
}
TimeAgo(
precedingString = "Created",
includeAgo = true,
dateStr = site.published
)
CommentsAndPosts(siteView = siteView)
site.sidebar?.also {
MyMarkdownText(
markdown = it,
color = Muted,
)
}
}
}

@Composable
fun CommentsAndPosts(siteView: SiteView) {
Row {
Text(
text = "${siFormat(siteView.counts.users_active_month)} users / month",
color = Muted,
)
DotSpacer()
Text(
text = "${siFormat(siteView.counts.posts)} posts",
color = Muted,
)
DotSpacer()
Text(
text = "${siFormat(siteView.counts.comments)} comments",
color = Muted,
)
}
}

@Preview
@Composable
fun SidebarPreview() {
Sidebar(siteView = sampleSiteView)
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/home/SidebarActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.jerboa.ui.components.home

import android.util.Log
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import com.jerboa.SimpleTopAppBar

@Composable
fun SidebarActivity(
siteViewModel: SiteViewModel,
navController: NavController,
) {

Log.d("jerboa", "got to sidebar activity")

val ctx = LocalContext.current
val scope = rememberCoroutineScope()
val title = "${siteViewModel.siteRes?.site_view?.site?.name} Sidebar"

Surface(color = MaterialTheme.colors.background) {
Scaffold(
topBar = {
SimpleTopAppBar(
text = title,
navController = navController
)
},
content = {
siteViewModel.siteRes?.site_view?.also {
Sidebar(it)
}
}
)
}
}
Loading

0 comments on commit 7427620

Please sign in to comment.