66using ServerBrowser . Game ;
77using ServerBrowser . UI . Components ;
88using System ;
9+ using System . Linq ;
910using System . Threading ;
1011using TMPro ;
1112using 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