Skip to content

Commit

Permalink
Merge pull request #196 from MohamedRejeb/0.6.x
Browse files Browse the repository at this point in the history
Update Coil to 3.0.1
  • Loading branch information
MohamedRejeb authored Nov 9, 2024
2 parents e476f68 + 57b3057 commit 93b84f9
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 50 deletions.
16 changes: 15 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ kotlinx-datetime = "0.6.1"
nexus-publish = "2.0.0"
documentfile = "1.0.1"
jna = "5.14.0"
coil = "3.0.0-alpha10"
coil = "3.0.1"

calf = "0.5.5"

Expand All @@ -20,6 +20,8 @@ kotlinx-coroutines = "1.9.0"
appcompat = "1.7.0"
android-lifecycle = "2.2.0"
play-services-location = "21.3.0"
compose-navigation = "2.7.0-alpha07"
ktor = "3.0.1"

[libraries]
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand All @@ -39,6 +41,7 @@ kotlinx-coroutines-javafx = { module = "org.jetbrains.kotlinx:kotlinx-coroutines
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }
nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin", version.ref = "nexus-publish" }

coil = { module = "io.coil-kt.coil3:coil", version.ref = "coil" }

# Android
Expand All @@ -49,11 +52,22 @@ jna = { module = "net.java.dev.jna:jna", version.ref = "jna" }

# For sample
navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation-compose" }
compose-navigation = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "compose-navigation" }

calf-ui = { module = "com.mohamedrejeb.calf:calf-ui", version.ref = "calf" }
calf-file-picker = { module = "com.mohamedrejeb.calf:calf-file-picker", version.ref = "calf" }
calf-permissions = { module = "com.mohamedrejeb.calf:calf-permissions", version.ref = "calf" }
calf-webview = { module = "com.mohamedrejeb.calf:calf-webview", version.ref = "calf" }

coil-compose = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil" }
coil-network-ktor = { module = "io.coil-kt.coil3:coil-network-ktor3", version.ref = "coil" }

ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }
ktor-client-wasm = { module = "io.ktor:ktor-client-js-wasm-js", version.ref = "ktor" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
Expand Down
33 changes: 33 additions & 0 deletions sample/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,41 @@ kotlin {
// Calf
api(projects.calfUi)
implementation(projects.calfFilePicker)
implementation(projects.calfFilePickerCoil)
implementation(projects.calfPermissions)
implementation(projects.calfWebview)
implementation(libs.compose.navigation)
implementation(projects.calfNavigation)

// Coil
implementation(libs.coil.compose)
implementation(libs.coil.network.ktor)

// Ktor
implementation(libs.ktor.client.core)
}

sourceSets.androidMain.dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.ktor.client.okhttp)
}

sourceSets.desktopMain.dependencies {
implementation(compose.desktop.currentOs)

implementation(libs.kotlinx.coroutines.swing)
implementation(libs.ktor.client.okhttp)
}

sourceSets.iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}

sourceSets.jsMain.dependencies {
implementation(libs.ktor.client.js)
}

sourceSets.wasmJsMain.dependencies {
implementation(libs.ktor.client.wasm)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mohamedrejeb.calf.sample.coil

import androidx.compose.runtime.Composable
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.annotation.ExperimentalCoilApi
import coil3.memory.MemoryCache
import coil3.network.ktor3.KtorNetworkFetcherFactory
import coil3.request.crossfade
import coil3.util.DebugLogger
import com.mohamedrejeb.calf.picker.coil.KmpFileFetcher

@OptIn(ExperimentalCoilApi::class)
@Composable
fun setSingletonImageLoaderFactory() {
coil3.compose.setSingletonImageLoaderFactory {
newImageLoader(
context = it,
debug = false,
)
}
}

private fun newImageLoader(
context: PlatformContext,
debug: Boolean = false,
): ImageLoader {
return ImageLoader.Builder(context)
.components {
add(KtorNetworkFetcherFactory())

// Add a custom fetcher for loading KmpFile objects.
add(KmpFileFetcher.Factory())
}
.memoryCache {
MemoryCache.Builder()
// Set the max size to 25% of the app's available memory.
.maxSizePercent(context, percent = 0.25)
.build()
}
// Show a short crossfade when loading images asynchronously.
.crossfade(true)
// Enable logging if this is a debug build.
.apply {
if (debug) {
logger(DebugLogger())
}
}
.build()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mohamedrejeb.calf.sample.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -24,67 +23,40 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import com.mohamedrejeb.calf.core.LocalPlatformContext
import com.mohamedrejeb.calf.io.readByteArray
import coil3.compose.AsyncImage
import com.mohamedrejeb.calf.io.KmpFile
import com.mohamedrejeb.calf.picker.FilePickerFileType
import com.mohamedrejeb.calf.picker.FilePickerSelectionMode
import com.mohamedrejeb.calf.picker.rememberFilePickerLauncher
import com.mohamedrejeb.calf.picker.toImageBitmap
import com.mohamedrejeb.calf.ui.toggle.AdaptiveSwitch
import kotlinx.coroutines.launch

@Composable
fun ImagePickerScreen(navigateBack: () -> Unit) {
val context = LocalPlatformContext.current
val scope = rememberCoroutineScope()

var imageBitmaps by remember {
mutableStateOf<List<ImageBitmap>>(emptyList())
var files by remember {
mutableStateOf<List<KmpFile>>(emptyList())
}
var isMultiple by remember { mutableStateOf(false) }

val singlePickerLauncher =
rememberFilePickerLauncher(
type = FilePickerFileType.Image,
selectionMode = FilePickerSelectionMode.Single,
onResult = { files ->
scope.launch {
imageBitmaps =
files.mapNotNull {
try {
it.readByteArray(context).toImageBitmap()
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
onResult = {
files = it
},
)

val multiplePickerLauncher =
rememberFilePickerLauncher(
type = FilePickerFileType.Image,
selectionMode = FilePickerSelectionMode.Multiple,
onResult = { files ->
scope.launch {
imageBitmaps =
files.mapNotNull {
try {
it.readByteArray(context).toImageBitmap()
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
onResult = {
files = it
},
)

Expand Down Expand Up @@ -149,9 +121,9 @@ fun ImagePickerScreen(navigateBack: () -> Unit) {
modifier = Modifier.padding(16.dp),
)

imageBitmaps.forEach {
Image(
bitmap = it,
files.forEach {
AsyncImage(
model = it,
contentDescription = "Image",
contentScale = ContentScale.FillWidth,
modifier =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import com.mohamedrejeb.calf.sample.coil.setSingletonImageLoaderFactory

private val DarkColorScheme =
darkColorScheme(
Expand Down Expand Up @@ -36,6 +37,8 @@ internal fun CalfTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit,
) {
setSingletonImageLoaderFactory()

val colorScheme =
if (darkTheme)
DarkColorScheme
Expand Down
6 changes: 1 addition & 5 deletions sample/web-js/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,4 @@ kotlin {
implementation(compose.foundation)
implementation(compose.ui)
}
}

compose.experimental {
web.application {}
}
}
6 changes: 1 addition & 5 deletions sample/web-wasm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

plugins {
alias(libs.plugins.kotlinMultiplatform)
Expand Down Expand Up @@ -26,7 +26,3 @@ kotlin {
implementation(compose.ui)
}
}

compose.experimental {
web.application {}
}

0 comments on commit 93b84f9

Please sign in to comment.