From c4d046858e0822b7c2c540ac2368b2c0e88e7a26 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 19 May 2019 17:00:02 +0200 Subject: general refectoring added 42 as dummy Token --- FireBase/Offline/ConcurrentOfflineDatabase.cs | 128 ++++++++++++++-------- FireBase/Offline/DatabaseExtensions.cs | 111 ++++++++++++------- FireBase/Offline/ISetHandler.cs | 8 +- FireBase/Offline/InitialPullStrategy.cs | 10 +- FireBase/Offline/Internals/MemberAccessVisitor.cs | 16 +-- FireBase/Offline/OfflineCacheAdapter.cs | 64 +++++------ FireBase/Offline/OfflineDatabase.cs | 126 +++++++++++++-------- FireBase/Offline/OfflineEntry.cs | 43 +++++--- FireBase/Offline/RealtimeDatabase.cs | 118 +++++++++++--------- FireBase/Offline/SetHandler.cs | 8 +- FireBase/Offline/StreamingOptions.cs | 10 +- FireBase/Offline/SyncOptions.cs | 10 +- 12 files changed, 389 insertions(+), 263 deletions(-) (limited to 'FireBase/Offline') diff --git a/FireBase/Offline/ConcurrentOfflineDatabase.cs b/FireBase/Offline/ConcurrentOfflineDatabase.cs index 5527168..1a9e607 100644 --- a/FireBase/Offline/ConcurrentOfflineDatabase.cs +++ b/FireBase/Offline/ConcurrentOfflineDatabase.cs @@ -1,23 +1,23 @@ -namespace Firebase.Database.Offline +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using LiteDB; + +namespace Firebase.Database.Offline { - using System; - using System.Collections; - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using LiteDB; - /// - /// The offline database. + /// The offline database. /// public class ConcurrentOfflineDatabase : IDictionary { - private readonly LiteRepository db; private readonly ConcurrentDictionary ccache; + private readonly LiteRepository db; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The item type which is used to determine the database file name. /// Custom string which will get appended to the file name. @@ -43,33 +43,41 @@ } /// - /// Gets the number of elements contained in the . + /// Gets the number of elements contained in the . /// - /// The number of elements contained in the . + /// The number of elements contained in the . public int Count => ccache.Count; /// - /// Gets a value indicating whether this is a read-only collection. + /// Gets a value indicating whether this is a read-only collection. /// public bool IsReadOnly => false; /// - /// Gets an containing the keys of the . + /// Gets an containing the keys of the + /// . /// - /// An containing the keys of the object that implements . + /// + /// An containing the keys of the object that + /// implements . + /// public ICollection Keys => ccache.Keys; /// - /// Gets an containing the values in the . + /// Gets an containing the values in the + /// . /// - /// An containing the values in the object that implements . + /// + /// An containing the values in the object that + /// implements . + /// public ICollection Values => ccache.Values; /// - /// Gets or sets the element with the specified key. + /// Gets or sets the element with the specified key. /// /// The key of the element to get or set. - /// The element with the specified key. + /// The element with the specified key. public OfflineEntry this[string key] { get => ccache[key]; @@ -82,7 +90,7 @@ } /// - /// Returns an enumerator that iterates through the collection. + /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() @@ -96,65 +104,82 @@ } /// - /// Adds an item to the . + /// Adds an item to the . /// - /// The object to add to the . + /// The object to add to the . public void Add(KeyValuePair item) { Add(item.Key, item.Value); } /// - /// Removes all items from the . - /// + /// Removes all items from the . + /// public void Clear() { ccache.Clear(); - db.Delete(Query.All()); + db.Delete(LiteDB.Query.All()); } /// - /// Determines whether the contains a specific value. + /// Determines whether the contains a specific value. /// - /// The object to locate in the . - /// True if is found in the ; otherwise, false. + /// The object to locate in the . + /// + /// True if is found in the ; + /// otherwise, false. + /// public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } /// - /// Copies the elements of the to an , starting at a particular index. + /// Copies the elements of the to an + /// , starting at a particular index. /// - /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. - /// The zero-based index in at which copying begins. + /// + /// The one-dimensional that is the destination of the elements copied + /// from . The must have + /// zero-based indexing. + /// + /// The zero-based index in at which copying begins. public void CopyTo(KeyValuePair[] array, int arrayIndex) { ccache.ToList().CopyTo(array, arrayIndex); } /// - /// Removes the first occurrence of a specific object from the . + /// Removes the first occurrence of a specific object from the + /// . /// - /// The object to remove from the . - /// True if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . + /// The object to remove from the . + /// + /// True if was successfully removed from the + /// ; otherwise, false. This method also returns false if + /// is not found in the original . + /// public bool Remove(KeyValuePair item) { return Remove(item.Key); } /// - /// Determines whether the contains an element with the specified key. + /// Determines whether the contains an element with the + /// specified key. /// - /// The key to locate in the . - /// True if the contains an element with the key; otherwise, false. + /// The key to locate in the . + /// + /// True if the contains an element with the key; + /// otherwise, false. + /// public bool ContainsKey(string key) { return ccache.ContainsKey(key); } /// - /// Adds an element with the provided key and value to the . + /// Adds an element with the provided key and value to the . /// /// The object to use as the key of the element to add. /// The object to use as the value of the element to add. @@ -165,10 +190,13 @@ } /// - /// Removes the element with the specified key from the . + /// Removes the element with the specified key from the . /// /// The key of the element to remove. - /// True if the element is successfully removed; otherwise, false. This method also returns false if was not found in the original . + /// + /// True if the element is successfully removed; otherwise, false. This method also returns false if + /// was not found in the original . + /// public bool Remove(string key) { ccache.TryRemove(key, out _); @@ -176,10 +204,18 @@ } /// - /// Gets the value associated with the specified key. - /// - /// The key whose value to get.When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. This parameter is passed uninitialized. - /// True if the object that implements contains an element with the specified key; otherwise, false. + /// Gets the value associated with the specified key. + /// + /// The key whose value to get. + /// + /// When this method returns, the value associated with the specified key, if the key is found; + /// otherwise, the default value for the type of the parameter. This parameter is passed + /// uninitialized. + /// + /// + /// True if the object that implements contains an + /// element with the specified key; otherwise, false. + /// public bool TryGetValue(string key, out OfflineEntry value) { return ccache.TryGetValue(key, out value); diff --git a/FireBase/Offline/DatabaseExtensions.cs b/FireBase/Offline/DatabaseExtensions.cs index 56dcf46..e7c4074 100644 --- a/FireBase/Offline/DatabaseExtensions.cs +++ b/FireBase/Offline/DatabaseExtensions.cs @@ -1,24 +1,26 @@ -namespace Firebase.Database.Offline -{ - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq.Expressions; - using System.Reflection; - using Query; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using Firebase.Database.Query; +namespace Firebase.Database.Offline +{ public static class DatabaseExtensions { /// - /// Create new instances of the . + /// Create new instances of the . /// /// Type of elements. /// Custom string which will get appended to the file name. /// Optional custom root element of received json items. - /// Realtime streaming options. + /// Realtime streaming options. /// Specifies what strategy should be used for initial pulling of server data. - /// Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. - /// The . + /// + /// Specifies whether changed items should actually be pushed to the server. It this is false, + /// then Put / Post / Delete will not affect server data. + /// + /// The . public static RealtimeDatabase AsRealtimeDatabase(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) @@ -29,16 +31,19 @@ } /// - /// Create new instances of the . + /// Create new instances of the . /// /// Type of elements. - /// Type of the custom to use. + /// Type of the custom to use. /// Custom string which will get appended to the file name. /// Optional custom root element of received json items. - /// Realtime streaming options. + /// Realtime streaming options. /// Specifies what strategy should be used for initial pulling of server data. - /// Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. - /// The . + /// + /// Specifies whether changed items should actually be pushed to the server. It this is false, + /// then Put / Post / Delete will not affect server data. + /// + /// The . public static RealtimeDatabase AsRealtimeDatabase(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, @@ -52,12 +57,15 @@ } /// - /// Overwrites existing object with given key leaving any missing properties intact in firebase. + /// Overwrites existing object with given key leaving any missing properties intact in firebase. /// /// The key. /// The object to set. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Patch(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -66,12 +74,15 @@ } /// - /// Overwrites existing object with given key. + /// Overwrites existing object with given key. /// /// The key. /// The object to set. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Put(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -80,11 +91,14 @@ } /// - /// Adds a new entity to the Database. + /// Adds a new entity to the Database. /// /// The object to add. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// /// The generated key for this object. public static string Post(this RealtimeDatabase db, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -97,11 +111,14 @@ } /// - /// Deletes the entity with the given key. + /// Deletes the entity with the given key. /// /// The key. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Delete(this RealtimeDatabase db, string key, bool syncOnline = true, int priority = 1) where T : class { @@ -109,7 +126,8 @@ } /// - /// Do a Put for a nested property specified by of an object with key . + /// Do a Put for a nested property specified by of an object with key + /// . /// /// Type of the root elements. /// Type of the property being modified @@ -118,7 +136,10 @@ /// Expression on the root element leading to target value to modify. /// Value to put. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Put(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -128,7 +149,8 @@ } /// - /// Do a Patch for a nested property specified by of an object with key . + /// Do a Patch for a nested property specified by of an object with key + /// . /// /// Type of the root elements. /// Type of the property being modified @@ -137,7 +159,10 @@ /// Expression on the root element leading to target value to modify. /// Value to patch. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Patch(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -147,7 +172,8 @@ } /// - /// Delete a nested property specified by of an object with key . This basically does a Put with null value. + /// Delete a nested property specified by of an object with key + /// . This basically does a Put with null value. /// /// Type of the root elements. /// Type of the property being modified @@ -156,7 +182,10 @@ /// Expression on the root element leading to target value to modify. /// Value to put. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Delete(this RealtimeDatabase db, string key, Expression> propertyExpression, bool syncOnline = true, int priority = 1) where T : class @@ -166,8 +195,9 @@ } /// - /// Post a new entity into the nested dictionary specified by of an object with key . - /// The key of the new entity is automatically generated. + /// Post a new entity into the nested dictionary specified by of an object with + /// key . + /// The key of the new entity is automatically generated. /// /// Type of the root elements. /// Type of the dictionary being modified @@ -177,7 +207,10 @@ /// Expression on the root element leading to target value to modify. /// Value to put. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Post(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -193,8 +226,9 @@ } /// - /// Delete an entity with key in the nested dictionary specified by of an object with key . - /// The key of the new entity is automatically generated. + /// Delete an entity with key in the nested dictionary specified by + /// of an object with key . + /// The key of the new entity is automatically generated. /// /// Type of the root elements. /// Type of the dictionary being modified @@ -204,7 +238,10 @@ /// Expression on the root element leading to target value to modify. /// Key within the nested dictionary to delete. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public static void Delete(this RealtimeDatabase db, string key, Expression>> propertyExpression, string dictionaryKey, bool syncOnline = true, int priority = 1) diff --git a/FireBase/Offline/ISetHandler.cs b/FireBase/Offline/ISetHandler.cs index e3b49b5..c04bd41 100644 --- a/FireBase/Offline/ISetHandler.cs +++ b/FireBase/Offline/ISetHandler.cs @@ -1,8 +1,8 @@ -namespace Firebase.Database.Offline -{ - using Query; - using System.Threading.Tasks; +using System.Threading.Tasks; +using Firebase.Database.Query; +namespace Firebase.Database.Offline +{ public interface ISetHandler { Task SetAsync(ChildQuery query, string key, OfflineEntry entry); diff --git a/FireBase/Offline/InitialPullStrategy.cs b/FireBase/Offline/InitialPullStrategy.cs index a1ae3f7..ca2bebf 100644 --- a/FireBase/Offline/InitialPullStrategy.cs +++ b/FireBase/Offline/InitialPullStrategy.cs @@ -1,23 +1,23 @@ namespace Firebase.Database.Offline { /// - /// Specifies the strategy for initial pull of server data. + /// Specifies the strategy for initial pull of server data. /// public enum InitialPullStrategy { /// - /// Don't pull anything. + /// Don't pull anything. /// None, /// - /// Pull only what isn't already stored offline. + /// Pull only what isn't already stored offline. /// MissingOnly, /// - /// Pull everything that exists on the server. + /// Pull everything that exists on the server. /// - Everything, + Everything } } \ No newline at end of file diff --git a/FireBase/Offline/Internals/MemberAccessVisitor.cs b/FireBase/Offline/Internals/MemberAccessVisitor.cs index 2bc0fc3..89a77da 100644 --- a/FireBase/Offline/Internals/MemberAccessVisitor.cs +++ b/FireBase/Offline/Internals/MemberAccessVisitor.cs @@ -1,10 +1,10 @@ -namespace Firebase.Database.Offline.Internals -{ - using System.Collections.Generic; - using System.Linq.Expressions; - using System.Reflection; - using Newtonsoft.Json; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using Newtonsoft.Json; +namespace Firebase.Database.Offline.Internals +{ public class MemberAccessVisitor : ExpressionVisitor { private readonly IList propertyNames = new List(); @@ -13,10 +13,6 @@ public IEnumerable PropertyNames => propertyNames; - public MemberAccessVisitor() - { - } - public override Expression Visit(Expression expr) { if (expr?.NodeType == ExpressionType.MemberAccess) diff --git a/FireBase/Offline/OfflineCacheAdapter.cs b/FireBase/Offline/OfflineCacheAdapter.cs index 0918a8c..3153d1b 100644 --- a/FireBase/Offline/OfflineCacheAdapter.cs +++ b/FireBase/Offline/OfflineCacheAdapter.cs @@ -1,10 +1,10 @@ -namespace Firebase.Database.Offline -{ - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +namespace Firebase.Database.Offline +{ internal class OfflineCacheAdapter : IDictionary, IDictionary { private readonly IDictionary database; @@ -19,14 +19,10 @@ throw new NotImplementedException(); } - public int Count => database.Count; - public bool IsSynchronized { get; } public object SyncRoot { get; } - public bool IsReadOnly => database.IsReadOnly; - object IDictionary.this[object key] { get => database[key.ToString()].Deserialize(); @@ -42,27 +38,10 @@ } } - public ICollection Keys => database.Keys; - ICollection IDictionary.Values { get; } ICollection IDictionary.Keys { get; } - public ICollection Values => database.Values.Select(o => o.Deserialize()).ToList(); - - public T this[string key] - { - get => database[key].Deserialize(); - - set - { - if (database.ContainsKey(key)) - database[key] = new OfflineEntry(key, value, database[key].Priority, database[key].SyncOptions); - else - database[key] = new OfflineEntry(key, value, 1, SyncOptions.None); - } - } - public bool Contains(object key) { return ContainsKey(key.ToString()); @@ -80,6 +59,32 @@ public bool IsFixedSize => false; + public void Add(object key, object value) + { + Add(key.ToString(), (T) value); + } + + public int Count => database.Count; + + public bool IsReadOnly => database.IsReadOnly; + + public ICollection Keys => database.Keys; + + public ICollection Values => database.Values.Select(o => o.Deserialize()).ToList(); + + public T this[string key] + { + get => database[key].Deserialize(); + + set + { + if (database.ContainsKey(key)) + database[key] = new OfflineEntry(key, value, database[key].Priority, database[key].SyncOptions); + else + database[key] = new OfflineEntry(key, value, 1, SyncOptions.None); + } + } + public IEnumerator> GetEnumerator() { return database.Select(d => new KeyValuePair(d.Key, d.Value.Deserialize())).GetEnumerator(); @@ -95,11 +100,6 @@ Add(item.Key, item.Value); } - public void Add(object key, object value) - { - Add(key.ToString(), (T) value); - } - public void Clear() { database.Clear(); diff --git a/FireBase/Offline/OfflineDatabase.cs b/FireBase/Offline/OfflineDatabase.cs index 3e6e7d8..be0380b 100644 --- a/FireBase/Offline/OfflineDatabase.cs +++ b/FireBase/Offline/OfflineDatabase.cs @@ -1,22 +1,22 @@ -namespace Firebase.Database.Offline +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using LiteDB; + +namespace Firebase.Database.Offline { - using System; - using System.Collections; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using LiteDB; - /// - /// The offline database. + /// The offline database. /// public class OfflineDatabase : IDictionary { - private readonly LiteRepository db; private readonly IDictionary cache; + private readonly LiteRepository db; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The item type which is used to determine the database file name. /// Custom string which will get appended to the file name. @@ -38,33 +38,41 @@ } /// - /// Gets the number of elements contained in the . + /// Gets the number of elements contained in the . /// - /// The number of elements contained in the . + /// The number of elements contained in the . public int Count => cache.Count; /// - /// Gets a value indicating whether this is a read-only collection. + /// Gets a value indicating whether this is a read-only collection. /// public bool IsReadOnly => cache.IsReadOnly; /// - /// Gets an containing the keys of the . + /// Gets an containing the keys of the + /// . /// - /// An containing the keys of the object that implements . + /// + /// An containing the keys of the object that + /// implements . + /// public ICollection Keys => cache.Keys; /// - /// Gets an containing the values in the . + /// Gets an containing the values in the + /// . /// - /// An containing the values in the object that implements . + /// + /// An containing the values in the object that + /// implements . + /// public ICollection Values => cache.Values; /// - /// Gets or sets the element with the specified key. + /// Gets or sets the element with the specified key. /// /// The key of the element to get or set. - /// The element with the specified key. + /// The element with the specified key. public OfflineEntry this[string key] { get => cache[key]; @@ -77,7 +85,7 @@ } /// - /// Returns an enumerator that iterates through the collection. + /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() @@ -91,65 +99,82 @@ } /// - /// Adds an item to the . + /// Adds an item to the . /// - /// The object to add to the . + /// The object to add to the . public void Add(KeyValuePair item) { Add(item.Key, item.Value); } /// - /// Removes all items from the . - /// + /// Removes all items from the . + /// public void Clear() { cache.Clear(); - db.Delete(Query.All()); + db.Delete(LiteDB.Query.All()); } /// - /// Determines whether the contains a specific value. + /// Determines whether the contains a specific value. /// - /// The object to locate in the . - /// True if is found in the ; otherwise, false. + /// The object to locate in the . + /// + /// True if is found in the ; + /// otherwise, false. + /// public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } /// - /// Copies the elements of the to an , starting at a particular index. + /// Copies the elements of the to an + /// , starting at a particular index. /// - /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. - /// The zero-based index in at which copying begins. + /// + /// The one-dimensional that is the destination of the elements copied + /// from . The must have + /// zero-based indexing. + /// + /// The zero-based index in at which copying begins. public void CopyTo(KeyValuePair[] array, int arrayIndex) { cache.CopyTo(array, arrayIndex); } /// - /// Removes the first occurrence of a specific object from the . + /// Removes the first occurrence of a specific object from the + /// . /// - /// The object to remove from the . - /// True if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . + /// The object to remove from the . + /// + /// True if was successfully removed from the + /// ; otherwise, false. This method also returns false if + /// is not found in the original . + /// public bool Remove(KeyValuePair item) { return Remove(item.Key); } /// - /// Determines whether the contains an element with the specified key. + /// Determines whether the contains an element with the + /// specified key. /// - /// The key to locate in the . - /// True if the contains an element with the key; otherwise, false. + /// The key to locate in the . + /// + /// True if the contains an element with the key; + /// otherwise, false. + /// public bool ContainsKey(string key) { return cache.ContainsKey(key); } /// - /// Adds an element with the provided key and value to the . + /// Adds an element with the provided key and value to the . /// /// The object to use as the key of the element to add. /// The object to use as the value of the element to add. @@ -160,10 +185,13 @@ } /// - /// Removes the element with the specified key from the . + /// Removes the element with the specified key from the . /// /// The key of the element to remove. - /// True if the element is successfully removed; otherwise, false. This method also returns false if was not found in the original . + /// + /// True if the element is successfully removed; otherwise, false. This method also returns false if + /// was not found in the original . + /// public bool Remove(string key) { cache.Remove(key); @@ -171,10 +199,18 @@ } /// - /// Gets the value associated with the specified key. - /// - /// The key whose value to get.When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. This parameter is passed uninitialized. - /// True if the object that implements contains an element with the specified key; otherwise, false. + /// Gets the value associated with the specified key. + /// + /// The key whose value to get. + /// + /// When this method returns, the value associated with the specified key, if the key is found; + /// otherwise, the default value for the type of the parameter. This parameter is passed + /// uninitialized. + /// + /// + /// True if the object that implements contains an + /// element with the specified key; otherwise, false. + /// public bool TryGetValue(string key, out OfflineEntry value) { return cache.TryGetValue(key, out value); diff --git a/FireBase/Offline/OfflineEntry.cs b/FireBase/Offline/OfflineEntry.cs index dfd5910..9feffa3 100644 --- a/FireBase/Offline/OfflineEntry.cs +++ b/FireBase/Offline/OfflineEntry.cs @@ -1,21 +1,24 @@ -namespace Firebase.Database.Offline -{ - using System; - using Newtonsoft.Json; +using System; +using Newtonsoft.Json; +namespace Firebase.Database.Offline +{ /// - /// Represents an object stored in offline storage. + /// Represents an object stored in offline storage. /// public class OfflineEntry { private object dataInstance; /// - /// Initializes a new instance of the class with an already serialized object. + /// Initializes a new instance of the class with an already serialized object. /// /// The key. /// The object. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// /// The sync options. public OfflineEntry(string key, object obj, string data, int priority, SyncOptions syncOptions, bool isPartial = false) @@ -31,11 +34,14 @@ } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The key. /// The object. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// /// The sync options. public OfflineEntry(string key, object obj, int priority, SyncOptions syncOptions, bool isPartial = false) : this(key, obj, JsonConvert.SerializeObject(obj), priority, syncOptions, isPartial) @@ -43,47 +49,48 @@ } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// public OfflineEntry() { } /// - /// Gets or sets the key of this entry. + /// Gets or sets the key of this entry. /// public string Key { get; set; } /// - /// Gets or sets the priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// Gets or sets the priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. /// public int Priority { get; set; } /// - /// Gets or sets the timestamp when this entry was last touched. + /// Gets or sets the timestamp when this entry was last touched. /// public DateTime Timestamp { get; set; } /// - /// Gets or sets the which define what sync state this entry is in. + /// Gets or sets the which define what sync state this entry is in. /// public SyncOptions SyncOptions { get; set; } /// - /// Gets or sets serialized JSON data. + /// Gets or sets serialized JSON data. /// public string Data { get; set; } /// - /// Specifies whether this is only a partial object. + /// Specifies whether this is only a partial object. /// public bool IsPartial { get; set; } /// - /// Deserializes into . The result is cached. + /// Deserializes into . The result is cached. /// /// Type of object to deserialize into. - /// Instance of . + /// Instance of . public T Deserialize() { return (T) (dataInstance ?? (dataInstance = JsonConvert.DeserializeObject(Data))); diff --git a/FireBase/Offline/RealtimeDatabase.cs b/FireBase/Offline/RealtimeDatabase.cs index 4d61027..973db46 100644 --- a/FireBase/Offline/RealtimeDatabase.cs +++ b/FireBase/Offline/RealtimeDatabase.cs @@ -1,50 +1,57 @@ -namespace Firebase.Database.Offline +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Net; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Reactive.Subjects; +using System.Reactive.Threading.Tasks; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Firebase.Database.Extensions; +using Firebase.Database.Offline.Internals; +using Firebase.Database.Query; +using Firebase.Database.Streaming; +using Newtonsoft.Json; + +namespace Firebase.Database.Offline { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reactive.Linq; - using System.Reactive.Subjects; - using System.Threading; - using System.Threading.Tasks; - using Extensions; - using Query; - using Streaming; - using System.Reactive.Threading.Tasks; - using System.Linq.Expressions; - using Internals; - using Newtonsoft.Json; - using System.Reflection; - using System.Reactive.Disposables; - /// - /// The real-time Database which synchronizes online and offline data. + /// The real-time Database which synchronizes online and offline data. /// /// Type of entities. - public partial class RealtimeDatabase : IDisposable where T : class + public class RealtimeDatabase : IDisposable where T : class { private readonly ChildQuery childQuery; private readonly string elementRoot; - private readonly StreamingOptions streamingOptions; - private readonly Subject> subject; + private readonly FirebaseCache firebaseCache; private readonly InitialPullStrategy initialPullStrategy; private readonly bool pushChanges; - private readonly FirebaseCache firebaseCache; + private readonly StreamingOptions streamingOptions; + private readonly Subject> subject; + private FirebaseSubscription firebaseSubscription; private bool isSyncRunning; private IObservable> observable; - private FirebaseSubscription firebaseSubscription; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The child query. /// The element Root. /// The offline database factory. /// Custom string which will get appended to the file name. /// Specifies whether changes should be streamed from the server. - /// Specifies if everything should be pull from the online storage on start. It only makes sense when is set to true. - /// Specifies whether changed items should actually be pushed to the server. If this is false, then Put / Post / Delete will not affect server data. + /// + /// Specifies if everything should be pull from the online storage on start. It only + /// makes sense when is set to true. + /// + /// + /// Specifies whether changed items should actually be pushed to the server. If this is false, + /// then Put / Post / Delete will not affect server data. + /// public RealtimeDatabase(ChildQuery childQuery, string elementRoot, Func> offlineDatabaseFactory, string filenameModifier, StreamingOptions streamingOptions, InitialPullStrategy initialPullStrategy, bool pushChanges, @@ -67,24 +74,34 @@ } /// - /// Event raised whenever an exception is thrown in the synchronization thread. Exception thrown in there are swallowed, so this event is the only way to get to them. + /// Gets the backing Database. /// - public event EventHandler SyncExceptionThrown; + public IDictionary Database { get; } + + public ISetHandler PutHandler { private get; set; } + + public void Dispose() + { + subject.OnCompleted(); + firebaseSubscription?.Dispose(); + } /// - /// Gets the backing Database. + /// Event raised whenever an exception is thrown in the synchronization thread. Exception thrown in there are + /// swallowed, so this event is the only way to get to them. /// - public IDictionary Database { get; private set; } - - public ISetHandler PutHandler { private get; set; } + public event EventHandler SyncExceptionThrown; /// - /// Overwrites existing object with given key. + /// Overwrites existing object with given key. /// /// The key. /// The object to set. /// Indicates whether the item should be synced online. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public void Set(string key, T obj, SyncOptions syncOptions, int priority = 1) { SetAndRaise(key, new OfflineEntry(key, obj, priority, syncOptions)); @@ -118,10 +135,13 @@ } /// - /// Fetches an object with the given key and adds it to the Database. + /// Fetches an object with the given key and adds it to the Database. /// /// The key. - /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// public void Pull(string key, int priority = 1) { if (!Database.ContainsKey(key)) @@ -132,7 +152,7 @@ } /// - /// Fetches everything from the remote database. + /// Fetches everything from the remote database. /// public async Task PullAsync() { @@ -142,7 +162,7 @@ .RetryAfterDelay>, FirebaseException>( childQuery.Client.Options.SyncPeriod, ex => ex.StatusCode == - System.Net.HttpStatusCode + HttpStatusCode .OK) // OK implies the request couldn't complete due to network error. .Select(e => ResetDatabaseFromInitial(e, false)) .SelectMany(e => e) @@ -164,7 +184,7 @@ } /// - /// Retrieves all offline items currently stored in local database. + /// Retrieves all offline items currently stored in local database. /// public IEnumerable> Once() { @@ -174,10 +194,10 @@ .ToList(); } - /// - /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. - /// - /// Stream of . + /// + /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. + /// + /// Stream of . public IObservable> AsObservable() { if (!isSyncRunning) @@ -212,7 +232,7 @@ .RetryAfterDelay>, FirebaseException>( childQuery.Client.Options.SyncPeriod, ex => ex.StatusCode == - System.Net.HttpStatusCode + HttpStatusCode .OK) // OK implies the request couldn't complete due to network error. .Select(e => ResetDatabaseFromInitial(e)) .SelectMany(e => e) @@ -230,12 +250,6 @@ return observable; } - public void Dispose() - { - subject.OnCompleted(); - firebaseSubscription?.Dispose(); - } - private IReadOnlyCollection> ResetDatabaseFromInitial( IReadOnlyCollection> collection, bool onlyWhenInitialEverything = true) { @@ -308,8 +322,6 @@ firebaseSubscription.ExceptionThrown += StreamingExceptionThrown; return new CompositeDisposable(firebaseSubscription.Run(), completeDisposable); - default: - break; } return completeDisposable; diff --git a/FireBase/Offline/SetHandler.cs b/FireBase/Offline/SetHandler.cs index 18a5131..6314c3c 100644 --- a/FireBase/Offline/SetHandler.cs +++ b/FireBase/Offline/SetHandler.cs @@ -1,8 +1,8 @@ -namespace Firebase.Database.Offline -{ - using Query; - using System.Threading.Tasks; +using System.Threading.Tasks; +using Firebase.Database.Query; +namespace Firebase.Database.Offline +{ public class SetHandler : ISetHandler { public virtual async Task SetAsync(ChildQuery query, string key, OfflineEntry entry) diff --git a/FireBase/Offline/StreamingOptions.cs b/FireBase/Offline/StreamingOptions.cs index 4a5f7b8..a420cbb 100644 --- a/FireBase/Offline/StreamingOptions.cs +++ b/FireBase/Offline/StreamingOptions.cs @@ -3,18 +3,20 @@ public enum StreamingOptions { /// - /// No realtime streaming. + /// No realtime streaming. /// None, /// - /// Streaming of only new items - not the existing ones. + /// Streaming of only new items - not the existing ones. /// LatestOnly, /// - /// Streaming of all items. This will also pull all existing items on start, so be mindful about the number of items in your DB. - /// When used, consider not setting the to because you would pointlessly pull everything twice. + /// Streaming of all items. This will also pull all existing items on start, so be mindful about the number of items in + /// your DB. + /// When used, consider not setting the to + /// because you would pointlessly pull everything twice. /// Everything } diff --git a/FireBase/Offline/SyncOptions.cs b/FireBase/Offline/SyncOptions.cs index aa3e21c..ca68d0a 100644 --- a/FireBase/Offline/SyncOptions.cs +++ b/FireBase/Offline/SyncOptions.cs @@ -1,27 +1,27 @@ namespace Firebase.Database.Offline { /// - /// Specifies type of sync requested for given data. + /// Specifies type of sync requested for given data. /// public enum SyncOptions { /// - /// No sync needed for given data. + /// No sync needed for given data. /// None, /// - /// Data should be pulled from firebase. + /// Data should be pulled from firebase. /// Pull, /// - /// Data should be put to firebase. + /// Data should be put to firebase. /// Put, /// - /// Data should be patched in firebase. + /// Data should be patched in firebase. /// Patch } -- cgit v1.2.3-54-g00ecf