@@ -59,11 +59,12 @@ import com.google.android.gms.maps.model.LatLng
5959import com.google.maps.android.ktx.addCircle
6060import kotlinx.coroutines.CancellationException
6161import kotlinx.coroutines.CompletableDeferred
62- import kotlinx.coroutines.Dispatchers
62+ import kotlinx.coroutines.CoroutineDispatcher
6363import kotlinx.coroutines.launch
6464import kotlinx.coroutines.withContext
6565import kotlinx.coroutines.withTimeoutOrNull
6666import org.koin.android.ext.android.inject
67+ import org.koin.core.qualifier.named
6768import timber.log.Timber
6869import java.util.Locale
6970
@@ -121,6 +122,7 @@ class QuakeDetailsActivity : AppCompatActivity(), OnMapReadyCallback {
121122 private var isSnapshotRequest: Boolean? = null
122123
123124 private val quakeStoryRenderer: QuakeStoryRenderer by inject()
125+ private val ioDispatcher: CoroutineDispatcher by inject(named(" ioDispatcher" ))
124126
125127 private lateinit var binding: ActivityQuakeDetailsBinding
126128
@@ -178,11 +180,9 @@ class QuakeDetailsActivity : AppCompatActivity(), OnMapReadyCallback {
178180 }
179181
180182 private fun refreshAd () {
181- lifecycleScope.launch(Dispatchers .IO ) {
182- MobileAds .initialize(this @QuakeDetailsActivity)
183- withContext(Dispatchers .Main ) {
184- loadNativeAd()
185- }
183+ lifecycleScope.launch {
184+ withContext(ioDispatcher) { MobileAds .initialize(this @QuakeDetailsActivity) }
185+ loadNativeAd()
186186 }
187187 }
188188
@@ -386,36 +386,36 @@ class QuakeDetailsActivity : AppCompatActivity(), OnMapReadyCallback {
386386
387387 /* *
388388 * Renders all [StickerDesign] variants (map snapshot may be null if it couldn't be
389- * captured) and opens [ShareQuakeBottomSheet] with them. Rendering happens off the main
390- * thread: the sticker views are never attached to a window, so it's safe to
391- * measure/layout/draw them from any thread, and PNG compression to the cache dir is I/O.
392- * Designs are rendered sequentially and each bitmap is recycled right after it's cached -
393- * doing all three at once would keep ~18 MB of bitmaps alive simultaneously.
389+ * captured) and opens [ShareQuakeBottomSheet] with them. Rendering happens on [ioDispatcher]:
390+ * the sticker views are never attached to a window, so it's safe to measure/layout/draw them
391+ * off the main thread, and PNG compression to the cache dir is I/O. Designs are rendered
392+ * sequentially and each bitmap is recycled right after it's cached - doing all three at once
393+ * would keep ~18 MB of bitmaps alive simultaneously.
394394 */
395395 private fun shareQuake (quake : Quake ) {
396396 captureMapSnapshot { mapSnapshot ->
397- lifecycleScope.launch( Dispatchers . Default ) {
397+ lifecycleScope.launch {
398398 try {
399- clearShareImageCache()
400-
401- val stickerUris = StickerDesign .entries.map { design ->
402- val sticker = quakeStoryRenderer.renderSticker(quake, mapSnapshot, design)
403- cacheImageUri(sticker, " sticker-${quake.quakeCode} -${design.name} " , Bitmap .CompressFormat .PNG )
404- .also { sticker.recycle() }
399+ val stickerUris = withContext(ioDispatcher) {
400+ clearShareImageCache()
401+
402+ StickerDesign .entries.map { design ->
403+ val sticker = quakeStoryRenderer.renderSticker(quake, mapSnapshot, design)
404+ cacheImageUri(sticker, " sticker-${quake.quakeCode} -${design.name} " , Bitmap .CompressFormat .PNG )
405+ .also { sticker.recycle() }
406+ }
405407 }
406- val magnitudeColor = quakeStoryRenderer.magnitudeColor(quake)
407408
408- withContext(Dispatchers .Main ) {
409- if (supportFragmentManager.isStateSaved) return @withContext
409+ if (supportFragmentManager.isStateSaved) return @launch
410410
411- ShareQuakeBottomSheet .newInstance(quake, stickerUris, magnitudeColor)
412- .show(supportFragmentManager, ShareQuakeBottomSheet . TAG )
413- }
411+ ShareQuakeBottomSheet
412+ .newInstance(quake, stickerUris, quakeStoryRenderer.magnitudeColor(quake) )
413+ .show(supportFragmentManager, ShareQuakeBottomSheet . TAG )
414414 } catch (e: CancellationException ) {
415415 throw e
416416 } catch (e: Exception ) {
417417 Timber .e(e, " Share image generation failed" )
418- withContext( Dispatchers . Main ) { toast(R .string.SHARE_ERROR ) }
418+ toast(R .string.SHARE_ERROR )
419419 }
420420 }
421421 }
0 commit comments