DatePicker.Selection.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace Bitsplash.DatePicker
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public partial class DatePickerContent : IDatePickerPrivate
  13. {
  14. DateTime? mSelectionFirst;
  15. DateTime? mDragFirst;
  16. DateTime? mDragTo;
  17. HashSet<DateTime> mDragSelectionRange = new HashSet<DateTime>();
  18. bool mDragSelectionChanged = false;
  19. Dictionary<DateTime, Color> mMarkerColors = new Dictionary<DateTime, Color>();
  20. public void SetAllMarkerColors(Dictionary<DateTime, Color> markers)
  21. {
  22. mMarkerColors.Clear();
  23. foreach (var pair in markers)
  24. mMarkerColors.Add(pair.Key.Date, pair.Value);
  25. RefreshSelection();
  26. }
  27. public void SetMarkerColor(DateTime date, Color color)
  28. {
  29. mMarkerColors[date.Date] = color;
  30. RefreshSelection();
  31. }
  32. public void ClearMarker(DateTime date)
  33. {
  34. mMarkerColors.Remove(date.Date);
  35. RefreshSelection();
  36. }
  37. public void ClearMarkerColor()
  38. {
  39. mMarkerColors.Clear();
  40. RefreshSelection();
  41. }
  42. void SelectOne(DateTime date)
  43. {
  44. mSelection.SelectOne(date);
  45. mSelectionFirst = date;
  46. }
  47. void ToogleOne(DateTime date)
  48. {
  49. if (mSelection.IsSingleDateSelected(date) && AllowEmptySelection)
  50. {
  51. mSelection.Clear();
  52. mSelectionFirst = null;
  53. }
  54. else
  55. SelectOne(date);
  56. }
  57. void UpdateSelection()
  58. {
  59. if(mDragSelectionChanged || ((IDatePickerCollectionPrivate) mSelection).Changed)
  60. {
  61. ((IDatePickerCollectionPrivate)mSelection).Changed = false;
  62. mDragSelectionChanged = false;
  63. RefreshSelection();
  64. }
  65. }
  66. void RefreshSelection()
  67. {
  68. for (int i = 0; i < mCells.Length; i++)
  69. {
  70. var date = mCells[i].DayValue.Date;
  71. bool withinMonth = date.Month == DisplayDate.Month && date.Year == DisplayDate.Year;
  72. Color markerColor;
  73. if (mMarkerColors.TryGetValue(date, out markerColor) == false)
  74. markerColor = new Color(0f, 0f, 0f, 0f);
  75. if (mCells[i].MarkerColor != markerColor)
  76. mCells[i].MarkerColor = markerColor;
  77. if ((mSelection.Contains(date) || mDragSelectionRange.Contains(date)) && withinMonth)
  78. {
  79. if (mCells[i].CellSelected == false)
  80. mCells[i].CellSelected = true;
  81. }
  82. else
  83. {
  84. if(mCells[i].CellSelected == true)
  85. mCells[i].CellSelected = false;
  86. }
  87. }
  88. }
  89. void LimitRangeToMonth(HashSet<DateTime> selection,DateTime month)
  90. {
  91. selection.RemoveWhere((x) => x.Month != month.Month || x.Year != month.Year);
  92. }
  93. void SelectRange(DateTime from,DateTime to)
  94. {
  95. mSelection.SelectRange(from, to);
  96. }
  97. void ToogleMultiple(DateTime date)
  98. {
  99. if (mSelection.Contains(date))
  100. {
  101. if (mSelection.Count > 1 || AllowEmptySelection)
  102. mSelection.Remove(date);
  103. }
  104. else
  105. {
  106. mSelection.Add(date);
  107. }
  108. }
  109. void ConnectSelection(DateTime date)
  110. {
  111. date = date.Date;
  112. if (mSelection.Contains(date)) // already within the selection
  113. return;
  114. if(mSelection.Count == 0 || mSelectionFirst.HasValue == false)
  115. {
  116. SelectOne(date);
  117. return;
  118. }
  119. SelectRange(mSelectionFirst.Value, date);
  120. }
  121. void ProcessRangeClick(DatePickerCell cell, int cellChildIndex)
  122. {
  123. if (mDatePickerInput.MultipleSelectionValue == MultipleSelectionInputValue.Append)
  124. ConnectSelection(cell.DayValue);
  125. else if (mDatePickerInput.MultipleSelectionValue == MultipleSelectionInputValue.Singular)
  126. ToogleOne(cell.DayValue);
  127. }
  128. void ProcessMultipleClick(DatePickerCell cell, int cellChildIndex)
  129. {
  130. if (mDatePickerInput.MultipleSelectionValue == MultipleSelectionInputValue.Append)
  131. ToogleMultiple(cell.DayValue);
  132. else if (mDatePickerInput.MultipleSelectionValue == MultipleSelectionInputValue.Singular)
  133. ToogleOne(cell.DayValue);
  134. }
  135. void ProcessSelectionClick(DatePickerCell cell,int cellChildIndex)
  136. {
  137. switch (SelectionMode)
  138. {
  139. case SelectionType.Single:
  140. ToogleOne(cell.DayValue);
  141. break;
  142. case SelectionType.Range:
  143. ProcessRangeClick(cell, cellChildIndex);
  144. break;
  145. case SelectionType.Multiple:
  146. ProcessMultipleClick(cell, cellChildIndex);
  147. break;
  148. }
  149. }
  150. protected virtual void OnCellClick(DatePickerCell cell, int cellChildIndex)
  151. {
  152. if (cell.CellEnabled == false)
  153. return;
  154. if (mDragFirst.HasValue || mDragTo.HasValue)
  155. return;
  156. ProcessSelectionClick(cell, cellChildIndex);
  157. }
  158. void RaiseSelectionChanged()
  159. {
  160. if (OnSelectionChanged != null)
  161. OnSelectionChanged.Invoke();
  162. }
  163. void IDatePickerPrivate.RaiseClick(int childIndex)
  164. {
  165. OnCellClick(mCells[childIndex], childIndex);
  166. }
  167. void IDatePickerPrivate.RaiseStartSelection(int childIndex)
  168. {
  169. if (SelectionMode == SelectionType.Single)
  170. return;
  171. DateTime dayValue = mCells[childIndex].DayValue;
  172. if (SelectionMode == SelectionType.Range || (SelectionMode == SelectionType.Multiple && mDatePickerInput.MultipleSelectionValue == MultipleSelectionInputValue.Singular))
  173. mSelection.SelectOne(dayValue);
  174. mDragFirst = dayValue;
  175. if (mDragTo.HasValue && mDragFirst.HasValue)
  176. {
  177. CommonMethods.SelectRange(mDragTo.Value, mDragFirst.Value, mDragSelectionRange);
  178. }
  179. else
  180. {
  181. mDragSelectionRange.Clear();
  182. mDragSelectionRange.Add(dayValue);
  183. }
  184. mSelectionFirst = null;
  185. LimitRangeToMonth(mDragSelectionRange, mMonthFirst);
  186. mDragSelectionChanged = true;
  187. }
  188. void IDatePickerPrivate.EndSelection()
  189. {
  190. if (SelectionMode == SelectionType.Single)
  191. return;
  192. if (mDragTo.HasValue && mDragFirst.HasValue)
  193. {
  194. CommonMethods.SelectRange(mDragTo.Value, mDragFirst.Value, mDragSelectionRange);
  195. LimitRangeToMonth(mDragSelectionRange, mMonthFirst);
  196. mSelection.AddItems(mDragSelectionRange);
  197. }
  198. mDragSelectionRange.Clear();
  199. mDragSelectionChanged = true;
  200. mDragTo = null;
  201. mDragFirst = null;
  202. }
  203. void IDatePickerPrivate.RaiseSelectionEnter(int childIndex, int fromChildIndex)
  204. {
  205. if (SelectionMode == SelectionType.Single)
  206. return;
  207. mSelectionFirst = null;
  208. mDragTo = mCells[childIndex].DayValue;
  209. if (mDragTo.HasValue && mDragFirst.HasValue)
  210. {
  211. CommonMethods.SelectRange(mDragTo.Value, mDragFirst.Value, mDragSelectionRange);
  212. LimitRangeToMonth(mDragSelectionRange, mMonthFirst);
  213. }
  214. mDragSelectionChanged = true;
  215. }
  216. void IDatePickerPrivate.RaiseSelectionExit(int childIndex, int fromChildIndex)
  217. {
  218. if (SelectionMode == SelectionType.Single)
  219. return;
  220. // if (mDragTo.HasValue && mDragTo.Value == mCells[childIndex].DayValue)
  221. // mDragTo = null;
  222. }
  223. }
  224. }