Skip to content

Commit

Permalink
removed coil
Browse files Browse the repository at this point in the history
  • Loading branch information
ismai117 committed Oct 21, 2024
1 parent 4a1e8e4 commit 77600ce
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 152 deletions.
9 changes: 2 additions & 7 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ kotlin {
}

wasmJs {
moduleName = "composeApp"
browser {
commonWebpackConfig {
outputFileName = "composeApp.js"
}
}
browser()
binaries.executable()
}

Expand Down Expand Up @@ -78,9 +73,9 @@ kotlin {
implementation(libs.kotlinx.serialization.json)
implementation(libs.bundles.ktor.common)
implementation(libs.bundles.koin.common)
implementation(libs.bundles.coil.common)
implementation(libs.napier)
implementation(libs.windowSizeClass)
api("io.github.qdsfdhvh:image-loader:1.9.0")
}
desktopMain.dependencies {
implementation(compose.desktop.currentOs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package imageLoader

import applicationContext
import com.seiko.imageloader.ImageLoader
import com.seiko.imageloader.component.setupDefaultComponents
import com.seiko.imageloader.intercept.painterMemoryCacheConfig
import com.seiko.imageloader.option.androidContext
import okio.Path.Companion.toOkioPath

actual fun generateImageLoader(): ImageLoader {
return ImageLoader {
options {
androidContext(applicationContext)
}
components {
setupDefaultComponents()
}
interceptor {
painterMemoryCacheConfig {
maxSize(50)
}
diskCacheConfig {
directory(applicationContext.cacheDir.resolve("image_cache").toOkioPath())
maxSizeBytes(512L * 1024 * 1024) // 512MB
}
}
}
}
33 changes: 14 additions & 19 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.navigation.compose.rememberNavController
import coil3.annotation.ExperimentalCoilApi
import coil3.asyncImageLoader
import coil3.compose.setSingletonImageLoaderFactory
import coil3.enableDiskCache
import com.seiko.imageloader.LocalImageLoader
import imageLoader.generateImageLoader
import navigation.Navigation
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.koin.compose.KoinApplication
import planets.di.planetsModule
import theme.PlanetsKMPTheme

@OptIn(ExperimentalCoilApi::class)
@Composable
@Preview
fun App(disableDiskCache: Boolean = false) = PlanetsKMPTheme {
fun App() = PlanetsKMPTheme {

val navController = rememberNavController()

Expand All @@ -30,20 +27,18 @@ fun App(disableDiskCache: Boolean = false) = PlanetsKMPTheme {
)
}
){

setSingletonImageLoaderFactory { context ->
if (disableDiskCache) context.asyncImageLoader() else
context.asyncImageLoader().enableDiskCache()
}

Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
CompositionLocalProvider(
LocalImageLoader provides remember { generateImageLoader() },
) {
Navigation(
navController = navController
)
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
) {
Navigation(
navController = navController
)
}
}

}
Expand Down
55 changes: 0 additions & 55 deletions composeApp/src/commonMain/kotlin/coil3/ImageLoader.kt

This file was deleted.

5 changes: 5 additions & 0 deletions composeApp/src/commonMain/kotlin/imageLoader/ImageLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package imageLoader

import com.seiko.imageloader.ImageLoader

expect fun generateImageLoader(): ImageLoader
26 changes: 12 additions & 14 deletions composeApp/src/commonMain/kotlin/planets/PlanetDetailScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
Expand Down Expand Up @@ -45,13 +44,11 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layout
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil3.ImageLoader
import coil3.compose.AsyncImage
import com.seiko.imageloader.rememberImagePainter
import getPlatform
import org.koin.compose.koinInject
import planets.domain.domain.Planet
Expand Down Expand Up @@ -173,7 +170,10 @@ private fun SharedTransitionScope.Mobile(
.parallaxLayoutModifier(scrollState, 2)
// .border(width = 1.dp, color = Color.White),
) {
ImageLoader(
val painter = rememberImagePainter(url = planet.img)
Image(
painter = painter,
contentDescription = planet.imgDescription,
modifier = modifier
.fillMaxSize()
.sharedElement(
Expand All @@ -182,13 +182,10 @@ private fun SharedTransitionScope.Mobile(
boundsTransform = { _, _ ->
tween(durationMillis = 1000)
}
),
url = planet.img,
description = planet.imgDescription
)
)
}


Column(
modifier = modifier
.padding(top = 32.dp)
Expand Down Expand Up @@ -246,7 +243,10 @@ private fun SharedTransitionScope.NonMobile(
.sizeIn(maxWidth = 500.dp, maxHeight = 500.dp)
.clip(CircleShape)
) {
ImageLoader(
val painter = rememberImagePainter(url = planet.img)
Image(
painter = painter,
contentDescription = planet.imgDescription,
modifier = modifier
.fillMaxSize()
.sharedElement(
Expand All @@ -255,9 +255,7 @@ private fun SharedTransitionScope.NonMobile(
boundsTransform = { _, _ ->
tween(durationMillis = 1000)
}
),
url = planet.img,
description = planet.imgDescription
)
)
}

Expand Down
23 changes: 7 additions & 16 deletions composeApp/src/commonMain/kotlin/planets/PlanetsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@ import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
Expand All @@ -45,13 +38,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil3.ImageLoader
import com.seiko.imageloader.rememberImagePainter
import getPlatform
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
Expand Down Expand Up @@ -147,7 +137,10 @@ fun SharedTransitionScope.PageItem(
.fillMaxWidth()
.heightIn(max = 400.dp)
) {
ImageLoader(
val painter = rememberImagePainter(url = planet.img)
Image(
painter = painter,
contentDescription = planet.imgDescription,
modifier = modifier
.fillMaxSize()
.sharedElement(
Expand All @@ -156,9 +149,7 @@ fun SharedTransitionScope.PageItem(
boundsTransform = { _, _ ->
tween(durationMillis = 1000)
}
),
url = planet.img,
description = planet.imgDescription,
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package imageLoader

import com.seiko.imageloader.ImageLoader
import com.seiko.imageloader.component.setupDefaultComponents
import com.seiko.imageloader.intercept.painterMemoryCacheConfig
import okio.Path.Companion.toOkioPath
import java.io.File

actual fun generateImageLoader(): ImageLoader {
return ImageLoader {
components {
setupDefaultComponents()
}
interceptor {
painterMemoryCacheConfig {
maxSize(50)
}
diskCacheConfig {
directory(getCacheDir().toOkioPath().resolve("image_cache"))
maxSizeBytes(512L * 1024 * 1024) // 512MB
}
}
}
}

enum class OperatingSystem {
Windows, Linux, MacOS, Unknown
}

private val currentOperatingSystem: OperatingSystem
get() {
val operSys = System.getProperty("os.name").lowercase()
return if (operSys.contains("win")) {
OperatingSystem.Windows
} else if (operSys.contains("nix") || operSys.contains("nux") ||
operSys.contains("aix")
) {
OperatingSystem.Linux
} else if (operSys.contains("mac")) {
OperatingSystem.MacOS
} else {
OperatingSystem.Unknown
}
}

private fun getCacheDir() = when (currentOperatingSystem) {
OperatingSystem.Windows -> File(System.getenv("AppData"), "$ApplicationName/cache")
OperatingSystem.Linux -> File(System.getProperty("user.home"), ".cache/$ApplicationName")
OperatingSystem.MacOS -> File(System.getProperty("user.home"), "Library/Caches/$ApplicationName")
else -> throw IllegalStateException("Unsupported operating system")
}

private const val ApplicationName = "Compose ImageLoader"
Loading

0 comments on commit 77600ce

Please sign in to comment.