ListSelectionPage.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 comal.timesheets
  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 : ContentPage
  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(Dictionary<string, Guid> valueImageIDs, string title = "Select")
  33. {
  34. InitializeComponent();
  35. Values = valueImageIDs.Keys.ToList();
  36. Title = title;
  37. StringImageIDs = valueImageIDs;
  38. List<Guid> ids = StringImageIDs.Values.ToList();
  39. Filter<Document> filter = new Filter<Document>(x => x.ID).IsEqualTo(ids[0]);
  40. foreach (Guid id in ids)
  41. {
  42. filter = filter.Or(x => x.ID).IsEqualTo(id);
  43. }
  44. Load(filter);
  45. }
  46. public ListSelectionPage(Dictionary<Guid, string> idValues, string title = "Select")
  47. {
  48. InitializeComponent();
  49. IDValues = idValues;
  50. Title = title;
  51. Values = idValues.Values.ToList();
  52. Load(IDValues);
  53. }
  54. private void Load()
  55. {
  56. foreach (string s in Values)
  57. {
  58. ListSelectionViewItem item = new ListSelectionViewItem
  59. {
  60. Value = s
  61. };
  62. Items.Add(item);
  63. }
  64. listView.ItemsSource = Items;
  65. }
  66. private void Load(Dictionary<Guid, string> idValues)
  67. {
  68. foreach (KeyValuePair<Guid, string> pair in idValues)
  69. {
  70. ListSelectionViewItem item = new ListSelectionViewItem
  71. {
  72. Value = pair.Value,
  73. ID = pair.Key
  74. };
  75. Items.Add(item);
  76. }
  77. listView.ItemsSource = Items;
  78. }
  79. private void Load(Filter<Document> filter)
  80. {
  81. CoreTable table = new Client<Document>().Query(filter, new Columns<Document>(x => x.ID, x => x.Data));
  82. foreach (CoreRow row in table.Rows)
  83. {
  84. Guid id = row.Get<Guid>("ID");
  85. byte[] data = row.Get<byte[]>("Data");
  86. string value = "";
  87. foreach (var v in StringImageIDs)
  88. {
  89. if (v.Value == id)
  90. {
  91. value = v.Key;
  92. break;
  93. }
  94. }
  95. ListSelectionViewItem item = new ListSelectionViewItem
  96. {
  97. Value = value,
  98. ImageSource = ImageSource.FromStream(() => { return new MemoryStream(data); }),
  99. ExtraRowHeight = 300
  100. };
  101. Items.Add(item);
  102. }
  103. listView.ItemsSource = Items;
  104. }
  105. private void SearchEnt_Changed(object sender, EventArgs e)
  106. {
  107. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  108. listView.ItemsSource = Items;
  109. else
  110. listView.ItemsSource = Items.Where(
  111. x => x.Value.Contains(searchEnt.Text) || x.Value.Contains(UpperCaseFirst(searchEnt.Text)) || x.Value.Contains(LowerCaseFirst(searchEnt.Text))
  112. || x.Value.Contains(searchEnt.Text.ToLower()) || x.Value.Contains(searchEnt.Text.ToUpper())
  113. );
  114. }
  115. private void Item_Tapped(object sender, EventArgs e)
  116. {
  117. ListSelectionViewItem item = listView.SelectedItem as ListSelectionViewItem;
  118. Device.BeginInvokeOnMainThread(() => { Navigation.PopAsync(); });
  119. if (item.ID != Guid.Empty)
  120. OnDictionaryItemTapped?.Invoke(item.ID, item.Value);
  121. else
  122. OnSimpleListTapped?.Invoke(item.Value);
  123. }
  124. static String UpperCaseFirst(string s)
  125. {
  126. char[] a = s.ToCharArray();
  127. a[0] = char.ToUpper(a[0]);
  128. return new string(a);
  129. }
  130. static String LowerCaseFirst(string s)
  131. {
  132. char[] a = s.ToCharArray();
  133. a[0] = char.ToLower(a[0]);
  134. return new string(a);
  135. }
  136. }
  137. public class ListSelectionViewItem
  138. {
  139. public Guid ID { get; set; }
  140. public string Value { get; set; }
  141. public double ExtraRowHeight { get; set; }
  142. public ImageSource ImageSource { get; set; }
  143. public ListSelectionViewItem()
  144. {
  145. ID = Guid.Empty;
  146. Value = "";
  147. ExtraRowHeight = 0;
  148. ImageSource = null;
  149. }
  150. }
  151. }