Skip to content

Commit

Permalink
Merge branch 'master' into feature/refactor_size_resolver
Browse files Browse the repository at this point in the history
# Conflicts:
#	image-loader/src/commonMain/kotlin/com/seiko/imageloader/model/ImageRequest.kt
  • Loading branch information
qdsfdhvh committed Nov 2, 2023
2 parents ecc2fb8 + 364f73b commit fec8598
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 218 deletions.
15 changes: 0 additions & 15 deletions image-loader/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ kotlin {
implementation(compose.ui)
}
}
jvmMain {
dependencies {
implementation(libs.ktor.client.okhttp)
}
}
androidMain {
dependencies {
implementation(libs.kotlinx.coroutines.android)
Expand Down Expand Up @@ -73,16 +68,6 @@ kotlin {
}
}
}
appleMain {
dependencies {
implementation(libs.ktor.client.darwin)
}
}
jsMain {
dependencies {
implementation(libs.ktor.client.js)
}
}
val noJsMain by creating {
dependsOn(commonMain.get())
jvmMain.get().dependsOn(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.seiko.imageloader.util

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.darwin.Darwin
import okio.FileSystem

actual typealias WeakReference<T> = kotlin.native.ref.WeakReference<T>

internal actual val httpEngine: HttpClientEngine get() = Darwin.create()

internal actual val defaultFileSystem: FileSystem? get() = FileSystem.SYSTEM
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.transformLatest
Expand All @@ -27,11 +25,6 @@ interface ImageLoader {

fun async(request: ImageRequest): Flow<ImageAction> = async(flowOf(request))

@Deprecated("", ReplaceWith("Use imageloader.async(request).filterIsInstance<ImageResult>().first()"))
suspend fun execute(request: ImageRequest): ImageResult {
return async(request).filterIsInstance<ImageResult>().first()
}

companion object
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ fun rememberImageActionPainter(
errorPainter: (@Composable () -> Painter)? = null,
): Painter {
return when (action) {
is ImageEvent -> placeholderPainter?.invoke() ?: EmptyPainter
is ImageResult -> rememberImageResultPainter(
result = action,
filterQuality = filterQuality,
errorPainter = errorPainter,
)
is ImageAction.Success -> rememberImageSuccessPainter(action, filterQuality)
is ImageAction.Loading -> placeholderPainter?.invoke() ?: EmptyPainter
is ImageAction.Failure -> errorPainter?.invoke() ?: EmptyPainter
}
}

Expand All @@ -73,18 +70,26 @@ fun rememberImageResultPainter(
errorPainter: (@Composable () -> Painter)? = null,
): Painter {
return when (result) {
is ImageResult.OfPainter -> remember(result) {
result.painter
is ImageAction.Success -> rememberImageSuccessPainter(result, filterQuality)
is ImageAction.Failure -> errorPainter?.invoke() ?: EmptyPainter
}
}

@Composable
fun rememberImageSuccessPainter(
action: ImageAction.Success,
filterQuality: FilterQuality = DefaultFilterQuality,
): Painter {
return when (action) {
is ImageResult.OfPainter -> remember(action) {
action.painter
}
is ImageResult.OfBitmap -> remember(result, filterQuality) {
result.bitmap.toPainter(filterQuality)
is ImageResult.OfBitmap -> remember(action, filterQuality) {
action.bitmap.toPainter(filterQuality)
}
is ImageResult.OfImage -> remember(result) {
result.image.toPainter()
is ImageResult.OfImage -> remember(action) {
action.image.toPainter()
}
is ImageResult.OfError,
is ImageResult.OfSource,
-> errorPainter?.invoke() ?: EmptyPainter
}.also { painter ->
if (painter is AnimationPainter && painter.isPlay()) {
LaunchedEffect(painter) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DecodeInterceptor : Interceptor {
}
}

private fun DecodeResult.toImageResult() = when (this) {
private fun DecodeResult.toImageResult(): ImageResult = when (this) {
is DecodeResult.OfBitmap -> ImageResult.OfBitmap(bitmap = bitmap)
is DecodeResult.OfImage -> ImageResult.OfImage(image = image)
is DecodeResult.OfPainter -> ImageResult.OfPainter(painter = painter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,49 @@ import com.seiko.imageloader.Poko
import okio.BufferedSource

@Immutable
sealed interface ImageAction
sealed interface ImageAction {
sealed interface Loading : ImageAction
sealed interface Success : ImageAction
sealed interface Failure : ImageAction {
val error: Throwable
}
}

@Immutable
sealed interface ImageEvent : ImageAction {
object Start : ImageEvent
object StartWithMemory : ImageEvent
object StartWithDisk : ImageEvent
object StartWithFetch : ImageEvent

@Poko class Progress(val progress: Float) : ImageEvent
sealed interface ImageEvent : ImageAction.Loading {
data object Start : ImageEvent
data object StartWithMemory : ImageEvent
data object StartWithDisk : ImageEvent
data object StartWithFetch : ImageEvent
}

@Immutable
sealed interface ImageResult : ImageAction {

@Immutable
@Poko
class OfSource(
val source: BufferedSource,
val dataSource: DataSource,
val extra: ExtraData = EmptyExtraData,
) : ImageResult
class OfBitmap(val bitmap: Bitmap) : ImageResult, ImageAction.Success

@Immutable
@Poko
class OfBitmap(val bitmap: Bitmap) : ImageResult
class OfImage(val image: Image) : ImageResult, ImageAction.Success

@Immutable
@Poko
class OfImage(val image: Image) : ImageResult
class OfPainter(val painter: Painter) : ImageResult, ImageAction.Success

@Immutable
@Poko
class OfPainter(val painter: Painter) : ImageResult
class OfError(override val error: Throwable) : ImageResult, ImageAction.Failure

@Immutable
@Poko
class OfError(val error: Throwable) : ImageResult
class OfSource(
val source: BufferedSource,
val dataSource: DataSource,
val extra: ExtraData = EmptyExtraData,
) : ImageResult, ImageAction.Failure {
override val error: Throwable
get() = IllegalStateException("failure to decode image source")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.seiko.imageloader.model
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.painter.Painter
import com.seiko.imageloader.Poko
import com.seiko.imageloader.component.ComponentRegistry
import com.seiko.imageloader.component.ComponentRegistryBuilder
import com.seiko.imageloader.intercept.Interceptor
Expand All @@ -23,8 +22,6 @@ class ImageRequest internal constructor(
internal val components: ComponentRegistry?,
internal val interceptors: List<Interceptor>?,
) {
@Deprecated("", ReplaceWith("ImageRequest(request) {}"))
fun newBuilder(block: ImageRequestBuilder.() -> Unit) = ImageRequest(this, block)

override fun equals(other: Any?): Boolean {
return other is ImageRequest &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import com.seiko.imageloader.util.DEFAULT_MAX_IMAGE_SIZE
val maxImageSize: Int,
val extra: ExtraData,
) {

@Deprecated("", ReplaceWith("Options(options) {}"))
fun newBuilder(block: OptionsBuilder.() -> Unit) = Options(this, block)

companion object {
internal const val REPEAT_INFINITE = -1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.seiko.imageloader.util

import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineDispatcher
import okio.BufferedSource
Expand All @@ -17,9 +16,7 @@ internal expect suspend fun ByteReadChannel.source(): BufferedSource

internal expect val ioDispatcher: CoroutineDispatcher

internal expect val httpEngine: HttpClientEngine

internal expect val defaultFileSystem: FileSystem?

internal val httpEngineFactory: () -> HttpClient
get() = { HttpClient(httpEngine) }
get() = { HttpClient() }
Loading

0 comments on commit fec8598

Please sign in to comment.