KanbanList.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. using InABox.Mobile;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. using XF.Material.Forms.UI.Dialogs;
  10. namespace PRS.Mobile
  11. {
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class KanbanList
  14. {
  15. private ICoreRepository _model;
  16. //private CoreObservableCollection<IKanbanShell> _itemssource = new CoreObservableCollection<IKanbanShell>();
  17. public ICoreRepository Model
  18. {
  19. get => _model;
  20. set
  21. {
  22. _model = value;
  23. RefreshData(false,true);
  24. }
  25. }
  26. public KanbanList()
  27. {
  28. _currentStatus = KanbanStatus.Open;
  29. InitializeComponent();
  30. ProgressVisible = true;
  31. // _tasks.ItemsSource = _itemssource;
  32. }
  33. protected override void OnAppearing()
  34. {
  35. base.OnAppearing();
  36. _tasks.ItemsSource = _model.Items;
  37. }
  38. protected override void OnDisappearing()
  39. {
  40. _model.SelectNone();
  41. _tasks.ItemsSource = null;
  42. base.OnDisappearing();
  43. }
  44. private void RefreshData(bool force, bool async)
  45. {
  46. if (async)
  47. _model.Refresh(force, () => Device.BeginInvokeOnMainThread(RefreshList));
  48. else
  49. {
  50. _model.Refresh(force);
  51. RefreshList();
  52. }
  53. }
  54. private void RefreshList()
  55. {
  56. _model.Search((o) => FilterShell(o as IKanbanShell));
  57. _tasks.ItemsSource = null;
  58. _tasks.ItemsSource = _model.Items;
  59. //_itemssource.Clear();
  60. //_itemssource.ReplaceRange(_model.Items.OfType<IKanbanShell>().Where(FilterShell));
  61. _tasks.LastUpdated = _model.LastUpdated;
  62. ProgressVisible = false;
  63. }
  64. private String _currentfilter = "";
  65. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  66. {
  67. _currentfilter = args.Text;
  68. RefreshList();
  69. }
  70. private KanbanStatus _currentStatus;
  71. private bool FilterShell(IKanbanShell shell)
  72. {
  73. bool bOK = shell.Status == _currentStatus;
  74. bOK = bOK && (
  75. String.IsNullOrWhiteSpace(_currentfilter)
  76. || shell.Summary.ToUpper().Contains(_currentfilter.ToUpper())
  77. || shell.Number.ToString().ToUpper().Contains(_currentfilter.ToUpper())
  78. || shell.Title.ToUpper().Contains(_currentfilter.ToUpper())
  79. );
  80. return bOK;
  81. }
  82. private void _tasks_OnRefresh(object sender, MobileListRefreshEventArgs args)
  83. {
  84. RefreshData(true,false);
  85. }
  86. private void Kanban_Clicked(object sender, EventArgs e)
  87. {
  88. if ((sender as MobileCard)?.BindingContext is IKanbanShell shell)
  89. {
  90. var editor = new KanbanEdit() { Item = shell };
  91. Navigation.PushAsync(editor);
  92. }
  93. }
  94. private void CheckBox_Changed(object sender, EventArgs e)
  95. {
  96. if ((sender as MobileCheckBox)?.BindingContext is KanbanShell shell)
  97. _model.ToggleSelection(shell);
  98. }
  99. private void SelectEmployee(IEnumerable<IKanbanShell> tasks)
  100. {
  101. ShowPopup(() => CreateEmployeeSelection("Select Employee", (employee) =>
  102. {
  103. foreach (var task in tasks)
  104. {
  105. task.EmployeeID = employee.ID;
  106. task.EmployeeName = employee.Name;
  107. }
  108. _model.SelectNone();
  109. RefreshList();
  110. }));
  111. }
  112. private async void ChangeStatus(IEnumerable<IKanbanShell> tasks)
  113. {
  114. var index = await MaterialDialog.Instance.SelectActionAsync(Enum.GetNames(typeof(KanbanStatus)));
  115. if (index > -1)
  116. {
  117. foreach (var task in tasks)
  118. task.Status = (KanbanStatus)index;
  119. _model.SelectNone();
  120. RefreshList();
  121. }
  122. }
  123. private View CreateEmployeeSelection(String caption, Action<EmployeeShell> selected)
  124. {
  125. SelectionView selection = new SelectionView
  126. {
  127. VerticalOptions = LayoutOptions.Fill,
  128. HorizontalOptions = LayoutOptions.Fill,
  129. CanSearch = false
  130. };
  131. selection.Configure += (sender, args) =>
  132. {
  133. args.Columns
  134. .BeginUpdate()
  135. .Clear()
  136. .Add(new MobileGridTextColumn<EmployeeShell>() { Column = x=>x.Name, Width = GridLength.Star, Caption = caption})
  137. .EndUpdate();
  138. };
  139. selection.Refresh += (sender, args) => App.Data.Employees.Refresh(false);
  140. selection.SelectionChanged += (sender, args) =>
  141. {
  142. selected(args.SelectedItems.FirstOrDefault() as EmployeeShell);
  143. DismissPopup();
  144. };
  145. selection.Load();
  146. return selection;
  147. }
  148. private void _reassign_OnClicked(object sender, EventArgs e)
  149. {
  150. SelectEmployee(SelectedItems());
  151. }
  152. private void _setstatus_OnClicked(object sender, EventArgs e)
  153. {
  154. ChangeStatus(SelectedItems());
  155. }
  156. private void _selectNone_OnClicked(object sender, EventArgs e)
  157. {
  158. _model.SelectNone();
  159. RefreshList();
  160. }
  161. private void _selectAll_OnClicked(object sender, EventArgs e)
  162. {
  163. _model.SelectAll();
  164. RefreshList();
  165. }
  166. private void _addtask_OnClicked(object sender, MobileMenuButtonClickedEventArgs args)
  167. {
  168. var shell = _model.AddItem() as IKanbanShell;
  169. shell.EmployeeID = App.Data.Me.ID;
  170. shell.DueDate = DateTime.Today;
  171. var editor = new KanbanEdit() { Item = shell };
  172. Navigation.PushAsync(editor);
  173. }
  174. private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
  175. {
  176. _currentStatus = (KanbanStatus)_tabStrip.SelectedItem.Index;
  177. RefreshList();
  178. }
  179. private void _selectionmenu_OnAppearing(object sender, EventArgs e)
  180. {
  181. bool anyselected = SelectedItems().Any();
  182. bool anyunselected = UnselectedItems().Any();
  183. _reassign.IsVisible = anyselected;
  184. _setstatus.IsVisible = anyselected;
  185. _selectAll.IsVisible = anyunselected;
  186. _selectNone.IsVisible = anyselected;
  187. _separator.IsVisible = (_reassign.IsVisible || _setstatus.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
  188. }
  189. private IEnumerable<IKanbanShell> SelectedItems()
  190. {
  191. return _model.SelectedItems.OfType<IKanbanShell>();
  192. }
  193. private IEnumerable<IKanbanShell> UnselectedItems()
  194. {
  195. return _model.Items.OfType<IKanbanShell>().Except(_model.SelectedItems.OfType<IKanbanShell>());
  196. }
  197. }
  198. }