Files
modernkeepass/WinAppCommon/Common/ObservableDictionary.cs

157 lines
4.3 KiB
C#
Raw Normal View History

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