From de0f076ef9ff546c9a90513259ad6c42cd2224b3 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sat, 29 Sep 2018 16:51:26 +0200 Subject: added firebase api --- FireBase/Offline/OfflineCacheAdapter.cs | 165 ++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 FireBase/Offline/OfflineCacheAdapter.cs (limited to 'FireBase/Offline/OfflineCacheAdapter.cs') diff --git a/FireBase/Offline/OfflineCacheAdapter.cs b/FireBase/Offline/OfflineCacheAdapter.cs new file mode 100644 index 0000000..a3761a0 --- /dev/null +++ b/FireBase/Offline/OfflineCacheAdapter.cs @@ -0,0 +1,165 @@ +namespace Firebase.Database.Offline +{ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; + + internal class OfflineCacheAdapter : IDictionary, IDictionary + { + private readonly IDictionary database; + + public OfflineCacheAdapter(IDictionary database) + { + this.database = database; + } + + public void CopyTo(Array array, int index) + { + throw new NotImplementedException(); + } + + public int Count => this.database.Count; + + public bool IsSynchronized { get; } + + public object SyncRoot { get; } + + public bool IsReadOnly => this.database.IsReadOnly; + + object IDictionary.this[object key] + { + get + { + return this.database[key.ToString()].Deserialize(); + } + + set + { + var keyString = key.ToString(); + if (this.database.ContainsKey(keyString)) + { + this.database[keyString] = new OfflineEntry(keyString, value, this.database[keyString].Priority, this.database[keyString].SyncOptions); + } + else + { + this.database[keyString] = new OfflineEntry(keyString, value, 1, SyncOptions.None); + } + } + } + + public ICollection Keys => this.database.Keys; + + ICollection IDictionary.Values { get; } + + ICollection IDictionary.Keys { get; } + + public ICollection Values => this.database.Values.Select(o => o.Deserialize()).ToList(); + + public T this[string key] + { + get + { + return this.database[key].Deserialize(); + } + + set + { + if (this.database.ContainsKey(key)) + { + this.database[key] = new OfflineEntry(key, value, this.database[key].Priority, this.database[key].SyncOptions); + } + else + { + this.database[key] = new OfflineEntry(key, value, 1, SyncOptions.None); + } + } + } + + public bool Contains(object key) + { + return this.ContainsKey(key.ToString()); + } + + IDictionaryEnumerator IDictionary.GetEnumerator() + { + throw new NotImplementedException(); + } + + public void Remove(object key) + { + this.Remove(key.ToString()); + } + + public bool IsFixedSize => false; + + public IEnumerator> GetEnumerator() + { + return this.database.Select(d => new KeyValuePair(d.Key, d.Value.Deserialize())).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + + public void Add(KeyValuePair item) + { + this.Add(item.Key, item.Value); + } + + public void Add(object key, object value) + { + this.Add(key.ToString(), (T)value); + } + + public void Clear() + { + this.database.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return this.ContainsKey(item.Key); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(KeyValuePair item) + { + return this.database.Remove(item.Key); + } + + public void Add(string key, T value) + { + this.database.Add(key, new OfflineEntry(key, value, 1, SyncOptions.None)); + } + + public bool ContainsKey(string key) + { + return this.database.ContainsKey(key); + } + + public bool Remove(string key) + { + return this.database.Remove(key); + } + + public bool TryGetValue(string key, out T value) + { + OfflineEntry val; + + if (this.database.TryGetValue(key, out val)) + { + value = val.Deserialize(); + return true; + } + + value = default(T); + return false; + } + } +} -- cgit v1.2.3-54-g00ecf