Files
crystall-punk-14/Content.Client/Store/Ui/StoreBoundUserInterface.cs

114 lines
3.2 KiB
C#
Raw Normal View History

2022-08-17 00:34:25 -04:00
using Content.Shared.Store;
using JetBrains.Annotations;
using System.Linq;
using Robust.Shared.Prototypes;
2022-08-17 00:34:25 -04:00
namespace Content.Client.Store.Ui;
[UsedImplicitly]
public sealed class StoreBoundUserInterface : BoundUserInterface
{
private IPrototypeManager _prototypeManager = default!;
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-08-17 00:34:25 -04:00
private StoreMenu? _menu;
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-08-17 00:34:25 -04:00
private string _windowName = Loc.GetString("store-ui-default-title");
[ViewVariables]
private string _search = string.Empty;
[ViewVariables]
private HashSet<ListingData> _listings = new();
2023-07-08 09:02:17 -07:00
public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
2022-08-17 00:34:25 -04:00
{
}
protected override void Open()
{
_menu = new StoreMenu(_windowName);
_menu.OpenCentered();
_menu.OnClose += Close;
_menu.OnListingButtonPressed += (_, listing) =>
{
SendMessage(new StoreBuyListingMessage(listing));
2022-08-17 00:34:25 -04:00
};
_menu.OnCategoryButtonPressed += (_, category) =>
{
_menu.CurrentCategory = category;
_menu?.UpdateListing();
2022-08-17 00:34:25 -04:00
};
_menu.OnWithdrawAttempt += (_, type, amount) =>
{
SendMessage(new StoreRequestWithdrawMessage(type, amount));
2022-08-17 00:34:25 -04:00
};
_menu.SearchTextUpdated += (_, search) =>
{
_search = search.Trim().ToLowerInvariant();
UpdateListingsWithSearchFilter();
};
_menu.OnRefundAttempt += (_) =>
{
SendMessage(new StoreRequestRefundMessage());
};
2022-08-17 00:34:25 -04:00
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (_menu == null)
return;
switch (state)
{
case StoreUpdateState msg:
_listings = msg.Listings;
_menu.UpdateBalance(msg.Balance);
UpdateListingsWithSearchFilter();
_menu.SetFooterVisibility(msg.ShowFooter);
_menu.UpdateRefund(msg.AllowRefund);
2022-08-17 00:34:25 -04:00
break;
case StoreInitializeState msg:
_windowName = msg.Name;
if (_menu != null && _menu.Window != null)
{
2022-08-17 00:34:25 -04:00
_menu.Window.Title = msg.Name;
}
2022-08-17 00:34:25 -04:00
break;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_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());
}
2022-08-17 00:34:25 -04:00
}