HistoryTabPageView.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. using Unity.Cloud.Collaborate.Assets;
  6. using Unity.Cloud.Collaborate.Components;
  7. using Unity.Cloud.Collaborate.Models.Structures;
  8. using Unity.Cloud.Collaborate.Presenters;
  9. using Unity.Cloud.Collaborate.UserInterface;
  10. using Unity.Cloud.Collaborate.Utilities;
  11. using Unity.Cloud.Collaborate.Views.Adapters.ListAdapters;
  12. using UnityEditor;
  13. using UnityEngine;
  14. using UnityEngine.Assertions;
  15. using UnityEngine.UIElements;
  16. namespace Unity.Cloud.Collaborate.Views
  17. {
  18. internal class HistoryTabPageView : TabPageComponent, IHistoryView
  19. {
  20. [CanBeNull]
  21. IHistoryPresenter m_Presenter;
  22. public const string UssClassName = "history-page";
  23. public const string PaginatorUssClassName = UssClassName + "__paginator";
  24. public const string ContentUssClassName = UssClassName + "__content";
  25. public const string NoticeUssClassName = UssClassName + "__notice";
  26. static readonly string k_LayoutPath = $"{CollaborateWindow.LayoutPath}/{nameof(HistoryTabPageView)}.uxml";
  27. readonly ScrollView m_Content;
  28. readonly ListNotice m_ListNotice;
  29. readonly Paginator m_Paginator;
  30. bool m_Active;
  31. public HistoryTabPageView()
  32. {
  33. AddToClassList(UssClassName);
  34. AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_LayoutPath).CloneTree(this);
  35. m_Paginator = this.Q<Paginator>(className: PaginatorUssClassName);
  36. m_Paginator.AddToClassList(UiConstants.ussHidden);
  37. m_Paginator.ClickedMovePage += OnClickedMovePage;
  38. m_Content = this.Q<ScrollView>(className: ContentUssClassName);
  39. // Add loading notice
  40. m_ListNotice = this.Q<ListNotice>(className: NoticeUssClassName);
  41. m_ListNotice.Text = StringAssets.loadingRevisions;
  42. }
  43. /// <inheritdoc />
  44. public IHistoryPresenter Presenter
  45. {
  46. set
  47. {
  48. m_Presenter = value;
  49. // If tab active before presenter has been added, call start once we have it.
  50. if (Active)
  51. {
  52. m_Presenter.Start();
  53. }
  54. }
  55. }
  56. /// <inheritdoc />
  57. public void SetBusyStatus(bool busy)
  58. {
  59. m_Paginator.SetEnabled(!busy);
  60. m_Content.SetEnabled(!busy);
  61. }
  62. /// <inheritdoc />
  63. public void SetHistoryList(IReadOnlyList<IHistoryEntry> list)
  64. {
  65. // Clear out old content
  66. m_ListNotice.AddToClassList(UiConstants.ussHidden);
  67. m_Content.Clear();
  68. // Show paginator
  69. m_Paginator.RemoveFromClassList(UiConstants.ussHidden);
  70. // Handle empty list case
  71. if (list.Count == 0)
  72. {
  73. m_ListNotice.Text = StringAssets.noticeNoRevisionsToDisplay;
  74. m_Content.RemoveFromClassList(UiConstants.ussHidden);
  75. return;
  76. }
  77. foreach (var entry in list)
  78. {
  79. // Add entry to the list
  80. m_Content.Add(CreateHistoryEntry(entry, false));
  81. }
  82. }
  83. /// <summary>
  84. /// Event handler for receiving page change requests.
  85. /// </summary>
  86. /// <param name="pageChange">
  87. /// Delta to change the page by: Paginator.MoveForwards, Paginator.MoveBackwards. Mapped to +1, -1 respectively.
  88. /// </param>
  89. void OnClickedMovePage(int pageChange)
  90. {
  91. Assert.IsNotNull(m_Presenter, "Invalid state when requesting page change.");
  92. if (pageChange == Paginator.MoveBackwards)
  93. {
  94. m_Presenter.PrevPage();
  95. }
  96. else
  97. {
  98. m_Presenter.NextPage();
  99. }
  100. }
  101. /// <inheritdoc />
  102. public void SetPage(int page, int max)
  103. {
  104. m_Paginator.SetPage(page, max);
  105. }
  106. /// <inheritdoc />
  107. public void SetSelection(IHistoryEntry entry)
  108. {
  109. // Hide paginator
  110. m_Paginator.AddToClassList(UiConstants.ussHidden);
  111. // Clear out old content
  112. m_ListNotice.AddToClassList(UiConstants.ussHidden);
  113. m_Content.Clear();
  114. // Add new content
  115. m_Content.Add(CreateHistoryEntry(entry, true));
  116. }
  117. /// <summary>
  118. /// Takes a IHistoryEntry and binds it to a created HistoryEntryComponent to be used in the history list.
  119. /// </summary>
  120. /// <param name="entry">History entry to bind</param>
  121. /// <param name="expanded">Whether or not to show its list of changed entries.</param>
  122. /// <returns>Inflated and bound component.</returns>
  123. HistoryEntryComponent CreateHistoryEntry([NotNull] IHistoryEntry entry, bool expanded)
  124. {
  125. Assert.IsNotNull(m_Presenter, "Invalid state when creating history entry");
  126. var comp = new HistoryEntryComponent();
  127. // Handle expanded vs compact layout
  128. if (expanded)
  129. {
  130. // Hide fields used for compact view
  131. comp.showFilesButton.AddToClassList(UiConstants.ussHidden);
  132. comp.cloudStatusText.AddToClassList(UiConstants.ussHidden);
  133. comp.changedFilesCount.text = $"Changes ( {entry.Changes.Count} )";
  134. var listAdapter = new HistoryEntryChangeListAdapter(m_Presenter, entry.RevisionId, entry.Changes.ToList());
  135. comp.changedFiles.SetAdapter(listAdapter);
  136. listAdapter.NotifyDataSetChanged();
  137. // Configure button
  138. comp.gotoButton.text = entry.GetGotoText();
  139. comp.gotoButton.clickable.clicked += () => m_Presenter.RequestGoto(entry.RevisionId, entry.Status);
  140. }
  141. else
  142. {
  143. // Hide fields used for expanded view
  144. comp.changedFilesCount.AddToClassList(UiConstants.ussHidden);
  145. comp.changedFiles.AddToClassList(UiConstants.ussHidden);
  146. comp.gotoButton.text = string.Empty;
  147. comp.gotoButton.AddToClassList(UiConstants.ussHidden);
  148. // Setup show button
  149. comp.showFilesButton.text = entry.Changes.Count == 1
  150. ? StringAssets.showChange
  151. : string.Format(StringAssets.showChanges, entry.Changes.Count);
  152. comp.showFilesButton.clickable.clicked += () => m_Presenter.SelectedRevisionId = entry.RevisionId;
  153. // TODO: cloud status text
  154. }
  155. // Trim whitespace on either side and grab initial for profile circle
  156. var trimmedAuthorName = entry.AuthorName.Trim();
  157. comp.profileInitial.text = trimmedAuthorName.Substring(0, 1).ToUpper();
  158. comp.authorName.text = trimmedAuthorName;
  159. // Display relative or absolute timestamp. If relative, show absolute as a tooltip.
  160. comp.timestamp.text = TimeStamp.GetTimeStamp(entry.Time);
  161. if (TimeStamp.UseRelativeTimeStamps)
  162. {
  163. comp.timestamp.tooltip = TimeStamp.GetLocalisedTimeStamp(entry.Time);
  164. }
  165. // Display revision id and show full length id as a tooltip
  166. comp.revisionId.text = $"ID: {entry.RevisionId.Substring(0, 10)}";
  167. comp.revisionId.tooltip = entry.RevisionId;
  168. comp.commitMessage.text = entry.Message;
  169. return comp;
  170. }
  171. /// <inheritdoc />
  172. public bool DisplayDialogue(string title, string message, string affirmative)
  173. {
  174. return EditorUtility.DisplayDialog(title, message, affirmative);
  175. }
  176. /// <inheritdoc />
  177. public bool DisplayDialogue(string title, string message, string affirmative, string negative)
  178. {
  179. return EditorUtility.DisplayDialog(title, message, affirmative, negative);
  180. }
  181. /// <inheritdoc />
  182. protected override void SetActive()
  183. {
  184. Assert.IsFalse(m_Active, "The view is already active.");
  185. m_Active = true;
  186. m_Presenter?.Start();
  187. }
  188. /// <inheritdoc />
  189. protected override void SetInactive()
  190. {
  191. Assert.IsTrue(m_Active, "The view is already inactive.");
  192. m_Active = false;
  193. m_Presenter?.Stop();
  194. }
  195. }
  196. }