Skip to content

Commit

Permalink
Update Android sample to haze-jetpack-compose (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes authored Oct 30, 2023
1 parent a57ef11 commit 57e41fa
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.1.2"
jetpackcompose-compiler = "1.5.3"
compose-multiplatform = "1.5.10-rc02"
ktlint = "1.0.1"
kotlin = "1.9.10"
Expand All @@ -20,6 +21,7 @@ mavenpublish = { id = "com.vanniktech.maven.publish", version = "0.25.3" }
[libraries]
imageloader = "io.github.qdsfdhvh:image-loader:1.6.8"
androidx-compose-ui = "androidx.compose.ui:ui:1.6.0-alpha08"
androidx-compose-material3 = "androidx.compose.material3:material3:1.2.0-alpha10"
androidx-core = "androidx.core:core-ktx:1.12.0"
androidx-activity-compose = "androidx.activity:activity-compose:1.8.0"

Expand Down
14 changes: 13 additions & 1 deletion sample/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ android {
versionName = "1.0"
}

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.jetpackcompose.compiler.get()
}

buildTypes {
release {
isMinifyEnabled = true
Expand Down Expand Up @@ -45,7 +53,11 @@ android {
}

dependencies {
implementation(projects.sample.shared)
implementation(projects.hazeJetpackCompose)

implementation(libs.imageloader)

implementation(libs.androidx.core)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.material3)
}
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),
)
}
}
}
}
}
}
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) }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import dev.chrisbanes.haze.sample.HazeSample

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down

0 comments on commit 57e41fa

Please sign in to comment.