ItemCollectionViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using MediaBrowser.Model.Dto;
  2. using System;
  3. using System.Threading;
  4. using System.Windows;
  5. namespace MediaBrowser.UI.ViewModels
  6. {
  7. /// <summary>
  8. /// Represents a view model that contains multiple items.
  9. /// This should be used if you want to display a button or list item that holds more than one item,
  10. /// and cycle through them periodically.
  11. /// </summary>
  12. public class ItemCollectionViewModel : BaseViewModel, IDisposable
  13. {
  14. private int RotationPeriodMs { get; set; }
  15. public ItemCollectionViewModel(int rotationPeriodMs = 10000, int rotationDevaiationMs = 2000)
  16. : base()
  17. {
  18. if (rotationDevaiationMs > 0)
  19. {
  20. rotationPeriodMs += new Random(Guid.NewGuid().GetHashCode()).Next(0 - rotationDevaiationMs, rotationDevaiationMs);
  21. }
  22. RotationPeriodMs = rotationPeriodMs;
  23. }
  24. /// <summary>
  25. /// Gets the timer that updates the current item
  26. /// </summary>
  27. private Timer CurrentItemTimer { get; set; }
  28. private string _name;
  29. /// <summary>
  30. /// Gets or sets the name of the collection
  31. /// </summary>
  32. public string Name
  33. {
  34. get { return _name; }
  35. set
  36. {
  37. _name = value;
  38. OnPropertyChanged("Name");
  39. }
  40. }
  41. private BaseItemDto[] _items;
  42. /// <summary>
  43. /// Gets or sets the list of items
  44. /// </summary>
  45. public BaseItemDto[] Items
  46. {
  47. get { return _items; }
  48. set
  49. {
  50. _items = value ?? new BaseItemDto[] { };
  51. OnPropertyChanged("Items");
  52. CurrentItemIndex = Items.Length == 0 ? -1 : 0;
  53. ReloadTimer();
  54. }
  55. }
  56. private int _currentItemIndex;
  57. /// <summary>
  58. /// Gets or sets the index of the current item
  59. /// </summary>
  60. public int CurrentItemIndex
  61. {
  62. get { return _currentItemIndex; }
  63. set
  64. {
  65. _currentItemIndex = value;
  66. OnPropertyChanged("CurrentItemIndex");
  67. OnPropertyChanged("CurrentItem");
  68. OnPropertyChanged("NextItem");
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the current item
  73. /// </summary>
  74. public BaseItemDto CurrentItem
  75. {
  76. get { return CurrentItemIndex == -1 ? null : Items[CurrentItemIndex]; }
  77. }
  78. /// <summary>
  79. /// Gets the next item
  80. /// </summary>
  81. public BaseItemDto NextItem
  82. {
  83. get
  84. {
  85. if (CurrentItem == null || CurrentItemIndex == -1)
  86. {
  87. return null;
  88. }
  89. var index = CurrentItemIndex + 1;
  90. if (index >= Items.Length)
  91. {
  92. index = 0;
  93. }
  94. return Items[index];
  95. }
  96. }
  97. /// <summary>
  98. /// Disposes the timer
  99. /// </summary>
  100. private void DisposeTimer()
  101. {
  102. if (CurrentItemTimer != null)
  103. {
  104. CurrentItemTimer.Dispose();
  105. }
  106. }
  107. /// <summary>
  108. /// Reloads the timer
  109. /// </summary>
  110. private void ReloadTimer()
  111. {
  112. DisposeTimer();
  113. // Don't bother unless there's at least two items
  114. if (Items.Length > 1)
  115. {
  116. CurrentItemTimer = new Timer(state => Application.Current.Dispatcher.InvokeAsync(IncrementCurrentItemIndex), null, RotationPeriodMs, RotationPeriodMs);
  117. }
  118. }
  119. /// <summary>
  120. /// Increments current item index, or resets it back to zero if we're at the end of the list
  121. /// </summary>
  122. private void IncrementCurrentItemIndex()
  123. {
  124. var newIndex = CurrentItemIndex + 1;
  125. if (newIndex >= Items.Length)
  126. {
  127. newIndex = 0;
  128. }
  129. CurrentItemIndex = newIndex;
  130. }
  131. /// <summary>
  132. /// Disposes the collection
  133. /// </summary>
  134. public void Dispose()
  135. {
  136. DisposeTimer();
  137. }
  138. }
  139. }