MobileViewModel.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using InABox.Core;
  3. using Xamarin.Forms;
  4. namespace InABox.Mobile
  5. {
  6. public abstract class MobileViewModel : BindableObject, IMobileViewModel
  7. {
  8. public Color SelectedColor => XF.Material.Forms.Material.Color.Surface;
  9. public Color SelectedTextColor => XF.Material.Forms.Material.Color.OnSurface;
  10. public Color UnselectedColor => XF.Material.Forms.Material.Color.Primary;
  11. public Color UnselectedTextColor => XF.Material.Forms.Material.Color.OnPrimary;
  12. public event MobileViewModelLoadedEvent Loaded;
  13. protected void OnLoaded()
  14. => Loaded?.Invoke(this, new MobileViewModelLoadedEventArgs());
  15. }
  16. public abstract class MobileViewModel<TEntity,TShell> : MobileViewModel
  17. where TEntity : Entity, IRemotable, IPersistent
  18. where TShell : IShell<TEntity>
  19. {
  20. public static readonly BindableProperty ItemProperty = BindableProperty.Create(
  21. nameof(Item),
  22. typeof(TShell),
  23. typeof(MobileViewModel<TEntity,TShell>)
  24. );
  25. public TShell Item
  26. {
  27. get => (TShell)GetValue(ItemProperty);
  28. set
  29. {
  30. SetValue(ItemProperty,value);
  31. DoLoad();
  32. OnLoaded();
  33. OnPropertyChanged(nameof(IsChanged));
  34. }
  35. }
  36. protected abstract void DoLoad();
  37. public abstract bool IsChanged { get; }
  38. public void DoChanged() => OnPropertyChanged(nameof(IsChanged));
  39. }
  40. }