using System.Collections.Generic; using System.Linq; using Windows.Foundation.Collections; namespace ModernKeePass.Common { /// /// Implementation of IObservableMap that supports reentrancy for use as a default view /// model. /// public class ObservableDictionary : IObservableMap { private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs { public ObservableDictionaryChangedEventArgs(CollectionChange change, string key) { CollectionChange = change; Key = key; } public CollectionChange CollectionChange { get; private set; } public string Key { get; private set; } } private readonly Dictionary _dictionary = new Dictionary(); public event MapChangedEventHandler MapChanged; private void InvokeMapChanged(CollectionChange change, string key) { var eventHandler = MapChanged; if (eventHandler != null) { eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key)); } } public void Add(string key, object value) { _dictionary.Add(key, value); InvokeMapChanged(CollectionChange.ItemInserted, key); } public void Add(KeyValuePair item) { Add(item.Key, item.Value); } public void AddRange(IEnumerable> values) { foreach (var value in values) { Add(value); } } public bool Remove(string key) { if (_dictionary.Remove(key)) { InvokeMapChanged(CollectionChange.ItemRemoved, key); return true; } return false; } public bool Remove(KeyValuePair item) { object currentValue; if (_dictionary.TryGetValue(item.Key, out currentValue) && Equals(item.Value, currentValue) && _dictionary.Remove(item.Key)) { InvokeMapChanged(CollectionChange.ItemRemoved, item.Key); return true; } return false; } public object this[string key] { get { return _dictionary[key]; } set { _dictionary[key] = value; InvokeMapChanged(CollectionChange.ItemChanged, key); } } public void Clear() { var priorKeys = _dictionary.Keys.ToArray(); _dictionary.Clear(); foreach (var key in priorKeys) { InvokeMapChanged(CollectionChange.ItemRemoved, key); } } public ICollection Keys { get { return _dictionary.Keys; } } public bool ContainsKey(string key) { return _dictionary.ContainsKey(key); } public bool TryGetValue(string key, out object value) { return _dictionary.TryGetValue(key, out value); } public ICollection Values { get { return _dictionary.Values; } } public bool Contains(KeyValuePair item) { return _dictionary.Contains(item); } public int Count { get { return _dictionary.Count; } } public bool IsReadOnly { get { return false; } } public IEnumerator> GetEnumerator() { return _dictionary.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _dictionary.GetEnumerator(); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { int arraySize = array.Length; foreach (var pair in _dictionary) { if (arrayIndex >= arraySize) break; array[arrayIndex++] = pair; } } } }