API to update new items with values from old items with the same key
maadani opened this issue · 11 comments
Hi Alex,
Can you please provide an interface / a way to update the new items with values from the items on the list?
Some UI values, like animation values, are needed to be kept after the item is updated. Since these values are UI related, the update should be done instantly (and not go through the model).
Pre version 2.0, I could have implement my own Container and do the update in the put(..) but on V2.0 I didn't find a way to do it.
At this point, I can't upgrade to 2.0.
Thanks in advance,
E.
Hey,
So, from the sounds of it, it sounds like you want to alter the backing TreeSet that contains the EventElements.
EventElement<>'s are only supposed to represent two things w.r.t. the Adapter's functionality:
a) The data type (via getData())
b) The view type (via getViewType())
And are Immutable. If the backing data changes, a new EventElement is created and replaces the original one. These EventElements are what I am storing in the internal TreeSet.
Any state that is related to Views themselves is better to go into the ViewHolder, and any "individual" view state is best to be kept separate, away from the data.
There seems to me to be a Separation of Concerns argument here, and from an architecture standpoint I do not want to open the underlying container for alteration.
If you want to store UI Data on a per-key basis, then I think what you may want to do here is keep around a Map
of Key -> UI Value Object within your custom adapter. You should be able to replicate the Subscriber
within RxRecyclerViewAdapter
, and hook into the observable you pass into the Adapter's constructor. From there, you can manage the Map, removing values or adding them as needed.
This would keep data contained and separated, and allow you to do whatever you want in terms of extra information relative to each data object being added to the map.
Hi Alex,
You are right, it is a "Separation of Concerns" and I agree with most of the things you have said.
In order to separate the Data from the UI, I'm using DisplayItems which wrap my items and sending them to the Adapter.
Can you please elaborate about replicating the Subscriber?
The RxSubscriber class is private and there is no reference to it in the Adapter.
Also, once the item is removed, there is no reference pointing to it.
I think that adding a callback/listener/observable which would be notified in the Subscriber::onNext, would do the trick. You already calling there the relevant notifyItem* method.
Thanks,
E.
Hey, since you're passed the observable to begin with in the constructor, you should just be able to add another subscriber to it via observable.subscribe
at construction time.
What I meant by replicating it was just using it as a guide.
I see your point.
However, can you guarantee that the recyclerview would be updated only after my subscriber's onNext is called?
The onNext is called from Handler::post which might be executed after the recyclerview is already displaying the new item (with the wrong UI values).
E.
No, I can see your point. Let me think about the right approach.
On 3 Jun 2015 4:58 pm, "maadani" notifications@github.com wrote:
I see your point.
However, can you guarantee that the recyclerview would be updated only
after my subscriber's onNext is called?The onNext is called from Handler::post which might be executed after the
recyclerview is already displaying the new item (with the wrong UI values).E.
—
Reply to this email directly or view it on GitHub
#5 (comment)
.
I think the best solution is to introduce a doOnNext operator on the incoming observable within your custom adapter's constructor.
I'll investigate this approach and provide more info tomorrow.
Actually, this could solve preprocessing but not postprocessing. Is there a reason this animation setup isn't done in bindviewholder?
In the bindviewholder I don't have access to the current item, only to the new one.
The problem is that the setup of the new item is depended on values from the current item.
I see. Still perhaps you should consider using the Map<K, UIData> idea structure mentioned early, from bindViewHolder, and keep these animation ui values separate from your data elements.
However, I can see the flexibility of having postprocess(eventelement) and preprocess(eventelement) as protected methods in RxRecyclerViewAdapter, as this would open up some flexibility and be very transparent to those already using the view as is.
I've added the following for release 2.1:
getIndexOf(EventElement<K,V>)
preProcessElement(EventElement<K,V>)
postProcessElement(EventElement<K,V>)
Version 2.1 has been released.