CollabHistoryPresenter.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. using UnityEditor.Connect;
  3. using UnityEditor.Web;
  4. namespace UnityEditor.Collaboration
  5. {
  6. internal class CollabHistoryPresenter
  7. {
  8. public const int ItemsPerPage = 5;
  9. ICollabHistoryWindow m_Window;
  10. ICollabHistoryItemFactory m_Factory;
  11. IRevisionsService m_Service;
  12. ConnectInfo m_ConnectState;
  13. CollabInfo m_CollabState;
  14. bool m_IsCollabError;
  15. int m_TotalRevisions;
  16. int m_CurrentPage;
  17. int m_RequestedPage;
  18. bool m_FetchInProgress;
  19. BuildAccess m_BuildAccess;
  20. string m_ProgressRevision;
  21. public bool BuildServiceEnabled {get; set; }
  22. public CollabHistoryPresenter(ICollabHistoryWindow window, ICollabHistoryItemFactory factory, IRevisionsService service)
  23. {
  24. m_Window = window;
  25. m_Factory = factory;
  26. m_Service = service;
  27. m_CurrentPage = 0;
  28. m_BuildAccess = new BuildAccess();
  29. m_Service.FetchRevisionsCallback += OnFetchRevisions;
  30. }
  31. public void OnWindowEnabled()
  32. {
  33. UnityConnect.instance.StateChanged += OnConnectStateChanged;
  34. Collab.instance.StateChanged += OnCollabStateChanged;
  35. Collab.instance.RevisionUpdated += OnCollabRevisionUpdated;
  36. Collab.instance.JobsCompleted += OnCollabJobsCompleted;
  37. Collab.instance.ErrorOccurred += OnCollabError;
  38. Collab.instance.ErrorCleared += OnCollabErrorCleared;
  39. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  40. m_ConnectState = UnityConnect.instance.GetConnectInfo();
  41. m_CollabState = Collab.instance.GetCollabInfo();
  42. m_Window.revisionActionsEnabled = !EditorApplication.isPlayingOrWillChangePlaymode;
  43. // Setup window callbacks
  44. m_Window.OnPageChangeAction = OnUpdatePage;
  45. m_Window.OnUpdateAction = OnUpdate;
  46. m_Window.OnRestoreAction = OnRestore;
  47. m_Window.OnGoBackAction = OnGoBack;
  48. m_Window.OnShowBuildAction = ShowBuildForCommit;
  49. m_Window.OnShowServicesAction = ShowServicePage;
  50. m_Window.itemsPerPage = ItemsPerPage;
  51. // Initialize data
  52. UpdateBuildServiceStatus();
  53. var state = RecalculateState();
  54. // Only try to load the page if we're ready
  55. if (state == HistoryState.Ready)
  56. OnUpdatePage(m_CurrentPage);
  57. m_Window.UpdateState(state, true);
  58. }
  59. public void OnWindowDisabled()
  60. {
  61. UnityConnect.instance.StateChanged -= OnConnectStateChanged;
  62. Collab.instance.StateChanged -= OnCollabStateChanged;
  63. Collab.instance.RevisionUpdated -= OnCollabRevisionUpdated;
  64. Collab.instance.JobsCompleted -= OnCollabJobsCompleted;
  65. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  66. }
  67. private void OnConnectStateChanged(ConnectInfo state)
  68. {
  69. m_ConnectState = state;
  70. m_Window.UpdateState(RecalculateState(), false);
  71. }
  72. private void OnCollabStateChanged(CollabInfo state)
  73. {
  74. // Sometimes a collab state change will trigger even though everything is the same
  75. if (m_CollabState.Equals(state))
  76. return;
  77. if (m_CollabState.tip != state.tip)
  78. OnUpdatePage(m_CurrentPage);
  79. m_CollabState = state;
  80. m_Window.UpdateState(RecalculateState(), false);
  81. if (state.inProgress)
  82. {
  83. m_Window.inProgressRevision = m_ProgressRevision;
  84. }
  85. else
  86. {
  87. m_Window.inProgressRevision = null;
  88. }
  89. }
  90. private void OnCollabRevisionUpdated(CollabInfo state)
  91. {
  92. OnUpdatePage(m_CurrentPage);
  93. }
  94. private void OnCollabJobsCompleted(CollabInfo state)
  95. {
  96. m_ProgressRevision = null;
  97. }
  98. private void OnCollabError()
  99. {
  100. m_IsCollabError = true;
  101. m_Window.UpdateState(RecalculateState(), false);
  102. }
  103. private void OnCollabErrorCleared()
  104. {
  105. m_IsCollabError = false;
  106. m_FetchInProgress = true;
  107. m_Service.GetRevisions(m_CurrentPage * ItemsPerPage, ItemsPerPage);
  108. m_Window.UpdateState(RecalculateState(), false);
  109. }
  110. private void OnPlayModeStateChanged(PlayModeStateChange stateChange)
  111. {
  112. // If entering play mode, disable
  113. if (stateChange == PlayModeStateChange.ExitingEditMode ||
  114. stateChange == PlayModeStateChange.EnteredPlayMode)
  115. {
  116. m_Window.revisionActionsEnabled = false;
  117. }
  118. // If exiting play mode, enable!
  119. else if (stateChange == PlayModeStateChange.EnteredEditMode ||
  120. stateChange == PlayModeStateChange.ExitingPlayMode)
  121. {
  122. m_Window.revisionActionsEnabled = true;
  123. }
  124. }
  125. private HistoryState RecalculateState()
  126. {
  127. if (!m_ConnectState.online)
  128. return HistoryState.Offline;
  129. if (m_ConnectState.maintenance || m_CollabState.maintenance)
  130. return HistoryState.Maintenance;
  131. if (!m_ConnectState.loggedIn)
  132. return HistoryState.LoggedOut;
  133. if (!m_CollabState.seat)
  134. return HistoryState.NoSeat;
  135. if (!Collab.instance.IsCollabEnabledForCurrentProject())
  136. return HistoryState.Disabled;
  137. if (!Collab.instance.IsConnected() || !m_CollabState.ready || m_FetchInProgress)
  138. return HistoryState.Waiting;
  139. if (m_ConnectState.error || m_IsCollabError)
  140. return HistoryState.Error;
  141. return HistoryState.Ready;
  142. }
  143. // TODO: Eventually this can be a listener on the build service status
  144. public void UpdateBuildServiceStatus()
  145. {
  146. foreach (var service in UnityConnectServiceCollection.instance.GetAllServiceInfos())
  147. {
  148. if (service.name.Equals("Build"))
  149. {
  150. BuildServiceEnabled = service.enabled;
  151. }
  152. }
  153. }
  154. public void ShowBuildForCommit(string revisionID)
  155. {
  156. m_BuildAccess.ShowBuildForCommit(revisionID);
  157. }
  158. public void ShowServicePage()
  159. {
  160. m_BuildAccess.ShowServicePage();
  161. }
  162. public void OnUpdatePage(int page)
  163. {
  164. m_FetchInProgress = true;
  165. m_Service.GetRevisions(page * ItemsPerPage, ItemsPerPage);
  166. m_Window.UpdateState(RecalculateState(), false);
  167. m_RequestedPage = page;
  168. }
  169. private void OnFetchRevisions(RevisionsResult data)
  170. {
  171. m_FetchInProgress = false;
  172. IEnumerable<RevisionData> items = null;
  173. if (data != null)
  174. {
  175. m_CurrentPage = m_RequestedPage;
  176. m_TotalRevisions = data.RevisionsInRepo;
  177. items = m_Factory.GenerateElements(data.Revisions, m_TotalRevisions, m_CurrentPage * ItemsPerPage, m_Service.tipRevision, m_Window.inProgressRevision, m_Window.revisionActionsEnabled, BuildServiceEnabled, m_Service.currentUser);
  178. }
  179. // State must be recalculated prior to inserting items
  180. m_Window.UpdateState(RecalculateState(), false);
  181. m_Window.UpdateRevisions(items, m_Service.tipRevision, m_TotalRevisions, m_CurrentPage);
  182. }
  183. private void OnRestore(string revisionId, bool updatetorevision)
  184. {
  185. m_ProgressRevision = revisionId;
  186. Collab.instance.ResyncToRevision(revisionId);
  187. }
  188. private void OnGoBack(string revisionId, bool updatetorevision)
  189. {
  190. m_ProgressRevision = revisionId;
  191. Collab.instance.GoBackToRevision(revisionId, false);
  192. }
  193. private void OnUpdate(string revisionId, bool updatetorevision)
  194. {
  195. m_ProgressRevision = revisionId;
  196. Collab.instance.Update(revisionId, updatetorevision);
  197. }
  198. }
  199. }