MainModel.cs
6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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;
/// <inheritdoc />
public event Action<bool> ConflictStatusChange;
/// <inheritdoc />
public event Action<bool> OperationStatusChange;
/// <inheritdoc />
public event Action<IProgressInfo> OperationProgressChange;
/// <inheritdoc />
public event Action<IErrorInfo> ErrorOccurred;
/// <inheritdoc />
public event Action ErrorCleared;
/// <inheritdoc />
public event Action<bool> RemoteRevisionsAvailabilityChange;
/// <inheritdoc />
public event Action<string> BackButtonStateUpdated;
/// <inheritdoc />
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);
}
/// <inheritdoc />
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();
}
/// <inheritdoc />
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();
}
/// <inheritdoc />
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);
}
/// <inheritdoc />
public void SaveState(IWindowCache cache)
{
// Cache data.
cache.TabIndex = CurrentTabIndex;
// Propagate save call to "child" models.
m_HistoryModel.SaveState(cache);
m_ChangesModel.SaveState(cache);
}
/// <inheritdoc />
public bool RemoteRevisionsAvailable => m_Provider.GetRemoteRevisionAvailability();
/// <inheritdoc />
public bool Conflicted => m_Provider.GetConflictedState();
/// <inheritdoc />
public IProgressInfo ProgressInfo => m_Provider.GetProgressState();
/// <inheritdoc />
public IErrorInfo ErrorInfo => m_Provider.GetErrorState();
/// <inheritdoc />
public int CurrentTabIndex { get; set; }
/// <inheritdoc />
public IHistoryModel ConstructHistoryModel()
{
return m_HistoryModel;
}
/// <inheritdoc />
public IChangesModel ConstructChangesModel()
{
return m_ChangesModel;
}
/// <inheritdoc />
public void ClearError()
{
m_Provider.ClearError();
}
/// <inheritdoc />
public void RequestSync()
{
m_Provider.RequestSync();
}
/// <inheritdoc />
public void RequestCancelJob()
{
m_Provider.RequestCancelJob();
}
/// <inheritdoc />
public (string id, string text, Action backAction)? GetBackNavigation()
{
return m_BackNavigation;
}
/// <inheritdoc />
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);
}
/// <inheritdoc />
public bool UnregisterBackNavigation(string id)
{
if (m_BackNavigation?.id != id) return false;
m_BackNavigation = null;
BackButtonStateUpdated?.Invoke(null);
return true;
}
/// <summary>
/// Event handler for when the availability of remote revisions changes.
/// </summary>
/// <param name="available">New availability status.</param>
void OnUpdatedRemoteRevisionsAvailability(bool available)
{
RemoteRevisionsAvailabilityChange?.Invoke(available);
}
/// <summary>
/// Event handler for when the conflicted status changes.
/// </summary>
/// <param name="conflicted">New conflicted status.</param>
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();
}
}
}