I'm trying to call recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,null,0) only after mDiffer.submitlist(list) is finished "diffing" and animating the list updates/changes.
Is there an AsyncListDiffer feature for onAfterSubmitList(Callback callback) that I could use to achieve this?
If not, is there anyway to find out when does submitList() finish its task so I could put my scrollToPosition(0) in there?
Update July 2020:
Google added a callback for this! See: https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListDiffer#submitList(java.util.List%3CT%3E,%20java.lang.Runnable)
Previous answer, from the bad old days:
First of all, I can't believe Google didn't provide a callback for this.
I dived into the source code of AsyncListDiffer, and I found that it's possible to receive a callback when all RecyclerView updates have been done - but the way to receive this callback is just bizarre.
You need to create a sub-class of BatchingListUpdateCallback, and then wrap your ListUpdateCallback (or AdapterListUpdateCallback as in most cases) inside it.
Then, you should override dispatchLastEvent. AsyncListDiffer will call this after all but one of the updates have been dispatched. In your dispatchLastEvent, you'll want to call the super implementation, so you don't break anything. You'll also want to invoke a custom callback, which is your way in.
In Kotlin that looks like:
class OnDiffDoneListUpdateCallback(
listUpdateCallback: ListUpdateCallback,
private val onDiffDoneCallback: () -> Unit
) : BatchingListUpdateCallback(listUpdateCallback) {
override fun dispatchLastEvent() {
super.dispatchLastEvent()
onDiffDoneCallback()
}
}
The last step is to then provide your custom OnDiffDoneListUpdateCallback to the AsyncListDiffer. To do this, you need to initialise the AsyncListDiffer for yourself - if you're using ListAdapter or something similar, you'll need to refactor so that you are in control of the AsyncListDiffer.
In Kotlin, that looks like:
private val asyncListDiffer = AsyncListDiffer<ItemType>(
OnDiffDoneListUpdateCallback(AdapterListUpdateCallback(adapter)) {
// here's your callback
doTheStuffAfterDiffIsDone()
},
AsyncDifferConfig.Builder<ItemType>(diffCallback).build()
)
Edit:
I forgot about the edge-cases, of course!
Sometimes, dispatchLastEvent isn't called, because AsyncListDiffer considers the update trivial. Here's the checks it does:
if (newList == mList) {
...
return;
}
// fast simple remove all
if (newList == null) {
...
mUpdateCallback.onRemoved(0, countRemoved);
return;
}
// fast simple first insert
if (mList == null) {
...
mUpdateCallback.onInserted(0, newList.size());
return;
}
I recommend doing these checks for yourself, just before you call asyncListDiffer.submitList(list). But of course, it couldn't be that easy! mList is private, and getCurrentList will return an empty list if mList is null, so is useless for this check. Instead you'll have to use reflection to access it:
val listField = AsyncListDiffer::class.java.getDeclaredField("mList")
listField.isAccessible = true
val asyncDifferList = listField.get(asyncListDiffer) as List<ItemType>?
asyncListDiffer.submitList(newList)
if(asyncDifferList == null) {
onDiffDone()
}
if(newList == null) {
onDiffDone()
}
if(newList == asyncDifferList) {
onDiffDone()
}
Edit 2: Now, I know what you're saying - surely there's an easier, less hacky way to do this? And the answer is... yes! Simply copy the entire AsyncListDiffer class into your project, and just add the callback yourself!
You can get advantage from registerAdapterDataObserver methods by listening to them (or what your needs need) i.e:
listAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
//
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
}
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
}
})
You can clear the register of your adapterObserver once needed using unregisterAdapterDataObserver.
Related
Is there a way to update (automatically) the RecyclerView when a list is populated with data?
I created a simple app (here is the repository for the app).
In HomeFragment there is a RecyclerView and a button to refresh the data.
The app works fine as long as I have the following code in HomeFragment to update the adapter whenever the StateFlow list gets data.
private fun setupObservers() {
lifecycleScope.launchWhenStarted {
vm.state.collect() {
if (it.list.isNotEmpty()) {
todoAdapter.data = it.list
} else {
todoAdapter.data = emptyList()
}
}
}
}
My question is, is there a away for the RecyclerView to update, without having to observe (or collect) the changes of the list of the StateFlow?
Something has to notify the RecyclerView adapter when the data has changed. Either you do it in a collector/observer, or you have to proactively do it in every place in your code where you do something that might affect the data. So, it is much easier and less error-prone to do it by collecting.
Side note, the if/else in your code doesn't accomplish anything useful. No reason to treat an empty list differently if you still end up passing an empty list to the adapter.
It's more correct to use repeatOnLifecycle (or flowWithLifecycle) than launchWhenStarted. See here.
private fun setupObservers() {
vm.state.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.onEach { todoAdapter.data = it.list }
.launchIn(viewLifecycleOwner.lifecycleScope)
}
I personally like to use an extension function like this to make it more concise wherever I'm collecting flows:
fun <T> Flow<T>.launchAndCollectWithLifecycle(
lifecycleOwner: LifecycleOwner,
state: Lifecycle.State = Lifecycle.State.STARTED,
action: suspend (T) -> Unit
) = flowWithLifecycle(lifecycleOwner.lifecycle, state)
.onEach(action)
.launchIn(lifecycleOwner.lifecycleScope)
Then your code would become:
private fun setupObservers() {
vm.state.launchAndCollectWithLifecycle(viewLifecycleOwner) {
todoAdapter.data = it.list
}
}
What is a proper way to communicate between the ViewModel and the View, Google architecture components give use LiveData in which the view subscribes to the changes and update itself accordingly, but this communication not suitable for single events, for example show message, show progress, hide progress etc.
There are some hacks like SingleLiveEvent in Googles example but it work only for 1 observer.
Some developers using EventBus but i think it can quickly get out of control when the project grows.
Is there a convenience and correct way to implement it, how do you implement it?
(Java examples welcome too)
Yeah I agree, SingleLiveEvent is a hacky solution and EventBus (in my experience) always lead to trouble.
I found a class called ConsumableValue a while back when reading the Google CodeLabs for Kotlin Coroutines, and I found it to be a good, clean solution that has served me well (ConsumableValue.kt):
class ConsumableValue<T>(private val data: T) {
private var consumed = false
/**
* Process this event, will only be called once
*/
#UiThread
fun handle(block: ConsumableValue<T>.(T) -> Unit) {
val wasConsumed = consumed
consumed = true
if (!wasConsumed) {
this.block(data)
}
}
/**
* Inside a handle lambda, you may call this if you discover that you cannot handle
* the event right now. It will mark the event as available to be handled by another handler.
*/
#UiThread
fun ConsumableValue<T>.markUnhandled() {
consumed = false
}
}
class MyViewModel : ViewModel {
private val _oneShotEvent = MutableLiveData<ConsumableValue<String>>()
val oneShotEvent: LiveData<ConsumableValue<String>>() = _oneShotData
fun fireEvent(msg: String) {
_oneShotEvent.value = ConsumableValue(msg)
}
}
// In Fragment or Activity
viewModel.oneShotEvent.observe(this, Observer { value ->
value?.handle { Log("TAG", "Message:$it")}
})
In short, the handle {...} block will only be called once, so there's no need for clearing the value if you return to a screen.
What about using Kotlin Flow?
I do not believe they have the same behavior that LiveData has where it would alway give you the latest value. Its just a subscription similar to the workaround SingleLiveEvent for LiveData.
Here is a video explaining the difference that I think you will find interesting and answer your questions
https://youtu.be/B8ppnjGPAGE?t=535
try this:
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
And wrapper it into LiveData
class ListViewModel : ViewModel {
private val _navigateToDetails = MutableLiveData<Event<String>>()
val navigateToDetails : LiveData<Event<String>>
get() = _navigateToDetails
fun userClicksOnButton(itemId: String) {
_navigateToDetails.value = Event(itemId) // Trigger the event by setting a new Event as a new value
}
}
And observe
myViewModel.navigateToDetails.observe(this, Observer {
it.getContentIfNotHandled()?.let { // Only proceed if the event has never been handled
startActivity(DetailsActivity...)
}
})
link reference: Use an Event wrapper
For showing/hiding progress dialogs and showing error messages from a failed network call on loading of the screen, you can use a wrapper that encapsulates the LiveData that the View is observing.
Details about this method are in the addendum to app architecture:
https://developer.android.com/jetpack/docs/guide#addendum
Define a Resource:
data class Resource<out T> constructor(
val state: ResourceState,
val data: T? = null,
val message: String? = null
)
And a ResourceState:
sealed class ResourceState {
object LOADING : ResourceState()
object SUCCESS : ResourceState()
object ERROR : ResourceState()
}
In the ViewModel, define your LiveData with the model wrapped in a Resource:
val exampleLiveData = MutableLiveData<Resource<ExampleModel>>()
Also in the ViewModel, define the method that makes the API call to load the data for the current screen:
fun loadDataForView() = compositeDisposable.add(
exampleUseCase.exampleApiCall()
.doOnSubscribe {
exampleLiveData.setLoading()
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
exampleLiveData.setSuccess(it)
},
{
exampleLiveData.setError(it.message)
}
)
)
In the View, set up the Observer on creation:
viewModel.exampleLiveData.observe(this, Observer {
updateResponse(it)
})
Here is the example updateResponse() method, showing/hiding progress, and showing an error if appropriate:
private fun updateResponse(resource: Resource<ExampleModel>?) {
resource?.let {
when (it.state) {
ResourceState.LOADING -> {
showProgress()
}
ResourceState.SUCCESS -> {
hideProgress()
// Use data to populate data on screen
// it.data will have the data of type ExampleModel
}
ResourceState.ERROR -> {
hideProgress()
// Show error message
// it.message will have the error message
}
}
}
}
You can easily achieve this by not using LiveData, and instead using Event-Emitter library that I wrote specifically to solve this problem without relying on LiveData (which is an anti-pattern outlined by Google, and I am not aware of any other relevant alternatives).
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
implementation 'com.github.Zhuinden:event-emitter:1.0.0'
If you also copy the LiveEvent class , then now you can do
private val emitter: EventEmitter<String> = EventEmitter()
val events: EventSource<String> get() = emitter
fun doSomething() {
emitter.emit("hello")
}
And
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = getViewModel<MyViewModel>()
viewModel.events.observe(viewLifecycleOwner) { event ->
// ...
}
}
// inline fun <reified T: ViewModel> Fragment.getViewModel(): T = ViewModelProviders.of(this).get(T::class.java)
For rationale, you can check out my article I wrote to explain why the alternatives aren't as valid approaches.
You can however nowadays also use a Channel(UNLIMITED) and expose it as a flow using asFlow(). That wasn't really applicable back in 2019.
Re-edit : I will use the result of this method to initialize the visibility of some buttons in my view
The accepted answer seems good in theory but the return value of first method is Flowable> instead of Flowable. Hence I cannot pass it as a parameter to subscription since it requires Flowable but it is Flowable>
Question Before Edit
I am using RxJava to observe a method I am required to call from SDK. Using this method I am trying to make an assertion about the existence of something but I do not know how long the call will take so it is hard for me to say terminate the subscription after x seconds.
override fun doesExist(): Boolean {
var doesExist = false
var subscription : Subscription
val flowable = Flowable.just(SDK.searchContact("contact"))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : FlowableSubscriber<Flowable<List<Contact>>> {
override fun onSubscribe(s: Subscription) {
subscription = s
}
override fun onNext(t: Flowable<List<Contact>>?) {
doesExist = true
}
override fun onComplete() {
Log.d("tagtagcomplete", "tagtagcomplete")
}
override fun onError(e: Throwable?) {
Log.d("tagtagerror", "tagtagerror")
}
return doesExist
}
So what I want to do is return true if I won't get any result from searchContract method and return false if I get a result. While using observables, I can create a subscription object and call it's methods but I could not figure out how to do it right way with flowables.
My confusion is the following: Method to sdk returns a Flowable<List<Contact>>> but in my opinion I need to check if a contact exists only once and stop
Right now my method does not go inside onError, onNext or onComplete. It just returns doesExist
I reedit my answer,following return type is you need.
fun doesExist(): Flowable<Single<Boolean>> {
return Flowable.just(Single.just(SDK.searchContact("contact")).map{ it.isEmpty()})
}
I searched for long time, I only can find a solution using addListenerForSingle Event which is triggered onDataChanged. It only triggered when there was a change on database....
How can I get a single value with page loaded (I mean onCreate)?
For example, get key is really easy,
mReference.child("kotran").child("isactive").getKey()
//isactive
But there no way to get value.
addListenerForSingleValueEvent will be called once, the first time you register such listener.
Take a look at this generic Kotlin example for the whole function:
override fun <T : Any> getValue(typeClass: KClass<T>, reference: String, vararg children: String): Single<T> =
Single.create({
val databaseReference = firebaseDatabase.getReference(reference)
databaseReference.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(dbError: DatabaseError?) {
it.onError(FirebaseDatabaseException(
dbError?.message ?: context stringOf R.string.database_error,
dbError?.details ?: exceptionDetails(reference, children))
)
}
override fun onDataChange(dataSnapshot: DataSnapshot?) {
val value = childDataSnapshot(dataSnapshot, children)?.getValue(typeClass.java)
if (value != null) {
it.onSuccess(value)
} else {
it.onError(RetreivedValueNullException(exceptionDetails(reference, children)))
}
}
})
})
It is returning Single<T>, however, if you're not using RxJava, just remove related Single.create wrap and return T
Implementation of exceptionDetails():
private fun exceptionDetails(reference: String, children: Array<out String>): String =
"Exception occurred on reference: $reference and children: ${children.forEach { String.format("-> %s", it) }}"
And usage example (if this function is in some FirebaseRepository:
firebaseRepository.getValue(Boolean::class, "kotran", "isActive")
.subscribeOn(Schedulers.io)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ /* do something with the value */},
{ /* do something with the error */})
More detailes available on this gist.
There is no getValue() method in Firebase that works directly on a reference, like getKey(). To get a value you need to use a listener. The onDataChange() method immediately returns the current value. Here is the official docs from Firebase. Please check the listen for value events section.
Hope it helps.
I have tried the new BottomSheetBehaviour with design library 23.0.2 but i think it too limited. When I change state with setState() method, the bottomsheet use ad animation to move to the new state.
How can I change state immediately, without animation? I don't see a public method to do that.
Unfortunately it looks like you can't. Invocation of BottomSheetBehavior's setState ends with synchronous or asynchronous call of startSettlingAnimation(child, state). And there is no way to override these methods behavior cause setState is final and startSettlingAnimation has package visible modifier. Check the sources for more information.
I have problems with the same, but in a bit different way - my UI state changes setHideable to false before that settling animation invokes, so I'm getting IllegalStateException there. I will consider usage of BottomSheetCallback to manage this properly.
If you want to remove the show/close animation you can use dialog.window?.setWindowAnimations(-1). For instance:
class MyDialog(): BottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.window?.setDimAmount(0f) // for removing the dimm
dialog.window?.setWindowAnimations(-1) // for removing the animation
return dialog
}
}
If you really need it, then you can resort to reflection:
fun BottomSheetBehavior.getViewDragHelper(): ViewDragHelper? = BottomSheetBehavior::class.java
.getDeclaredField("viewDragHelper")
.apply { isAccessible = true }
.let { field -> field.get(this) as? ViewDragHelper? }
fun ViewDragHelper.getScroller(): OverScroller? = ViewDragHelper::class.java
.getDeclaredField("mScroller")
.apply { isAccessible = true }
.let { field -> field.get(this) as? OverScroller? }
Then you can use these extension methods when the state changes:
bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetCallback() {
override fun onSlide(view: View, offset: Float) {}
override fun onStateChanged(view: View, state: Int) {
if (state == STATE_SETTLING) {
try {
bottomSheetBehavior.getViewDragHelper()?.getScroller()?.abortAnimation()
} catch(e: Throwable) {}
}
}
})
I will add that the code is not perfect, getting fields every time the state changes is not efficient, and this is done for the sake of simplicity.