diff --git a/Content.Server/_Cats/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs b/Content.Server/_Cats/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs index 986a0c0f2f..ff1bb32e19 100644 --- a/Content.Server/_Cats/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs +++ b/Content.Server/_Cats/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs @@ -24,6 +24,7 @@ public sealed class NanoChatCartridgeSystem : EntitySystem [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IPrototypeManager _prototype = default!; [Dependency] private readonly SharedNanoChatSystem _nanoChat = default!; + [Dependency] private readonly SharedUserInterfaceSystem _ui = default!; [Dependency] private readonly StationSystem _station = default!; // Messages in notifications get cut off after this point @@ -38,6 +39,20 @@ public override void Initialize() SubscribeLocalEvent(OnMessage); } + private void UpdateClosed(Entity ent) + { + if (!TryComp(ent, out var cartridge) || + cartridge.LoaderUid is not {} pda || + !TryComp(pda, out var loader) || + !GetCardEntity(pda, out var card)) + { + return; + } + + // if you switch to another program or close the pda UI, allow notifications for the selected chat + _nanoChat.SetClosed((card, card.Comp), loader.ActiveProgram != ent.Owner || !_ui.IsUiOpen(pda, PdaUiKey.Key)); + } + public override void Update(float frameTime) { base.Update(frameTime); @@ -49,6 +64,9 @@ public override void Update(float frameTime) if (cartridge.LoaderUid == null) continue; + // keep it up to date without handling ui open/close events on the pda or adding code when changing active program + UpdateClosed((uid, nanoChat)); + // Check if we need to update our card reference if (!TryComp(cartridge.LoaderUid, out var pda)) continue; @@ -381,8 +399,7 @@ private void DeliverMessageToRecipient(Entity sender, _nanoChat.AddMessage((recipient, recipient.Comp), senderNumber.Value, message with { DeliveryFailed = false }); - - if (_nanoChat.GetCurrentChat((recipient, recipient.Comp)) != senderNumber) + if (recipient.Comp.IsClosed || _nanoChat.GetCurrentChat((recipient, recipient.Comp)) != senderNumber) HandleUnreadNotification(recipient, message); var msgEv = new NanoChatMessageReceivedEvent(recipient); diff --git a/Content.Shared/_Cats/NanoChat/NanoChatCardComponent.cs b/Content.Shared/_Cats/NanoChat/NanoChatCardComponent.cs index 4e5059fa72..b00b7ff2b8 100644 --- a/Content.Shared/_Cats/NanoChat/NanoChatCardComponent.cs +++ b/Content.Shared/_Cats/NanoChat/NanoChatCardComponent.cs @@ -14,6 +14,13 @@ public sealed partial class NanoChatCardComponent : Component [DataField, AutoNetworkedField] public uint? Number; + /// + /// Whether a PDA has this card's UI closed. + /// Used for notifications. + /// + [DataField] + public bool IsClosed; + /// /// All chat recipients stored on this card. /// diff --git a/Content.Shared/_Cats/NanoChat/SharedNanoChatSystem.cs b/Content.Shared/_Cats/NanoChat/SharedNanoChatSystem.cs index c739b85aca..e237431199 100644 --- a/Content.Shared/_Cats/NanoChat/SharedNanoChatSystem.cs +++ b/Content.Shared/_Cats/NanoChat/SharedNanoChatSystem.cs @@ -49,13 +49,24 @@ private void OnExamined(Entity ent, ref ExaminedEvent arg /// public void SetNumber(Entity card, uint number) { - if (!Resolve(card, ref card.Comp)) + if (!Resolve(card, ref card.Comp) || card.Comp.Number == number) return; card.Comp.Number = number; Dirty(card); } + /// + /// Sets IsClosed for a card. + /// + public void SetClosed(Entity card, bool closed) + { + if (!Resolve(card, ref card.Comp)) + return; + + card.Comp.IsClosed = closed; + } + /// /// Gets the recipients dictionary from a card. /// @@ -170,7 +181,7 @@ public bool GetNotificationsMuted(Entity card) /// public void SetNotificationsMuted(Entity card, bool muted) { - if (!Resolve(card, ref card.Comp)) + if (!Resolve(card, ref card.Comp) || card.Comp.NotificationsMuted == muted) return; card.Comp.NotificationsMuted = muted;