Skip to content

Commit

Permalink
Add edge-to-edge to Jetnews
Browse files Browse the repository at this point in the history
  • Loading branch information
JolandaVerhoef committed Nov 20, 2023
1 parent 30ff90a commit 89ea885
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 46 deletions.
14 changes: 0 additions & 14 deletions JetNews/app/src/main/java/com/example/jetnews/ui/JetnewsApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fun JetnewsApp(
/**
* Determine the drawer state to pass to the modal drawer.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun rememberSizeAwareDrawerState(isExpandedScreen: Boolean): DrawerState {
val drawerState = rememberDrawerState(DrawerValue.Closed)
Expand All @@ -114,16 +113,3 @@ private fun rememberSizeAwareDrawerState(isExpandedScreen: Boolean): DrawerState
DrawerState(DrawerValue.Closed)
}
}

/**
* Determine the content padding to apply to the different screens of the app
*/
@Composable
fun rememberContentPaddingForScreen(
additionalTop: Dp = 0.dp,
excludeTop: Boolean = false
) =
WindowInsets.systemBars
.only(if (excludeTop) WindowInsetsSides.Bottom else WindowInsetsSides.Vertical)
.add(WindowInsets(top = additionalTop))
.asPaddingValues()
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ package com.example.jetnews.ui
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.core.view.WindowCompat
import com.example.jetnews.JetnewsApplication

class MainActivity : ComponentActivity() {

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)

val appContainer = (application as JetnewsApplication).container
setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.Intent
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
Expand Down Expand Up @@ -157,10 +158,12 @@ private fun ArticleScreenContent(
) { innerPadding ->
PostContent(
post = post,
contentPadding = innerPadding,
modifier = Modifier
.nestedScroll(scrollBehavior.nestedScrollConnection)
// innerPadding takes into account the top and bottom bar
.padding(innerPadding),
// .padding(innerPadding)
,
state = lazyListState,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ private val defaultSpacerSize = 16.dp
fun PostContent(
post: Post,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(0.dp),
state: LazyListState = rememberLazyListState()
) {
LazyColumn(
contentPadding = PaddingValues(defaultSpacerSize),
modifier = modifier,
contentPadding = contentPadding,
modifier = modifier.padding(horizontal = defaultSpacerSize),
state = state,
) {
postContentItems(post)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ import com.example.jetnews.ui.article.postContentItems
import com.example.jetnews.ui.article.sharePost
import com.example.jetnews.ui.components.JetnewsSnackbarHost
import com.example.jetnews.ui.modifiers.interceptKey
import com.example.jetnews.ui.rememberContentPaddingForScreen
import com.example.jetnews.ui.theme.JetnewsTheme
import com.example.jetnews.ui.utils.BookmarkButton
import com.example.jetnews.ui.utils.FavoriteButton
Expand Down Expand Up @@ -144,11 +143,7 @@ fun HomeFeedWithArticleDetailsScreen(
openDrawer = openDrawer,
snackbarHostState = snackbarHostState,
modifier = modifier,
) { hasPostsUiState, contentModifier ->
val contentPadding = rememberContentPaddingForScreen(
additionalTop = if (showTopAppBar) 0.dp else 8.dp,
excludeTop = showTopAppBar
)
) { hasPostsUiState, contentPadding, contentModifier ->
Row(contentModifier) {
PostList(
postsFeed = hasPostsUiState.postsFeed,
Expand Down Expand Up @@ -246,17 +241,14 @@ fun HomeFeedScreen(
openDrawer = openDrawer,
snackbarHostState = snackbarHostState,
modifier = modifier
) { hasPostsUiState, contentModifier ->
) { hasPostsUiState, contentPadding, contentModifier ->
PostList(
postsFeed = hasPostsUiState.postsFeed,
favorites = hasPostsUiState.favorites,
showExpandedSearch = !showTopAppBar,
onArticleTapped = onSelectPost,
onToggleFavorite = onToggleFavorite,
contentPadding = rememberContentPaddingForScreen(
additionalTop = if (showTopAppBar) 0.dp else 8.dp,
excludeTop = showTopAppBar
),
contentPadding = contentPadding,
modifier = contentModifier,
state = homeListLazyListState,
searchInput = searchInput,
Expand Down Expand Up @@ -286,6 +278,7 @@ private fun HomeScreenWithList(
modifier: Modifier = Modifier,
hasPostsContent: @Composable (
uiState: HomeUiState.HasPosts,
contentPadding: PaddingValues,
modifier: Modifier
) -> Unit
) {
Expand All @@ -303,9 +296,7 @@ private fun HomeScreenWithList(
},
modifier = modifier
) { innerPadding ->
val contentModifier = Modifier
.padding(innerPadding)
.nestedScroll(scrollBehavior.nestedScrollConnection)
val contentModifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection)

LoadingContent(
empty = when (uiState) {
Expand All @@ -317,13 +308,13 @@ private fun HomeScreenWithList(
onRefresh = onRefreshPosts,
content = {
when (uiState) {
is HomeUiState.HasPosts -> hasPostsContent(uiState, contentModifier)
is HomeUiState.HasPosts -> hasPostsContent(uiState, innerPadding, contentModifier)
is HomeUiState.NoPosts -> {
if (uiState.errorMessages.isEmpty()) {
// if there are no posts, and no error, let the user refresh manually
TextButton(
onClick = onRefreshPosts,
modifier.fillMaxSize()
modifier.padding(innerPadding).fillMaxSize()
) {
Text(
stringResource(id = R.string.home_tap_to_load_content),
Expand All @@ -332,7 +323,10 @@ private fun HomeScreenWithList(
}
} else {
// there's currently an error showing, don't show any content
Box(contentModifier.fillMaxSize()) { /* empty screen */ }
Box(
contentModifier
.padding(innerPadding)
.fillMaxSize()) { /* empty screen */ }
}
}
}
Expand Down
11 changes: 1 addition & 10 deletions JetNews/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,5 @@
limitations under the License.
-->
<resources>

<!-- Allows us to override platform level specific attributes in their
respective values-vXX folder. -->
<style name="Platform.Theme.Jetnews" parent="Theme.Material3.DayNight">
<item name="android:statusBarColor">@color/black30</item>
</style>

<!-- The actual theme we use. This varies for light theme (here),
and values-night for dark theme. -->
<style name="Theme.Jetnews" parent="Platform.Theme.Jetnews" />
<style name="Theme.Jetnews" parent="android:Theme.Material.Light.NoActionBar"/>
</resources>

0 comments on commit 89ea885

Please sign in to comment.