Skip to content

Commit

Permalink
tweak(ui): don't leak cell objects, nicer loading animation
Browse files Browse the repository at this point in the history
  • Loading branch information
roydejong committed May 30, 2021
1 parent cb7e517 commit 8e9a88d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
1 change: 1 addition & 0 deletions ServerBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<Compile Include="UI\Components\CreateServerExtensions.cs" />
<Compile Include="UI\Components\HostedGameCellData.cs" />
<Compile Include="UI\Components\HostedGameCellExtensions.cs" />
<Compile Include="UI\Components\ListLoadingControl.cs" />
<Compile Include="UI\PluginUi.cs" />
<Compile Include="UI\FloatingNotification.cs" />
<Compile Include="UI\ViewControllers\ServerBrowserViewController.cs" />
Expand Down
39 changes: 39 additions & 0 deletions UI/Components/ListLoadingControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using UnityEngine;

namespace ServerBrowser.UI.Components
{
public class ListLoadingControl
{
private static Transform _controlTemplate;

private static void LoadTemplate()
{
if (_controlTemplate != null)
return;

var nativeGameServerList = Resources.FindObjectsOfTypeAll<GameServersListTableView>().First();

if (nativeGameServerList == null)
return;

_controlTemplate = nativeGameServerList.transform.Find("TableView/Viewport/MainLoadingControl");
}

public static LoadingControl Create(Transform parentTransform)
{
LoadTemplate();

if (_controlTemplate == null)
return null;

var newObject = Object.Instantiate(_controlTemplate, parentTransform, false);

var loadingControl = newObject.GetComponent<LoadingControl>();
loadingControl.gameObject.SetActive(true);
loadingControl.Hide();

return loadingControl;
}
}
}
2 changes: 1 addition & 1 deletion UI/FloatingNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void Awake()
/// Please don't look at this code because cloning such a huge object is kinda gross
///
/// It was fun doing this in a really ugly way
/// Now eventually I'll try to get it to work without ugliness :)
/// Now eventually I'll try to get it to work without ugliness :) maybe
////////////////////////////////////////////////////////////////////////////////////////

// Clone the "main screen" for the lobby, normally used to show currently selected song in the distance
Expand Down
36 changes: 31 additions & 5 deletions UI/ViewControllers/ServerBrowserViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ServerBrowser.Game;
using ServerBrowser.UI.Components;
using System;
using System.Linq;
using System.Threading;
using TMPro;
using UnityEngine;
Expand All @@ -21,17 +22,29 @@ public class ServerBrowserViewController : BeatSaberMarkupLanguage.ViewControlle
private HostedGameFilters _filters = new HostedGameFilters();
private CancellationTokenSource _imageLoadCancellation = null;
private HostedGameData _selectedGame = null;
private LoadingControl _loadingControl = null;

#region Activation / Deactivation

public override void __Activate(bool addedToHierarchy, bool screenSystemEnabling)
{
base.__Activate(addedToHierarchy, screenSystemEnabling);

// Attach loading control
if (_loadingControl == null)
{
_loadingControl = ListLoadingControl.Create(GameList.gameObject.transform);
if (_loadingControl != null)
_loadingControl.didPressRefreshButtonEvent += RefreshButtonClick;
}

// Begin listening for API browse responses
HostedGameBrowser.OnUpdate += LobbyBrowser_OnUpdate;

// Reset the UI
SetInitialUiState();

// Perform initial refresh
_ = HostedGameBrowser.FullRefresh(_filters);
}

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

ClearSelection();
CancelImageLoading();

StatusText.text = "Loading...";
StatusText.color = Color.gray;

if (_loadingControl != null)
_loadingControl.ShowLoading("Loading servers...");

// make sure the table is fully cleared, if we don't do the cell gameobjects continue to add on every load
GameList.data.Clear();
GameList.tableView.DeleteCells(0, GameList.tableView.numberOfCells);

// sometimes the non-primary buttons become disabled if the server browser
// isn't opened until after level selection, so let's ensure they're active
RefreshButton.gameObject.SetActive(true);
Expand All @@ -68,9 +91,6 @@ private void SetInitialUiState()

PageUpButton.interactable = false;
PageDownButton.interactable = false;

ClearSelection();
CancelImageLoading();
}

private void CancelImageLoading(bool reset = true)
Expand Down Expand Up @@ -132,6 +152,9 @@ private void LobbyBrowser_OnUpdate()
{
StatusText.text = "Sorry, no servers found";
}

if (_loadingControl != null)
_loadingControl.ShowText("No servers found", true);
}
else
{
Expand All @@ -158,6 +181,9 @@ private void LobbyBrowser_OnUpdate()
{
GameList.data.Add(new HostedGameCellData(_imageLoadCancellation, CellUpdateCallback, lobby));
}

if (_loadingControl != null)
_loadingControl.Hide();
}

if (!MpSession.GetLocalPlayerHasMultiplayerExtensions())
Expand Down Expand Up @@ -408,7 +434,7 @@ private void PageUpButtonClick()
{
if (HostedGameBrowser.PageIndex > 0)
{
CancelImageLoading();
SetInitialUiState();
_ = HostedGameBrowser.LoadPage((HostedGameBrowser.PageIndex - 1) * HostedGameBrowser.PageSize, _filters);
}
}
Expand All @@ -418,7 +444,7 @@ private void PageDownButtonClick()
{
if (HostedGameBrowser.PageIndex < HostedGameBrowser.TotalPageCount - 1)
{
CancelImageLoading();
SetInitialUiState();
_ = HostedGameBrowser.LoadPage((HostedGameBrowser.PageIndex + 1) * HostedGameBrowser.PageSize, _filters);
}
}
Expand Down

0 comments on commit 8e9a88d

Please sign in to comment.