MobileList.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 MobileList
  15. {
  16. public static readonly BindableProperty PullToRefreshProperty = BindableProperty.Create(
  17. nameof(PullToRefresh),
  18. typeof(bool),
  19. typeof(MobileList),
  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(MobileList));
  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(MobileList),
  67. false);
  68. public bool ShowRecordCount
  69. {
  70. get => (bool)GetValue(ShowRecordCountProperty);
  71. set
  72. {
  73. SetValue(ShowRecordCountProperty,value);
  74. UpdateSummaryRow();
  75. }
  76. }
  77. private void UpdateSummaryRow()
  78. {
  79. Device.BeginInvokeOnMainThread(() =>
  80. {
  81. _lastupdate.IsVisible = !LastUpdated.IsEmpty();
  82. _lastupdate.Text = $"{DateTimeToAgeConverter.FormatTime(LastUpdated)}";
  83. _pulltorefresh.IsVisible = PullToRefresh;
  84. int count = (ItemsSource as IList)?.Count ?? 0;
  85. _numrecords.Text = $"{count} record{(count == 1 ? "" : "s")}";
  86. _numrecords.IsVisible = ShowRecordCount && ItemsSource is IList;
  87. _refreshcard.IsVisible = _lastupdate.IsVisible || _pulltorefresh.IsVisible || _numrecords.IsVisible;
  88. _emptylist.IsVisible = !ShowRecordCount;
  89. });
  90. }
  91. private void ItemsSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
  92. {
  93. CheckChanged();
  94. UpdateSummaryRow();
  95. }
  96. private void CheckChanged()
  97. {
  98. //_list.IsVisible = ItemsSource?.GetEnumerator().MoveNext() == true;
  99. //_nodata.IsVisible = !_list.IsVisible;
  100. }
  101. private double _spacing;
  102. public double Spacing
  103. {
  104. get => _spacing;
  105. set
  106. {
  107. _spacing = value;
  108. var layout = _list?.ItemsLayout as LinearItemsLayout;
  109. if (layout != null)
  110. layout.ItemSpacing = _spacing;
  111. }
  112. }
  113. private bool _unevenrows;
  114. public bool HasUnevenRows
  115. {
  116. get => _unevenrows;
  117. set
  118. {
  119. _unevenrows = value;
  120. if (_list != null)
  121. _list.ItemSizingStrategy =
  122. value
  123. ? ItemSizingStrategy.MeasureAllItems
  124. : ItemSizingStrategy.MeasureFirstItem;
  125. }
  126. }
  127. public event MobileListRefreshEvent RefreshRequested;
  128. public MobileList()
  129. {
  130. InitializeComponent();
  131. _refresher.Command = new Command(DoRefresh);
  132. Spacing = 5;
  133. HasUnevenRows = true;
  134. }
  135. private void DoRefresh(object sender)
  136. {
  137. if (_refresher != null)
  138. {
  139. //var src = _list.ItemsSource;
  140. _list.ItemsSource = null;
  141. _refresher.IsRefreshing = true;
  142. RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
  143. _refresher.IsRefreshing = false;
  144. //_list.ItemsSource = src;
  145. }
  146. }
  147. }
  148. }