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 473
Issue #7313: Use ThumbnailLoader for the TabViewHolder #7356
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc6a582
Close #7352: Always set scaleFactor on TabThumbnailView
jonalmeida 0a95c91
Close #7313: Use storage/loader to get thumbnails
jonalmeida 9c1a921
Issue #7313: Update sample browser to use thumbnail loader
jonalmeida 1f143e1
Issue #7313: Add changelog for tabs tray changes
jonalmeida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would putting this parameter after the closure prevent us from using the trailing closures when initializing the
TabsAdapter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boek I explicitly wanted to avoid that since we've had issues (mozilla-mobile/fenix#10495) where the scope of the trailing closure did not make it obvious it was being invoked during the
onCreateViewHolder
call of the adapter and that could cause crashes depending on what work was done in that closure.I thought it was better to have an explicit named param in that case to avoid regressions.