This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
7356: Issue #7313: Use ThumbnailLoader for the TabViewHolder r=gabrielluong,boek a=jonalmeida Instead of trying to inline the thumbnail images from disk into the TabsTrayPresenter, we can load them from the `ThumbnailStorage` via the `ThumbnailLoader` and rely on the `TabsTrayPresenter` to consume new thumbnail updates only from the store. This fixes some perf issues, inconsistencies, and duplicate updates to the tabs tray. Co-authored-by: Jonathan Almeida <[email protected]>
- Loading branch information
Showing
22 changed files
with
345 additions
and
45 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
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
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: 68 additions & 0 deletions
68
.../thumbnails/src/main/java/mozilla/components/browser/thumbnails/loader/ThumbnailLoader.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,68 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.browser.thumbnails.loader | ||
|
||
import android.graphics.drawable.Drawable | ||
import android.widget.ImageView | ||
import androidx.annotation.MainThread | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import mozilla.components.browser.thumbnails.R | ||
import mozilla.components.browser.thumbnails.storage.ThumbnailStorage | ||
import mozilla.components.support.images.CancelOnDetach | ||
import mozilla.components.support.images.loader.ImageLoader | ||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* An implementation of [ImageLoader] for loading thumbnails into a [ImageView]. | ||
*/ | ||
class ThumbnailLoader(private val storage: ThumbnailStorage) : ImageLoader { | ||
|
||
override fun loadIntoView( | ||
view: ImageView, | ||
id: String, | ||
placeholder: Drawable?, | ||
error: Drawable? | ||
) { | ||
CoroutineScope(Dispatchers.Main).launch { | ||
loadIntoViewInternal(WeakReference(view), id, placeholder, error) | ||
} | ||
} | ||
|
||
@MainThread | ||
private suspend fun loadIntoViewInternal( | ||
view: WeakReference<ImageView>, | ||
id: String, | ||
placeholder: Drawable?, | ||
error: Drawable? | ||
) { | ||
// If we previously started loading into the view, cancel the job. | ||
val existingJob = view.get()?.getTag(R.id.mozac_browser_thumbnails_tag_job) as? Job | ||
existingJob?.cancel() | ||
|
||
view.get()?.setImageDrawable(placeholder) | ||
|
||
// Create a loading job | ||
val deferredThumbnail = storage.loadThumbnail(id) | ||
|
||
view.get()?.setTag(R.id.mozac_browser_thumbnails_tag_job, deferredThumbnail) | ||
val onAttachStateChangeListener = CancelOnDetach(deferredThumbnail).also { | ||
view.get()?.addOnAttachStateChangeListener(it) | ||
} | ||
|
||
try { | ||
val thumbnail = deferredThumbnail.await() | ||
view.get()?.setImageBitmap(thumbnail) | ||
} catch (e: CancellationException) { | ||
view.get()?.setImageDrawable(error) | ||
} finally { | ||
view.get()?.removeOnAttachStateChangeListener(onAttachStateChangeListener) | ||
view.get()?.setTag(R.id.mozac_browser_thumbnails_tag_job, null) | ||
} | ||
} | ||
} |
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,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
|
||
<resources> | ||
<item name="mozac_browser_thumbnails_tag_job" type="id" /> | ||
</resources> |
Oops, something went wrong.