Skip to content

Commit

Permalink
refactor(prefetch): android
Browse files Browse the repository at this point in the history
  • Loading branch information
duguyihou committed Sep 14, 2024
1 parent b854e76 commit fba68e9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions android/src/main/java/com/turboimage/TurboImageModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import okhttp3.Headers

class TurboImageModule(private val context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
private var imageLoader: ImageLoader? = null
private var imageLoader: ImageLoader? = null

override fun getName(): String = REACT_CLASS

@ReactMethod
fun prefetch(sources: ReadableArray, cachePolicy: String, promise: Promise) {
var completedRequestCount = 0
fun handleRequestCount() {
completedRequestCount++
if (sources.size() == completedRequestCount) {
promise.resolve(true)
}
}

val imageRequests = sources.toArrayList().map { source ->
val uri = (source as HashMap<*, *>)["uri"] as String
val headers = source["headers"] as? HashMap<*, *>
Expand All @@ -28,10 +36,18 @@ class TurboImageModule(private val context: ReactApplicationContext) :
ImageRequest.Builder(context)
.headers(headersBuilder.build())
.data(uri)
.listener(
onSuccess = { _, _ -> handleRequestCount() },
onError = { _, _ -> handleRequestCount() }
)
.build()
} else {
ImageRequest.Builder(context)
.data(uri)
.listener(
onSuccess = { _, _ -> handleRequestCount() },
onError = { _, _ -> handleRequestCount() }
)
.build()
}
}
Expand All @@ -41,7 +57,6 @@ class TurboImageModule(private val context: ReactApplicationContext) :
imageRequests.forEach { imageRequest ->
imageLoader?.enqueue(imageRequest)
}
promise.resolve("Success")
}

@ReactMethod
Expand Down

0 comments on commit fba68e9

Please sign in to comment.