CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.02k stars 296 forks source link

Request to include `ObservableRangeCollection`, an updated `ObservableCollection` available as part of Xamarin.CommunityToolkit and MVVM Helpers #256

Closed egvijayanand closed 2 years ago

egvijayanand commented 2 years ago

Overview

Request to include the ObservableRangeCollection, an updated ObservableCollection which raises the notification once rather than raising it for every item and it will not be part of the CommunityToolkit.Maui as this MVVM Toolkit is the recommended way to move forward for all MVVM-based applications.

And this type provides a multitude of methods to work on a range of items.

API breakdown

https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/ObservableRangeCollection.shared.cs

Usage example

var items = new ObservableRangeCollection<string>();
items.AddRange(new[] { "First", "Second", "Third", "Fourth" });
items.RemoveRange(new[] { "First", "Second" });
items.ReplaceRange(new[] { "iOS", "Android", "macOS", "Tizen", "Windows" });

Breaking change?

No

Alternatives

As of now copy and paste the code into all available projects.

Additional context

This is pretty useful for lazy loading such as the Pagination of a collection.

Query the items from the remote API via network calls or from the local databases such as SQLite.

Help us help you

No, just wanted to propose this

Sergio0694 commented 2 years ago

This has been proposed already, but is not planned. We're trying to just push for .NET 7 to add support for this built-in 🙂 See https://github.com/dotnet/runtime/issues/18087.

tranb3r commented 1 year ago

Hi, One year later, it does not seem like this request has made any real progress in dotnet runtime. Meanwhile, our maui apps still have to depend on 3rd party libs like MVVM Helpers, even when using CommunityToolkit.MVVM, which is a bit of a nonsense.

@Sergio0694 Could you please reconsider this proposition to include in CommunityToolkit.MVVM the ObservableRangeCollection? Thanks

egvijayanand commented 1 year ago

IMO, this should've made it to the v8.2.0 of the toolkit. Unfortunately, this issue stands closed. @Sergio0694 kindly consider reopening this to make the toolkit even more usable.

tranb3r commented 1 year ago

@Sergio0694 Sorry to ask again... is your answer a definite no?

Sergio0694 commented 1 year ago

That's correct, this is not something we'll add 🙂 It's already tracked in the runtime repo (for adding this to the BCL), and besides, adding such a type to the MVVM Toolkit would face the same issues anyway: it'd be completely unusable on most common UI frameworks as of today (WPF, UWP, WinUI 3, MAUI, at the very least).

tranb3r commented 1 year ago

But, so far I'm using MvvmHelpers's implementation, and it seems perfectly fine... unless there is something I'm not understanding. So, could you please summarize here the reason why you think it'd be completely unusable in MAUI? I'm not sure I want to read several hundreds of comments from the dotnet/runtime issue...

egvijayanand commented 1 year ago

That's correct, this is not something we'll ad

There's a lot of traction on this feature and .NET 8 being an LTS release, many would prefer to migrate to this release.

Since hardly there's little time left, maybe two more public previews, before the API freeze in an RC release.

So kindly highlight it to the runtime team on the need to implement this at the earliest.

Thanks in advance.

Sergio0694 commented 1 year ago

"could you please summarize here the reason why you think it'd be completely unusable in MAUI?"

@tranb3r I've added more context in https://github.com/dotnet/runtime/issues/18087#issuecomment-1605582234 and following comments, you can just read from there and not go through the entire discussion. TLDR: it's complicated and will cause problems on some frameworks 😅

"So kindly highlight it to the runtime team on the need to implement this at the earliest."

They're aware of the request, but as we mentioned in that discussion, this is a particularly tricky problem. I am not optimistic this will make it in .NET 8 at all. In fact at this point I'm relatively confident it'll miss this release, unfortunately.

tranb3r commented 1 year ago

Ok, so I understand it's too complicated to implement it in the BCL. However, isn't it the purpose of CommunityToolkits to fill the gap and implement features that people need, without waiting for major releases of dotnet? Maybe this could be part of CommunityToolkit.Maui? (or is there any incompatibility with Maui?)

Sergio0694 commented 1 year ago

"Ok, so I understand it's too complicated to implement it in the BCL."

No it's actually the other way around 😅 Implementing this in the BCL is very easy. The issue is that it wouldn't work in many of these UI frameworks. Which as I mentioned, is also why adding this API somewhere else (eg. in the MVVM Toolkit) wouldn't help. If you tried to then use it eg. in a UWP or WinUI 3, or WPF app, it wouldn't work, so you'd just be back at square 1, just with extra steps.

tranb3r commented 1 year ago

So, again, maybe this could be added to CommunityToolkit.Maui? At least Maui apps would benefit from it immediately.

They rejected it because they thought it would be implemented in dotnet or CommunityToolkit.Mvvm. But maybe they could reconsider the proposal, now that it's clear it's not going to be part of dotnet or CommunityToolkit.Mvvm soon. https://github.com/CommunityToolkit/Maui/issues/99#issuecomment-1118217493

cc @brminnick What do you think?

Sergio0694 commented 1 year ago

Adding this to the MAUI toolkit would have the same exact issues. It might work in some scenarios, but eg. if you're running your MAUI app on Windows, which would use WinUI 3 as backend, then that'd have the same problem we already mentioned, and it wouldn't work. Same if MAUI happened to get a WPF backend, that'd also not work with this, for instance.

tranb3r commented 1 year ago

OK. Thank you for the clarifications!