-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
First pass at observing nested property changes #156
Conversation
This is just a WIP of how we can do inner property changes. I am not sure if this setup would be acceptable enough or if we would want to change the overall implementation. I would be glad to get some feedback on it. If this is appropriate, I can expand it to allow three levels of nesting and will implement the INotifyPropertyChanging variant. |
|
The need for this is common when view models have late-bound and replaced properties. In the case of For example, if there is a user interface that has a main view model with a selection function, you can end up needing to listen to that property and respond when it changes. Below is an example of a view model that would be used to display a list of players and the current selected player. When a player is selected, we will want to show that player's score which will dynamically update.
In this example, Let me know if that helps explain the use case for this. It is usual that a depth of 2 is sufficient for these kinds of listeners. A depth of 3 (e.g. |
Thank you, that example was very clear, and I can see how it would indeed be beneficial! |
Added a new method to observe changes in nested properties of INotifyPropertyChanged objects. This allows for more granular observation of property changes, particularly useful when dealing with complex objects. Also updated the test suite to cover this new functionality.
The ObservePropertyChanged and ObservePropertyChanging methods now support multiple property selectors, allowing for nested observation of property changes. Corresponding unit tests have also been added.
558affc
to
996bc83
Compare
These are dependent on `PropertyChanged` until the end of the chain where we will switch to `PropertyChanging`, so these have been updated to leverage the `ObservePropertyChanged` methods.
This PR contains extensions for ObservePropertyChanged and ObservePropertyChanging that allow for up to two levels of nested property change detection (i.e. x.PropertyOne.PropertyTwo.PropertyThree). I feel like this amount of depth will cover a vast majority of the use cases for nested properties. Below are a few implementation notes
|
Thank you, I think it's very well considered! I'll merge and release it! |
I tried this feature, which was added in Ver1.1.6, but it seems that I cannot resolve the nullable warning. If there is a solution, I would like to know. #nullable enable
using System.ComponentModel;
using R3;
MainViewModel vm = new();
vm.ObservePropertyChanged(x => x.SelectedPlayer, x => x.Score)
.Subscribe(Console.WriteLine);
class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public IReadOnlyList<Player> Players { get; } = [new Player(1), new Player(2)];
public Player? SelectedPlayer { get; set; }
}
class Player(int score) : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public int Score { get; } = score;
} |
If only to remove the warning, However, this looks like it would be better to improve the operator definition. |
Added a new method to observe changes in nested properties of INotifyPropertyChanged objects. This allows for more granular observation of property changes, particularly useful when dealing with complex objects. Also updated the test suite to cover this new functionality.