BililiveRecorder/BililiveRecorder.WPF/Models/LogModel.cs

68 lines
2.0 KiB
C#
Raw Normal View History

2020-11-27 18:51:02 +08:00
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
namespace BililiveRecorder.WPF.Models
{
internal class LogModel : ObservableCollection<string>, IDisposable
{
private const int MAX_LINE = 50;
private bool disposedValue;
public static void AddLog(string log) => LogReceived?.Invoke(null, log);
public static event EventHandler<string> LogReceived;
public LogModel() : base(new[] { "" })
{
2021-01-01 14:46:27 +08:00
LogReceived += this.LogModel_LogReceived;
2020-11-27 18:51:02 +08:00
}
private void LogModel_LogReceived(object sender, string e)
{
2021-01-01 14:46:27 +08:00
_ = Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, (Action<string>)this.AddLogToCollection, e);
2020-11-27 18:51:02 +08:00
}
private void AddLogToCollection(string e)
{
2021-01-01 14:46:27 +08:00
this.Add(e);
while (this.Count > MAX_LINE)
2020-11-27 18:51:02 +08:00
{
2021-01-01 14:46:27 +08:00
this.RemoveItem(0);
2020-11-27 18:51:02 +08:00
}
}
protected virtual void Dispose(bool disposing)
{
2021-01-01 14:46:27 +08:00
if (!this.disposedValue)
2020-11-27 18:51:02 +08:00
{
if (disposing)
{
// dispose managed state (managed objects)
2021-01-01 14:46:27 +08:00
LogReceived -= this.LogModel_LogReceived;
this.ClearItems();
2020-11-27 18:51:02 +08:00
}
// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
2021-01-01 14:46:27 +08:00
this.disposedValue = true;
2020-11-27 18:51:02 +08:00
}
}
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~LogModel()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
2021-01-01 14:46:27 +08:00
this.Dispose(disposing: true);
2020-11-27 18:51:02 +08:00
GC.SuppressFinalize(this);
}
}
}