Use delegates & events

This commit is contained in:
mashed-potatoes 2020-06-28 19:05:03 +05:00
parent ab65086cfe
commit 61bbfa98e5
4 changed files with 9 additions and 22 deletions

View file

@ -58,7 +58,7 @@ namespace PotatoNV_next.Controls
#if DEBUG #if DEBUG
Log.PrintDebug = true; Log.PrintDebug = true;
#endif #endif
Log.AttachListener(AppendLine); Log.Notify += AppendLine;
} }
} }
} }

View file

@ -28,7 +28,7 @@ namespace PotatoNV_next.Controls
InitializeComponent(); InitializeComponent();
usbController = new UsbController(); usbController = new UsbController();
usbController.AddListener(HandleDevices); usbController.Notify += HandleDevices;
usbController.StartWorker(); usbController.StartWorker();
} }

View file

@ -10,7 +10,8 @@ namespace PotatoNV_next.Utils
{ {
public static bool PrintDebug { get; set; } = false; public static bool PrintDebug { get; set; } = false;
private static List<Action<LogEventArgs>> actions = new List<Action<LogEventArgs>>(); public delegate void LogHandler(LogEventArgs logEventArgs);
public static event LogHandler Notify;
public enum Status public enum Status
{ {
@ -44,10 +45,7 @@ namespace PotatoNV_next.Utils
Message = message + Environment.NewLine Message = message + Environment.NewLine
}; };
foreach (var act in actions) Notify?.Invoke(args);
{
act?.Invoke(args);
}
} }
public static void Debug(string message) public static void Debug(string message)
@ -69,11 +67,6 @@ namespace PotatoNV_next.Utils
{ {
AppendToLog(Status.Error, message); AppendToLog(Status.Error, message);
} }
public static void AttachListener(Action<LogEventArgs> act)
{
actions.Add(act);
}
} }
public class LogEventArgs : EventArgs public class LogEventArgs : EventArgs

View file

@ -111,11 +111,13 @@ namespace PotatoNV_next.Utils
#endregion #endregion
#region Caller #region Caller
private List<Action<Device[]>> listeners = new List<Action<Device[]>>(); public delegate void DevicesHandler(Device[] devices);
public event DevicesHandler Notify;
private void UpdateList() private void UpdateList()
{ {
var list = new List<Device>(); var list = new List<Device>();
try try
{ {
list.AddRange(GetDownloadVCOMDevices()); list.AddRange(GetDownloadVCOMDevices());
@ -136,15 +138,7 @@ namespace PotatoNV_next.Utils
cachedDevices = sns; cachedDevices = sns;
foreach (var listener in listeners) Notify?.Invoke(list.ToArray());
{
listener?.Invoke(list.ToArray());
}
}
public void AddListener(Action<Device[]> action)
{
listeners.Add(action);
} }
#endregion #endregion
} }