MobileToolGrid.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.VisibleChanged -= ItemChanged;
  49. }
  50. if (args.NewItems != null)
  51. {
  52. foreach (var item in args.NewItems.OfType<MobileToolItem>())
  53. item.VisibleChanged += 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. bool bDirty = false;
  69. foreach (var item in VisibleItems)
  70. {
  71. bDirty = iRow != item.Row || iCol != item.Column;
  72. item.Row = iRow;
  73. item.Column = iCol;
  74. iCol++;
  75. if (iCol == Columns)
  76. {
  77. iRow++;
  78. iCol = 0;
  79. }
  80. }
  81. if (bDirty)
  82. LayoutChanged?.Invoke(this,new ToolGridLayoutChangedEventArgs(iRow,Columns));
  83. }
  84. public Color BackgroundColor { get; set; }
  85. public event ToolGridLayoutChangedEvent LayoutChanged;
  86. }
  87. [XamlCompilation(XamlCompilationOptions.Compile)]
  88. public partial class MobileToolGrid
  89. {
  90. public IList<IMobileToolItem> Items => _viewModel.Items;
  91. public event EventHandler BeforeTap;
  92. public event EventHandler AfterTap;
  93. private ToolGridViewModel _viewModel = new();
  94. public void BeginUpdate()
  95. {
  96. _viewModel?.BeginUpdate();
  97. }
  98. public void EndUpdate()
  99. {
  100. _viewModel.EndUpdate();
  101. }
  102. public MobileToolGrid()
  103. {
  104. _viewModel.LayoutChanged += _viewModel_OnLayoutChanged;
  105. BeginUpdate();
  106. InitializeComponent();
  107. EndUpdate();
  108. }
  109. public void Refresh()
  110. {
  111. _viewModel.Refresh();
  112. }
  113. private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  114. nameof(BorderColor),
  115. typeof(Color),
  116. typeof(MobileToolGrid),
  117. Material.Color.SecondaryVariant
  118. );
  119. public Color BorderColor
  120. {
  121. get => (Color)GetValue(BorderColorProperty);
  122. set => SetValue(BorderColorProperty, value);
  123. }
  124. private bool debounce = false;
  125. private async void ToolItem_Tapped(object sender, EventArgs e)
  126. {
  127. if (debounce)
  128. return;
  129. debounce = true;
  130. BeforeTap?.Invoke(this, EventArgs.Empty);
  131. try
  132. {
  133. if ((sender is Frame frame) && (frame.BindingContext is MobileToolItem item) && item.IsEnabled)
  134. {
  135. frame.Scale = 0.5;
  136. await frame.ScaleTo(1, 150);
  137. item.DoTap();
  138. }
  139. }
  140. catch (Exception err)
  141. {
  142. MobileLogging.Log(err,"MobileToolGrid");
  143. }
  144. AfterTap?.Invoke(this, EventArgs.Empty);
  145. debounce = false;
  146. }
  147. private void _viewModel_OnLayoutChanged(object sender, ToolGridLayoutChangedEventArgs args)
  148. {
  149. _flexgrid.RowDefinitions.Clear();
  150. for (int i=0; i< args.RowCount; i++)
  151. _flexgrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto});
  152. _flexgrid.ColumnDefinitions.Clear();
  153. for (int i=0; i< args.ColCount; i++)
  154. _flexgrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star});
  155. BindableLayout.SetItemsSource(_flexgrid, null);
  156. BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
  157. }
  158. }
  159. }