123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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 PRS.Mobile
- {
- public delegate void ListSelectionSimpleItemTapped(string Value);
- public delegate void ListSelectionDictionaryItemTapped(Guid ID, string value);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class ListSelectionPage
- {
- List<String> Values = new List<string>();
- List<ListSelectionViewItem> Items = new List<ListSelectionViewItem>();
- Dictionary<string, Guid> StringImageIDs = new Dictionary<string, Guid>();
- public event ListSelectionSimpleItemTapped OnSimpleListTapped;
- public event ListSelectionDictionaryItemTapped OnDictionaryItemTapped;
- Dictionary<Guid, string> IDValues = new Dictionary<Guid, string>();
- bool Multiselect = false;
-
- public ListSelectionPage(List<string> values, string title = "Select")
- {
- InitializeComponent();
- Values = values;
- Title = title;
- Load();
- }
- public ListSelectionPage(string title, params string[] values)
- {
- InitializeComponent();
- Values = values.ToList();
- Title = title;
- Load();
- }
- public ListSelectionPage(Dictionary<string, Guid> valueImageIDs, string title = "Select")
- {
- InitializeComponent();
- Values = valueImageIDs.Keys.ToList();
- Title = title;
- StringImageIDs = valueImageIDs;
- List<Guid> ids = StringImageIDs.Values.ToList();
- Filter<Document> filter = new Filter<Document>(x => x.ID).IsEqualTo(ids[0]);
- foreach (Guid id in ids)
- {
- filter = filter.Or(x => x.ID).IsEqualTo(id);
- }
- Load(filter);
- }
- public ListSelectionPage(Dictionary<Guid, string> 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<Guid, string> idValues)
- {
- foreach (KeyValuePair<Guid, string> pair in idValues)
- {
- ListSelectionViewItem item = new ListSelectionViewItem
- {
- Value = pair.Value,
- ID = pair.Key
- };
- Items.Add(item);
- }
- listView.ItemsSource = Items;
- }
- private void Load(Filter<Document> filter)
- {
- CoreTable table = new Client<Document>().Query(filter, new Columns<Document>(ColumnTypeFlags.None).Add(x => x.ID, x => x.Data));
- foreach (CoreRow row in table.Rows)
- {
- Guid id = row.Get<Guid>("ID");
- byte[] data = row.Get<byte[]>("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;
- }
- }
- }
|