Skip to content

Commit

Permalink
Cleanup ImageLoaderModule (facebook#44693)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#44693

Changelog: [Internal]
Kotlin related simplifications

Reviewed By: oprisnik

Differential Revision: D57858309

fbshipit-source-id: 02ae17db13f5877ab83b05beadff3ff666aeff81
  • Loading branch information
steelrooter authored and facebook-github-bot committed May 28, 2024
1 parent 24aed6e commit 3c5a3e0
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
*/
@ReactMethod
override public fun getSize(uriString: String?, promise: Promise) {
if (uriString == null || uriString.isEmpty()) {
if (uriString.isNullOrEmpty()) {
promise.reject(ERROR_INVALID_URI, "Cannot get the size of an image for an empty URI")
return
}
Expand All @@ -94,16 +94,16 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
protected override fun onNewResultImpl(
dataSource: DataSource<CloseableReference<CloseableImage>>
) {
if (!dataSource.isFinished()) {
if (!dataSource.isFinished) {
return
}
val ref = dataSource.getResult()
val ref = dataSource.result
if (ref != null) {
try {
val image: CloseableImage = ref.get()
val sizes: WritableMap = Arguments.createMap()
sizes.putInt("width", image.getWidth())
sizes.putInt("height", image.getHeight())
sizes.putInt("width", image.width)
sizes.putInt("height", image.height)
promise.resolve(sizes)
} catch (e: Exception) {
promise.reject(ERROR_GET_SIZE_FAILURE, e)
Expand All @@ -118,7 +118,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
protected override fun onFailureImpl(
dataSource: DataSource<CloseableReference<CloseableImage>>
) {
promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.getFailureCause())
promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.failureCause)
}
}
dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance())
Expand All @@ -138,7 +138,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
headers: ReadableMap?,
promise: Promise
) {
if (uriString == null || uriString.isEmpty()) {
if (uriString.isNullOrEmpty()) {
promise.reject(ERROR_INVALID_URI, "Cannot get the size of an image for an empty URI")
return
}
Expand All @@ -154,16 +154,16 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
protected override fun onNewResultImpl(
dataSource: DataSource<CloseableReference<CloseableImage>>
) {
if (!dataSource.isFinished()) {
if (!dataSource.isFinished) {
return
}
val ref = dataSource.getResult()
val ref = dataSource.result
if (ref != null) {
try {
val image: CloseableImage = ref.get()
val sizes: WritableMap = Arguments.createMap()
sizes.putInt("width", image.getWidth())
sizes.putInt("height", image.getHeight())
sizes.putInt("width", image.width)
sizes.putInt("height", image.height)
promise.resolve(sizes)
} catch (e: Exception) {
promise.reject(ERROR_GET_SIZE_FAILURE, e)
Expand All @@ -178,7 +178,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
protected override fun onFailureImpl(
dataSource: DataSource<CloseableReference<CloseableImage>>
) {
promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.getFailureCause())
promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.failureCause)
}
}
dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance())
Expand All @@ -198,7 +198,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
promise: Promise
) {
val requestId = requestIdAsDouble.toInt()
if (uriString == null || uriString.isEmpty()) {
if (uriString.isNullOrEmpty()) {
promise.reject(ERROR_INVALID_URI, "Cannot prefetch an image for an empty URI")
return
}
Expand Down Expand Up @@ -243,14 +243,14 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis
@ReactMethod
override public fun queryCache(uris: ReadableArray, promise: Promise) {
// perform cache interrogation in async task as disk cache checks are expensive
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "StaticFieldLeak")
object : GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
protected override fun doInBackgroundGuarded(vararg params: Void) {
val result: WritableMap = Arguments.createMap()
val imagePipeline: ImagePipeline = this@ImageLoaderModule.imagePipeline
for (i in 0 until uris.size()) {
val uriString: String = uris.getString(i)
if (!uriString.isNullOrEmpty()) {
if (uriString.isNotEmpty()) {
val uri = Uri.parse(uriString)
if (imagePipeline.isInBitmapMemoryCache(uri)) {
result.putString(uriString, "memory")
Expand All @@ -271,7 +271,7 @@ public class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventLis

private fun removeRequest(requestId: Int): DataSource<Void?>? {
synchronized(enqueuedRequestMonitor) {
val request: DataSource<Void?> = enqueuedRequests.get(requestId)
val request: DataSource<Void?>? = enqueuedRequests.get(requestId)
enqueuedRequests.remove(requestId)
return request
}
Expand Down

0 comments on commit 3c5a3e0

Please sign in to comment.