123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.Linq;
- using System.Threading.Tasks;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms;
- namespace InABox.Mobile
- {
- public class ToolGridLayoutChangedEventArgs : EventArgs
- {
- public int RowCount { get; private set; }
- public int ColCount { get; private set; }
- public ToolGridLayoutChangedEventArgs(int rowcount, int colcount)
- {
- RowCount = rowcount;
- ColCount = colcount;
- }
- }
- public delegate void ToolGridLayoutChangedEvent(object sender, ToolGridLayoutChangedEventArgs args);
-
- public class ToolGridViewModel : BindableObject
- {
- public IList<IMobileToolItem> Items { get; private set; }
- public IList<IMobileToolItem> VisibleItems { get; private set; }
-
- public int Columns { get; set; }
-
- public ToolGridViewModel()
- {
- Columns = Device.Idiom == TargetIdiom.Tablet ? 6 : 4;
- Items = new ObservableCollection<IMobileToolItem>();
- VisibleItems = new ObservableCollection<IMobileToolItem>();
- ((ObservableCollection<IMobileToolItem>)Items).CollectionChanged += (sender, args) =>
- {
- Refresh();
- };
- }
- public void Refresh()
- {
- VisibleItems.Clear();
- DoLayout();
- }
- private void DoLayout()
- {
- int iRow = 0;
- int iCol = 0;
- foreach (var item in Items.Where(x => x.IsVisible))
- {
- item.Row = iRow;
- item.Column = iCol;
- VisibleItems.Add(item);
- iCol++;
- if (iCol == Columns)
- {
- iRow++;
- iCol = 0;
- }
- }
- LayoutChanged?.Invoke(this,new ToolGridLayoutChangedEventArgs(iRow,Columns));
- }
- public Color BackgroundColor { get; set; }
- public event ToolGridLayoutChangedEvent LayoutChanged;
- }
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class MobileToolGrid
- {
- public IList<IMobileToolItem> Items => _viewModel.Items;
-
- public event EventHandler BeforeTap;
- public event EventHandler AfterTap;
-
- public MobileToolGrid()
- {
- InitializeComponent();
- BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
- }
- public void Refresh()
- {
- _viewModel.Refresh();
- }
- private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
- nameof(BorderColor),
- typeof(Color),
- typeof(MobileToolGrid),
- Material.Color.SecondaryVariant
- );
-
- public Color BorderColor
- {
- get => (Color)GetValue(BorderColorProperty);
- set => SetValue(BorderColorProperty, value);
- }
- private bool debounce = false;
- private async void ToolItem_Tapped(object sender, EventArgs e)
- {
- if (debounce)
- return;
- debounce = true;
- BeforeTap?.Invoke(this, EventArgs.Empty);
- try
- {
-
- if ((sender is Frame frame) && (frame.BindingContext is MobileToolItem item) && item.IsEnabled)
- {
- frame.Scale = 0.5;
- await frame.ScaleTo(1, 150);
- item.DoTap();
- }
- }
- catch (Exception err)
- {
- MobileLogging.Log(err,"MobileToolGrid");
- }
- AfterTap?.Invoke(this, EventArgs.Empty);
- debounce = false;
- }
- private void _viewModel_OnLayoutChanged(object sender, ToolGridLayoutChangedEventArgs args)
- {
- _flexgrid.RowDefinitions.Clear();
- for (int i=0; i< args.RowCount; i++)
- _flexgrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto});
-
- _flexgrid.ColumnDefinitions.Clear();
- for (int i=0; i< args.ColCount; i++)
- _flexgrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star});
- }
-
- }
- }
|