MobileList.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. _numrecords.Text = $"{(ItemsSource as IList)?.Count ?? 0} records";
  85. _numrecords.IsVisible = ShowRecordCount && ItemsSource is IList;
  86. _refreshcard.IsVisible = _lastupdate.IsVisible || _pulltorefresh.IsVisible || _numrecords.IsVisible;
  87. _emptylist.IsVisible = !ShowRecordCount;
  88. });
  89. }
  90. private void ItemsSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
  91. {
  92. CheckChanged();
  93. UpdateSummaryRow();
  94. }
  95. private void CheckChanged()
  96. {
  97. //_list.IsVisible = ItemsSource?.GetEnumerator().MoveNext() == true;
  98. //_nodata.IsVisible = !_list.IsVisible;
  99. }
  100. private double _spacing;
  101. public double Spacing
  102. {
  103. get => _spacing;
  104. set
  105. {
  106. _spacing = value;
  107. var layout = _list?.ItemsLayout as LinearItemsLayout;
  108. if (layout != null)
  109. layout.ItemSpacing = _spacing;
  110. }
  111. }
  112. private bool _unevenrows;
  113. public bool HasUnevenRows
  114. {
  115. get => _unevenrows;
  116. set
  117. {
  118. _unevenrows = value;
  119. if (_list != null)
  120. _list.ItemSizingStrategy =
  121. value
  122. ? ItemSizingStrategy.MeasureAllItems
  123. : ItemSizingStrategy.MeasureFirstItem;
  124. }
  125. }
  126. public event MobileListRefreshEvent RefreshRequested;
  127. public MobileList()
  128. {
  129. InitializeComponent();
  130. _refresher.Command = new Command(DoRefresh);
  131. Spacing = 5;
  132. HasUnevenRows = true;
  133. }
  134. private void DoRefresh(object sender)
  135. {
  136. if (_refresher != null)
  137. {
  138. //var src = _list.ItemsSource;
  139. _list.ItemsSource = null;
  140. _refresher.IsRefreshing = true;
  141. RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
  142. _refresher.IsRefreshing = false;
  143. //_list.ItemsSource = src;
  144. }
  145. }
  146. }
  147. }