MobileToolGrid.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Syncfusion.Data.Extensions;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms;
  12. namespace InABox.Mobile
  13. {
  14. public class ToolGridLayoutChangedEventArgs : EventArgs
  15. {
  16. public int RowCount { get; private set; }
  17. public int ColCount { get; private set; }
  18. public ToolGridLayoutChangedEventArgs(int rowcount, int colcount)
  19. {
  20. RowCount = rowcount;
  21. ColCount = colcount;
  22. }
  23. }
  24. public delegate void ToolGridLayoutChangedEvent(object sender, ToolGridLayoutChangedEventArgs args);
  25. public class ToolGridViewModel : BindableObject
  26. {
  27. public CoreObservableCollection<IMobileToolItem> Items { get; private set; }
  28. public CoreObservableCollection<IMobileToolItem> VisibleItems { get; private set; }
  29. public int Columns { get; set; }
  30. private bool _disableUpdate = false;
  31. public void BeginUpdate()
  32. => _disableUpdate = true;
  33. public void EndUpdate()
  34. {
  35. _disableUpdate = false;
  36. Refresh();
  37. }
  38. public ToolGridViewModel()
  39. {
  40. Columns = Device.Idiom == TargetIdiom.Tablet ? 6 : 4;
  41. VisibleItems = new CoreObservableCollection<IMobileToolItem>();
  42. Items = new CoreObservableCollection<IMobileToolItem>();
  43. Items.CollectionChanged += (sender, args) =>
  44. {
  45. if (args.OldItems != null)
  46. {
  47. foreach (var item in args.OldItems?.OfType<MobileToolItem>())
  48. item.Changed -= ItemChanged;
  49. }
  50. if (args.NewItems != null)
  51. {
  52. foreach (var item in args.NewItems.OfType<MobileToolItem>())
  53. item.Changed += ItemChanged;
  54. }
  55. Refresh();
  56. };
  57. }
  58. private void ItemChanged(object sender, EventArgs e)
  59. => Refresh();
  60. public void Refresh()
  61. {
  62. if (_disableUpdate)
  63. return;
  64. VisibleItems.Clear();
  65. VisibleItems.AddRange(Items.Where(x => x.IsVisible).ToArray());
  66. int iRow = 0;
  67. int iCol = 0;
  68. foreach (var item in VisibleItems)
  69. {
  70. item.Row = iRow;
  71. item.Column = iCol;
  72. iCol++;
  73. if (iCol == Columns)
  74. {
  75. iRow++;
  76. iCol = 0;
  77. }
  78. }
  79. LayoutChanged?.Invoke(this,new ToolGridLayoutChangedEventArgs(iRow,Columns));
  80. }
  81. public Color BackgroundColor { get; set; }
  82. public event ToolGridLayoutChangedEvent LayoutChanged;
  83. }
  84. [XamlCompilation(XamlCompilationOptions.Compile)]
  85. public partial class MobileToolGrid
  86. {
  87. public IList<IMobileToolItem> Items => _viewModel.Items;
  88. public event EventHandler BeforeTap;
  89. public event EventHandler AfterTap;
  90. private ToolGridViewModel _viewModel = new();
  91. public void BeginUpdate()
  92. {
  93. _viewModel?.BeginUpdate();
  94. }
  95. public void EndUpdate()
  96. {
  97. _viewModel.EndUpdate();
  98. }
  99. public MobileToolGrid()
  100. {
  101. _viewModel.LayoutChanged += _viewModel_OnLayoutChanged;
  102. BeginUpdate();
  103. InitializeComponent();
  104. EndUpdate();
  105. }
  106. public void Refresh()
  107. {
  108. _viewModel.Refresh();
  109. }
  110. private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  111. nameof(BorderColor),
  112. typeof(Color),
  113. typeof(MobileToolGrid),
  114. Material.Color.SecondaryVariant
  115. );
  116. public Color BorderColor
  117. {
  118. get => (Color)GetValue(BorderColorProperty);
  119. set => SetValue(BorderColorProperty, value);
  120. }
  121. private bool debounce = false;
  122. private async void ToolItem_Tapped(object sender, EventArgs e)
  123. {
  124. if (debounce)
  125. return;
  126. debounce = true;
  127. BeforeTap?.Invoke(this, EventArgs.Empty);
  128. try
  129. {
  130. if ((sender is Frame frame) && (frame.BindingContext is MobileToolItem item) && item.IsEnabled)
  131. {
  132. frame.Scale = 0.5;
  133. await frame.ScaleTo(1, 150);
  134. item.DoTap();
  135. }
  136. }
  137. catch (Exception err)
  138. {
  139. MobileLogging.Log(err,"MobileToolGrid");
  140. }
  141. AfterTap?.Invoke(this, EventArgs.Empty);
  142. debounce = false;
  143. }
  144. private void _viewModel_OnLayoutChanged(object sender, ToolGridLayoutChangedEventArgs args)
  145. {
  146. _flexgrid.RowDefinitions.Clear();
  147. for (int i=0; i< args.RowCount; i++)
  148. _flexgrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto});
  149. _flexgrid.ColumnDefinitions.Clear();
  150. for (int i=0; i< args.ColCount; i++)
  151. _flexgrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star});
  152. BindableLayout.SetItemsSource(_flexgrid, null);
  153. BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
  154. }
  155. }
  156. }