using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace comal.timesheets { public delegate void ListSelectionSimpleItemTapped(string Value); public delegate void ListSelectionDictionaryItemTapped(Guid ID, string value); [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ListSelectionPage : ContentPage { List Values = new List(); List Items = new List(); Dictionary StringImageIDs = new Dictionary(); public event ListSelectionSimpleItemTapped OnSimpleListTapped; public event ListSelectionDictionaryItemTapped OnDictionaryItemTapped; Dictionary IDValues = new Dictionary(); bool Multiselect = false; public ListSelectionPage(List values, string title = "Select") { InitializeComponent(); Values = values; Title = title; Load(); } public ListSelectionPage(Dictionary valueImageIDs, string title = "Select") { InitializeComponent(); Values = valueImageIDs.Keys.ToList(); Title = title; StringImageIDs = valueImageIDs; List ids = StringImageIDs.Values.ToList(); Filter filter = new Filter(x => x.ID).IsEqualTo(ids[0]); foreach (Guid id in ids) { filter = filter.Or(x => x.ID).IsEqualTo(id); } Load(filter); } public ListSelectionPage(Dictionary idValues, string title = "Select") { InitializeComponent(); IDValues = idValues; Title = title; Values = idValues.Values.ToList(); Load(IDValues); } private void Load() { foreach (string s in Values) { ListSelectionViewItem item = new ListSelectionViewItem { Value = s }; Items.Add(item); } listView.ItemsSource = Items; } private void Load(Dictionary idValues) { foreach (KeyValuePair pair in idValues) { ListSelectionViewItem item = new ListSelectionViewItem { Value = pair.Value, ID = pair.Key }; Items.Add(item); } listView.ItemsSource = Items; } private void Load(Filter filter) { CoreTable table = new Client().Query(filter, new Columns(x => x.ID, x => x.Data)); foreach (CoreRow row in table.Rows) { Guid id = row.Get("ID"); byte[] data = row.Get("Data"); string value = ""; foreach (var v in StringImageIDs) { if (v.Value == id) { value = v.Key; break; } } ListSelectionViewItem item = new ListSelectionViewItem { Value = value, ImageSource = ImageSource.FromStream(() => { return new MemoryStream(data); }), ExtraRowHeight = 300 }; Items.Add(item); } listView.ItemsSource = Items; } private void SearchEnt_Changed(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(searchEnt.Text)) listView.ItemsSource = Items; else listView.ItemsSource = Items.Where( x => x.Value.Contains(searchEnt.Text) || x.Value.Contains(UpperCaseFirst(searchEnt.Text)) || x.Value.Contains(LowerCaseFirst(searchEnt.Text)) || x.Value.Contains(searchEnt.Text.ToLower()) || x.Value.Contains(searchEnt.Text.ToUpper()) ); } private void Item_Tapped(object sender, EventArgs e) { ListSelectionViewItem item = listView.SelectedItem as ListSelectionViewItem; Device.BeginInvokeOnMainThread(() => { Navigation.PopAsync(); }); if (item.ID != Guid.Empty) OnDictionaryItemTapped?.Invoke(item.ID, item.Value); else OnSimpleListTapped?.Invoke(item.Value); } static String UpperCaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToUpper(a[0]); return new string(a); } static String LowerCaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToLower(a[0]); return new string(a); } } public class ListSelectionViewItem { public Guid ID { get; set; } public string Value { get; set; } public double ExtraRowHeight { get; set; } public ImageSource ImageSource { get; set; } public ListSelectionViewItem() { ID = Guid.Empty; Value = ""; ExtraRowHeight = 0; ImageSource = null; } } }