|
@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
|
|
|
using System.Globalization;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
+using Syncfusion.Data.Extensions;
|
|
|
using Xamarin.Essentials;
|
|
|
using Xamarin.Forms;
|
|
|
using Xamarin.Forms.Xaml;
|
|
@@ -26,39 +27,63 @@ namespace InABox.Mobile
|
|
|
|
|
|
public class ToolGridViewModel : BindableObject
|
|
|
{
|
|
|
- public IList<IMobileToolItem> Items { get; private set; }
|
|
|
+ public CoreObservableCollection<IMobileToolItem> Items { get; private set; }
|
|
|
|
|
|
- public IList<IMobileToolItem> VisibleItems { get; private set; }
|
|
|
+ public CoreObservableCollection<IMobileToolItem> VisibleItems { get; private set; }
|
|
|
|
|
|
public int Columns { get; set; }
|
|
|
+
|
|
|
+ private bool _disableUpdate = false;
|
|
|
|
|
|
+ public void BeginUpdate()
|
|
|
+ => _disableUpdate = true;
|
|
|
+
|
|
|
+ public void EndUpdate()
|
|
|
+ {
|
|
|
+ _disableUpdate = false;
|
|
|
+ Refresh();
|
|
|
+ }
|
|
|
public ToolGridViewModel()
|
|
|
{
|
|
|
Columns = Device.Idiom == TargetIdiom.Tablet ? 6 : 4;
|
|
|
- Items = new ObservableCollection<IMobileToolItem>();
|
|
|
+ VisibleItems = new CoreObservableCollection<IMobileToolItem>();
|
|
|
|
|
|
- VisibleItems = new ObservableCollection<IMobileToolItem>();
|
|
|
- ((ObservableCollection<IMobileToolItem>)Items).CollectionChanged += (sender, args) =>
|
|
|
+ Items = new CoreObservableCollection<IMobileToolItem>();
|
|
|
+ Items.CollectionChanged += (sender, args) =>
|
|
|
{
|
|
|
+
|
|
|
+ if (args.OldItems != null)
|
|
|
+ {
|
|
|
+ foreach (var item in args.OldItems?.OfType<MobileToolItem>())
|
|
|
+ item.Changed -= ItemChanged;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (args.NewItems != null)
|
|
|
+ {
|
|
|
+ foreach (var item in args.NewItems.OfType<MobileToolItem>())
|
|
|
+ item.Changed += ItemChanged;
|
|
|
+ }
|
|
|
+
|
|
|
Refresh();
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ private void ItemChanged(object sender, EventArgs e)
|
|
|
+ => Refresh();
|
|
|
+
|
|
|
public void Refresh()
|
|
|
{
|
|
|
+ if (_disableUpdate)
|
|
|
+ return;
|
|
|
+
|
|
|
VisibleItems.Clear();
|
|
|
- DoLayout();
|
|
|
- }
|
|
|
-
|
|
|
- private void DoLayout()
|
|
|
- {
|
|
|
+ VisibleItems.AddRange(Items.Where(x => x.IsVisible).ToArray());
|
|
|
int iRow = 0;
|
|
|
int iCol = 0;
|
|
|
- foreach (var item in Items.Where(x => x.IsVisible))
|
|
|
+ foreach (var item in VisibleItems)
|
|
|
{
|
|
|
item.Row = iRow;
|
|
|
item.Column = iCol;
|
|
|
- VisibleItems.Add(item);
|
|
|
iCol++;
|
|
|
if (iCol == Columns)
|
|
|
{
|
|
@@ -83,11 +108,25 @@ namespace InABox.Mobile
|
|
|
|
|
|
public event EventHandler BeforeTap;
|
|
|
public event EventHandler AfterTap;
|
|
|
+
|
|
|
+ private ToolGridViewModel _viewModel = new();
|
|
|
+
|
|
|
+ public void BeginUpdate()
|
|
|
+ {
|
|
|
+ _viewModel?.BeginUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void EndUpdate()
|
|
|
+ {
|
|
|
+ _viewModel.EndUpdate();
|
|
|
+ }
|
|
|
|
|
|
public MobileToolGrid()
|
|
|
{
|
|
|
+ _viewModel.LayoutChanged += _viewModel_OnLayoutChanged;
|
|
|
+ BeginUpdate();
|
|
|
InitializeComponent();
|
|
|
- BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
|
|
|
+ EndUpdate();
|
|
|
}
|
|
|
|
|
|
public void Refresh()
|
|
@@ -144,6 +183,8 @@ namespace InABox.Mobile
|
|
|
_flexgrid.ColumnDefinitions.Clear();
|
|
|
for (int i=0; i< args.ColCount; i++)
|
|
|
_flexgrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star});
|
|
|
+ BindableLayout.SetItemsSource(_flexgrid, null);
|
|
|
+ BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
|
|
|
|
|
|
}
|
|
|
|