using System.Collections.Generic;
namespace PKHeX.Core
{
///
/// Pushes slot update notifications out to all subscribers.
///
public sealed class SlotPublisher
{
///
/// All instances that provide a view on individual content.
///
public List> Subscribers { get; } = new List>();
public ISlotInfo? Previous { get; private set; }
public SlotTouchType PreviousType { get; private set; } = SlotTouchType.None;
public PKM? PreviousPKM { get; private set; }
///
/// Notifies all with the latest slot change details.
///
/// Last interacted slot
/// Last interacted slot interaction type
/// Last interacted slot interaction data
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pkm)
{
foreach (var sub in Subscribers)
ResetView(sub, slot, type, pkm);
Previous = slot;
PreviousType = type;
PreviousPKM = pkm;
}
private void ResetView(ISlotViewer sub, ISlotInfo slot, SlotTouchType type, PKM pkm)
{
if (Previous != null)
sub.NotifySlotOld(Previous);
if (!(slot is SlotInfoBox b) || sub.ViewIndex == b.Box)
sub.NotifySlotChanged(slot, type, pkm);
}
public void ResetView(ISlotViewer sub)
{
if (Previous == null || PreviousPKM == null)
return;
ResetView(sub, Previous, PreviousType, PreviousPKM);
}
}
}