Skip to content

Commit

Permalink
[example] Migrate the image viewer on the new compose resources (#4433)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok authored Mar 8, 2024
1 parent 71e0c92 commit 52c4bf3
Show file tree
Hide file tree
Showing 50 changed files with 95 additions and 128 deletions.
14 changes: 5 additions & 9 deletions examples/imageviewer/desktopApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ plugins {
}

kotlin {
jvm {
withJava()
}
jvm()
sourceSets {
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation(project(":shared"))
}
jvmMain.dependencies {
implementation(compose.desktop.currentOs)
implementation(project(":shared"))
}
}
}
Expand All @@ -28,7 +24,7 @@ compose.desktop {
packageName = "ImageViewer"
packageVersion = "1.0.0"

val iconsRoot = project.file("../shared/src/commonMain/resources")
val iconsRoot = project.file("desktop-icons")
macOS {
iconFile.set(iconsRoot.resolve("icon-mac.icns"))
}
Expand Down
4 changes: 0 additions & 4 deletions examples/imageviewer/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ org.gradle.configuration-cache=true
org.gradle.caching=true
org.jetbrains.compose.experimental.jscanvas.enabled=true
org.jetbrains.compose.experimental.macos.enabled=true
kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.native.useEmbeddableCompilerJar=true
# Enable kotlin/native experimental memory model
kotlin.native.binary.memoryModel=experimental
kotlin.version=1.9.22
agp.version=8.0.2
compose.version=1.6.0
75 changes: 31 additions & 44 deletions examples/imageviewer/shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("OPT_IN_IS_NOT_ENABLED")

plugins {
kotlin("multiplatform")
id("com.android.library")
Expand All @@ -26,47 +24,41 @@ kotlin {
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
//implementation(compose.materialIconsExtended) // TODO not working on iOS for now
implementation("org.jetbrains.compose.components:components-resources:1.6.0-dev1275")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")

// Kotlin Coroutines 1.7.1 contains Dispatchers.IO for iOS
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
all {
languageSettings {
optIn("org.jetbrains.compose.resources.ExperimentalResourceApi")
}
}
val androidMain by getting {
dependencies {
api("androidx.activity:activity-compose:1.7.2")
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.10.1")
implementation("androidx.camera:camera-camera2:1.2.3")
implementation("androidx.camera:camera-lifecycle:1.2.3")
implementation("androidx.camera:camera-view:1.2.3")
implementation("com.google.accompanist:accompanist-permissions:0.29.2-rc")
implementation("com.google.android.gms:play-services-maps:18.1.0")
implementation("com.google.android.gms:play-services-location:21.0.1")
implementation("com.google.maps.android:maps-compose:2.11.2")
}
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.components.resources)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}

val desktopMain by getting {
dependencies {
implementation(compose.desktop.common)
implementation(project(":mapview-desktop"))
}
androidMain.dependencies {
api("androidx.activity:activity-compose:1.8.2")
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.12.0")
implementation("androidx.camera:camera-camera2:1.3.1")
implementation("androidx.camera:camera-lifecycle:1.3.1")
implementation("androidx.camera:camera-view:1.3.1")
implementation("com.google.accompanist:accompanist-permissions:0.29.2-rc")
implementation("com.google.android.gms:play-services-maps:18.2.0")
implementation("com.google.android.gms:play-services-location:21.1.0")
implementation("com.google.maps.android:maps-compose:2.11.2")
}

val desktopTest by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation(compose.desktop.uiTestJUnit4)
}
val desktopMain by getting
desktopMain.dependencies {
implementation(compose.desktop.common)
implementation(project(":mapview-desktop"))
}
val desktopTest by getting
desktopTest.dependencies {
implementation(compose.desktop.currentOs)
implementation(compose.desktop.uiTestJUnit4)
}
}
}
Expand All @@ -76,15 +68,10 @@ android {
namespace = "example.imageviewer.shared"
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

defaultConfig {
minSdk = 26
}
compileOptions {

This comment has been minimized.

Copy link
@igordmn

igordmn Mar 11, 2024

Collaborator

After removing these lines, ./gradlew compileReleaseKotlinAndroid fails with:

* What went wrong:
Execution failed for task ':shared:compileReleaseKotlinAndroid'.
> Inconsistent JVM-target compatibility detected for tasks 'compileReleaseJavaWithJavac' (1.8) and 'compileReleaseKotlinAndroid' (17).

Restoring it helps.

cc @terrakok

JAVA_HOME points to JDK 17

PR: #4467

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlin {
jvmToolchain(17)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import example.imageviewer.ImageStorage
import example.imageviewer.PlatformStorableImage
import example.imageviewer.model.PictureData
import example.imageviewer.toImageBitmap
import imageviewer.shared.generated.resources.Res
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.readResourceBytes
import java.io.File

private const val maxStorableImageSizePx = 2000
Expand Down Expand Up @@ -102,7 +100,6 @@ class AndroidImageStorage(
picture.jpgFile.readBytes().toImageBitmap()
}

@OptIn(ExperimentalResourceApi::class)
suspend fun getUri(context: Context, picture: PictureData): Uri = withContext(Dispatchers.IO) {
if (!sharedImagesDir.exists()) {
sharedImagesDir.mkdirs()
Expand All @@ -117,7 +114,7 @@ class AndroidImageStorage(
if (!tempFileToShare.exists()) {
tempFileToShare.createNewFile()
}
tempFileToShare.writeBytes(readResourceBytes(picture.resource))
tempFileToShare.writeBytes(Res.readBytes(picture.resource))
}
}
FileProvider.getUriForFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ import example.imageviewer.icon.IconPhotoCamera
import example.imageviewer.model.GpsPosition
import example.imageviewer.model.PictureData
import example.imageviewer.model.createCameraPictureData
import imageviewer.shared.generated.resources.Res
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.readResourceBytes
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.Executors
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
Expand Down Expand Up @@ -69,7 +67,6 @@ actual fun CameraView(
}
}

@OptIn(ExperimentalResourceApi::class)
@SuppressLint("MissingPermission")
@Composable
private fun CameraWithGrantedPermission(
Expand Down Expand Up @@ -176,7 +173,7 @@ private fun CameraWithGrantedPermission(
delay(5000)
if (capturePhotoStarted) {
addLocationInfoAndReturnResult(
readResourceBytes("android-emulator-photo.jpg").toImageBitmap()
Res.readBytes("files/android-emulator-photo.jpg").toImageBitmap()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.ImageBitmap
import example.imageviewer.filter.PlatformContext
import example.imageviewer.model.PictureData
import imageviewer.shared.generated.resources.Res
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.readResourceBytes

@OptIn(ExperimentalResourceApi::class)
abstract class Dependencies {
abstract val notification: Notification
abstract val imageStorage: ImageStorage
Expand All @@ -22,7 +20,7 @@ abstract class Dependencies {
val imageProvider: ImageProvider = object : ImageProvider {
override suspend fun getImage(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> {
readResourceBytes(picture.resource).toImageBitmap()
Res.readBytes(picture.resource).toImageBitmap()
}

is PictureData.Camera -> {
Expand All @@ -32,7 +30,7 @@ abstract class Dependencies {

override suspend fun getThumbnail(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> {
readResourceBytes(picture.thumbnailResource).toImageBitmap()
Res.readBytes(picture.thumbnailResource).toImageBitmap()
}

is PictureData.Camera -> {
Expand Down
Loading

0 comments on commit 52c4bf3

Please sign in to comment.