MobileListView.xaml.cs 5.7 KB

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