BililiveRecorder/BililiveRecorder.WPF/Converters/RoomListInterceptConverter.cs

59 lines
2.4 KiB
C#
Raw Normal View History

2020-11-27 18:51:02 +08:00
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows.Data;
using BililiveRecorder.Core;
namespace BililiveRecorder.WPF.Converters
{
internal class RoomListInterceptConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is RecorderWrapper ? value : value is IRecorder recorder ? new RecorderWrapper(recorder) : value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
private class RecorderWrapper : ObservableCollection<IRecordedRoom>
{
private readonly IRecorder recorder;
public RecorderWrapper(IRecorder recorder) : base(recorder)
{
this.recorder = recorder;
2021-01-01 14:46:27 +08:00
this.Add(null);
2020-11-27 18:51:02 +08:00
recorder.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (e.NewItems.Count != 1) throw new NotImplementedException("Wrapper Add Item Count != 1");
2021-01-01 14:46:27 +08:00
this.InsertItem(e.NewStartingIndex, e.NewItems[0] as IRecordedRoom);
2020-11-27 18:51:02 +08:00
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldItems.Count != 1) throw new NotImplementedException("Wrapper Remove Item Count != 1");
2021-01-01 14:46:27 +08:00
if (!this.Remove(e.OldItems[0] as IRecordedRoom)) throw new NotImplementedException("Wrapper Remove Item Sync Fail");
2020-11-27 18:51:02 +08:00
break;
case NotifyCollectionChangedAction.Replace:
throw new NotImplementedException("Wrapper Replace Item");
case NotifyCollectionChangedAction.Move:
throw new NotImplementedException("Wrapper Move Item");
case NotifyCollectionChangedAction.Reset:
2021-01-01 14:46:27 +08:00
this.ClearItems();
this.Add(null);
2020-11-27 18:51:02 +08:00
break;
default:
break;
}
};
}
}
}
}