Skip to content

Commit 8e9a88d

Browse files
committed
tweak(ui): don't leak cell objects, nicer loading animation
1 parent cb7e517 commit 8e9a88d

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

ServerBrowser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
<Compile Include="UI\Components\CreateServerExtensions.cs" />
229229
<Compile Include="UI\Components\HostedGameCellData.cs" />
230230
<Compile Include="UI\Components\HostedGameCellExtensions.cs" />
231+
<Compile Include="UI\Components\ListLoadingControl.cs" />
231232
<Compile Include="UI\PluginUi.cs" />
232233
<Compile Include="UI\FloatingNotification.cs" />
233234
<Compile Include="UI\ViewControllers\ServerBrowserViewController.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Linq;
2+
using UnityEngine;
3+
4+
namespace ServerBrowser.UI.Components
5+
{
6+
public class ListLoadingControl
7+
{
8+
private static Transform _controlTemplate;
9+
10+
private static void LoadTemplate()
11+
{
12+
if (_controlTemplate != null)
13+
return;
14+
15+
var nativeGameServerList = Resources.FindObjectsOfTypeAll<GameServersListTableView>().First();
16+
17+
if (nativeGameServerList == null)
18+
return;
19+
20+
_controlTemplate = nativeGameServerList.transform.Find("TableView/Viewport/MainLoadingControl");
21+
}
22+
23+
public static LoadingControl Create(Transform parentTransform)
24+
{
25+
LoadTemplate();
26+
27+
if (_controlTemplate == null)
28+
return null;
29+
30+
var newObject = Object.Instantiate(_controlTemplate, parentTransform, false);
31+
32+
var loadingControl = newObject.GetComponent<LoadingControl>();
33+
loadingControl.gameObject.SetActive(true);
34+
loadingControl.Hide();
35+
36+
return loadingControl;
37+
}
38+
}
39+
}

UI/FloatingNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void Awake()
129129
/// Please don't look at this code because cloning such a huge object is kinda gross
130130
///
131131
/// It was fun doing this in a really ugly way
132-
/// Now eventually I'll try to get it to work without ugliness :)
132+
/// Now eventually I'll try to get it to work without ugliness :) maybe
133133
////////////////////////////////////////////////////////////////////////////////////////
134134

135135
// Clone the "main screen" for the lobby, normally used to show currently selected song in the distance

UI/ViewControllers/ServerBrowserViewController.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using ServerBrowser.Game;
77
using ServerBrowser.UI.Components;
88
using System;
9+
using System.Linq;
910
using System.Threading;
1011
using TMPro;
1112
using UnityEngine;
@@ -21,17 +22,29 @@ public class ServerBrowserViewController : BeatSaberMarkupLanguage.ViewControlle
2122
private HostedGameFilters _filters = new HostedGameFilters();
2223
private CancellationTokenSource _imageLoadCancellation = null;
2324
private HostedGameData _selectedGame = null;
25+
private LoadingControl _loadingControl = null;
2426

2527
#region Activation / Deactivation
2628

2729
public override void __Activate(bool addedToHierarchy, bool screenSystemEnabling)
2830
{
2931
base.__Activate(addedToHierarchy, screenSystemEnabling);
3032

33+
// Attach loading control
34+
if (_loadingControl == null)
35+
{
36+
_loadingControl = ListLoadingControl.Create(GameList.gameObject.transform);
37+
if (_loadingControl != null)
38+
_loadingControl.didPressRefreshButtonEvent += RefreshButtonClick;
39+
}
40+
41+
// Begin listening for API browse responses
3142
HostedGameBrowser.OnUpdate += LobbyBrowser_OnUpdate;
3243

44+
// Reset the UI
3345
SetInitialUiState();
3446

47+
// Perform initial refresh
3548
_ = HostedGameBrowser.FullRefresh(_filters);
3649
}
3750

@@ -53,9 +66,19 @@ private void SetInitialUiState()
5366
{
5467
MpModeSelection.SetTitle("Server Browser");
5568

69+
ClearSelection();
70+
CancelImageLoading();
71+
5672
StatusText.text = "Loading...";
5773
StatusText.color = Color.gray;
5874

75+
if (_loadingControl != null)
76+
_loadingControl.ShowLoading("Loading servers...");
77+
78+
// make sure the table is fully cleared, if we don't do the cell gameobjects continue to add on every load
79+
GameList.data.Clear();
80+
GameList.tableView.DeleteCells(0, GameList.tableView.numberOfCells);
81+
5982
// sometimes the non-primary buttons become disabled if the server browser
6083
// isn't opened until after level selection, so let's ensure they're active
6184
RefreshButton.gameObject.SetActive(true);
@@ -68,9 +91,6 @@ private void SetInitialUiState()
6891

6992
PageUpButton.interactable = false;
7093
PageDownButton.interactable = false;
71-
72-
ClearSelection();
73-
CancelImageLoading();
7494
}
7595

7696
private void CancelImageLoading(bool reset = true)
@@ -132,6 +152,9 @@ private void LobbyBrowser_OnUpdate()
132152
{
133153
StatusText.text = "Sorry, no servers found";
134154
}
155+
156+
if (_loadingControl != null)
157+
_loadingControl.ShowText("No servers found", true);
135158
}
136159
else
137160
{
@@ -158,6 +181,9 @@ private void LobbyBrowser_OnUpdate()
158181
{
159182
GameList.data.Add(new HostedGameCellData(_imageLoadCancellation, CellUpdateCallback, lobby));
160183
}
184+
185+
if (_loadingControl != null)
186+
_loadingControl.Hide();
161187
}
162188

163189
if (!MpSession.GetLocalPlayerHasMultiplayerExtensions())
@@ -408,7 +434,7 @@ private void PageUpButtonClick()
408434
{
409435
if (HostedGameBrowser.PageIndex > 0)
410436
{
411-
CancelImageLoading();
437+
SetInitialUiState();
412438
_ = HostedGameBrowser.LoadPage((HostedGameBrowser.PageIndex - 1) * HostedGameBrowser.PageSize, _filters);
413439
}
414440
}
@@ -418,7 +444,7 @@ private void PageDownButtonClick()
418444
{
419445
if (HostedGameBrowser.PageIndex < HostedGameBrowser.TotalPageCount - 1)
420446
{
421-
CancelImageLoading();
447+
SetInitialUiState();
422448
_ = HostedGameBrowser.LoadPage((HostedGameBrowser.PageIndex + 1) * HostedGameBrowser.PageSize, _filters);
423449
}
424450
}

0 commit comments

Comments
 (0)