HistoryTabPageView.cs
8.3 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Unity.Cloud.Collaborate.Assets;
using Unity.Cloud.Collaborate.Components;
using Unity.Cloud.Collaborate.Models.Structures;
using Unity.Cloud.Collaborate.Presenters;
using Unity.Cloud.Collaborate.UserInterface;
using Unity.Cloud.Collaborate.Utilities;
using Unity.Cloud.Collaborate.Views.Adapters.ListAdapters;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Views
{
internal class HistoryTabPageView : TabPageComponent, IHistoryView
{
[CanBeNull]
IHistoryPresenter m_Presenter;
public const string UssClassName = "history-page";
public const string PaginatorUssClassName = UssClassName + "__paginator";
public const string ContentUssClassName = UssClassName + "__content";
public const string NoticeUssClassName = UssClassName + "__notice";
static readonly string k_LayoutPath = $"{CollaborateWindow.LayoutPath}/{nameof(HistoryTabPageView)}.uxml";
readonly ScrollView m_Content;
readonly ListNotice m_ListNotice;
readonly Paginator m_Paginator;
bool m_Active;
public HistoryTabPageView()
{
AddToClassList(UssClassName);
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_LayoutPath).CloneTree(this);
m_Paginator = this.Q<Paginator>(className: PaginatorUssClassName);
m_Paginator.AddToClassList(UiConstants.ussHidden);
m_Paginator.ClickedMovePage += OnClickedMovePage;
m_Content = this.Q<ScrollView>(className: ContentUssClassName);
// Add loading notice
m_ListNotice = this.Q<ListNotice>(className: NoticeUssClassName);
m_ListNotice.Text = StringAssets.loadingRevisions;
}
/// <inheritdoc />
public IHistoryPresenter Presenter
{
set
{
m_Presenter = value;
// If tab active before presenter has been added, call start once we have it.
if (Active)
{
m_Presenter.Start();
}
}
}
/// <inheritdoc />
public void SetBusyStatus(bool busy)
{
m_Paginator.SetEnabled(!busy);
m_Content.SetEnabled(!busy);
}
/// <inheritdoc />
public void SetHistoryList(IReadOnlyList<IHistoryEntry> list)
{
// Clear out old content
m_ListNotice.AddToClassList(UiConstants.ussHidden);
m_Content.Clear();
// Show paginator
m_Paginator.RemoveFromClassList(UiConstants.ussHidden);
// Handle empty list case
if (list.Count == 0)
{
m_ListNotice.Text = StringAssets.noticeNoRevisionsToDisplay;
m_Content.RemoveFromClassList(UiConstants.ussHidden);
return;
}
foreach (var entry in list)
{
// Add entry to the list
m_Content.Add(CreateHistoryEntry(entry, false));
}
}
/// <summary>
/// Event handler for receiving page change requests.
/// </summary>
/// <param name="pageChange">
/// Delta to change the page by: Paginator.MoveForwards, Paginator.MoveBackwards. Mapped to +1, -1 respectively.
/// </param>
void OnClickedMovePage(int pageChange)
{
Assert.IsNotNull(m_Presenter, "Invalid state when requesting page change.");
if (pageChange == Paginator.MoveBackwards)
{
m_Presenter.PrevPage();
}
else
{
m_Presenter.NextPage();
}
}
/// <inheritdoc />
public void SetPage(int page, int max)
{
m_Paginator.SetPage(page, max);
}
/// <inheritdoc />
public void SetSelection(IHistoryEntry entry)
{
// Hide paginator
m_Paginator.AddToClassList(UiConstants.ussHidden);
// Clear out old content
m_ListNotice.AddToClassList(UiConstants.ussHidden);
m_Content.Clear();
// Add new content
m_Content.Add(CreateHistoryEntry(entry, true));
}
/// <summary>
/// Takes a IHistoryEntry and binds it to a created HistoryEntryComponent to be used in the history list.
/// </summary>
/// <param name="entry">History entry to bind</param>
/// <param name="expanded">Whether or not to show its list of changed entries.</param>
/// <returns>Inflated and bound component.</returns>
HistoryEntryComponent CreateHistoryEntry([NotNull] IHistoryEntry entry, bool expanded)
{
Assert.IsNotNull(m_Presenter, "Invalid state when creating history entry");
var comp = new HistoryEntryComponent();
// Handle expanded vs compact layout
if (expanded)
{
// Hide fields used for compact view
comp.showFilesButton.AddToClassList(UiConstants.ussHidden);
comp.cloudStatusText.AddToClassList(UiConstants.ussHidden);
comp.changedFilesCount.text = $"Changes ( {entry.Changes.Count} )";
var listAdapter = new HistoryEntryChangeListAdapter(m_Presenter, entry.RevisionId, entry.Changes.ToList());
comp.changedFiles.SetAdapter(listAdapter);
listAdapter.NotifyDataSetChanged();
// Configure button
comp.gotoButton.text = entry.GetGotoText();
comp.gotoButton.clickable.clicked += () => m_Presenter.RequestGoto(entry.RevisionId, entry.Status);
}
else
{
// Hide fields used for expanded view
comp.changedFilesCount.AddToClassList(UiConstants.ussHidden);
comp.changedFiles.AddToClassList(UiConstants.ussHidden);
comp.gotoButton.text = string.Empty;
comp.gotoButton.AddToClassList(UiConstants.ussHidden);
// Setup show button
comp.showFilesButton.text = entry.Changes.Count == 1
? StringAssets.showChange
: string.Format(StringAssets.showChanges, entry.Changes.Count);
comp.showFilesButton.clickable.clicked += () => m_Presenter.SelectedRevisionId = entry.RevisionId;
// TODO: cloud status text
}
// Trim whitespace on either side and grab initial for profile circle
var trimmedAuthorName = entry.AuthorName.Trim();
comp.profileInitial.text = trimmedAuthorName.Substring(0, 1).ToUpper();
comp.authorName.text = trimmedAuthorName;
// Display relative or absolute timestamp. If relative, show absolute as a tooltip.
comp.timestamp.text = TimeStamp.GetTimeStamp(entry.Time);
if (TimeStamp.UseRelativeTimeStamps)
{
comp.timestamp.tooltip = TimeStamp.GetLocalisedTimeStamp(entry.Time);
}
// Display revision id and show full length id as a tooltip
comp.revisionId.text = $"ID: {entry.RevisionId.Substring(0, 10)}";
comp.revisionId.tooltip = entry.RevisionId;
comp.commitMessage.text = entry.Message;
return comp;
}
/// <inheritdoc />
public bool DisplayDialogue(string title, string message, string affirmative)
{
return EditorUtility.DisplayDialog(title, message, affirmative);
}
/// <inheritdoc />
public bool DisplayDialogue(string title, string message, string affirmative, string negative)
{
return EditorUtility.DisplayDialog(title, message, affirmative, negative);
}
/// <inheritdoc />
protected override void SetActive()
{
Assert.IsFalse(m_Active, "The view is already active.");
m_Active = true;
m_Presenter?.Start();
}
/// <inheritdoc />
protected override void SetInactive()
{
Assert.IsTrue(m_Active, "The view is already inactive.");
m_Active = false;
m_Presenter?.Stop();
}
}
}