MainModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using JetBrains.Annotations;
  3. using Unity.Cloud.Collaborate.Models.Api;
  4. using Unity.Cloud.Collaborate.Models.Structures;
  5. using Unity.Cloud.Collaborate.UserInterface;
  6. using UnityEngine.Assertions;
  7. namespace Unity.Cloud.Collaborate.Models
  8. {
  9. internal class MainModel : IMainModel
  10. {
  11. [NotNull]
  12. readonly ISourceControlProvider m_Provider;
  13. /// <inheritdoc />
  14. public event Action<bool> ConflictStatusChange;
  15. /// <inheritdoc />
  16. public event Action<bool> OperationStatusChange;
  17. /// <inheritdoc />
  18. public event Action<IProgressInfo> OperationProgressChange;
  19. /// <inheritdoc />
  20. public event Action<IErrorInfo> ErrorOccurred;
  21. /// <inheritdoc />
  22. public event Action ErrorCleared;
  23. /// <inheritdoc />
  24. public event Action<bool> RemoteRevisionsAvailabilityChange;
  25. /// <inheritdoc />
  26. public event Action<string> BackButtonStateUpdated;
  27. /// <inheritdoc />
  28. public event Action StateChanged;
  29. [NotNull]
  30. readonly IHistoryModel m_HistoryModel;
  31. [NotNull]
  32. readonly IChangesModel m_ChangesModel;
  33. (string id, string text, Action backEvent)? m_BackNavigation;
  34. public MainModel([NotNull] ISourceControlProvider provider)
  35. {
  36. m_Provider = provider;
  37. m_HistoryModel = new HistoryModel(m_Provider);
  38. m_ChangesModel = new ChangesModel(m_Provider);
  39. }
  40. /// <inheritdoc />
  41. public void OnStart()
  42. {
  43. // Setup events
  44. m_Provider.UpdatedOperationStatus += OnUpdatedOperationStatus;
  45. m_Provider.UpdatedOperationProgress += OnUpdatedOperationProgress;
  46. m_Provider.ErrorOccurred += OnErrorOccurred;
  47. m_Provider.ErrorCleared += OnErrorCleared;
  48. m_Provider.UpdatedConflictState += OnUpdatedConflictState;
  49. m_Provider.UpdatedRemoteRevisionsAvailability += OnUpdatedRemoteRevisionsAvailability;
  50. // Propagate event to "child" models.
  51. m_HistoryModel.OnStart();
  52. m_ChangesModel.OnStart();
  53. }
  54. /// <inheritdoc />
  55. public void OnStop()
  56. {
  57. // Clean up.
  58. m_Provider.UpdatedOperationStatus -= OnUpdatedOperationStatus;
  59. m_Provider.UpdatedOperationProgress -= OnUpdatedOperationProgress;
  60. m_Provider.ErrorOccurred -= OnErrorOccurred;
  61. m_Provider.ErrorCleared -= OnErrorCleared;
  62. m_Provider.UpdatedConflictState -= OnUpdatedConflictState;
  63. m_Provider.UpdatedRemoteRevisionsAvailability -= OnUpdatedRemoteRevisionsAvailability;
  64. // Propagate event to "child" models.
  65. m_HistoryModel.OnStop();
  66. m_ChangesModel.OnStop();
  67. }
  68. /// <inheritdoc />
  69. public void RestoreState(IWindowCache cache)
  70. {
  71. // Read in cached data.
  72. CurrentTabIndex = cache.TabIndex;
  73. StateChanged?.Invoke();
  74. // Propagate restore call to "child" models.
  75. m_HistoryModel.RestoreState(cache);
  76. m_ChangesModel.RestoreState(cache);
  77. }
  78. /// <inheritdoc />
  79. public void SaveState(IWindowCache cache)
  80. {
  81. // Cache data.
  82. cache.TabIndex = CurrentTabIndex;
  83. // Propagate save call to "child" models.
  84. m_HistoryModel.SaveState(cache);
  85. m_ChangesModel.SaveState(cache);
  86. }
  87. /// <inheritdoc />
  88. public bool RemoteRevisionsAvailable => m_Provider.GetRemoteRevisionAvailability();
  89. /// <inheritdoc />
  90. public bool Conflicted => m_Provider.GetConflictedState();
  91. /// <inheritdoc />
  92. public IProgressInfo ProgressInfo => m_Provider.GetProgressState();
  93. /// <inheritdoc />
  94. public IErrorInfo ErrorInfo => m_Provider.GetErrorState();
  95. /// <inheritdoc />
  96. public int CurrentTabIndex { get; set; }
  97. /// <inheritdoc />
  98. public IHistoryModel ConstructHistoryModel()
  99. {
  100. return m_HistoryModel;
  101. }
  102. /// <inheritdoc />
  103. public IChangesModel ConstructChangesModel()
  104. {
  105. return m_ChangesModel;
  106. }
  107. /// <inheritdoc />
  108. public void ClearError()
  109. {
  110. m_Provider.ClearError();
  111. }
  112. /// <inheritdoc />
  113. public void RequestSync()
  114. {
  115. m_Provider.RequestSync();
  116. }
  117. /// <inheritdoc />
  118. public void RequestCancelJob()
  119. {
  120. m_Provider.RequestCancelJob();
  121. }
  122. /// <inheritdoc />
  123. public (string id, string text, Action backAction)? GetBackNavigation()
  124. {
  125. return m_BackNavigation;
  126. }
  127. /// <inheritdoc />
  128. public void RegisterBackNavigation(string id, string text, Action backAction)
  129. {
  130. Assert.IsTrue(m_BackNavigation == null, "There should only be one back navigation registered at a time.");
  131. m_BackNavigation = (id, text, backAction);
  132. BackButtonStateUpdated?.Invoke(text);
  133. }
  134. /// <inheritdoc />
  135. public bool UnregisterBackNavigation(string id)
  136. {
  137. if (m_BackNavigation?.id != id) return false;
  138. m_BackNavigation = null;
  139. BackButtonStateUpdated?.Invoke(null);
  140. return true;
  141. }
  142. /// <summary>
  143. /// Event handler for when the availability of remote revisions changes.
  144. /// </summary>
  145. /// <param name="available">New availability status.</param>
  146. void OnUpdatedRemoteRevisionsAvailability(bool available)
  147. {
  148. RemoteRevisionsAvailabilityChange?.Invoke(available);
  149. }
  150. /// <summary>
  151. /// Event handler for when the conflicted status changes.
  152. /// </summary>
  153. /// <param name="conflicted">New conflicted status.</param>
  154. void OnUpdatedConflictState(bool conflicted)
  155. {
  156. ConflictStatusChange?.Invoke(conflicted);
  157. }
  158. void OnUpdatedOperationStatus(bool inProgress)
  159. {
  160. OperationStatusChange?.Invoke(inProgress);
  161. }
  162. void OnUpdatedOperationProgress(IProgressInfo progressInfo)
  163. {
  164. OperationProgressChange?.Invoke(progressInfo);
  165. }
  166. void OnErrorOccurred(IErrorInfo errorInfo)
  167. {
  168. ErrorOccurred?.Invoke(errorInfo);
  169. }
  170. void OnErrorCleared()
  171. {
  172. ErrorCleared?.Invoke();
  173. }
  174. }
  175. }