MobileViewModel.cs 1.7 KB

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