-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lp/image preloading executors (#505)
* Prefetch using executors - we had added code to prefetch using coroutines - executors support for same task is added - made method to return resource executor - need to handle null config as there is tight coupling - added method in IO executor to have callback on same thread. * Method overload + dependency - redendant dependency is removed - method overload to hide detail about return type * CtExecutors without config dependency - we needed to pass config just to have logger in it - logger static methods can be used if logging is not required on acc level - created methods to support the same * Method overload for resurce download executor - thread pool size defaults to 8 * Strategy pattern used - Instead of having same file with 2 methods we used strategy pattern. - Refactored into different files * Async -> Limited parallelism - used limited parallelism instead of using async - this will give desired functionality to limit no of threads * Impl -> Strategy - ref correction * Method invocation - called overloaded method. * Preload handling - preloading using coroutine executors - parsed inapp media to get urls - removed inapp dismiss code, we have to figure correct place * Handled cache parsing failure - we fallback to api - this case is unlikely but added as fail safe
- Loading branch information
Showing
17 changed files
with
276 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
clevertap-core/src/main/java/com/clevertap/android/sdk/inapp/images/InAppImagePreloader.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...e/src/main/java/com/clevertap/android/sdk/inapp/images/preload/InAppImagePreloadConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.clevertap.android.sdk.inapp.images.preload | ||
|
||
internal data class InAppImagePreloadConfig( | ||
val parallelDownloads: Int, | ||
) { | ||
companion object { | ||
private const val DEFAULT_PARALLEL_DOWNLOAD = 4 | ||
|
||
fun default() : InAppImagePreloadConfig = InAppImagePreloadConfig( | ||
parallelDownloads = DEFAULT_PARALLEL_DOWNLOAD | ||
) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../main/java/com/clevertap/android/sdk/inapp/images/preload/InAppImagePreloaderCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.clevertap.android.sdk.inapp.images.preload | ||
|
||
import com.clevertap.android.sdk.ILogger | ||
import com.clevertap.android.sdk.inapp.images.InAppResourceProvider | ||
import com.clevertap.android.sdk.utils.CtDefaultDispatchers | ||
import com.clevertap.android.sdk.utils.DispatcherProvider | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import kotlin.system.measureTimeMillis | ||
|
||
internal class InAppImagePreloaderCoroutine @JvmOverloads constructor( | ||
override val inAppImageProvider: InAppResourceProvider, | ||
override val logger: ILogger? = null, | ||
private val dispatchers: DispatcherProvider = CtDefaultDispatchers(), | ||
override val config: InAppImagePreloadConfig = InAppImagePreloadConfig.default() | ||
) : InAppImagePreloaderStrategy { | ||
|
||
private val jobs: MutableList<Job> = mutableListOf() | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun preloadImages(urls: List<String>) { | ||
|
||
val handler = CoroutineExceptionHandler { _, throwable -> | ||
logger?.verbose("Cancelled image pre fetch \n ${throwable.stackTrace}") | ||
} | ||
val scope = CoroutineScope(dispatchers.io().limitedParallelism(config.parallelDownloads)) | ||
|
||
urls.forEach { url -> | ||
val job = scope.launch(handler) { | ||
logger?.verbose("started image url fetch $url") | ||
|
||
val mils = measureTimeMillis { | ||
inAppImageProvider.fetchInAppImage(url) | ||
} | ||
|
||
logger?.verbose("finished image url fetch $url in $mils ms") | ||
} | ||
jobs.add(job) | ||
} | ||
} | ||
|
||
override fun cleanup() { | ||
jobs.map { job -> job.cancel() } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../main/java/com/clevertap/android/sdk/inapp/images/preload/InAppImagePreloaderExecutors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.clevertap.android.sdk.inapp.images.preload | ||
|
||
import com.clevertap.android.sdk.ILogger | ||
import com.clevertap.android.sdk.inapp.images.InAppResourceProvider | ||
import com.clevertap.android.sdk.task.CTExecutors | ||
|
||
internal class InAppImagePreloaderExecutors @JvmOverloads constructor( | ||
private val executor: CTExecutors, | ||
override val inAppImageProvider: InAppResourceProvider, | ||
override val logger: ILogger? = null, | ||
override val config: InAppImagePreloadConfig = InAppImagePreloadConfig.default() | ||
) : InAppImagePreloaderStrategy { | ||
|
||
override fun preloadImages(urls: List<String>) { | ||
|
||
for (url in urls) { | ||
val task = executor.ioTaskNonUi<Void>() | ||
|
||
task.execute("tag") { | ||
inAppImageProvider.fetchInAppImage(url) | ||
null | ||
} | ||
} | ||
} | ||
|
||
override fun cleanup() { | ||
//executor?.shutdown | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...c/main/java/com/clevertap/android/sdk/inapp/images/preload/InAppImagePreloaderStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.clevertap.android.sdk.inapp.images.preload | ||
|
||
import com.clevertap.android.sdk.ILogger | ||
import com.clevertap.android.sdk.inapp.images.InAppResourceProvider | ||
|
||
internal interface InAppImagePreloaderStrategy { | ||
|
||
val inAppImageProvider: InAppResourceProvider | ||
|
||
val logger: ILogger? | ||
|
||
val config: InAppImagePreloadConfig | ||
|
||
fun preloadImages(urls: List<String>) | ||
fun cleanup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.