Skip to content

Commit

Permalink
302 refactor the code (#303)
Browse files Browse the repository at this point in the history
* refactor: prefetch in Android

* refactor: prefetch in iOS

* chore: remove unnecessary import statements
  • Loading branch information
duguyihou authored Aug 18, 2024
1 parent 3762bcd commit d5101fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions android/src/main/java/com/turboimage/TurboImageModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,41 @@ package com.turboimage

import coil.Coil
import coil.annotation.ExperimentalCoilApi
import coil.imageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest
import com.facebook.react.bridge.*
import com.facebook.react.bridge.ReactContextBaseJavaModule
import okhttp3.Headers

class TurboImageModule(private val context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
private val imageLoader = Coil.imageLoader(context)

override fun getName(): String = REACT_CLASS

@ReactMethod
fun prefetch(sources: ReadableArray, promise: Promise) {
sources.toArrayList().forEach { source ->
val imageRequests = sources.toArrayList().map { source ->
val uri = (source as HashMap<*, *>)["uri"] as String
val headers = source["headers"] as? HashMap<*, *>

if (headers != null) {
val requestHeaders = Headers.Builder()
val headersBuilder = Headers.Builder()
headers.map { (key, value) ->
requestHeaders.add(key as String, value as String)
headersBuilder.add(key as String, value as String)
}
val request = ImageRequest.Builder(context)
.headers(requestHeaders.build())
ImageRequest.Builder(context)
.headers(headersBuilder.build())
.data(uri)
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.build()
context.imageLoader.enqueue(request)
} else {
val request = ImageRequest.Builder(context)
ImageRequest.Builder(context)
.data(uri)
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.build()
context.imageLoader.enqueue(request)
}
}
imageRequests.forEach { imageRequest ->
imageLoader.enqueue(imageRequest)
}
promise.resolve("Success")
}

Expand Down
2 changes: 1 addition & 1 deletion ios/TurboImageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension TurboImageViewManager {
return ImageRequest(urlRequest: urlRequest)
}.compactMap{ $0 }

prefetcher._startPrefetching(with: imageRequests)
prefetcher.startPrefetching(with: imageRequests)
resolve("Success")
}

Expand Down

0 comments on commit d5101fc

Please sign in to comment.