diff options
Diffstat (limited to 'FireBase/Offline')
-rw-r--r-- | FireBase/Offline/ConcurrentOfflineDatabase.cs | 128 | ||||
-rw-r--r-- | FireBase/Offline/DatabaseExtensions.cs | 111 | ||||
-rw-r--r-- | FireBase/Offline/ISetHandler.cs | 8 | ||||
-rw-r--r-- | FireBase/Offline/InitialPullStrategy.cs | 10 | ||||
-rw-r--r-- | FireBase/Offline/Internals/MemberAccessVisitor.cs | 16 | ||||
-rw-r--r-- | FireBase/Offline/OfflineCacheAdapter.cs | 64 | ||||
-rw-r--r-- | FireBase/Offline/OfflineDatabase.cs | 126 | ||||
-rw-r--r-- | FireBase/Offline/OfflineEntry.cs | 43 | ||||
-rw-r--r-- | FireBase/Offline/RealtimeDatabase.cs | 118 | ||||
-rw-r--r-- | FireBase/Offline/SetHandler.cs | 8 | ||||
-rw-r--r-- | FireBase/Offline/StreamingOptions.cs | 10 | ||||
-rw-r--r-- | FireBase/Offline/SyncOptions.cs | 10 |
12 files changed, 389 insertions, 263 deletions
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; - /// <summary> - /// The offline database. + /// The offline database. /// </summary> public class ConcurrentOfflineDatabase : IDictionary<string, OfflineEntry> { - private readonly LiteRepository db; private readonly ConcurrentDictionary<string, OfflineEntry> ccache; + private readonly LiteRepository db; /// <summary> - /// Initializes a new instance of the <see cref="OfflineDatabase"/> class. + /// Initializes a new instance of the <see cref="OfflineDatabase" /> class. /// </summary> /// <param name="itemType"> The item type which is used to determine the database file name. </param> /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> @@ -43,33 +43,41 @@ } /// <summary> - /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <returns> The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> + /// <returns> The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />. </returns> public int Count => ccache.Count; /// <summary> - /// Gets a value indicating whether this is a read-only collection. + /// Gets a value indicating whether this is a read-only collection. /// </summary> public bool IsReadOnly => false; /// <summary> - /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Gets an <see cref="T:System.Collections.Generic.ICollection`1" /> containing the keys of the + /// <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> - /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// An <see cref="T:System.Collections.Generic.ICollection`1" /> containing the keys of the object that + /// implements <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public ICollection<string> Keys => ccache.Keys; /// <summary> - /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Gets an <see cref="T:System.Collections.Generic.ICollection`1" /> containing the values in the + /// <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> - /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// An <see cref="T:System.Collections.Generic.ICollection`1" /> containing the values in the object that + /// implements <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public ICollection<OfflineEntry> Values => ccache.Values; /// <summary> - /// Gets or sets the element with the specified key. + /// Gets or sets the element with the specified key. /// </summary> /// <param name="key">The key of the element to get or set.</param> - /// <returns> The element with the specified key. </returns> + /// <returns> The element with the specified key. </returns> public OfflineEntry this[string key] { get => ccache[key]; @@ -82,7 +90,7 @@ } /// <summary> - /// Returns an enumerator that iterates through the collection. + /// Returns an enumerator that iterates through the collection. /// </summary> /// <returns> An enumerator that can be used to iterate through the collection. </returns> public IEnumerator<KeyValuePair<string, OfflineEntry>> GetEnumerator() @@ -96,65 +104,82 @@ } /// <summary> - /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> + /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> public void Add(KeyValuePair<string, OfflineEntry> item) { Add(item.Key, item.Value); } /// <summary> - /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>. - /// </summary> + /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />. + /// </summary> public void Clear() { ccache.Clear(); - db.Delete<OfflineEntry>(Query.All()); + db.Delete<OfflineEntry>(LiteDB.Query.All()); } /// <summary> - /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value. + /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a specific value. /// </summary> - /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> - /// <returns> True if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. </returns> + /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> + /// <returns> + /// True if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.ICollection`1" />; + /// otherwise, false. + /// </returns> public bool Contains(KeyValuePair<string, OfflineEntry> item) { return ContainsKey(item.Key); } /// <summary> - /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index. + /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1" /> to an + /// <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index. /// </summary> - /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param> - /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param> + /// <param name="array"> + /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied + /// from <see cref="T:System.Collections.Generic.ICollection`1" />. The <see cref="T:System.Array" /> must have + /// zero-based indexing. + /// </param> + /// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param> public void CopyTo(KeyValuePair<string, OfflineEntry>[] array, int arrayIndex) { ccache.ToList().CopyTo(array, arrayIndex); } /// <summary> - /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Removes the first occurrence of a specific object from the + /// <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> - /// <returns> True if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> + /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> + /// <returns> + /// True if <paramref name="item" /> was successfully removed from the + /// <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if + /// <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />. + /// </returns> public bool Remove(KeyValuePair<string, OfflineEntry> item) { return Remove(item.Key); } /// <summary> - /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key. + /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the + /// specified key. /// </summary> - /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param> - /// <returns> True if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false. </returns> + /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2" />.</param> + /// <returns> + /// True if the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the key; + /// otherwise, false. + /// </returns> public bool ContainsKey(string key) { return ccache.ContainsKey(key); } /// <summary> - /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The object to use as the key of the element to add.</param> /// <param name="value">The object to use as the value of the element to add.</param> @@ -165,10 +190,13 @@ } /// <summary> - /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The key of the element to remove.</param> - /// <returns> True if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// True if the element is successfully removed; otherwise, false. This method also returns false if + /// <paramref name="key" /> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public bool Remove(string key) { ccache.TryRemove(key, out _); @@ -176,10 +204,18 @@ } /// <summary> - /// Gets the value associated with the specified key. - /// </summary> - /// <param name="key">The key whose value to get.</param><param name="value">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 <paramref name="value"/> parameter. This parameter is passed uninitialized.</param> - /// <returns> True if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false. </returns> + /// Gets the value associated with the specified key. + /// </summary> + /// <param name="key">The key whose value to get.</param> + /// <param name="value"> + /// 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 <paramref name="value" /> parameter. This parameter is passed + /// uninitialized. + /// </param> + /// <returns> + /// True if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an + /// element with the specified key; otherwise, false. + /// </returns> 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 { /// <summary> - /// Create new instances of the <see cref="RealtimeDatabase{T}"/>. + /// Create new instances of the <see cref="RealtimeDatabase{T}" />. /// </summary> /// <typeparam name="T"> Type of elements. </typeparam> /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> /// <param name="elementRoot"> Optional custom root element of received json items. </param> - /// <param name="streamingOptions"> Realtime streaming options. </param> + /// <param name="streamingOptions"> Realtime streaming options. </param> /// <param name="initialPullStrategy"> Specifies what strategy should be used for initial pulling of server data. </param> - /// <param name="pushChanges"> Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. </param> - /// <returns> The <see cref="RealtimeDatabase{T}"/>. </returns> + /// <param name="pushChanges"> + /// Specifies whether changed items should actually be pushed to the server. It this is false, + /// then Put / Post / Delete will not affect server data. + /// </param> + /// <returns> The <see cref="RealtimeDatabase{T}" />. </returns> public static RealtimeDatabase<T> AsRealtimeDatabase<T>(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) @@ -29,16 +31,19 @@ } /// <summary> - /// Create new instances of the <see cref="RealtimeDatabase{T}"/>. + /// Create new instances of the <see cref="RealtimeDatabase{T}" />. /// </summary> /// <typeparam name="T"> Type of elements. </typeparam> - /// <typeparam name="TSetHandler"> Type of the custom <see cref="ISetHandler{T}"/> to use. </typeparam> + /// <typeparam name="TSetHandler"> Type of the custom <see cref="ISetHandler{T}" /> to use. </typeparam> /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> /// <param name="elementRoot"> Optional custom root element of received json items. </param> - /// <param name="streamingOptions"> Realtime streaming options. </param> + /// <param name="streamingOptions"> Realtime streaming options. </param> /// <param name="initialPullStrategy"> Specifies what strategy should be used for initial pulling of server data. </param> - /// <param name="pushChanges"> Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. </param> - /// <returns> The <see cref="RealtimeDatabase{T}"/>. </returns> + /// <param name="pushChanges"> + /// Specifies whether changed items should actually be pushed to the server. It this is false, + /// then Put / Post / Delete will not affect server data. + /// </param> + /// <returns> The <see cref="RealtimeDatabase{T}" />. </returns> public static RealtimeDatabase<T> AsRealtimeDatabase<T, TSetHandler>(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, @@ -52,12 +57,15 @@ } /// <summary> - /// 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. /// </summary> /// <param name="key"> The key. </param> /// <param name="obj"> The object to set. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Patch<T>(this RealtimeDatabase<T> db, string key, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -66,12 +74,15 @@ } /// <summary> - /// Overwrites existing object with given key. + /// Overwrites existing object with given key. /// </summary> /// <param name="key"> The key. </param> /// <param name="obj"> The object to set. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Put<T>(this RealtimeDatabase<T> db, string key, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -80,11 +91,14 @@ } /// <summary> - /// Adds a new entity to the Database. + /// Adds a new entity to the Database. /// </summary> /// <param name="obj"> The object to add. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> /// <returns> The generated key for this object. </returns> public static string Post<T>(this RealtimeDatabase<T> db, T obj, bool syncOnline = true, int priority = 1) where T : class @@ -97,11 +111,14 @@ } /// <summary> - /// Deletes the entity with the given key. + /// Deletes the entity with the given key. /// </summary> /// <param name="key"> The key. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Delete<T>(this RealtimeDatabase<T> db, string key, bool syncOnline = true, int priority = 1) where T : class { @@ -109,7 +126,8 @@ } /// <summary> - /// Do a Put for a nested property specified by <paramref name="propertyExpression"/> of an object with key <paramref name="key"/>. + /// Do a Put for a nested property specified by <paramref name="propertyExpression" /> of an object with key + /// <paramref name="key" />. /// </summary> /// <typeparam name="T"> Type of the root elements. </typeparam> /// <typeparam name="TProperty"> Type of the property being modified</typeparam> @@ -118,7 +136,10 @@ /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param> /// <param name="value"> Value to put. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Put<T, TProperty>(this RealtimeDatabase<T> db, string key, Expression<Func<T, TProperty>> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -128,7 +149,8 @@ } /// <summary> - /// Do a Patch for a nested property specified by <paramref name="propertyExpression"/> of an object with key <paramref name="key"/>. + /// Do a Patch for a nested property specified by <paramref name="propertyExpression" /> of an object with key + /// <paramref name="key" />. /// </summary> /// <typeparam name="T"> Type of the root elements. </typeparam> /// <typeparam name="TProperty"> Type of the property being modified</typeparam> @@ -137,7 +159,10 @@ /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param> /// <param name="value"> Value to patch. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Patch<T, TProperty>(this RealtimeDatabase<T> db, string key, Expression<Func<T, TProperty>> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -147,7 +172,8 @@ } /// <summary> - /// Delete a nested property specified by <paramref name="propertyExpression"/> of an object with key <paramref name="key"/>. This basically does a Put with null value. + /// Delete a nested property specified by <paramref name="propertyExpression" /> of an object with key + /// <paramref name="key" />. This basically does a Put with null value. /// </summary> /// <typeparam name="T"> Type of the root elements. </typeparam> /// <typeparam name="TProperty"> Type of the property being modified</typeparam> @@ -156,7 +182,10 @@ /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param> /// <param name="value"> Value to put. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Delete<T, TProperty>(this RealtimeDatabase<T> db, string key, Expression<Func<T, TProperty>> propertyExpression, bool syncOnline = true, int priority = 1) where T : class @@ -166,8 +195,9 @@ } /// <summary> - /// Post a new entity into the nested dictionary specified by <paramref name="propertyExpression"/> of an object with key <paramref name="key"/>. - /// The key of the new entity is automatically generated. + /// Post a new entity into the nested dictionary specified by <paramref name="propertyExpression" /> of an object with + /// key <paramref name="key" />. + /// The key of the new entity is automatically generated. /// </summary> /// <typeparam name="T"> Type of the root elements. </typeparam> /// <typeparam name="TSelector"> Type of the dictionary being modified</typeparam> @@ -177,7 +207,10 @@ /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param> /// <param name="value"> Value to put. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Post<T, TSelector, TProperty>(this RealtimeDatabase<T> db, string key, Expression<Func<T, TSelector>> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) @@ -193,8 +226,9 @@ } /// <summary> - /// Delete an entity with key <paramref name="dictionaryKey"/> in the nested dictionary specified by <paramref name="propertyExpression"/> of an object with key <paramref name="key"/>. - /// The key of the new entity is automatically generated. + /// Delete an entity with key <paramref name="dictionaryKey" /> in the nested dictionary specified by + /// <paramref name="propertyExpression" /> of an object with key <paramref name="key" />. + /// The key of the new entity is automatically generated. /// </summary> /// <typeparam name="T"> Type of the root elements. </typeparam> /// <typeparam name="TSelector"> Type of the dictionary being modified</typeparam> @@ -204,7 +238,10 @@ /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param> /// <param name="dictionaryKey"> Key within the nested dictionary to delete. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public static void Delete<T, TProperty>(this RealtimeDatabase<T> db, string key, Expression<Func<T, IDictionary<string, TProperty>>> 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<in T> { 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 { /// <summary> - /// Specifies the strategy for initial pull of server data. + /// Specifies the strategy for initial pull of server data. /// </summary> public enum InitialPullStrategy { /// <summary> - /// Don't pull anything. + /// Don't pull anything. /// </summary> None, /// <summary> - /// Pull only what isn't already stored offline. + /// Pull only what isn't already stored offline. /// </summary> MissingOnly, /// <summary> - /// Pull everything that exists on the server. + /// Pull everything that exists on the server. /// </summary> - 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<string> propertyNames = new List<string>(); @@ -13,10 +13,6 @@ public IEnumerable<string> 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<TKey, T> : IDictionary<string, T>, IDictionary { private readonly IDictionary<string, OfflineEntry> 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<T>(); @@ -42,27 +38,10 @@ } } - public ICollection<string> Keys => database.Keys; - ICollection IDictionary.Values { get; } ICollection IDictionary.Keys { get; } - public ICollection<T> Values => database.Values.Select(o => o.Deserialize<T>()).ToList(); - - public T this[string key] - { - get => database[key].Deserialize<T>(); - - 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<string> Keys => database.Keys; + + public ICollection<T> Values => database.Values.Select(o => o.Deserialize<T>()).ToList(); + + public T this[string key] + { + get => database[key].Deserialize<T>(); + + 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<KeyValuePair<string, T>> GetEnumerator() { return database.Select(d => new KeyValuePair<string, T>(d.Key, d.Value.Deserialize<T>())).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; - /// <summary> - /// The offline database. + /// The offline database. /// </summary> public class OfflineDatabase : IDictionary<string, OfflineEntry> { - private readonly LiteRepository db; private readonly IDictionary<string, OfflineEntry> cache; + private readonly LiteRepository db; /// <summary> - /// Initializes a new instance of the <see cref="OfflineDatabase"/> class. + /// Initializes a new instance of the <see cref="OfflineDatabase" /> class. /// </summary> /// <param name="itemType"> The item type which is used to determine the database file name. </param> /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> @@ -38,33 +38,41 @@ } /// <summary> - /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <returns> The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> + /// <returns> The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />. </returns> public int Count => cache.Count; /// <summary> - /// Gets a value indicating whether this is a read-only collection. + /// Gets a value indicating whether this is a read-only collection. /// </summary> public bool IsReadOnly => cache.IsReadOnly; /// <summary> - /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Gets an <see cref="T:System.Collections.Generic.ICollection`1" /> containing the keys of the + /// <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> - /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// An <see cref="T:System.Collections.Generic.ICollection`1" /> containing the keys of the object that + /// implements <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public ICollection<string> Keys => cache.Keys; /// <summary> - /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Gets an <see cref="T:System.Collections.Generic.ICollection`1" /> containing the values in the + /// <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> - /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// An <see cref="T:System.Collections.Generic.ICollection`1" /> containing the values in the object that + /// implements <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public ICollection<OfflineEntry> Values => cache.Values; /// <summary> - /// Gets or sets the element with the specified key. + /// Gets or sets the element with the specified key. /// </summary> /// <param name="key">The key of the element to get or set.</param> - /// <returns> The element with the specified key. </returns> + /// <returns> The element with the specified key. </returns> public OfflineEntry this[string key] { get => cache[key]; @@ -77,7 +85,7 @@ } /// <summary> - /// Returns an enumerator that iterates through the collection. + /// Returns an enumerator that iterates through the collection. /// </summary> /// <returns> An enumerator that can be used to iterate through the collection. </returns> public IEnumerator<KeyValuePair<string, OfflineEntry>> GetEnumerator() @@ -91,65 +99,82 @@ } /// <summary> - /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> + /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> public void Add(KeyValuePair<string, OfflineEntry> item) { Add(item.Key, item.Value); } /// <summary> - /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>. - /// </summary> + /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />. + /// </summary> public void Clear() { cache.Clear(); - db.Delete<OfflineEntry>(Query.All()); + db.Delete<OfflineEntry>(LiteDB.Query.All()); } /// <summary> - /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value. + /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a specific value. /// </summary> - /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> - /// <returns> True if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. </returns> + /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> + /// <returns> + /// True if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.ICollection`1" />; + /// otherwise, false. + /// </returns> public bool Contains(KeyValuePair<string, OfflineEntry> item) { return ContainsKey(item.Key); } /// <summary> - /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index. + /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1" /> to an + /// <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index. /// </summary> - /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param> - /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param> + /// <param name="array"> + /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied + /// from <see cref="T:System.Collections.Generic.ICollection`1" />. The <see cref="T:System.Array" /> must have + /// zero-based indexing. + /// </param> + /// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param> public void CopyTo(KeyValuePair<string, OfflineEntry>[] array, int arrayIndex) { cache.CopyTo(array, arrayIndex); } /// <summary> - /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>. + /// Removes the first occurrence of a specific object from the + /// <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> - /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> - /// <returns> True if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> + /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> + /// <returns> + /// True if <paramref name="item" /> was successfully removed from the + /// <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if + /// <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />. + /// </returns> public bool Remove(KeyValuePair<string, OfflineEntry> item) { return Remove(item.Key); } /// <summary> - /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key. + /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the + /// specified key. /// </summary> - /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param> - /// <returns> True if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false. </returns> + /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2" />.</param> + /// <returns> + /// True if the <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an element with the key; + /// otherwise, false. + /// </returns> public bool ContainsKey(string key) { return cache.ContainsKey(key); } /// <summary> - /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The object to use as the key of the element to add.</param> /// <param name="value">The object to use as the value of the element to add.</param> @@ -160,10 +185,13 @@ } /// <summary> - /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>. + /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The key of the element to remove.</param> - /// <returns> True if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> + /// <returns> + /// True if the element is successfully removed; otherwise, false. This method also returns false if + /// <paramref name="key" /> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2" />. + /// </returns> public bool Remove(string key) { cache.Remove(key); @@ -171,10 +199,18 @@ } /// <summary> - /// Gets the value associated with the specified key. - /// </summary> - /// <param name="key">The key whose value to get.</param><param name="value">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 <paramref name="value"/> parameter. This parameter is passed uninitialized.</param> - /// <returns> True if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false. </returns> + /// Gets the value associated with the specified key. + /// </summary> + /// <param name="key">The key whose value to get.</param> + /// <param name="value"> + /// 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 <paramref name="value" /> parameter. This parameter is passed + /// uninitialized. + /// </param> + /// <returns> + /// True if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2" /> contains an + /// element with the specified key; otherwise, false. + /// </returns> 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 +{ /// <summary> - /// Represents an object stored in offline storage. + /// Represents an object stored in offline storage. /// </summary> public class OfflineEntry { private object dataInstance; /// <summary> - /// Initializes a new instance of the <see cref="OfflineEntry"/> class with an already serialized object. + /// Initializes a new instance of the <see cref="OfflineEntry" /> class with an already serialized object. /// </summary> /// <param name="key"> The key. </param> /// <param name="obj"> The object. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> /// <param name="syncOptions"> The sync options. </param> public OfflineEntry(string key, object obj, string data, int priority, SyncOptions syncOptions, bool isPartial = false) @@ -31,11 +34,14 @@ } /// <summary> - /// Initializes a new instance of the <see cref="OfflineEntry"/> class. + /// Initializes a new instance of the <see cref="OfflineEntry" /> class. /// </summary> /// <param name="key"> The key. </param> /// <param name="obj"> The object. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> /// <param name="syncOptions"> The sync options. </param> 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 @@ } /// <summary> - /// Initializes a new instance of the <see cref="OfflineEntry"/> class. + /// Initializes a new instance of the <see cref="OfflineEntry" /> class. /// </summary> public OfflineEntry() { } /// <summary> - /// Gets or sets the key of this entry. + /// Gets or sets the key of this entry. /// </summary> public string Key { get; set; } /// <summary> - /// 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. /// </summary> public int Priority { get; set; } /// <summary> - /// Gets or sets the timestamp when this entry was last touched. + /// Gets or sets the timestamp when this entry was last touched. /// </summary> public DateTime Timestamp { get; set; } /// <summary> - /// Gets or sets the <see cref="SyncOptions"/> which define what sync state this entry is in. + /// Gets or sets the <see cref="SyncOptions" /> which define what sync state this entry is in. /// </summary> public SyncOptions SyncOptions { get; set; } /// <summary> - /// Gets or sets serialized JSON data. + /// Gets or sets serialized JSON data. /// </summary> public string Data { get; set; } /// <summary> - /// Specifies whether this is only a partial object. + /// Specifies whether this is only a partial object. /// </summary> public bool IsPartial { get; set; } /// <summary> - /// Deserializes <see cref="Data"/> into <typeparamref name="T"/>. The result is cached. + /// Deserializes <see cref="Data" /> into <typeparamref name="T" />. The result is cached. /// </summary> /// <typeparam name="T"> Type of object to deserialize into. </typeparam> - /// <returns> Instance of <typeparamref name="T"/>. </returns> + /// <returns> Instance of <typeparamref name="T" />. </returns> public T Deserialize<T>() { return (T) (dataInstance ?? (dataInstance = JsonConvert.DeserializeObject<T>(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; - /// <summary> - /// The real-time Database which synchronizes online and offline data. + /// The real-time Database which synchronizes online and offline data. /// </summary> /// <typeparam name="T"> Type of entities. </typeparam> - public partial class RealtimeDatabase<T> : IDisposable where T : class + public class RealtimeDatabase<T> : IDisposable where T : class { private readonly ChildQuery childQuery; private readonly string elementRoot; - private readonly StreamingOptions streamingOptions; - private readonly Subject<FirebaseEvent<T>> subject; + private readonly FirebaseCache<T> firebaseCache; private readonly InitialPullStrategy initialPullStrategy; private readonly bool pushChanges; - private readonly FirebaseCache<T> firebaseCache; + private readonly StreamingOptions streamingOptions; + private readonly Subject<FirebaseEvent<T>> subject; + private FirebaseSubscription<T> firebaseSubscription; private bool isSyncRunning; private IObservable<FirebaseEvent<T>> observable; - private FirebaseSubscription<T> firebaseSubscription; /// <summary> - /// Initializes a new instance of the <see cref="RealtimeDatabase{T}"/> class. + /// Initializes a new instance of the <see cref="RealtimeDatabase{T}" /> class. /// </summary> /// <param name="childQuery"> The child query. </param> /// <param name="elementRoot"> The element Root. </param> /// <param name="offlineDatabaseFactory"> The offline database factory. </param> /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> /// <param name="streamChanges"> Specifies whether changes should be streamed from the server. </param> - /// <param name="pullEverythingOnStart"> Specifies if everything should be pull from the online storage on start. It only makes sense when <see cref="streamChanges"/> is set to true. </param> - /// <param name="pushChanges"> Specifies whether changed items should actually be pushed to the server. If this is false, then Put / Post / Delete will not affect server data. </param> + /// <param name="pullEverythingOnStart"> + /// Specifies if everything should be pull from the online storage on start. It only + /// makes sense when <see cref="streamChanges" /> is set to true. + /// </param> + /// <param name="pushChanges"> + /// Specifies whether changed items should actually be pushed to the server. If this is false, + /// then Put / Post / Delete will not affect server data. + /// </param> public RealtimeDatabase(ChildQuery childQuery, string elementRoot, Func<Type, string, IDictionary<string, OfflineEntry>> offlineDatabaseFactory, string filenameModifier, StreamingOptions streamingOptions, InitialPullStrategy initialPullStrategy, bool pushChanges, @@ -67,24 +74,34 @@ } /// <summary> - /// 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. /// </summary> - public event EventHandler<ExceptionEventArgs> SyncExceptionThrown; + public IDictionary<string, OfflineEntry> Database { get; } + + public ISetHandler<T> PutHandler { private get; set; } + + public void Dispose() + { + subject.OnCompleted(); + firebaseSubscription?.Dispose(); + } /// <summary> - /// 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. /// </summary> - public IDictionary<string, OfflineEntry> Database { get; private set; } - - public ISetHandler<T> PutHandler { private get; set; } + public event EventHandler<ExceptionEventArgs> SyncExceptionThrown; /// <summary> - /// Overwrites existing object with given key. + /// Overwrites existing object with given key. /// </summary> /// <param name="key"> The key. </param> /// <param name="obj"> The object to set. </param> /// <param name="syncOnline"> Indicates whether the item should be synced online. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public void Set(string key, T obj, SyncOptions syncOptions, int priority = 1) { SetAndRaise(key, new OfflineEntry(key, obj, priority, syncOptions)); @@ -118,10 +135,13 @@ } /// <summary> - /// 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. /// </summary> /// <param name="key"> The key. </param> - /// <param name="priority"> The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. </param> + /// <param name="priority"> + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher + /// priority. + /// </param> public void Pull(string key, int priority = 1) { if (!Database.ContainsKey(key)) @@ -132,7 +152,7 @@ } /// <summary> - /// Fetches everything from the remote database. + /// Fetches everything from the remote database. /// </summary> public async Task PullAsync() { @@ -142,7 +162,7 @@ .RetryAfterDelay<IReadOnlyCollection<FirebaseObject<T>>, 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 @@ } /// <summary> - /// Retrieves all offline items currently stored in local database. + /// Retrieves all offline items currently stored in local database. /// </summary> public IEnumerable<FirebaseObject<T>> Once() { @@ -174,10 +194,10 @@ .ToList(); } - /// <summary> - /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. - /// </summary> - /// <returns> Stream of <see cref="FirebaseEvent{T}"/>. </returns> + /// <summary> + /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. + /// </summary> + /// <returns> Stream of <see cref="FirebaseEvent{T}" />. </returns> public IObservable<FirebaseEvent<T>> AsObservable() { if (!isSyncRunning) @@ -212,7 +232,7 @@ .RetryAfterDelay<IReadOnlyCollection<FirebaseObject<T>>, 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<FirebaseObject<T>> ResetDatabaseFromInitial( IReadOnlyCollection<FirebaseObject<T>> 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<T> : ISetHandler<T> { 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 { /// <summary> - /// No realtime streaming. + /// No realtime streaming. /// </summary> None, /// <summary> - /// Streaming of only new items - not the existing ones. + /// Streaming of only new items - not the existing ones. /// </summary> LatestOnly, /// <summary> - /// 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 <see cref="InitialPullStrategy"/> to <see cref="InitialPullStrategy.Everything"/> 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 <see cref="InitialPullStrategy" /> to + /// <see cref="InitialPullStrategy.Everything" /> because you would pointlessly pull everything twice. /// </summary> 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 { /// <summary> - /// Specifies type of sync requested for given data. + /// Specifies type of sync requested for given data. /// </summary> public enum SyncOptions { /// <summary> - /// No sync needed for given data. + /// No sync needed for given data. /// </summary> None, /// <summary> - /// Data should be pulled from firebase. + /// Data should be pulled from firebase. /// </summary> Pull, /// <summary> - /// Data should be put to firebase. + /// Data should be put to firebase. /// </summary> Put, /// <summary> - /// Data should be patched in firebase. + /// Data should be patched in firebase. /// </summary> Patch } |