MobileListView.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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(MobileListView),
  16. false);
  17. public bool PullToRefresh
  18. {
  19. get => (bool)GetValue(PullToRefreshProperty);
  20. set
  21. {
  22. SetValue(PullToRefreshProperty,value);
  23. _list.IsPullToRefreshEnabled = 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 static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
  38. nameof(ItemsSource),
  39. typeof(IEnumerable),
  40. typeof(MobileListView),
  41. propertyChanged:ItemsSourcePropertyChanged);
  42. private static void ItemsSourcePropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
  43. {
  44. (bindable as MobileListView)?.Refresh(newvalue as IEnumerable);
  45. }
  46. public IEnumerable ItemsSource
  47. {
  48. get => GetValue(ItemsSourceProperty) as IEnumerable;
  49. set => SetValue(ItemsSourceProperty, value);
  50. }
  51. private void Refresh(IEnumerable value)
  52. {
  53. if (_list.ItemsSource is INotifyCollectionChanged old)
  54. old.CollectionChanged -= ItemsSourceChanged;
  55. _list.ItemsSource = value;
  56. CheckChanged();
  57. if (value is INotifyCollectionChanged observable)
  58. observable.CollectionChanged += ItemsSourceChanged;
  59. UpdateSummaryRow();
  60. }
  61. public static readonly BindableProperty LastUpdatedProperty = BindableProperty.Create(
  62. nameof(LastUpdated),
  63. typeof(DateTime),
  64. typeof(MobileListView));
  65. public DateTime LastUpdated
  66. {
  67. get => (DateTime)GetValue(LastUpdatedProperty);
  68. set
  69. {
  70. SetValue(LastUpdatedProperty,value);
  71. UpdateSummaryRow();
  72. }
  73. }
  74. public static readonly BindableProperty ShowRecordCountProperty = BindableProperty.Create(
  75. nameof(ShowRecordCount),
  76. typeof(bool),
  77. typeof(MobileListView),
  78. false);
  79. public bool ShowRecordCount
  80. {
  81. get => (bool)GetValue(ShowRecordCountProperty);
  82. set
  83. {
  84. SetValue(ShowRecordCountProperty,value);
  85. UpdateSummaryRow();
  86. }
  87. }
  88. public static readonly BindableProperty EmptyTextProperty = BindableProperty.Create(
  89. nameof(EmptyText),
  90. typeof(string),
  91. typeof(MobileListView),
  92. "No Data Available");
  93. public string EmptyText
  94. {
  95. get => (string)GetValue(EmptyTextProperty);
  96. set => SetValue(EmptyTextProperty,value);
  97. }
  98. private void UpdateSummaryRow()
  99. {
  100. Device.BeginInvokeOnMainThread(() =>
  101. {
  102. _lastupdate.IsVisible = !LastUpdated.IsEmpty();
  103. _lastupdate.Text = $"{DateTimeToAgeConverter.FormatTime(LastUpdated)}";
  104. _pulltorefresh.IsVisible = PullToRefresh;
  105. int count = (ItemsSource as IList)?.Count ?? 0;
  106. _numrecords.Text = $"{count} record{(count == 1 ? "" : "s")}";
  107. _numrecords.IsVisible = ShowRecordCount && ItemsSource is IList;
  108. _refreshcard.IsVisible = /*_lastupdate.IsVisible || */ _pulltorefresh.IsVisible || _numrecords.IsVisible;
  109. //_emptylist.IsVisible = !ShowRecordCount;
  110. });
  111. }
  112. private void ItemsSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
  113. {
  114. CheckChanged();
  115. UpdateSummaryRow();
  116. }
  117. private void CheckChanged()
  118. {
  119. //_list.IsVisible = ItemsSource?.GetEnumerator().MoveNext() == true;
  120. //_nodata.IsVisible = !_list.IsVisible;
  121. }
  122. private bool _unevenrows;
  123. public bool HasUnevenRows
  124. {
  125. get => _unevenrows;
  126. set
  127. {
  128. _unevenrows = value;
  129. if (_list != null)
  130. _list.HasUnevenRows = value;
  131. }
  132. }
  133. public event MobileListRefreshEvent RefreshRequested;
  134. public MobileListView()
  135. {
  136. InitializeComponent();
  137. _list.RefreshCommand = new Command(DoRefresh);
  138. HasUnevenRows = true;
  139. }
  140. private void DoRefresh(object sender)
  141. {
  142. if (PullToRefresh)
  143. {
  144. _list.IsRefreshing = true;
  145. RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
  146. _list.IsRefreshing = false;
  147. }
  148. }
  149. }
  150. }