MobileCollectionView.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using InABox.Core;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Xaml;
  7. namespace InABox.Mobile
  8. {
  9. public class MobileListRefreshEventArgs : EventArgs
  10. {
  11. }
  12. public delegate void MobileListRefreshEvent(object sender, MobileListRefreshEventArgs args);
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class MobileCollectionView
  15. {
  16. public static readonly BindableProperty PullToRefreshProperty = BindableProperty.Create(
  17. nameof(PullToRefresh),
  18. typeof(bool),
  19. typeof(MobileCollectionView),
  20. false);
  21. public bool PullToRefresh
  22. {
  23. get => (bool)GetValue(PullToRefreshProperty);
  24. set
  25. {
  26. SetValue(PullToRefreshProperty,value);
  27. _refresher.IsEnabled = value;
  28. UpdateSummaryRow();
  29. }
  30. }
  31. public DataTemplate ItemTemplate
  32. {
  33. get => _list.ItemTemplate;
  34. set => _list.ItemTemplate = value;
  35. }
  36. public IEnumerable ItemsSource
  37. {
  38. get => _list.ItemsSource;
  39. set
  40. {
  41. if (_list.ItemsSource is INotifyCollectionChanged old)
  42. old.CollectionChanged -= ItemsSourceChanged;
  43. _list.ItemsSource = value;
  44. CheckChanged();
  45. if (value is INotifyCollectionChanged observable)
  46. observable.CollectionChanged += ItemsSourceChanged;
  47. UpdateSummaryRow();
  48. }
  49. }
  50. public static readonly BindableProperty LastUpdatedProperty = BindableProperty.Create(
  51. nameof(LastUpdated),
  52. typeof(DateTime),
  53. typeof(MobileCollectionView));
  54. public DateTime LastUpdated
  55. {
  56. get => (DateTime)GetValue(LastUpdatedProperty);
  57. set
  58. {
  59. SetValue(LastUpdatedProperty,value);
  60. UpdateSummaryRow();
  61. }
  62. }
  63. public static readonly BindableProperty ShowRecordCountProperty = BindableProperty.Create(
  64. nameof(ShowRecordCount),
  65. typeof(bool),
  66. typeof(MobileCollectionView),
  67. false);
  68. public bool ShowRecordCount
  69. {
  70. get => (bool)GetValue(ShowRecordCountProperty);
  71. set
  72. {
  73. SetValue(ShowRecordCountProperty,value);
  74. UpdateSummaryRow();
  75. }
  76. }
  77. public static readonly BindableProperty EmptyTextProperty = BindableProperty.Create(
  78. nameof(EmptyText),
  79. typeof(string),
  80. typeof(MobileCollectionView),
  81. "No Data Available");
  82. public string EmptyText
  83. {
  84. get => (string)GetValue(EmptyTextProperty);
  85. set => SetValue(EmptyTextProperty,value);
  86. }
  87. private void UpdateSummaryRow()
  88. {
  89. Device.BeginInvokeOnMainThread(() =>
  90. {
  91. _lastupdate.IsVisible = !LastUpdated.IsEmpty();
  92. _lastupdate.Text = $"{DateTimeToAgeConverter.FormatTime(LastUpdated)}";
  93. _pulltorefresh.IsVisible = PullToRefresh;
  94. int count = (ItemsSource as IList)?.Count ?? 0;
  95. _numrecords.Text = $"{count} record{(count == 1 ? "" : "s")}";
  96. _numrecords.IsVisible = ShowRecordCount && ItemsSource is IList;
  97. _refreshcard.IsVisible = /*_lastupdate.IsVisible || */ _pulltorefresh.IsVisible || _numrecords.IsVisible;
  98. _emptylist.IsVisible = !ShowRecordCount;
  99. });
  100. }
  101. private void ItemsSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
  102. {
  103. CheckChanged();
  104. UpdateSummaryRow();
  105. }
  106. private void CheckChanged()
  107. {
  108. //_list.IsVisible = ItemsSource?.GetEnumerator().MoveNext() == true;
  109. //_nodata.IsVisible = !_list.IsVisible;
  110. }
  111. private double _spacing;
  112. public double Spacing
  113. {
  114. get => _spacing;
  115. set
  116. {
  117. _spacing = value;
  118. var layout = _list?.ItemsLayout as LinearItemsLayout;
  119. if (layout != null)
  120. layout.ItemSpacing = _spacing;
  121. }
  122. }
  123. private bool _unevenrows;
  124. public bool HasUnevenRows
  125. {
  126. get => _unevenrows;
  127. set
  128. {
  129. _unevenrows = value;
  130. if (_list != null)
  131. _list.ItemSizingStrategy =
  132. value
  133. ? ItemSizingStrategy.MeasureAllItems
  134. : ItemSizingStrategy.MeasureFirstItem;
  135. }
  136. }
  137. public event MobileListRefreshEvent RefreshRequested;
  138. public MobileCollectionView()
  139. {
  140. InitializeComponent();
  141. _refresher.Command = new Command(DoRefresh);
  142. Spacing = 5;
  143. HasUnevenRows = true;
  144. }
  145. private void DoRefresh(object sender)
  146. {
  147. if (_refresher != null)
  148. {
  149. //var src = _list.ItemsSource;
  150. _list.ItemsSource = null;
  151. _refresher.IsRefreshing = true;
  152. RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
  153. _refresher.IsRefreshing = false;
  154. //_list.ItemsSource = src;
  155. }
  156. }
  157. }
  158. }