Something went wrong. Try again.
Modular scanner/radio client and multi-broadcast server. uniscan.dexrn.me
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136using System;using System.Collections.Specialized;using ObservableCollections;
namespace UniScan.Client.App.Core.Helpers;
//literally a hack to create a new observable list from a view so that I can derive more viewspublic class SynchronizedViewMirror<T, TNew> : IDisposable{ private readonly ObservableList<TNew> _output = []; public IReadOnlyObservableList<TNew> Output => _output; public INotifyCollectionChangedSynchronizedViewList<TNew> OutputView { get; }
private readonly ISynchronizedView<T, TNew> _view; public SynchronizedViewMirror(ISynchronizedView<T, TNew> view) { _view = view;
OutputView = Output.CreateView(x => x) .ToNotifyCollectionChanged(SynchronizationContextCollectionEventDispatcher.Current); Reset(); Attach(); } private void Attach() { _view.ViewChanged += OnViewChanged; }
private void Detach() { _view.ViewChanged -= OnViewChanged; }
private void Reset() { _output.Clear(); _output.AddRange(_view); }
private void OnViewChanged(in SynchronizedViewChangedEventArgs<T, TNew> args) { switch (args.Action) { case NotifyCollectionChangedAction.Add: if (args.IsSingleItem) { //we lost the plot man if (args.NewStartingIndex != -1) { _output.Insert(args.NewStartingIndex, args.NewItem.View); } else { _output.Add(args.NewItem.View); }
break; }
if (args.NewStartingIndex != -1) { _output.InsertRange(args.NewStartingIndex, args.NewViews); } else { _output.AddRange(args.NewViews); }
break; case NotifyCollectionChangedAction.Remove: if (args.IsSingleItem) { if (args.OldStartingIndex != -1) { _output.RemoveAt(args.OldStartingIndex); } else { _output.Remove(args.OldItem.View); }
break; }
if (args.OldStartingIndex != -1) { _output.RemoveRange(args.OldStartingIndex, args.OldViews.Length); } else { foreach (TNew v in args.OldViews) { _output.Remove(v); } }
break; case NotifyCollectionChangedAction.Replace: if (args.IsSingleItem) { _output[args.NewStartingIndex] = args.NewItem.View; break; } //TODO
break; case NotifyCollectionChangedAction.Move: if (args.NewStartingIndex != -1 || args.OldStartingIndex != -1) { Reset();//just rebuild the whole thing because you're fucked if this happens somehow return; } _output.Move(args.OldStartingIndex, args.NewStartingIndex); break; case NotifyCollectionChangedAction.Reset: Reset(); break; default: break; } }
public void Dispose() { Detach(); _output.Clear(); OutputView.Dispose(); }}