-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Android sample to haze-jetpack-compose (#9)
- Loading branch information
1 parent
a57ef11
commit 57e41fa
Showing
5 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
sample/android/src/main/kotlin/dev/chrisbanes/haze/sample/android/HazeSample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023, Christopher Banes and the Haze project contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dev.chrisbanes.haze.sample.android | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.BoxWithConstraints | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.LargeTopAppBar | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBarDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.dp | ||
import dev.chrisbanes.haze.haze | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun HazeSample(appTitle: String) { | ||
MaterialTheme { | ||
Scaffold( | ||
topBar = { | ||
LargeTopAppBar( | ||
title = { Text(text = appTitle) }, | ||
colors = TopAppBarDefaults.largeTopAppBarColors(Color.Transparent), | ||
modifier = Modifier.fillMaxWidth(), | ||
) | ||
}, | ||
modifier = Modifier.fillMaxSize(), | ||
) { contentPadding -> | ||
BoxWithConstraints { | ||
val topBarBounds = with(LocalDensity.current) { | ||
Rect( | ||
Offset(0f, 0f), | ||
Offset(maxWidth.toPx(), contentPadding.calculateTopPadding().toPx()), | ||
) | ||
} | ||
|
||
LazyVerticalGrid( | ||
columns = GridCells.Adaptive(128.dp), | ||
verticalArrangement = Arrangement.spacedBy(8.dp), | ||
horizontalArrangement = Arrangement.spacedBy(8.dp), | ||
contentPadding = contentPadding, | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.haze( | ||
topBarBounds, | ||
backgroundColor = MaterialTheme.colorScheme.surface, | ||
), | ||
) { | ||
items(50) { index -> | ||
ImageItem( | ||
text = "${index + 1}", | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.aspectRatio(3 / 4f), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
sample/android/src/main/kotlin/dev/chrisbanes/haze/sample/android/ImageItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2023, Christopher Banes and the Haze project contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dev.chrisbanes.haze.sample.android | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.sizeIn | ||
import androidx.compose.foundation.layout.wrapContentSize | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.unit.dp | ||
import com.seiko.imageloader.rememberImagePainter | ||
|
||
/** | ||
* Simple pager item which displays an image | ||
*/ | ||
@Composable | ||
internal fun ImageItem( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Surface(modifier) { | ||
Box { | ||
Image( | ||
painter = rememberImagePainter(rememberRandomSampleImageUrl(width = 400)), | ||
contentScale = ContentScale.Crop, | ||
contentDescription = null, | ||
modifier = Modifier.fillMaxSize(), | ||
) | ||
|
||
Text( | ||
text = text, | ||
modifier = Modifier | ||
.align(Alignment.BottomEnd) | ||
.padding(16.dp) | ||
.background(MaterialTheme.colorScheme.surface, RoundedCornerShape(4.dp)) | ||
.sizeIn(minWidth = 40.dp, minHeight = 40.dp) | ||
.padding(8.dp) | ||
.wrapContentSize(Alignment.Center), | ||
) | ||
} | ||
} | ||
} | ||
|
||
private val rangeForRandom = (0..100000) | ||
|
||
fun randomSampleImageUrl( | ||
seed: Int = rangeForRandom.random(), | ||
width: Int = 300, | ||
height: Int = width, | ||
): String = "https://picsum.photos/seed/$seed/$width/$height" | ||
|
||
/** | ||
* Remember a URL generate by [randomSampleImageUrl]. | ||
*/ | ||
@Composable | ||
fun rememberRandomSampleImageUrl( | ||
seed: Int = rangeForRandom.random(), | ||
width: Int = 300, | ||
height: Int = width, | ||
): String = remember { randomSampleImageUrl(seed, width, height) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters