179 lines
5.8 KiB
C#
179 lines
5.8 KiB
C#
using Sarand.Net.Mobile.Maui.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sarand.Net.Mobile.Maui.Controls
|
|
{
|
|
//=======================================================================
|
|
public class SamPicker : Picker
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public class SamPickerItem
|
|
{
|
|
public SysId Id { get; set; } = new SysId();
|
|
public String Code { get; set; } = "";
|
|
public String Caption { get; set; } = "";
|
|
public Object? Object { get; set; }
|
|
public override string ToString()
|
|
{
|
|
return Caption;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<SamPickerItem> pickerItems = new List<SamPickerItem>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SamPicker()
|
|
{
|
|
DefaultFontSize = FontSize;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
|
|
public double DefaultFontSize { get; }
|
|
public bool BeingPopulated { get; protected set; } = false;
|
|
|
|
// Native behaviour modified by SamHandler.
|
|
public bool NativeNoBorder { get; set; } = Sam.Visuals.Form.Controls.NativeNoBorder;
|
|
public bool NativeNoUnderline { get; set; } = Sam.Visuals.Form.Controls.NativeNoUnderline;
|
|
public bool NativeBackgroundClear { get; set; } = Sam.Visuals.Form.Controls.NativeBackgroundClear;
|
|
|
|
//-------------------------------------------------------------------
|
|
// P o p u l a t e I t e m s
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Does the list contain an item with the same Id.
|
|
/// <para>NB Invalid Id values are matched.</para>
|
|
/// </summary>
|
|
public bool ContainsItemId(SysId id)
|
|
{
|
|
return pickerItems.Exists(item => SysId.ToInt(item.Id) == SysId.ToInt(id));
|
|
}
|
|
/// <summary>
|
|
/// Does the list contain an item with the same code.
|
|
/// <para>NB Codes are case-insensitive.</para>
|
|
/// </summary>
|
|
public bool ContainsItemCode(String code)
|
|
{
|
|
return pickerItems.Exists(item => item.Code.EqualsCI(code));
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Remove all items from the picker.</summary>
|
|
public void ClearItems()
|
|
{
|
|
Items.Clear();
|
|
ItemsSource = null;
|
|
pickerItems.Clear();
|
|
}
|
|
/// <summary>Replace existing picker item list.</summary>
|
|
public void SetItems(List<SamPickerItem> pickerItems)
|
|
{
|
|
ClearItems();
|
|
this.pickerItems.AddRange(pickerItems);
|
|
RefreshPickerItems();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Add an item identified by a SysId value.
|
|
/// <para>NB Fails if an item already exists with the same id.</para>
|
|
/// </summary>
|
|
public bool AddItem(SysId id, String caption)
|
|
{
|
|
if (ContainsItemId(id)) return false;
|
|
pickerItems.Add(new SamPickerItem() { Id = id, Caption = caption });
|
|
RefreshPickerItems();
|
|
return ContainsItemId(id);
|
|
}
|
|
/// <summary>
|
|
/// Add an item identified by a code value.
|
|
/// <para>NB Fails if an item already exists with the same case-insensitive code.</para>
|
|
/// </summary>
|
|
public bool AddItem(String code, String caption)
|
|
{
|
|
if (ContainsItemCode(code)) return false;
|
|
pickerItems.Add(new SamPickerItem() { Code = code, Caption = caption });
|
|
RefreshPickerItems();
|
|
return ContainsItemCode(code);
|
|
}
|
|
//-------------------------------------------------------------------
|
|
private void RefreshPickerItems()
|
|
{
|
|
//SetBinding(Picker.ItemsSourceProperty, BindableProperty.Create());
|
|
//ItemDisplayBinding = new Binding("Caption");
|
|
// Picker items doesn't automatically keep up to date with teh ItemsSource list.
|
|
// So clear it and reset to force the refresh.
|
|
ItemsSource = null;
|
|
ItemsSource = pickerItems;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// I t e m S e l e c t i o n
|
|
//-------------------------------------------------------------------
|
|
public SamPickerItem? SelectedPickerItem
|
|
{
|
|
get
|
|
{
|
|
object selectedItem = SelectedItem;
|
|
if (selectedItem == null || !(selectedItem is SamPickerItem)) return null;
|
|
return (selectedItem as SamPickerItem);
|
|
}
|
|
set
|
|
{
|
|
if (SelectedPickerItem == value) return;
|
|
if (ItemsSource == null || !ItemsSource.Contains(value)) return;
|
|
base.SelectedItem = value;
|
|
}
|
|
}
|
|
public SysId SelectedItemId
|
|
{
|
|
get
|
|
{
|
|
SamPickerItem? selectedPickerItem = SelectedPickerItem;
|
|
return selectedPickerItem?.Id ?? new SysId();
|
|
}
|
|
set
|
|
{
|
|
SelectedPickerItem = pickerItems.Find(item => item.Id.Equals(value));
|
|
}
|
|
}
|
|
public String SelectedItemCode
|
|
{
|
|
get
|
|
{
|
|
SamPickerItem? selectedPickerItem = SelectedPickerItem;
|
|
return selectedPickerItem?.Code ?? "";
|
|
}
|
|
set
|
|
{
|
|
SelectedPickerItem = pickerItems.Find(item => item.Code.EqualsCI(value));
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// C o n t r o l E v e n t s
|
|
//-------------------------------------------------------------------
|
|
// Picker population completed.
|
|
public class PopulatedEventArgs : EventArgs { }
|
|
public delegate void PopulatedEventHandler(object? sender, PopulatedEventArgs e);
|
|
/// <summary>Picker population completed event handler.</summary>
|
|
public event PopulatedEventHandler? Populated;
|
|
protected void InvokePopulatedEvent()
|
|
{
|
|
if (Populated != null) Populated.Invoke(this, new PopulatedEventArgs() { });
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|