Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports #987

Merged
merged 18 commits into from
Feb 9, 2025
Merged

Ports #987

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Content.Client.UserInterface.Controls;
using Robust.Client.UserInterface;
using Robust.Shared.Input;
using System.Linq;
using Content.Shared.Backmen.Medical.SmartFridge;
using SmartFridgeMenu = Content.Client.Backmen.Medical.SmartFridge.UI.SmartFridgeMenu;

namespace Content.Client.Backmen.Medical.SmartFridge;

public sealed class SmartFridgeBoundUserInterface(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey)
{
[ViewVariables]
private SmartFridgeMenu? _menu;

[ViewVariables]
private List<SmartFridgeInventoryItem> _cachedInventory = [];
Comment on lines +15 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Необходимо очистить кэш при закрытии интерфейса

Кэшированный инвентарь может привести к утечке памяти, так как список никогда не очищается.

+ protected override void Dispose(bool disposing) 
+ {
+     base.Dispose(disposing);
+     if (disposing)
+     {
+         _cachedInventory.Clear();
+         _menu?.Dispose();
+         _menu = null;
+     }
+ }

Committable suggestion skipped: line range outside the PR's diff.


protected override void Open()
{
base.Open();

_menu = this.CreateWindow<SmartFridgeMenu>();
_menu.OpenCenteredLeft();
_menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
_menu.OnItemSelected += OnItemSelected;
Refresh();
Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Отписка от события OnItemSelected

Необходимо отписаться от события при закрытии интерфейса во избежание утечек памяти.

 protected override void Open()
 {
     base.Open();
     _menu = this.CreateWindow<SmartFridgeMenu>();
     _menu.OpenCenteredLeft();
     _menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
     _menu.OnItemSelected += OnItemSelected;
     Refresh();
 }

+ protected override void Dispose(bool disposing)
+ {
+     base.Dispose(disposing);
+     if (_menu != null)
+     {
+         _menu.OnItemSelected -= OnItemSelected;
+     }
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_menu = this.CreateWindow<SmartFridgeMenu>();
_menu.OpenCenteredLeft();
_menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
_menu.OnItemSelected += OnItemSelected;
Refresh();
_menu = this.CreateWindow<SmartFridgeMenu>();
_menu.OpenCenteredLeft();
_menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
_menu.OnItemSelected += OnItemSelected;
Refresh();
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_menu != null)
{
_menu.OnItemSelected -= OnItemSelected;
}
}

}

public void Refresh()
{
var system = EntMan.System<SmartFridgeSystem>();
_cachedInventory = system.GetInventoryClient(Owner);

_menu?.Populate(_cachedInventory);
}

private void OnItemSelected(GUIBoundKeyEventArgs args, ListData data)
{
if (args.Function != EngineKeyFunctions.UIClick)
return;

if (data is not VendorItemsListData { ItemIndex: var itemIndex })
return;

if (_cachedInventory.Count == 0)
return;

var selectedItem = _cachedInventory.ElementAtOrDefault(itemIndex);

if (selectedItem == null)
return;

SendMessage(new SmartFridgeEjectMessage(selectedItem.StorageSlotId));
}
}
24 changes: 24 additions & 0 deletions Content.Client/Backmen/Medical/SmartFridge/SmartFridgeSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Content.Shared.Backmen.Medical.SmartFridge;
using SmartFridgeComponent = Content.Shared.Backmen.Medical.SmartFridge.SmartFridgeComponent;

namespace Content.Client.Backmen.Medical.SmartFridge;

public sealed class SmartFridgeSystem : SharedSmartFridgeSystem
{
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SmartFridgeComponent, AfterAutoHandleStateEvent>(OnVendingAfterState);
}

private void OnVendingAfterState(EntityUid uid, SmartFridgeComponent component, ref AfterAutoHandleStateEvent args)
{
if (_uiSystem.TryGetOpenUi<SmartFridgeBoundUserInterface>(uid, SmartFridgeUiKey.Key, out var bui))
{
bui.Refresh();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<controls:FancyWindow
xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:co="clr-namespace:Content.Client.UserInterface.Controls"
MinSize="350,300"
SetSize="400,400"
Title="{Loc 'smart-fridge-title'}">

<PanelContainer StyleClasses="WindowPanel">
<BoxContainer Name="MainContainer" Orientation="Vertical" Margin="10">
<BoxContainer Orientation="Vertical" Margin="0 0 0 8">
<Label Text="{Loc 'smart-fridge-search'}:" Margin="0 0 0 5" HorizontalAlignment="Center"/>
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True"/>
</BoxContainer>

<PanelContainer StyleClasses="WindowPanelInner" Margin="0 0 0 8" VerticalExpand="True">
<BoxContainer Orientation="Vertical" Name="ContentPanel" Margin="10" VerticalExpand="True">
<co:SearchListContainer Name="VendingContents" VerticalExpand="True"/>
<Label Name="OutOfStockLabel"
Text="{Loc 'vending-machine-component-try-eject-out-of-stock'}"
Margin="4"
HorizontalExpand="True"
HorizontalAlignment="Center"
Visible="False"/>
</BoxContainer>
</PanelContainer>

<PanelContainer StyleClasses="LowDivider" Margin="0 8 0 0"/>
<BoxContainer Orientation="Horizontal" Margin="0 4 0 0" VerticalAlignment="Bottom">
<Label Text="{Loc 'vending-machine-flavor-left'}" StyleClasses="WindowFooterText" Margin="0 0 10 0"/>
<Label Text="{Loc 'vending-machine-flavor-right'}" StyleClasses="WindowFooterText"
HorizontalAlignment="Right" HorizontalExpand="True" Margin="0 0 5 0"/>
<TextureRect StyleClasses="NTLogoDark" Stretch="KeepAspectCentered"
VerticalAlignment="Center" HorizontalAlignment="Right" SetSize="24 24"/>
</BoxContainer>
</BoxContainer>
</PanelContainer>
</controls:FancyWindow>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Content.Client.UserInterface.Controls;
using Content.Client.VendingMachines.UI;
using Content.Shared.Backmen.Medical.SmartFridge;
using Robust.Client.UserInterface.XAML;
using Robust.Client.UserInterface;
using Robust.Shared.Prototypes;
using Robust.Client.AutoGenerated;

namespace Content.Client.Backmen.Medical.SmartFridge.UI;

[GenerateTypedNameReferences]
public sealed partial class SmartFridgeMenu : FancyWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;

private readonly Dictionary<EntProtoId, EntityUid> _dummies = [];

Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Очистка dummy-сущностей

Необходимо реализовать очистку dummy-сущностей при уничтожении окна.

 private readonly Dictionary<EntProtoId, EntityUid> _dummies = [];

+ protected override void Dispose(bool disposing)
+ {
+     base.Dispose(disposing);
+     if (disposing)
+     {
+         foreach (var dummy in _dummies.Values)
+         {
+             _entityManager.DeleteEntity(dummy);
+         }
+         _dummies.Clear();
+     }
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private readonly Dictionary<EntProtoId, EntityUid> _dummies = [];
private readonly Dictionary<EntProtoId, EntityUid> _dummies = [];
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
foreach (var dummy in _dummies.Values)
{
_entityManager.DeleteEntity(dummy);
}
_dummies.Clear();
}
}

public event Action<GUIBoundKeyEventArgs, ListData>? OnItemSelected;

public SmartFridgeMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

VendingContents.SearchBar = SearchBar;
VendingContents.DataFilterCondition += DataFilterCondition;
VendingContents.GenerateItem += GenerateButton;
VendingContents.ItemKeyBindDown += (args, data) => OnItemSelected?.Invoke(args, data);
}

private static bool DataFilterCondition(string filter, ListData data)
{
if (data is not VendorItemsListData { ItemText: var text })
return false;

return string.IsNullOrEmpty(filter) || text.Contains(filter, StringComparison.CurrentCultureIgnoreCase);
}

private void GenerateButton(ListData data, ListContainerButton button)
{
if (data is not VendorItemsListData { ItemProtoID: var protoId, ItemText: var text })
return;

button.AddChild(new VendingMachineItem(protoId, text));
button.ToolTip = text;
}

public void Populate(List<SmartFridgeInventoryItem> inventory)
{
if (inventory.Count == 0)
{
SearchBar.Visible = false;
VendingContents.Visible = false;
OutOfStockLabel.Visible = true;
return;
}

SearchBar.Visible = true;
VendingContents.Visible = true;
OutOfStockLabel.Visible = false;

var listData = new List<VendorItemsListData>();

for (var i = 0; i < inventory.Count; i++)
{
var entry = inventory[i];

if (!_prototypeManager.TryIndex(entry.Id, out var prototype))
continue;

if (!_dummies.TryGetValue(entry.Id, out var dummy))
{
dummy = _entityManager.Spawn(entry.Id);
_dummies.Add(entry.Id, dummy);
}
Comment on lines +74 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Добавить обработку ошибок при создании сущностей

Необходимо добавить обработку ошибок при создании dummy-сущностей.

-dummy = _entityManager.Spawn(entry.Id);
-_dummies.Add(entry.Id, dummy);
+try
+{
+    dummy = _entityManager.Spawn(entry.Id);
+    _dummies.Add(entry.Id, dummy);
+}
+catch (Exception ex)
+{
+    Logger.ErrorS("smartfridge", ex, $"Failed to spawn dummy entity for {entry.Id}");
+    continue;
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dummy = _entityManager.Spawn(entry.Id);
_dummies.Add(entry.Id, dummy);
}
try
{
dummy = _entityManager.Spawn(entry.Id);
_dummies.Add(entry.Id, dummy);
}
catch (Exception ex)
{
Logger.ErrorS("smartfridge", ex, $"Failed to spawn dummy entity for {entry.Id}");
continue;
}


var itemText = $"{entry.ItemName} [{entry.Quantity}]";
listData.Add(new VendorItemsListData(prototype.ID, itemText, i));
}

VendingContents.PopulateList(listData);
}
}
Loading
Loading