Improve shared MAUI controls and tests

This commit is contained in:
Andy Howard
2026-10-06 14:13:28 +01:00
parent 0406adfc99
commit 5daf7ce97f
106 changed files with 2299 additions and 805 deletions
@@ -0,0 +1,44 @@
using Sarand.Net.Standard.Base.Utils;
using System;
namespace Sarand.Net.Mobile.Maui.Logic
{
// --- Date value state ---
public sealed class SamDateValueState
{
private DateTime date = DateTime.MinValue;
private bool invalid = true;
public DateTime Date { get { return invalid ? DateTime.MinValue : date; } }
public bool IsInvalid { get { return invalid; } }
public bool SetDate(DateTime value)
{
if (Date.Equals(value)) return false;
if (DateUtil.IsValid(value))
{
date = value;
invalid = false;
}
else
{
date = DateTime.MinValue;
invalid = true;
}
return true;
}
public bool TryInvalidate(out DateTime previousDate)
{
if (invalid)
{
previousDate = DateTime.MinValue;
return false;
}
previousDate = date;
date = DateTime.MinValue;
invalid = true;
return true;
}
}
}
@@ -0,0 +1,18 @@
using Sarand.Net.Mobile.Maui.Controls;
using System;
using System.Globalization;
namespace Sarand.Net.Mobile.Maui.Logic
{
// --- Entry text format options ---
public sealed class SamEntryTextFormatOptions
{
public SamEntry.TextCaseType TextCase { get; init; } = SamEntry.TextCaseType.Normal;
public bool NumericOnly { get; init; }
public byte Decimals { get; init; }
public bool TrimText { get; init; }
public TextInfo TextInfo { get; init; } = CultureInfo.CurrentCulture.TextInfo;
// --- The End ---
}
}
@@ -0,0 +1,48 @@
using Sarand.Net.Standard.Base.Utils;
using System;
namespace Sarand.Net.Mobile.Maui.Logic
{
// --- Entry text formatting ---
public static class SamEntryTextFormatter
{
public static String Format(String? text, SamEntryTextFormatOptions options)
{
String originalText = text ?? "";
String formattedText = originalText;
if (StringUtil.IsEmpty(formattedText)) return formattedText;
if (options.TrimText)
formattedText = StringUtil.TrimAll(formattedText);
if (options.NumericOnly)
{
if (options.Decimals > 0)
{
decimal value = StringUtil.ToDecimal(formattedText);
if (value != 0.0m)
formattedText = value.ToString("N" + options.Decimals);
else if (!formattedText.Equals("0"))
formattedText = "";
}
else
{
long value = StringUtil.ToLong(formattedText);
if (value != 0)
formattedText = value.ToString();
else if (!formattedText.Equals("0"))
formattedText = "";
}
}
else if (options.TextCase == Controls.SamEntry.TextCaseType.Upper)
formattedText = options.TextInfo.ToUpper(formattedText);
else if (options.TextCase == Controls.SamEntry.TextCaseType.Lower)
formattedText = options.TextInfo.ToLower(formattedText);
else if (options.TextCase == Controls.SamEntry.TextCaseType.Title)
formattedText = StringUtil.ToTitleCase(formattedText, options.TextInfo);
return formattedText;
}
// --- The End ---
}
}
@@ -0,0 +1,61 @@
using Sarand.Net.Mobile.Maui.Controls;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
namespace Sarand.Net.Mobile.Maui.Logic
{
// --- Picker item collection ---
public sealed class SamPickerItemCollection
{
private readonly List<SamPicker.SamPickerItem> items = new List<SamPicker.SamPickerItem>();
public List<SamPicker.SamPickerItem> Items { get { return items; } }
public void Clear()
{
items.Clear();
}
public void Replace(IEnumerable<SamPicker.SamPickerItem> replacementItems)
{
items.Clear();
items.AddRange(replacementItems);
}
public bool ContainsId(SysId id)
{
return items.Exists(item => SysId.ToInt(item.Id) == SysId.ToInt(id));
}
public bool ContainsCode(String code)
{
return items.Exists(item => item.Code.EqualsCI(code));
}
public bool AddById(SamPicker.SamPickerItem item)
{
if (ContainsId(item.Id)) return false;
items.Add(item);
return true;
}
public bool AddByCode(SamPicker.SamPickerItem item)
{
if (ContainsCode(item.Code)) return false;
items.Add(item);
return true;
}
public SamPicker.SamPickerItem? FindById(SysId id)
{
return items.Find(item => item.Id.Equals(id));
}
public SamPicker.SamPickerItem? FindByCode(String code)
{
return items.Find(item => item.Code.EqualsCI(code));
}
}
}
@@ -0,0 +1,36 @@
using Sarand.Net.Mobile.Maui.Views;
using Sarand.Net.Mobile.Maui.Controls;
using System.Collections.Generic;
namespace Sarand.Net.Mobile.Maui.Logic
{
// --- Sam view tree operations ---
public static class SamViewTreeUtil
{
public static void ConfigureChildren(Element? root)
{
foreach (Element element in GetElements(root))
if (element is ISamView samView)
samView.ConfigureComponents();
}
public static void SetChildControlActivity(Element? root, bool enabled)
{
foreach (Element element in GetElements(root))
{
if (element is ISamView samView)
samView.SetControlActivity(enabled);
if (element is ISamControl samControl)
samControl.SetControlActivity(enabled);
}
}
private static IEnumerable<Element> GetElements(Element? root)
{
if (root == null) yield break;
yield return root;
foreach (Element descendant in root.GetVisualTreeDescendants())
yield return descendant;
}
}
}