C
C#2y ago
Indeed

✅ WPF Store Singleton Action PropertyChanged optimization

Hi! I have a singleton in my app responsible for weather management currently each of my properties on the object has an Action<T> where T is property's type, however that means i have to write an action for each property is there a better way?
public class AppStore {
private List<WeatherData> _weatherForecasts;
public IEnumerable<WeatherData> WeatherForecasts => _weatherForecasts;
public Location? Location { get; private set; }
public event Action<Location?> LocationChanged;
public event Action<IEnumerable<WeatherData>> WeatherForecastsChanged;

public bool IsLoading { get; internal set; }
public bool IsFetching { get; internal set; }
public event Action<bool> LoadingChanged;
public event Action<bool> FetchingChanged;

private async Task FetchWeather() {
if (Location == null) {
_weatherForecasts.Clear();
WeatherForecastsChanged?.Invoke(_weatherForecasts);
return;
}

SetLoading(true);
IEnumerable<WeatherData> weather = await _weatherProvider.GetWeatherAsync(Location, DateTime.Now, DateTime.Now.AddDays(3));
_weatherForecasts = weather.ToList();
WeatherForecastsChanged?.Invoke(_weatherForecasts);
SetLoading(false);
}

private void SetLoading(bool value) {
if (IsLoading == value) return;

IsLoading = value;
LoadingChanged?.Invoke(value);
}

//...
}
public class AppStore {
private List<WeatherData> _weatherForecasts;
public IEnumerable<WeatherData> WeatherForecasts => _weatherForecasts;
public Location? Location { get; private set; }
public event Action<Location?> LocationChanged;
public event Action<IEnumerable<WeatherData>> WeatherForecastsChanged;

public bool IsLoading { get; internal set; }
public bool IsFetching { get; internal set; }
public event Action<bool> LoadingChanged;
public event Action<bool> FetchingChanged;

private async Task FetchWeather() {
if (Location == null) {
_weatherForecasts.Clear();
WeatherForecastsChanged?.Invoke(_weatherForecasts);
return;
}

SetLoading(true);
IEnumerable<WeatherData> weather = await _weatherProvider.GetWeatherAsync(Location, DateTime.Now, DateTime.Now.AddDays(3));
_weatherForecasts = weather.ToList();
WeatherForecastsChanged?.Invoke(_weatherForecasts);
SetLoading(false);
}

private void SetLoading(bool value) {
if (IsLoading == value) return;

IsLoading = value;
LoadingChanged?.Invoke(value);
}

//...
}
8 Replies
sibber
sibber2y ago
implement INotifyPropertyChanged
Indeed
Indeed2y ago
How would i subscribe to it as right now i can do this with actions
_appStore.LocationChanged += OnLocationUpdate;
_appStore.WeatherForecastsChanged += OnWeatherUpdate;
_appStore.LoadingChanged += OnLoadingUpdate;
_appStore.LocationChanged += OnLocationUpdate;
_appStore.WeatherForecastsChanged += OnWeatherUpdate;
_appStore.LoadingChanged += OnLoadingUpdate;
private void MainViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(CurrentViewModel)) {
OnPropertyChanged(nameof(CurrentViewModel));
}
}
private void MainViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(CurrentViewModel)) {
OnPropertyChanged(nameof(CurrentViewModel));
}
}
?
sibber
sibber2y ago
subscribe to PropertyChanged and switch on the property name
Indeed
Indeed2y ago
Oh okay, thank you. That makes sense
sibber
sibber2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Indeed
Indeed2y ago
oh, i forgot it existed here. Thanks once again! ❤️
sibber
sibber2y ago
np