ListSelectionPage.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace PRS.Mobile
  12. {
  13. public delegate void ListSelectionSimpleItemTapped(string Value);
  14. public delegate void ListSelectionDictionaryItemTapped(Guid ID, string value);
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class ListSelectionPage
  17. {
  18. List<String> Values = new List<string>();
  19. List<ListSelectionViewItem> Items = new List<ListSelectionViewItem>();
  20. Dictionary<string, Guid> StringImageIDs = new Dictionary<string, Guid>();
  21. public event ListSelectionSimpleItemTapped OnSimpleListTapped;
  22. public event ListSelectionDictionaryItemTapped OnDictionaryItemTapped;
  23. Dictionary<Guid, string> IDValues = new Dictionary<Guid, string>();
  24. bool Multiselect = false;
  25. public ListSelectionPage(List<string> values, string title = "Select")
  26. {
  27. InitializeComponent();
  28. Values = values;
  29. Title = title;
  30. Load();
  31. }
  32. public ListSelectionPage(string title, params string[] values)
  33. {
  34. InitializeComponent();
  35. Values = values.ToList();
  36. Title = title;
  37. Load();
  38. }
  39. public ListSelectionPage(Dictionary<string, Guid> valueImageIDs, string title = "Select")
  40. {
  41. InitializeComponent();
  42. Values = valueImageIDs.Keys.ToList();
  43. Title = title;
  44. StringImageIDs = valueImageIDs;
  45. List<Guid> ids = StringImageIDs.Values.ToList();
  46. Filter<Document> filter = new Filter<Document>(x => x.ID).IsEqualTo(ids[0]);
  47. foreach (Guid id in ids)
  48. {
  49. filter = filter.Or(x => x.ID).IsEqualTo(id);
  50. }
  51. Load(filter);
  52. }
  53. public ListSelectionPage(Dictionary<Guid, string> idValues, string title = "Select")
  54. {
  55. InitializeComponent();
  56. IDValues = idValues;
  57. Title = title;
  58. Values = idValues.Values.ToList();
  59. Load(IDValues);
  60. }
  61. private void Load()
  62. {
  63. foreach (string s in Values)
  64. {
  65. ListSelectionViewItem item = new ListSelectionViewItem
  66. {
  67. Value = s
  68. };
  69. Items.Add(item);
  70. }
  71. listView.ItemsSource = Items;
  72. }
  73. private void Load(Dictionary<Guid, string> idValues)
  74. {
  75. foreach (KeyValuePair<Guid, string> pair in idValues)
  76. {
  77. ListSelectionViewItem item = new ListSelectionViewItem
  78. {
  79. Value = pair.Value,
  80. ID = pair.Key
  81. };
  82. Items.Add(item);
  83. }
  84. listView.ItemsSource = Items;
  85. }
  86. private void Load(Filter<Document> filter)
  87. {
  88. CoreTable table = new Client<Document>().Query(filter, new Columns<Document>(ColumnTypeFlags.None).Add(x => x.ID, x => x.Data));
  89. foreach (CoreRow row in table.Rows)
  90. {
  91. Guid id = row.Get<Guid>("ID");
  92. byte[] data = row.Get<byte[]>("Data");
  93. string value = "";
  94. foreach (var v in StringImageIDs)
  95. {
  96. if (v.Value == id)
  97. {
  98. value = v.Key;
  99. break;
  100. }
  101. }
  102. ListSelectionViewItem item = new ListSelectionViewItem
  103. {
  104. Value = value,
  105. ImageSource = ImageSource.FromStream(() => { return new MemoryStream(data); }),
  106. ExtraRowHeight = 300
  107. };
  108. Items.Add(item);
  109. }
  110. listView.ItemsSource = Items;
  111. }
  112. private void SearchEnt_Changed(object sender, EventArgs e)
  113. {
  114. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  115. listView.ItemsSource = Items;
  116. else
  117. listView.ItemsSource = Items.Where(
  118. x => x.Value.Contains(searchEnt.Text) || x.Value.Contains(UpperCaseFirst(searchEnt.Text)) || x.Value.Contains(LowerCaseFirst(searchEnt.Text))
  119. || x.Value.Contains(searchEnt.Text.ToLower()) || x.Value.Contains(searchEnt.Text.ToUpper())
  120. );
  121. }
  122. private void Item_Tapped(object sender, EventArgs e)
  123. {
  124. ListSelectionViewItem item = listView.SelectedItem as ListSelectionViewItem;
  125. Device.BeginInvokeOnMainThread(() => { Navigation.PopAsync(); });
  126. if (item.ID != Guid.Empty)
  127. OnDictionaryItemTapped?.Invoke(item.ID, item.Value);
  128. else
  129. OnSimpleListTapped?.Invoke(item.Value);
  130. }
  131. static String UpperCaseFirst(string s)
  132. {
  133. char[] a = s.ToCharArray();
  134. a[0] = char.ToUpper(a[0]);
  135. return new string(a);
  136. }
  137. static String LowerCaseFirst(string s)
  138. {
  139. char[] a = s.ToCharArray();
  140. a[0] = char.ToLower(a[0]);
  141. return new string(a);
  142. }
  143. }
  144. public class ListSelectionViewItem
  145. {
  146. public Guid ID { get; set; }
  147. public string Value { get; set; }
  148. public double ExtraRowHeight { get; set; }
  149. public ImageSource ImageSource { get; set; }
  150. public ListSelectionViewItem()
  151. {
  152. ID = Guid.Empty;
  153. Value = "";
  154. ExtraRowHeight = 0;
  155. ImageSource = null;
  156. }
  157. }
  158. }