using System.ComponentModel;
using System.Runtime.CompilerServices;
using Titano.Core.Localization;
namespace Titano.Ui.Mvvm;
/// Minimal INotifyPropertyChanged base.
public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected bool Set(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// Signals that every property changed (used after a culture switch).
protected void OnAllPropertiesChanged() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(string.Empty));
}
/// View model whose computed, localized texts refresh when the UI language changes.
public abstract class LocalizedViewModel : ObservableObject, IDisposable
{
protected LocalizedViewModel() => Loc.CultureChanged += OnCultureChanged;
public virtual void Dispose()
{
Loc.CultureChanged -= OnCultureChanged;
GC.SuppressFinalize(this);
}
private void OnCultureChanged(object? sender, EventArgs e) => OnLanguageChanged();
///
/// The UI language changed: every property is read again. A view model that keeps texts in objects (chart series
/// and legends, rows, cached sentences) builds them again here, then calls the base.
///
protected virtual void OnLanguageChanged() => OnAllPropertiesChanged();
}