Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash and improve MvxRecyclerViewAdapter and MvxRecyclerViewHolder #3366

Merged
merged 6 commits into from
May 14, 2019
Merged

Fix crash and improve MvxRecyclerViewAdapter and MvxRecyclerViewHolder #3366

merged 6 commits into from
May 14, 2019

Conversation

softlion
Copy link
Contributor

@softlion softlion commented Apr 9, 2019

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Bug fix + refactor of MvxRecyclerViewAdapter and MvxRecyclerViewHolder

⤵️ What is the current behavior?

  • Potential crash when updating ItemsSource from worker thread.
  • Inconsistencies when changing Adapter, making ItemClick and ItemLongClick not work until items are recycled and shown again

🆕 What is the new behavior (if this is a feature change)?

Fixes the above crash and inconsistencies
Allows extensibility through new virtual methods:

  • OnItemViewClick
  • OnItemViewLongClick
  • ExecuteCommandOnItem

Also improved cleaning up of resources when Dispose is called from the Java side

💥 Does this PR introduce a breaking change?

No

🐛 Recommendations for testing

📝 Links to relevant issues/docs

Fixes #3295

🤔 Checklist before submitting

@softlion softlion requested a review from a team April 9, 2019 06:19
@Cheesebaron Cheesebaron changed the title Fix #3295 Do not post NotifyDataSetChanged on MainLooper Apr 9, 2019
@Cheesebaron Cheesebaron added the t/bug Bug type label Apr 9, 2019
@martijn00
Copy link
Contributor

@softlion can you address the comments?

@softlion

This comment was marked as abuse.

@martijn00 martijn00 requested a review from Cheesebaron May 13, 2019 09:40
@martijn00
Copy link
Contributor

martijn00 commented May 13, 2019

@softlion we use rebase instead of merge to get changes from develop into the branch you make changes in. I did one on your branch, but that means that you have to delete your local branch and check it out again because the history is overwritten.

If you want i can do it again to get rid of the merge commit. Or you commit your changes to my comments first, and then i do a rebase.

@softlion

This comment was marked as abuse.

@Cheesebaron
Copy link
Member

Cheesebaron commented May 13, 2019

I've rebased it for you. Can you please check the File Changed tab and verify that those are the correct changes? @softlion

@softlion

This comment was marked as abuse.

@Cheesebaron
Copy link
Member

@softlion actually I do have access to it. As soon as you make a Pull Request, I have access to push to that branch. Unless you remove the check mark in the checkbox on the PR, which allows/disallows others to push to it.

@Cheesebaron Cheesebaron self-assigned this May 13, 2019
@Cheesebaron Cheesebaron changed the title Do not post NotifyDataSetChanged on MainLooper Fix crash and improve MvxRecyclerViewAdapter and MvxRecyclerViewHolder May 13, 2019
Cheesebaron
Cheesebaron previously approved these changes May 13, 2019
@Cheesebaron
Copy link
Member

This looks good to me. Also tested it on a sample app I made, which tested both, ItemClick, ItemLongClick, adding, removing items, adding items in bulk. Everything seems OK.

@martijn00 martijn00 merged commit 2972a1d into MvvmCross:develop May 14, 2019
@martijn00 martijn00 added this to the 6.3.0 milestone May 14, 2019
@fadaEntrepidus
Copy link

fadaEntrepidus commented Jun 6, 2019

If I had my ViewHolder like this:

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType){ var bc = new MvxAndroidBindingContext(parent.Context, BindingContext.LayoutInflaterHolder); var v = InflateViewForHolder(parent, viewType, bc); return new ViewHolder(v, bc) { Click = ItemClick, LongClick = ItemLongClick }; return new ViewHolder(v, bc); }

public override void OnAttachedToWindow() { try { base.OnAttachedToWindow(); ... } catch (Exception ex) { Mvx.IoCProvider.Resolve<ILogger>().LogException("TAG", ex, nameof(View.Click)); } }

How should I be with the current changes? Because it gives me this error:

MonoDroid: UNHANDLED EXCEPTION:

MonoDroid: System.ArgumentNullException: missing source event info in MvxWeakEventSubscription

MonoDroid: Parameter name: sourceEventInfo

MonoDroid: at MvvmCross.WeakSubscription.MvxWeakEventSubscription2[TSource,TEventArgs]..ctor (Android.Views.View source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler1[TEventArgs] targetEventHandler) [0x0001d] in :0

@tbalcom
Copy link
Contributor

tbalcom commented Jun 6, 2019

You may be running into the issue described in #3411. Check there for a solution.

@fadaEntrepidus
Copy link

You may be running into the issue described in #3411. Check there for a solution.

Yesss!! Work. Thanks @tbalcom

@sdebruyn
Copy link
Contributor

sdebruyn commented Jul 2, 2019

This did introduce a breaking change by adding events to the IMvxRecyclerViewHolder. Maybe some documentation should be added.

@anne-k
Copy link

anne-k commented Jul 3, 2019

This change removes public and protected items from the MvxRecyclerViewHolder, which does in fact make it breaking imo. Sure, none of them were in the documentation, but if they're accessible you can expect they will be used.

To be more specific, what this breaks in my app is some of my custom ViewHolders/Adapters. That is a usage not covered at all in the documentation but I don't consider it a strange thing to do. The MvxRecyclerView can cover a lot of use cases with axml and TemplateSelectors only, but not all of them :)

Edit: I'm only noticing this now because the issue it caused was a very subtle one, but: what is the purpose of moving the call to ItemTemplateSelector.GetItemLayoutId to GetItemViewType? Because I'm only seeing downsides:

  • The name of the Adapter method is GetItemViewType which sets the reasonable expectation that what it returns is the value of the ItemTemplateSelector's GetItemViewType method. Instead, it returns the value of GetItemLayoutId.
  • We now have two non-private, virtual methods with a parameter called viewType (OnCreateViewHolder and InflateViewForHolder) where what is passed as this parameter is not some symbolic viewType int, but an int that represents an Android layout ID. It no longer corresponds to what the ItemTemplateSelector calls viewType. Nothing in the method descriptions or the parameter names suggests this, only seeing how it is used in the source code tells you what this int really is.
  • Regardless of the names of methods and parameters and opinions on the current implementation, it is a change from how it was implemented before. The interfaces of these three methods were changed, which means any code using or overriding any of these methods potentially needs to be changed. And unlike the properties that were removed from the ViewHolder, this change is a very subtle one. If you're unlucky, your code will still compile, but do something different than what it did before.

All of this makes me feel like I'm not supposed to subclass the MvxRecyclerAdapter. Is that your official view on these classes, that they are to be used as-is only? I don't really want to have to choose between having the Mvx features and being able to add my own features.

@softlion

This comment was marked as abuse.

@anne-k
Copy link

anne-k commented Jul 5, 2019

@softlion Ah, I didn't know that! That's interesting, that makes it make more sense. It's a bit unfortunate that it doesn't match with the TemplateSelector's viewtype anymore but changing that would break stuff for a lot more people, I get that you don't want to do that.

Thank you for replying!

@softlion

This comment was marked as abuse.

@Cheesebaron Cheesebaron added the t/breaking Breaking Change type label Jul 5, 2019
@softlion softlion deleted the fix-3295 branch January 5, 2020 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/breaking Breaking Change type t/bug Bug type
Development

Successfully merging this pull request may close these issues.

MvxRecycler view must not post NotifyDataSetChanged on MainLooper
7 participants