using Framework; using System.Collections; using System.Collections.Generic; using System.Linq; using Framework.Condition; using static Framework.EventManager; public abstract class RedPointNode : IEnumerable { private bool _isDisposed; private EventName[] _eventsToRegister; private int _reactionID; protected List _children = new(); //该红点节点需要注册的事件,当事件发生时刷新数量 protected abstract EventName[] eventsToRegister { get; } //数目要override,去算各自系统对应的消息数 protected abstract int GetCount(); public int MessageCount { get; private set; } public int ID { get; private set; } public bool IsDynamic => ID > RedPointManager.RED_POINT_NODE_DYNAMIC_ID_START; public RedPointNode parent { get; set; } public bool Valid => !_isDisposed; public object param { get; set; } protected RedPointNode(int id) { ID = id; } public void AddChild(RedPointNode child) { _children.Add(child); } public void RemoveChild(RedPointNode child) { _children.Remove(child); } public void Init() { _eventsToRegister = eventsToRegister; RegisterSignal(); RefreshCount(); } public void Dispose() { if (!_isDisposed) { UnRegisterSignal(); _children = null; parent = null; _isDisposed = true; SendChange(); } } private void RegisterSignal() { if (_eventsToRegister != null) _reactionID = ConditionReactor.Instance.AddReaction(Refresh, _eventsToRegister.ToList(), OnRefreshFinish); } private void UnRegisterSignal() { if (_eventsToRegister != null) ConditionReactor.Instance.RemoveReaction(_reactionID); } private bool Refresh(object arg = null) { var oldCount = MessageCount; MessageCount = GetCount(); return MessageCount != oldCount; } private void OnRefreshFinish(bool result, object callBackParam, int id) { if (result) { SendChange(); } } public void RefreshCount() { if (Refresh()) { SendChange(); } } private void SendChange() { EventManager.Instance.Send(EventName.RedPointDataChange, this); if (parent != null) { parent.RefreshCount(); } } public IEnumerator GetEnumerator() { for (int i = _children.Count - 1; i >= 0; i--) { yield return _children[i]; } } }