HistoryPresenter.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using JetBrains.Annotations;
  4. using Unity.Cloud.Collaborate.Assets;
  5. using Unity.Cloud.Collaborate.Models;
  6. using Unity.Cloud.Collaborate.Views;
  7. using Unity.Cloud.Collaborate.Models.Structures;
  8. using UnityEngine;
  9. using UnityEngine.Assertions;
  10. namespace Unity.Cloud.Collaborate.Presenters
  11. {
  12. internal class HistoryPresenter : IHistoryPresenter
  13. {
  14. internal const int pageSize = 10;
  15. internal const string historyEntrySelectedId = "history-entry-selected";
  16. int m_MaxPages;
  17. bool m_IsStarted;
  18. [NotNull]
  19. readonly IHistoryView m_View;
  20. [NotNull]
  21. readonly IHistoryModel m_HistoryModel;
  22. [NotNull]
  23. readonly IMainModel m_MainModel;
  24. public HistoryPresenter([NotNull] IHistoryView view, [NotNull] IHistoryModel historyModel, [NotNull] IMainModel mainModel)
  25. {
  26. m_View = view;
  27. m_HistoryModel = historyModel;
  28. m_MainModel = mainModel;
  29. }
  30. /// <inheritdoc />
  31. public void Start()
  32. {
  33. Assert.IsFalse(m_IsStarted, "The presenter has already been started.");
  34. m_IsStarted = true;
  35. m_HistoryModel.HistoryListUpdated += OnHistoryListUpdated;
  36. m_HistoryModel.SelectedRevisionReceived += OnSelectedRevisionReceived;
  37. m_HistoryModel.EntryCountUpdated += OnEntryCountUpdated;
  38. m_HistoryModel.HistoryListReceived += OnHistoryListReceived;
  39. m_HistoryModel.BusyStatusUpdated += OnBusyStatusUpdated;
  40. m_HistoryModel.StateChanged += OnStateChanged;
  41. PopulateInitialData();
  42. }
  43. /// <inheritdoc />
  44. public void Stop()
  45. {
  46. Assert.IsTrue(m_IsStarted, "The presenter has already been stopped.");
  47. m_IsStarted = false;
  48. m_HistoryModel.HistoryListUpdated -= OnHistoryListUpdated;
  49. m_HistoryModel.SelectedRevisionReceived -= OnSelectedRevisionReceived;
  50. m_HistoryModel.EntryCountUpdated -= OnEntryCountUpdated;
  51. m_HistoryModel.HistoryListReceived -= OnHistoryListReceived;
  52. m_HistoryModel.BusyStatusUpdated -= OnBusyStatusUpdated;
  53. m_HistoryModel.StateChanged -= OnStateChanged;
  54. m_MainModel.UnregisterBackNavigation(historyEntrySelectedId);
  55. }
  56. /// <summary>
  57. /// Refresh state from the model.
  58. /// </summary>
  59. void OnStateChanged()
  60. {
  61. PopulateInitialData();
  62. }
  63. /// <summary>
  64. /// Populate the view with the initial data from the model.
  65. /// </summary>
  66. void PopulateInitialData()
  67. {
  68. m_View.SetBusyStatus(m_HistoryModel.Busy);
  69. if (!string.IsNullOrEmpty(m_HistoryModel.SelectedRevisionId))
  70. {
  71. m_HistoryModel.RequestSingleRevision(m_HistoryModel.SelectedRevisionId);
  72. }
  73. else if (!string.IsNullOrEmpty(m_HistoryModel.SavedRevisionId))
  74. {
  75. m_HistoryModel.RequestSingleRevision(m_HistoryModel.SavedRevisionId);
  76. }
  77. else
  78. {
  79. // Request initial data
  80. m_HistoryModel.RequestPageOfRevisions(pageSize);
  81. }
  82. m_HistoryModel.RequestEntryNumber();
  83. }
  84. /// <summary>
  85. /// Event handler to receive updated busy status.
  86. /// </summary>
  87. /// <param name="busy">New busy status.</param>
  88. void OnBusyStatusUpdated(bool busy)
  89. {
  90. m_View.SetBusyStatus(busy);
  91. }
  92. /// <summary>
  93. /// Event handler to receive requested history list.
  94. /// </summary>
  95. /// <param name="list">Received history list.</param>
  96. void OnHistoryListReceived(IReadOnlyList<IHistoryEntry> list)
  97. {
  98. if (list == null)
  99. {
  100. // Return back to first page of entries
  101. m_HistoryModel.PageNumber = 0;
  102. m_HistoryModel.RequestPageOfRevisions(pageSize);
  103. Debug.LogError("Request page does not exist.");
  104. return;
  105. }
  106. m_MainModel.UnregisterBackNavigation(historyEntrySelectedId);
  107. m_View.SetHistoryList(list);
  108. }
  109. /// <summary>
  110. /// Event handler to receive updated history entry count.
  111. /// </summary>
  112. /// <param name="count">New entry count.</param>
  113. void OnEntryCountUpdated(int? count)
  114. {
  115. if (count == null)
  116. {
  117. Debug.LogError("Unable to fetch number of revisions");
  118. return;
  119. }
  120. m_MaxPages = (count.Value - 1) / pageSize;
  121. m_View.SetPage(m_HistoryModel.PageNumber, m_MaxPages);
  122. }
  123. /// <summary>
  124. /// Event handler to receive requested single revision.
  125. /// </summary>
  126. /// <param name="entry">Received single revision.</param>
  127. void OnSelectedRevisionReceived(IHistoryEntry entry)
  128. {
  129. if (entry == null)
  130. {
  131. // Return back to all revisions list
  132. m_HistoryModel.RequestPageOfRevisions(pageSize);
  133. Debug.LogError("Unable to find requested revision");
  134. return;
  135. }
  136. m_MainModel.RegisterBackNavigation(historyEntrySelectedId, StringAssets.allHistory, OnBackEvent);
  137. m_View.SetSelection(entry);
  138. }
  139. /// <summary>
  140. /// Event handler for when the model has received a message of an updated history list.
  141. /// </summary>
  142. void OnHistoryListUpdated()
  143. {
  144. // Request updated number of entries.
  145. m_HistoryModel.RequestEntryNumber();
  146. // Request either single revision or list of revisions depending on current state.
  147. if (m_HistoryModel.IsRevisionSelected)
  148. {
  149. Assert.AreNotEqual(string.Empty, m_HistoryModel.SelectedRevisionId, "There should be a revision id at this point.");
  150. m_HistoryModel.RequestSingleRevision(m_HistoryModel.SelectedRevisionId);
  151. }
  152. else
  153. {
  154. m_HistoryModel.RequestPageOfRevisions(pageSize);
  155. }
  156. }
  157. /// <summary>
  158. /// Event handler for when the back button is pressed.
  159. /// </summary>
  160. void OnBackEvent()
  161. {
  162. // Return back to all revisions list
  163. m_HistoryModel.RequestPageOfRevisions(pageSize);
  164. }
  165. /// <inheritdoc />
  166. public void PrevPage()
  167. {
  168. m_HistoryModel.PageNumber = Math.Max(m_HistoryModel.PageNumber - 1, 0);
  169. m_HistoryModel.RequestPageOfRevisions(pageSize);
  170. m_View.SetPage(m_HistoryModel.PageNumber, m_MaxPages);
  171. }
  172. /// <inheritdoc />
  173. public void NextPage()
  174. {
  175. m_HistoryModel.PageNumber = Math.Min(m_HistoryModel.PageNumber + 1, m_MaxPages);
  176. m_HistoryModel.RequestPageOfRevisions(pageSize);
  177. m_View.SetPage(m_HistoryModel.PageNumber, m_MaxPages);
  178. }
  179. /// <inheritdoc />
  180. public string SelectedRevisionId
  181. {
  182. set
  183. {
  184. if (m_HistoryModel.SelectedRevisionId == value) return;
  185. m_HistoryModel.RequestSingleRevision(value);
  186. }
  187. }
  188. /// <inheritdoc />
  189. public void RequestGoto(string revisionId, HistoryEntryStatus status)
  190. {
  191. switch (status) {
  192. case HistoryEntryStatus.Ahead:
  193. m_HistoryModel.RequestUpdateTo(revisionId);
  194. break;
  195. case HistoryEntryStatus.Current:
  196. m_HistoryModel.RequestRestoreTo(revisionId);
  197. break;
  198. case HistoryEntryStatus.Behind:
  199. m_HistoryModel.RequestGoBackTo(revisionId);
  200. break;
  201. default:
  202. throw new ArgumentOutOfRangeException(nameof(status), status, null);
  203. }
  204. }
  205. /// <inheritdoc />
  206. public bool SupportsRevert => m_HistoryModel.SupportsRevert;
  207. /// <inheritdoc />
  208. public void RequestRevert(string revisionId, IReadOnlyList<string> files)
  209. {
  210. m_HistoryModel.RequestRevert(revisionId, files);
  211. }
  212. }
  213. }