mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-02-16 10:58:31 -05:00
Start modernization
This commit is contained in:
29
Application/Data/RealmObservableCollection.cs
Normal file
29
Application/Data/RealmObservableCollection.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Realms;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace FeedCenter.Data
|
||||
{
|
||||
public class RealmObservableCollection<T> : ObservableCollection<T> where T : IRealmObject
|
||||
{
|
||||
private readonly Realm _realm;
|
||||
|
||||
public RealmObservableCollection(Realm realm) : base(realm.All<T>())
|
||||
{
|
||||
_realm = realm;
|
||||
}
|
||||
|
||||
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
if (e.OldItems != null)
|
||||
foreach (T item in e.OldItems)
|
||||
_realm.Remove(item);
|
||||
|
||||
if (e.NewItems != null)
|
||||
foreach (T item in e.NewItems)
|
||||
_realm.Add(item);
|
||||
|
||||
base.OnCollectionChanged(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user