using System; using JetBrains.Annotations; using Unity.Cloud.Collaborate.Models.Api; using Unity.Cloud.Collaborate.Models.Structures; using Unity.Cloud.Collaborate.UserInterface; using UnityEngine.Assertions; namespace Unity.Cloud.Collaborate.Models { internal class MainModel : IMainModel { [NotNull] readonly ISourceControlProvider m_Provider; /// public event Action ConflictStatusChange; /// public event Action OperationStatusChange; /// public event Action OperationProgressChange; /// public event Action ErrorOccurred; /// public event Action ErrorCleared; /// public event Action RemoteRevisionsAvailabilityChange; /// public event Action BackButtonStateUpdated; /// public event Action StateChanged; [NotNull] readonly IHistoryModel m_HistoryModel; [NotNull] readonly IChangesModel m_ChangesModel; (string id, string text, Action backEvent)? m_BackNavigation; public MainModel([NotNull] ISourceControlProvider provider) { m_Provider = provider; m_HistoryModel = new HistoryModel(m_Provider); m_ChangesModel = new ChangesModel(m_Provider); } /// public void OnStart() { // Setup events m_Provider.UpdatedOperationStatus += OnUpdatedOperationStatus; m_Provider.UpdatedOperationProgress += OnUpdatedOperationProgress; m_Provider.ErrorOccurred += OnErrorOccurred; m_Provider.ErrorCleared += OnErrorCleared; m_Provider.UpdatedConflictState += OnUpdatedConflictState; m_Provider.UpdatedRemoteRevisionsAvailability += OnUpdatedRemoteRevisionsAvailability; // Propagate event to "child" models. m_HistoryModel.OnStart(); m_ChangesModel.OnStart(); } /// public void OnStop() { // Clean up. m_Provider.UpdatedOperationStatus -= OnUpdatedOperationStatus; m_Provider.UpdatedOperationProgress -= OnUpdatedOperationProgress; m_Provider.ErrorOccurred -= OnErrorOccurred; m_Provider.ErrorCleared -= OnErrorCleared; m_Provider.UpdatedConflictState -= OnUpdatedConflictState; m_Provider.UpdatedRemoteRevisionsAvailability -= OnUpdatedRemoteRevisionsAvailability; // Propagate event to "child" models. m_HistoryModel.OnStop(); m_ChangesModel.OnStop(); } /// public void RestoreState(IWindowCache cache) { // Read in cached data. CurrentTabIndex = cache.TabIndex; StateChanged?.Invoke(); // Propagate restore call to "child" models. m_HistoryModel.RestoreState(cache); m_ChangesModel.RestoreState(cache); } /// public void SaveState(IWindowCache cache) { // Cache data. cache.TabIndex = CurrentTabIndex; // Propagate save call to "child" models. m_HistoryModel.SaveState(cache); m_ChangesModel.SaveState(cache); } /// public bool RemoteRevisionsAvailable => m_Provider.GetRemoteRevisionAvailability(); /// public bool Conflicted => m_Provider.GetConflictedState(); /// public IProgressInfo ProgressInfo => m_Provider.GetProgressState(); /// public IErrorInfo ErrorInfo => m_Provider.GetErrorState(); /// public int CurrentTabIndex { get; set; } /// public IHistoryModel ConstructHistoryModel() { return m_HistoryModel; } /// public IChangesModel ConstructChangesModel() { return m_ChangesModel; } /// public void ClearError() { m_Provider.ClearError(); } /// public void RequestSync() { m_Provider.RequestSync(); } /// public void RequestCancelJob() { m_Provider.RequestCancelJob(); } /// public (string id, string text, Action backAction)? GetBackNavigation() { return m_BackNavigation; } /// public void RegisterBackNavigation(string id, string text, Action backAction) { Assert.IsTrue(m_BackNavigation == null, "There should only be one back navigation registered at a time."); m_BackNavigation = (id, text, backAction); BackButtonStateUpdated?.Invoke(text); } /// public bool UnregisterBackNavigation(string id) { if (m_BackNavigation?.id != id) return false; m_BackNavigation = null; BackButtonStateUpdated?.Invoke(null); return true; } /// /// Event handler for when the availability of remote revisions changes. /// /// New availability status. void OnUpdatedRemoteRevisionsAvailability(bool available) { RemoteRevisionsAvailabilityChange?.Invoke(available); } /// /// Event handler for when the conflicted status changes. /// /// New conflicted status. void OnUpdatedConflictState(bool conflicted) { ConflictStatusChange?.Invoke(conflicted); } void OnUpdatedOperationStatus(bool inProgress) { OperationStatusChange?.Invoke(inProgress); } void OnUpdatedOperationProgress(IProgressInfo progressInfo) { OperationProgressChange?.Invoke(progressInfo); } void OnErrorOccurred(IErrorInfo errorInfo) { ErrorOccurred?.Invoke(errorInfo); } void OnErrorCleared() { ErrorCleared?.Invoke(); } } }