-
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.
Loading status checks…
Feature: File and absolute path support (#50)
* add support for file paths * Update README.md * clean up gradle files * fix extensions * add demo * run apiDump
- Loading branch information
Showing
39 changed files
with
615 additions
and
40 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
45 changes: 45 additions & 0 deletions
45
demo/composeApp/src/androidMain/kotlin/com/kmpalette/demo/FileDemoScreen.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,45 @@ | ||
package com.kmpalette.demo | ||
|
||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import com.kmpalette.demo.dominant.DominantDemoContent | ||
import com.kmpalette.loader.FilePathLoader | ||
import com.kmpalette.rememberDominantColorState | ||
|
||
actual class FileDemoScreen : Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
val filePath = remember { sampleFile() } | ||
val dominantColorState = rememberDominantColorState(loader = FilePathLoader) { | ||
clearFilters() | ||
} | ||
var errorMessage: String? by remember { mutableStateOf(null) } | ||
var image: ImageBitmap? by remember { mutableStateOf(null) } | ||
LaunchedEffect(filePath) { | ||
try { | ||
image = FilePathLoader.load(filePath) | ||
} catch (cause: Throwable) { | ||
cause.printStackTrace() | ||
errorMessage = cause.message | ||
} | ||
dominantColorState.updateFrom(filePath) | ||
} | ||
|
||
DominantDemoContent( | ||
dominantColorState = dominantColorState, | ||
imageBitmap = image, | ||
) { | ||
if (errorMessage != null) { | ||
Text(errorMessage!!) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
demo/composeApp/src/androidMain/kotlin/com/kmpalette/demo/sampleFile.android.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,5 @@ | ||
package com.kmpalette.demo | ||
|
||
actual fun sampleFile(): String { | ||
return AndroidApp.sampleFile().absolutePath | ||
} |
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
7 changes: 7 additions & 0 deletions
7
demo/composeApp/src/commonMain/kotlin/com/kmpalette/demo/sampleFile.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,7 @@ | ||
package com.kmpalette.demo | ||
|
||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
expect fun sampleFile(): String | ||
|
||
expect class FileDemoScreen() : Screen |
17 changes: 17 additions & 0 deletions
17
demo/composeApp/src/desktopMain/kotlin/com/kmpalette/demo/sampleFile.desktop.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,17 @@ | ||
package com.kmpalette.demo | ||
|
||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
actual fun sampleFile(): String { | ||
error("Not supported by this platform") | ||
} | ||
|
||
actual class FileDemoScreen actual constructor(): Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
Text("Not supported by this platform") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
demo/composeApp/src/jsMain/kotlin/com/kmpalette/demo/sampleFile.js.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,17 @@ | ||
package com.kmpalette.demo | ||
|
||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
actual fun sampleFile(): String { | ||
error("Not supported by this platform") | ||
} | ||
|
||
actual class FileDemoScreen actual constructor(): Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
Text("Not supported by this platform") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
demo/composeApp/src/nativeMain/kotlin/com/kmpalette/demo/sampleFile.native.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,17 @@ | ||
package com.kmpalette.demo | ||
|
||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
actual fun sampleFile(): String { | ||
error("Not supported by this platform") | ||
} | ||
|
||
actual class FileDemoScreen actual constructor(): Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
Text("Not supported by this platform") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# File Extensions | ||
|
||
This extension provides `FilePathLoader` and `PathLoader` a `ImageBitmapLoader` for loading images | ||
from an absolute path, or from an Okio `Path` object. | ||
|
||
| Library | Loader | Input Class | Demo | | ||
|----------------------------------|---------------------------------|-------------------|------| | ||
| [okio](https://square.github.io) | `PathLoader` / `FilePathLoader` | `Path` / `String` | N/A | | ||
|
||
## Setup | ||
|
||
In order to use these extensions you will need to add the Okio dependency to | ||
your `build.gradle.kts`, you will need the `core` library as well. | ||
|
||
Then you need to add the following to your `build.gradle.kts` file: | ||
|
||
```kotlin | ||
kotlin { | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation(libs.kmpalette.core) | ||
implementation(libs.kmpalette.extensions.file) | ||
implementation("com.squareup.okio:okio:<latest okio version>") | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Now you can use the `FilePathLoader` to load images from an absolute path: | ||
|
||
```kotlin | ||
@Composable | ||
fun MyComposable(absolutePath: String) { | ||
val paletteState = rememberPaletteState(loader = FilePathLoader) | ||
LaunchedEffect(absolutePath) { | ||
paletteState.generate(absolutePath) | ||
} | ||
} | ||
``` | ||
|
||
Or if you already have a `Path` object: | ||
|
||
```kotlin | ||
@Composable | ||
fun MyComposable(path: Path) { | ||
val paletteState = rememberPaletteState(loader = PathLoader) | ||
LaunchedEffect(path) { | ||
paletteState.generate(path) | ||
} | ||
} | ||
``` | ||
|
||
The Image will be fetched, converted into a `ImageBitmap` then a palette will be generated. |
Oops, something went wrong.