ListSelectionPage.xaml.cs 5.3 KB

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