Uplink store interface searchable with a searchbar. (#24287)

* Can now search the uplink store interface with a searchbar.

* Search text updates no longer send server messages. Persists listings locally.

* Formatting fixes and tidying.

* Added helper method to get localised name and description (or otherwise, entity name and description) of store listing items.

* Update Content.Client/Store/Ui/StoreMenu.xaml

* Review change; moved localisation helper functions to their own class.

* Prevent thread-unsafe behaviour as-per review.

* Remove dummy boxcontainer

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
J. Brown
2024-03-31 05:09:15 +01:00
committed by GitHub
parent ce71cde429
commit de62ec204b
5 changed files with 91 additions and 23 deletions

View File

@@ -1,22 +1,27 @@
using Content.Shared.Store;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using System.Linq;
using System.Threading;
using Serilog;
using Timer = Robust.Shared.Timing.Timer;
using Robust.Shared.Prototypes;
namespace Content.Client.Store.Ui;
[UsedImplicitly]
public sealed class StoreBoundUserInterface : BoundUserInterface
{
private IPrototypeManager _prototypeManager = default!;
[ViewVariables]
private StoreMenu? _menu;
[ViewVariables]
private string _windowName = Loc.GetString("store-ui-default-title");
[ViewVariables]
private string _search = "";
[ViewVariables]
private HashSet<ListingData> _listings = new();
public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
@@ -49,6 +54,12 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
SendMessage(new StoreRequestUpdateInterfaceMessage());
};
_menu.SearchTextUpdated += (_, search) =>
{
_search = search.Trim().ToLowerInvariant();
UpdateListingsWithSearchFilter();
};
_menu.OnRefundAttempt += (_) =>
{
SendMessage(new StoreRequestRefundMessage());
@@ -64,10 +75,10 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
switch (state)
{
case StoreUpdateState msg:
_menu.UpdateBalance(msg.Balance);
_menu.PopulateStoreCategoryButtons(msg.Listings);
_listings = msg.Listings;
_menu.UpdateListing(msg.Listings.ToList());
_menu.UpdateBalance(msg.Balance);
UpdateListingsWithSearchFilter();
_menu.SetFooterVisibility(msg.ShowFooter);
_menu.UpdateRefund(msg.AllowRefund);
break;
@@ -89,4 +100,19 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
_menu?.Close();
_menu?.Dispose();
}
private void UpdateListingsWithSearchFilter()
{
if (_menu == null)
return;
var filteredListings = new HashSet<ListingData>(_listings);
if (!string.IsNullOrEmpty(_search))
{
filteredListings.RemoveWhere(listingData => !ListingLocalisationHelpers.GetLocalisedNameOrEntityName(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search) &&
!ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search));
}
_menu.PopulateStoreCategoryButtons(filteredListings);
_menu.UpdateListing(filteredListings.ToList());
}
}