-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Add demo for new PainterLoader (#75)
- Loading branch information
Showing
12 changed files
with
173 additions
and
25 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
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
41 changes: 41 additions & 0 deletions
41
.../composeApp/src/commonMain/kotlin/com/kmpalette/demo/dominant/NetworkPainterDemoScreen.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,41 @@ | ||
package com.kmpalette.demo.dominant | ||
|
||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import com.kmpalette.loader.rememberPainterLoader | ||
import com.kmpalette.rememberDominantColorState | ||
import io.kamel.core.Resource | ||
import io.kamel.image.asyncPainterResource | ||
import io.ktor.http.Url | ||
|
||
class NetworkPainterDemoScreen : Screen { | ||
|
||
private val demoImageUrl = Url("https://picsum.photos/600/300") | ||
|
||
@Composable | ||
override fun Content() { | ||
when (val painterResource = asyncPainterResource(demoImageUrl)) { | ||
is Resource.Failure -> Text("Failed to load image") | ||
is Resource.Loading -> CircularProgressIndicator() | ||
is Resource.Success -> SuccessContent(painterResource) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SuccessContent(resource: Resource.Success<Painter>) { | ||
val loader = rememberPainterLoader() | ||
val dominantColorState = rememberDominantColorState(loader) | ||
LaunchedEffect(resource) { | ||
dominantColorState.updateFrom(resource.value) | ||
} | ||
|
||
DominantDemoContent( | ||
dominantColorState = dominantColorState, | ||
painter = resource.value, | ||
) | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
demo/composeApp/src/commonMain/kotlin/com/kmpalette/demo/palette/PainterPaletteScreen.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,22 @@ | ||
package com.kmpalette.demo.palette | ||
|
||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import com.kmpalette.loader.rememberPainterLoader | ||
import com.kmpalette.rememberPaletteState | ||
import io.github.skeptick.libres.compose.painterResource | ||
|
||
class PainterPaletteScreen : Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
val painters = images.map { it.painterResource() } | ||
|
||
val loader = rememberPainterLoader() | ||
PaletteScreen( | ||
images = painters, | ||
paletteState = rememberPaletteState(loader), | ||
painterResource = { _, painter -> painter }, | ||
) | ||
} | ||
} |
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
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
40 changes: 40 additions & 0 deletions
40
kmpalette-core/src/commonMain/kotlin/com/kmpalette/loader/PainterLoader.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,40 @@ | ||
package com.kmpalette.loader | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import com.kmpalette.internal.PainterImage | ||
|
||
/** | ||
* A default loader that takes an [Painter], draws it as an [ImageBitmap] and returns that. | ||
*/ | ||
public class PainterLoader( | ||
private val density: Density, | ||
private val layoutDirection: LayoutDirection, | ||
) : ImageBitmapLoader<Painter> { | ||
|
||
override suspend fun load(input: Painter): ImageBitmap { | ||
return PainterImage(input, density, layoutDirection).asBitmap() | ||
} | ||
} | ||
|
||
/** | ||
* Create and remember a [PainterLoader] that uses the [LocalDensity] and [LocalLayoutDirection] | ||
* to generate a Palette from a [Painter]. | ||
* | ||
* @param[density] The [Density] to use when drawing the [Painter]. | ||
* @param[layoutDirection] The [LayoutDirection] to use when drawing the [Painter]. | ||
* @return A [PainterLoader] for the given [density] and [layoutDirection]. | ||
*/ | ||
@Composable | ||
public fun rememberPainterLoader( | ||
density: Density = LocalDensity.current, | ||
layoutDirection: LayoutDirection = LocalLayoutDirection.current, | ||
): PainterLoader = remember(density, layoutDirection) { | ||
PainterLoader(density, layoutDirection) | ||
} |