This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move adapter Job setup code to helper class (#3407)
- Loading branch information
1 parent
eb1ea88
commit 8cd1a0c
Showing
8 changed files
with
74 additions
and
104 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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/org/mozilla/fenix/utils/AdapterWithJob.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 org.mozilla.fenix.utils | ||
|
||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import kotlinx.coroutines.Job | ||
|
||
/* 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/. */ | ||
|
||
/** | ||
* [RecyclerView.Adapter] with a [Job] for coroutines. | ||
* The adapterJob is setup when the adapter is attached to a RecyclerView and canceled when detached. | ||
*/ | ||
abstract class AdapterWithJob<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() { | ||
protected lateinit var adapterJob: Job | ||
|
||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { | ||
super.onAttachedToRecyclerView(recyclerView) | ||
adapterJob = Job() | ||
} | ||
|
||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { | ||
super.onDetachedFromRecyclerView(recyclerView) | ||
adapterJob.cancel() | ||
} | ||
} | ||
|
||
/** | ||
* [ListAdapter] with a [Job] for coroutines. | ||
* The adapterJob is setup when the adapter is attached to a RecyclerView and canceled when detached. | ||
*/ | ||
abstract class ListAdapterWithJob<T, VH : RecyclerView.ViewHolder>( | ||
diffCallback: DiffUtil.ItemCallback<T> | ||
) : ListAdapter<T, VH>(diffCallback) { | ||
protected lateinit var adapterJob: Job | ||
|
||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { | ||
super.onAttachedToRecyclerView(recyclerView) | ||
adapterJob = Job() | ||
} | ||
|
||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { | ||
super.onDetachedFromRecyclerView(recyclerView) | ||
adapterJob.cancel() | ||
} | ||
} |