Fixed edge cases with selections
This commit is contained in:
@@ -34,7 +34,8 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool initialized;
|
private bool initialized;
|
||||||
|
|
||||||
private bool selfDisposing;
|
[ObservableProperty]
|
||||||
|
private int selectedIndex = 0;
|
||||||
|
|
||||||
public ObservableCollectionViewModel(IServiceProvider provider,
|
public ObservableCollectionViewModel(IServiceProvider provider,
|
||||||
IServiceFactory factory,
|
IServiceFactory factory,
|
||||||
@@ -181,6 +182,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
}
|
}
|
||||||
catch (InvalidCastException)
|
catch (InvalidCastException)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Add(item!);
|
Add(item!);
|
||||||
@@ -205,7 +207,6 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
}
|
}
|
||||||
|
|
||||||
ClearItems();
|
ClearItems();
|
||||||
|
|
||||||
clearing = false;
|
clearing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +230,6 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
|
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
{
|
{
|
||||||
selfDisposing = true;
|
|
||||||
|
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
Disposer.Dispose(this);
|
Disposer.Dispose(this);
|
||||||
}
|
}
|
||||||
@@ -315,15 +314,17 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
IsCompatibleObject(value) ?
|
IsCompatibleObject(value) ?
|
||||||
IndexOf((TViewModel)value!) : -1;
|
IndexOf((TViewModel)value!) : -1;
|
||||||
|
|
||||||
public async Task Initialize()
|
public Task Initialize()
|
||||||
{
|
{
|
||||||
if (Initialized)
|
if (Initialized)
|
||||||
{
|
{
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
Initialized = true;
|
Initialized = true;
|
||||||
Enumerate();
|
Enumerate();
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Insert(int index, TViewModel item) =>
|
public void Insert(int index, TViewModel item) =>
|
||||||
@@ -360,6 +361,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
|
|
||||||
public virtual Task OnDeactivating() =>
|
public virtual Task OnDeactivating() =>
|
||||||
Task.CompletedTask;
|
Task.CompletedTask;
|
||||||
|
|
||||||
public bool Remove(TViewModel item)
|
public bool Remove(TViewModel item)
|
||||||
{
|
{
|
||||||
int index = collection.IndexOf(item);
|
int index = collection.IndexOf(item);
|
||||||
@@ -409,14 +411,14 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
{
|
{
|
||||||
Disposer.Add(this, item);
|
Disposer.Add(this, item);
|
||||||
Disposer.Add(item, Disposable.Create(() =>
|
Disposer.Add(item, Disposable.Create(() =>
|
||||||
|
{
|
||||||
|
if (item is IRemovable && !clearing)
|
||||||
{
|
{
|
||||||
if (item is IList collection)
|
if (item is IList collection)
|
||||||
{
|
{
|
||||||
collection.Clear();
|
collection.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item is IRemovable && !clearing)
|
|
||||||
{
|
|
||||||
Remove(item);
|
Remove(item);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@@ -426,6 +428,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
|
|
||||||
protected virtual IEnumerate PrepareEnumeration(object? key) =>
|
protected virtual IEnumerate PrepareEnumeration(object? key) =>
|
||||||
new EnumerateEventArgs<TViewModel>() with { Key = key };
|
new EnumerateEventArgs<TViewModel>() with { Key = key };
|
||||||
|
|
||||||
protected virtual void RemoveItem(int index) =>
|
protected virtual void RemoveItem(int index) =>
|
||||||
collection.RemoveAt(index);
|
collection.RemoveAt(index);
|
||||||
|
|
||||||
@@ -437,6 +440,19 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
|
|
||||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) =>
|
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) =>
|
||||||
CollectionChanged?.Invoke(this, args);
|
CollectionChanged?.Invoke(this, args);
|
||||||
|
|
||||||
|
partial void OnSelectedIndexChanged(int oldValue, int newValue)
|
||||||
|
{
|
||||||
|
if (oldValue >= 0 && oldValue <= this.Count -1 && this[oldValue] is ISelectable removed)
|
||||||
|
{
|
||||||
|
removed.Selected = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newValue >= 0 && newValue <= this.Count - 1 && this[newValue] is ISelectable added)
|
||||||
|
{
|
||||||
|
added.Selected = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class ObservableCollectionViewModel<TValue, TViewModel>(IServiceProvider provider,
|
public partial class ObservableCollectionViewModel<TValue, TViewModel>(IServiceProvider provider,
|
||||||
|
|||||||
Reference in New Issue
Block a user