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

Fix #849: Loading progress for image #850

Merged
merged 25 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ee8b700
feat: Show loading progress for Image Component as a solve for
qamarelsafadi Jul 22, 2023
42b16bf
Merge remote-tracking branch 'origin/main'
qamarelsafadi Jul 22, 2023
3284eb6
feat: Show loading progress for Image Component as a solve for
qamarelsafadi Jul 22, 2023
7a05f7d
feat: Show loading progress for Image Component as a solve for
qamarelsafadi Jul 22, 2023
a294adb
feat: fix format issue
qamarelsafadi Jul 24, 2023
06e91fd
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Jul 24, 2023
f4859e0
Merge remote-tracking branch 'origin/loading-progress-for-image' into…
qamarelsafadi Jul 24, 2023
8a19780
feat: fix format issue
qamarelsafadi Jul 25, 2023
57025e7
fix: review notes
qamarelsafadi Jul 26, 2023
8209bd7
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Jul 26, 2023
9fb3804
Merge remote-tracking branch 'origin/loading-progress-for-image' into…
qamarelsafadi Jul 26, 2023
9c1ec17
fix: modifier to Modifier
qamarelsafadi Jul 27, 2023
54723b5
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Jul 27, 2023
427d65e
Merge remote-tracking branch 'origin/loading-progress-for-image' into…
qamarelsafadi Jul 27, 2023
f1a2993
feat: remove add placeholder todo
qamarelsafadi Jul 27, 2023
e276212
feat: remove check if placeholder null and put a default placeholder …
qamarelsafadi Jul 27, 2023
9f3103c
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Jul 29, 2023
3595d8d
feat: move the placeholder default value to the method
qamarelsafadi Jul 29, 2023
5bbcc90
feat: make painter in one line
qamarelsafadi Jul 29, 2023
8aa0457
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Aug 3, 2023
5abc69f
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Aug 10, 2023
f203e9a
feat: replacing SubcomposeAsyncImage as its not recommended to use wi…
qamarelsafadi Aug 10, 2023
5d7e33d
fix formatting
qamarelsafadi Aug 11, 2023
2db3e0c
Merge branch 'android:main' into loading-progress-for-image
qamarelsafadi Aug 11, 2023
308f51f
Merge remote-tracking branch 'origin/loading-progress-for-image' into…
qamarelsafadi Aug 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,28 @@

package com.google.samples.apps.nowinandroid.core.designsystem.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter.State.Error
import coil.compose.AsyncImagePainter.State.Loading
import coil.compose.rememberAsyncImagePainter
import com.google.samples.apps.nowinandroid.core.designsystem.R
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalTintTheme

/**
Expand All @@ -31,14 +48,37 @@ fun DynamicAsyncImage(
imageUrl: String,
contentDescription: String?,
modifier: Modifier = Modifier,
placeholder: Painter? = null,
placeholder: Painter = painterResource(R.drawable.ic_placeholder_default),
) {
val iconTint = LocalTintTheme.current.iconTint
AsyncImage(
placeholder = placeholder,
var isLoading by remember { mutableStateOf(true) }
var isError by remember { mutableStateOf(false) }
val imageLoader = rememberAsyncImagePainter(
model = imageUrl,
contentDescription = contentDescription,
colorFilter = if (iconTint != null) ColorFilter.tint(iconTint) else null,
modifier = modifier,
onState = { state ->
isLoading = state is Loading
isError = state is Error
},
)
Box(
modifier = modifier,
contentAlignment = Alignment.Center,
) {
if (isLoading) {
// Display a progress bar while loading
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
.size(80.dp),
color = MaterialTheme.colorScheme.tertiary,
)
}
Image(
contentScale = ContentScale.Crop,
painter = if (isError.not()) imageLoader else placeholder,
contentDescription = contentDescription,
colorFilter = if (iconTint != null) ColorFilter.tint(iconTint) else null,
modifier = modifier,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.samples.apps.nowinandroid.core.ui

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -31,6 +32,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
Expand All @@ -57,7 +59,9 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import com.google.samples.apps.nowinandroid.core.designsystem.R.drawable
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaIconToggleButton
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopicTag
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
Expand All @@ -72,7 +76,6 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.Locale
import com.google.samples.apps.nowinandroid.core.designsystem.R as DesignsystemR

/**
* [NewsResource] card used on the following screens: For You, Saved
Expand Down Expand Up @@ -147,21 +150,41 @@ fun NewsResourceCardExpanded(
fun NewsResourceHeaderImage(
qamarelsafadi marked this conversation as resolved.
Show resolved Hide resolved
headerImageUrl: String?,
) {
AsyncImage(
placeholder = if (LocalInspectionMode.current) {
painterResource(DesignsystemR.drawable.ic_placeholder_default)
} else {
// TODO b/228077205, show specific loading image visual
null
var isLoading by remember { mutableStateOf(true) }
var isError by remember { mutableStateOf(false) }
val imageLoader = rememberAsyncImagePainter(
model = headerImageUrl,
onState = { state ->
isLoading = state is AsyncImagePainter.State.Loading
isError = state is AsyncImagePainter.State.Error
},
)
Box(
modifier = Modifier
.fillMaxWidth()
.height(180.dp),
contentScale = ContentScale.Crop,
model = headerImageUrl,
// TODO b/226661685: Investigate using alt text of image to populate content description
contentDescription = null, // decorative image
)
contentAlignment = Alignment.Center,
) {
if (isLoading) {
// Display a progress bar while loading
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
.size(80.dp),
color = MaterialTheme.colorScheme.tertiary,
)
}

Image(
modifier = Modifier
.fillMaxWidth()
.height(180.dp),
contentScale = ContentScale.Crop,
painter = if (isError.not()) imageLoader else painterResource(drawable.ic_placeholder_default),
// TODO b/226661685: Investigate using alt text of image to populate content description
contentDescription = null, // decorative image,
)
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ fun TopicIcon(
modifier: Modifier = Modifier,
) {
DynamicAsyncImage(
// TODO b/228077205, show loading image visual instead of static placeholder
placeholder = painterResource(R.drawable.ic_icon_placeholder),
imageUrl = imageUrl,
contentDescription = null, // decorative
Expand Down