DatePickerCollection.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Bitsplash.DatePicker
  7. {
  8. /// <summary>
  9. /// holds dates selected by the date picker
  10. /// </summary>
  11. public class DatePickerCollection : IDatePickerCollectionPrivate
  12. {
  13. bool mInvalid = false;
  14. HashSet<DateTime> mData = new HashSet<DateTime>();
  15. bool mChanged = false;
  16. bool mAllowEmpty = false;
  17. DateTime[] mItems;
  18. bool IDatePickerCollectionPrivate.Changed { get { return mChanged; } set { mChanged = value; } }
  19. bool IDatePickerCollectionPrivate.AllowEmpty { get { return mAllowEmpty; } set
  20. {
  21. mAllowEmpty = value;
  22. if(mAllowEmpty == false && mData.Count ==0)
  23. {
  24. ValidateNonEmpty();
  25. Invalidate();
  26. }
  27. }
  28. }
  29. event Action InnerSelectionModified;
  30. event Action IDatePickerCollectionPrivate.SelectionModified
  31. {
  32. add
  33. {
  34. InnerSelectionModified += value;
  35. }
  36. remove
  37. {
  38. InnerSelectionModified -= value;
  39. }
  40. }
  41. public int Count
  42. {
  43. get { return mData.Count; }
  44. }
  45. /// <summary>
  46. /// selects a range of dates , clearing all other dates
  47. /// </summary>
  48. /// <param name="from"></param>
  49. /// <param name="to"></param>
  50. public void SelectRange(DateTime from,DateTime to)
  51. {
  52. if (CommonMethods.IsRangeSelected(from, to, mData))
  53. return;
  54. CommonMethods.SelectRange(from, to, mData);
  55. Invalidate();
  56. }
  57. void Invalidate()
  58. {
  59. mChanged = true;
  60. mInvalid = true;
  61. if (InnerSelectionModified != null)
  62. InnerSelectionModified();
  63. }
  64. /// <summary>
  65. /// returns true if a date is the only date selected
  66. /// </summary>
  67. /// <param name="date"></param>
  68. /// <returns></returns>
  69. public bool IsSingleDateSelected(DateTime date)
  70. {
  71. date = date.Date;
  72. if (mData.Contains(date) && mData.Count == 1)
  73. return true;
  74. return false;
  75. }
  76. /// <summary>
  77. /// selects one date clearing all other dates
  78. /// </summary>
  79. /// <param name="date"></param>
  80. public void SelectOne(DateTime date)
  81. {
  82. date = date.Date;
  83. if (mData.Contains(date) && mData.Count == 1)
  84. return;
  85. mData.Clear();
  86. Add(date);
  87. }
  88. void ValidateNonEmpty()
  89. {
  90. if (mAllowEmpty == false && mData.Count == 0)
  91. mData.Add(DateTime.Today.Date);
  92. }
  93. /// <summary>
  94. /// clears all selected dates. If the selection cannot be empty , DateTime.Today is set as the selection
  95. /// </summary>
  96. public void Clear()
  97. {
  98. if (mData.Count == 0)
  99. return;
  100. if (mAllowEmpty == false && IsSingleDateSelected(DateTime.Today))
  101. return;
  102. mData.Clear();
  103. ValidateNonEmpty();
  104. Invalidate();
  105. }
  106. /// <summary>
  107. /// appends all the items to the date collection
  108. /// </summary>
  109. /// <param name="range"></param>
  110. public void AddItems(HashSet<DateTime> range)
  111. {
  112. bool changed = false;
  113. foreach(DateTime d in range)
  114. {
  115. if (mData.Add(d))
  116. changed = true;
  117. }
  118. if (changed)
  119. Invalidate();
  120. }
  121. /// <summary>
  122. /// adds a date into the date selection.
  123. /// </summary>
  124. /// <param name="date"></param>
  125. public void Add(DateTime date)
  126. {
  127. if(mData.Add(date.Date))
  128. Invalidate();
  129. }
  130. /// <summary>
  131. /// returns true if the date collection contains the specified date
  132. /// </summary>
  133. /// <param name="date"></param>
  134. /// <returns></returns>
  135. public bool Contains(DateTime date)
  136. {
  137. return mData.Contains(date.Date);
  138. }
  139. /// <summary>
  140. /// gets the date item at index
  141. /// </summary>
  142. /// <param name="index"></param>
  143. /// <returns></returns>
  144. public DateTime GetItem(int index)
  145. {
  146. if(mInvalid || mItems == null)
  147. {
  148. mInvalid = false;
  149. mItems = mData.OrderBy(x => x).ToArray();
  150. }
  151. return mItems[index];
  152. }
  153. /// <summary>
  154. /// removes a date from the collection if it present. returns true if the date was present and was removed
  155. /// </summary>
  156. /// <param name="date"></param>
  157. /// <returns></returns>
  158. public bool Remove(DateTime date)
  159. {
  160. if (mData.Remove(date.Date))
  161. {
  162. ValidateNonEmpty();
  163. Invalidate();
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. }