Skip to content

Commit b0922b3

Browse files
Merge pull request #467 from angeles-bilbao6/bug-fix459/top-bar
[Jetnews] TopAppBar doesn't match design spec
2 parents 27ba312 + 873bc3c commit b0922b3

File tree

10 files changed

+172
-23
lines changed

10 files changed

+172
-23
lines changed

JetNews/app/src/main/java/com/example/jetnews/ui/JetnewsApp.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.jetnews.ui
1818

19+
import androidx.compose.material.MaterialTheme
1920
import androidx.compose.material.Scaffold
2021
import androidx.compose.material.rememberScaffoldState
2122
import androidx.compose.runtime.Composable
@@ -38,8 +39,9 @@ fun JetnewsApp(
3839
JetnewsTheme {
3940
ProvideWindowInsets {
4041
val systemUiController = rememberSystemUiController()
42+
val darkIcons = MaterialTheme.colors.isLight
4143
SideEffect {
42-
systemUiController.setSystemBarsColor(Color.Transparent, darkIcons = false)
44+
systemUiController.setSystemBarsColor(Color.Transparent, darkIcons = darkIcons)
4345
}
4446

4547
val navController = rememberNavController()

JetNews/app/src/main/java/com/example/jetnews/ui/article/ArticleScreen.kt

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ package com.example.jetnews.ui.article
1919
import android.content.Context
2020
import android.content.Intent
2121
import android.content.res.Configuration.UI_MODE_NIGHT_YES
22+
import androidx.compose.foundation.Image
2223
import androidx.compose.foundation.layout.Row
2324
import androidx.compose.foundation.layout.Spacer
2425
import androidx.compose.foundation.layout.fillMaxWidth
2526
import androidx.compose.foundation.layout.height
2627
import androidx.compose.foundation.layout.padding
28+
import androidx.compose.foundation.layout.size
29+
import androidx.compose.foundation.layout.wrapContentWidth
30+
import androidx.compose.foundation.lazy.rememberLazyListState
31+
import androidx.compose.foundation.shape.CircleShape
2732
import androidx.compose.material.AlertDialog
2833
import androidx.compose.material.Icon
2934
import androidx.compose.material.IconButton
@@ -46,6 +51,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
4651
import androidx.compose.runtime.setValue
4752
import androidx.compose.ui.Alignment
4853
import androidx.compose.ui.Modifier
54+
import androidx.compose.ui.draw.clip
4955
import androidx.compose.ui.platform.LocalContext
5056
import androidx.compose.ui.res.painterResource
5157
import androidx.compose.ui.res.stringResource
@@ -60,6 +66,7 @@ import com.example.jetnews.model.Post
6066
import com.example.jetnews.ui.components.InsetAwareTopAppBar
6167
import com.example.jetnews.ui.home.BookmarkButton
6268
import com.example.jetnews.ui.theme.JetnewsTheme
69+
import com.example.jetnews.utils.isScrolled
6370
import com.example.jetnews.utils.supportWideScreen
6471
import com.google.accompanist.insets.navigationBarsPadding
6572
import kotlinx.coroutines.runBlocking
@@ -116,25 +123,45 @@ fun ArticleScreen(
116123
if (showDialog) {
117124
FunctionalityNotAvailablePopup { showDialog = false }
118125
}
119-
126+
val scrollState = rememberLazyListState()
120127
Scaffold(
121128
topBar = {
122129
InsetAwareTopAppBar(
123130
title = {
124-
Text(
125-
text = stringResource(id = R.string.article_published_in, formatArgs = arrayOf(post.publication?.name.orEmpty())),
126-
style = MaterialTheme.typography.subtitle2,
127-
color = LocalContentColor.current
128-
)
131+
Row(
132+
modifier = Modifier
133+
.fillMaxWidth()
134+
.wrapContentWidth(align = Alignment.CenterHorizontally)
135+
.padding(start = 30.dp)
136+
) {
137+
Image(
138+
painter = painterResource(id = R.drawable.icon_article_background),
139+
contentDescription = null,
140+
modifier = Modifier
141+
.clip(CircleShape)
142+
.size(36.dp)
143+
)
144+
Text(
145+
text = stringResource(id = R.string.published_in, post.publication?.name ?: ""),
146+
style = MaterialTheme.typography.subtitle2,
147+
color = LocalContentColor.current,
148+
modifier = Modifier
149+
.padding(start = 10.dp)
150+
.weight(1.5f)
151+
)
152+
}
129153
},
130154
navigationIcon = {
131155
IconButton(onClick = onBack) {
132156
Icon(
133157
imageVector = Icons.Filled.ArrowBack,
134-
contentDescription = stringResource(R.string.cd_navigate_up)
158+
contentDescription = stringResource(R.string.cd_navigate_up),
159+
tint = MaterialTheme.colors.primary
135160
)
136161
}
137-
}
162+
},
163+
elevation = if (!scrollState.isScrolled) 0.dp else 4.dp,
164+
backgroundColor = MaterialTheme.colors.surface
138165
)
139166
},
140167
bottomBar = {
@@ -148,6 +175,7 @@ fun ArticleScreen(
148175
) { innerPadding ->
149176
PostContent(
150177
post = post,
178+
state = scrollState,
151179
modifier = Modifier
152180
// innerPadding takes into account the top and bottom bar
153181
.padding(innerPadding)

JetNews/app/src/main/java/com/example/jetnews/ui/article/PostContent.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import androidx.compose.foundation.layout.padding
3030
import androidx.compose.foundation.layout.size
3131
import androidx.compose.foundation.layout.width
3232
import androidx.compose.foundation.lazy.LazyColumn
33+
import androidx.compose.foundation.lazy.LazyListState
3334
import androidx.compose.foundation.lazy.items
35+
import androidx.compose.foundation.lazy.rememberLazyListState
3436
import androidx.compose.foundation.shape.CircleShape
3537
import androidx.compose.material.Colors
3638
import androidx.compose.material.ContentAlpha
@@ -80,8 +82,13 @@ import com.example.jetnews.ui.theme.JetnewsTheme
8082
private val defaultSpacerSize = 16.dp
8183

8284
@Composable
83-
fun PostContent(post: Post, modifier: Modifier = Modifier) {
85+
fun PostContent(
86+
post: Post,
87+
state: LazyListState = rememberLazyListState(),
88+
modifier: Modifier = Modifier
89+
) {
8490
LazyColumn(
91+
state = state,
8592
modifier = modifier.padding(horizontal = defaultSpacerSize)
8693
) {
8794
item {

JetNews/app/src/main/java/com/example/jetnews/ui/home/HomeScreen.kt

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import androidx.compose.foundation.layout.fillMaxSize
2424
import androidx.compose.foundation.layout.padding
2525
import androidx.compose.foundation.layout.wrapContentSize
2626
import androidx.compose.foundation.lazy.LazyColumn
27+
import androidx.compose.foundation.lazy.LazyListState
2728
import androidx.compose.foundation.lazy.LazyRow
2829
import androidx.compose.foundation.lazy.items
30+
import androidx.compose.foundation.lazy.rememberLazyListState
2931
import androidx.compose.material.CircularProgressIndicator
3032
import androidx.compose.material.Divider
3133
import androidx.compose.material.ExperimentalMaterialApi
@@ -38,6 +40,8 @@ import androidx.compose.material.SnackbarHost
3840
import androidx.compose.material.SnackbarResult
3941
import androidx.compose.material.Text
4042
import androidx.compose.material.TextButton
43+
import androidx.compose.material.icons.Icons
44+
import androidx.compose.material.icons.filled.Search
4145
import androidx.compose.material.rememberScaffoldState
4246
import androidx.compose.runtime.Composable
4347
import androidx.compose.runtime.LaunchedEffect
@@ -59,6 +63,7 @@ import com.example.jetnews.data.posts.impl.BlockingFakePostsRepository
5963
import com.example.jetnews.model.Post
6064
import com.example.jetnews.ui.components.InsetAwareTopAppBar
6165
import com.example.jetnews.ui.theme.JetnewsTheme
66+
import com.example.jetnews.utils.isScrolled
6267
import com.example.jetnews.utils.supportWideScreen
6368
import com.google.accompanist.insets.LocalWindowInsets
6469
import com.google.accompanist.insets.rememberInsetsPaddingValues
@@ -122,21 +127,44 @@ fun HomeScreen(
122127
openDrawer: () -> Unit,
123128
scaffoldState: ScaffoldState
124129
) {
130+
val scrollState = rememberLazyListState()
125131
Scaffold(
126132
scaffoldState = scaffoldState,
127133
snackbarHost = { SnackbarHost(hostState = it, modifier = Modifier.systemBarsPadding()) },
128134
topBar = {
129135
val title = stringResource(id = R.string.app_name)
130136
InsetAwareTopAppBar(
131-
title = { Text(text = title) },
137+
title = {
138+
Icon(
139+
painter = painterResource(R.drawable.ic_jetnews_wordmark),
140+
contentDescription = title,
141+
tint = MaterialTheme.colors.onBackground,
142+
modifier = Modifier
143+
.fillMaxSize()
144+
.padding(bottom = 4.dp, top = 10.dp)
145+
)
146+
},
132147
navigationIcon = {
133148
IconButton(onClick = openDrawer) {
134149
Icon(
135150
painter = painterResource(R.drawable.ic_jetnews_logo),
136-
contentDescription = stringResource(R.string.cd_open_navigation_drawer)
151+
contentDescription = stringResource(R.string.cd_open_navigation_drawer),
152+
tint = MaterialTheme.colors.primary
137153
)
138154
}
139-
}
155+
},
156+
actions = {
157+
IconButton(
158+
onClick = { /* TODO: Open search */ }
159+
) {
160+
Icon(
161+
imageVector = Icons.Filled.Search,
162+
contentDescription = stringResource(R.string.cd_search)
163+
)
164+
}
165+
},
166+
backgroundColor = MaterialTheme.colors.surface,
167+
elevation = if (!scrollState.isScrolled) 0.dp else 4.dp
140168
)
141169
}
142170
) { innerPadding ->
@@ -156,7 +184,8 @@ fun HomeScreen(
156184
navigateToArticle = navigateToArticle,
157185
favorites = uiState.favorites,
158186
onToggleFavorite = onToggleFavorite,
159-
modifier = modifier.supportWideScreen()
187+
modifier = modifier.supportWideScreen(),
188+
scrollState = scrollState
160189
)
161190
}
162191
)
@@ -240,10 +269,11 @@ private fun HomeScreenErrorAndContent(
240269
onRefresh: () -> Unit,
241270
navigateToArticle: (String) -> Unit,
242271
onToggleFavorite: (String) -> Unit,
243-
modifier: Modifier = Modifier
272+
modifier: Modifier = Modifier,
273+
scrollState: LazyListState
244274
) {
245275
if (posts.isNotEmpty()) {
246-
PostList(posts, navigateToArticle, favorites, onToggleFavorite, modifier)
276+
PostList(posts, navigateToArticle, favorites, onToggleFavorite, modifier, scrollState)
247277
} else if (!isShowingErrors) {
248278
// if there are no posts, and no error, let the user refresh manually
249279
TextButton(onClick = onRefresh, modifier.fillMaxSize()) {
@@ -271,7 +301,8 @@ private fun PostList(
271301
navigateToArticle: (postId: String) -> Unit,
272302
favorites: Set<String>,
273303
onToggleFavorite: (String) -> Unit,
274-
modifier: Modifier = Modifier
304+
modifier: Modifier = Modifier,
305+
scrollState: LazyListState = rememberLazyListState(),
275306
) {
276307
val postTop = posts[3]
277308
val postsSimple = posts.subList(0, 2)
@@ -280,6 +311,7 @@ private fun PostList(
280311

281312
LazyColumn(
282313
modifier = modifier,
314+
state = scrollState,
283315
contentPadding = rememberInsetsPaddingValues(
284316
insets = LocalWindowInsets.current.systemBars,
285317
applyTop = false

JetNews/app/src/main/java/com/example/jetnews/ui/interests/InterestsScreen.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.Box
2323
import androidx.compose.foundation.layout.Column
2424
import androidx.compose.foundation.layout.Row
2525
import androidx.compose.foundation.layout.Spacer
26+
import androidx.compose.foundation.layout.fillMaxWidth
2627
import androidx.compose.foundation.layout.heightIn
2728
import androidx.compose.foundation.layout.padding
2829
import androidx.compose.foundation.layout.paddingFromBaseline
@@ -41,6 +42,8 @@ import androidx.compose.material.Surface
4142
import androidx.compose.material.Tab
4243
import androidx.compose.material.TabRow
4344
import androidx.compose.material.Text
45+
import androidx.compose.material.icons.Icons
46+
import androidx.compose.material.icons.filled.Search
4447
import androidx.compose.material.rememberScaffoldState
4548
import androidx.compose.runtime.Composable
4649
import androidx.compose.runtime.collectAsState
@@ -54,6 +57,7 @@ import androidx.compose.ui.res.painterResource
5457
import androidx.compose.ui.res.stringResource
5558
import androidx.compose.ui.semantics.heading
5659
import androidx.compose.ui.semantics.semantics
60+
import androidx.compose.ui.text.style.TextAlign
5761
import androidx.compose.ui.tooling.preview.Devices
5862
import androidx.compose.ui.tooling.preview.Preview
5963
import androidx.compose.ui.unit.dp
@@ -163,16 +167,36 @@ fun InterestsScreen(
163167
Scaffold(
164168
scaffoldState = scaffoldState,
165169
topBar = {
170+
166171
InsetAwareTopAppBar(
167-
title = { Text(stringResource(id = R.string.interests_title)) },
172+
title = {
173+
Text(
174+
text = stringResource(R.string.cd_interests),
175+
modifier = Modifier.fillMaxWidth(),
176+
textAlign = TextAlign.Center
177+
)
178+
},
168179
navigationIcon = {
169180
IconButton(onClick = openDrawer) {
170181
Icon(
171182
painter = painterResource(R.drawable.ic_jetnews_logo),
172-
contentDescription = stringResource(R.string.cd_open_navigation_drawer)
183+
contentDescription = stringResource(R.string.cd_open_navigation_drawer),
184+
tint = MaterialTheme.colors.primary
173185
)
174186
}
175-
}
187+
},
188+
actions = {
189+
IconButton(
190+
onClick = { /* TODO: Open search */ }
191+
) {
192+
Icon(
193+
imageVector = Icons.Filled.Search,
194+
contentDescription = stringResource(R.string.cd_search)
195+
)
196+
}
197+
},
198+
backgroundColor = MaterialTheme.colors.surface,
199+
elevation = 0.dp
176200
)
177201
}
178202
) {

JetNews/app/src/main/java/com/example/jetnews/ui/theme/Theme.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ private val LightThemeColors = lightColors(
3030
secondary = Red700,
3131
secondaryVariant = Red900,
3232
onSecondary = Color.White,
33-
error = Red800
33+
error = Red800,
34+
onBackground = Color.Black,
35+
3436
)
3537

3638
private val DarkThemeColors = darkColors(
@@ -39,7 +41,8 @@ private val DarkThemeColors = darkColors(
3941
onPrimary = Color.Black,
4042
secondary = Red300,
4143
onSecondary = Color.Black,
42-
error = Red200
44+
error = Red200,
45+
onBackground = Color.White
4346
)
4447

4548
@Composable
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.jetnews.utils
18+
19+
import androidx.compose.foundation.lazy.LazyListState
20+
21+
val LazyListState.isScrolled: Boolean
22+
get() = firstVisibleItemIndex > 0 || firstVisibleItemScrollOffset > 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2020 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
in compliance with the License. You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software distributed under the License
11+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
or implied. See the License for the specific language governing permissions and limitations under
13+
the License.
14+
-->
15+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
16+
android:width="48dp"
17+
android:height="48dp"
18+
android:viewportWidth="24.0"
19+
android:viewportHeight="24.0">
20+
<group>
21+
<path android:name="square"
22+
android:fillColor="#FF073042"
23+
android:pathData="M0,0 L24,0 L24,24 L0,24 z" />
24+
<path android:fillColor="#3DDC84"
25+
android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z"/>
26+
27+
</group>
28+
</vector>

0 commit comments

Comments
 (0)