From 22de520962be46cd489cdd9a1d37f99c74f8c968 Mon Sep 17 00:00:00 2001 From: Chanan Braunstein Date: Sat, 2 Jun 2018 10:23:41 -0600 Subject: [PATCH] LogToConsole on context and storage sets --- src/BlazorDB/BlazorDB.csproj | 2 +- src/BlazorDB/IStorageContext.cs | 1 + src/BlazorDB/Storage/Logger.cs | 11 ++++++++-- src/BlazorDB/StorageContext.cs | 13 ++++++++++++ src/BlazorDB/StorageSet.cs | 6 +++++- src/FluxorIntegration/Pages/Index.cshtml | 9 ++++++++- .../Store/Movies/LogContextAction.cs | 8 ++++++++ .../Store/Movies/LogContextReducer.cs | 20 +++++++++++++++++++ 8 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/FluxorIntegration/Store/Movies/LogContextAction.cs create mode 100644 src/FluxorIntegration/Store/Movies/LogContextReducer.cs diff --git a/src/BlazorDB/BlazorDB.csproj b/src/BlazorDB/BlazorDB.csproj index 10ef9ff..2672604 100644 --- a/src/BlazorDB/BlazorDB.csproj +++ b/src/BlazorDB/BlazorDB.csproj @@ -7,7 +7,7 @@ false 7.3 BlazorDB - 0.1.1 + 0.1.2 Chanan Braunstein Blazor localStorage Database In memory, persisted to localstorage, database for .net Blazor browser framework diff --git a/src/BlazorDB/IStorageContext.cs b/src/BlazorDB/IStorageContext.cs index 56069f9..94952ea 100644 --- a/src/BlazorDB/IStorageContext.cs +++ b/src/BlazorDB/IStorageContext.cs @@ -3,5 +3,6 @@ public interface IStorageContext { int SaveChanges(); + void LogToConsole(); } } diff --git a/src/BlazorDB/Storage/Logger.cs b/src/BlazorDB/Storage/Logger.cs index ba8751c..f00320d 100644 --- a/src/BlazorDB/Storage/Logger.cs +++ b/src/BlazorDB/Storage/Logger.cs @@ -10,10 +10,17 @@ internal static class Logger private const string Normal = "color: black; font-style: normal;"; internal static bool LogDebug { get; set; } = true; - internal static void StartContextType(Type contextType) + internal static void LogStorageSetToConsole(Type type, object list) { if (!LogDebug) return; - BlazorLogger.Logger.GroupCollapsed($"Context loaded: %c{contextType.Namespace}.{contextType.Name}", Blue); + BlazorLogger.Logger.Log($"StorageSet<{type.GetGenericArguments()[0].Name}>: %o", list); + } + + internal static void StartContextType(Type contextType, bool loading = true) + { + if (!LogDebug) return; + var message = loading ? " loading" : " log"; + BlazorLogger.Logger.GroupCollapsed($"Context{message}: %c{contextType.Namespace}.{contextType.Name}", Blue); } internal static void ContextSaved(Type contextType) diff --git a/src/BlazorDB/StorageContext.cs b/src/BlazorDB/StorageContext.cs index 29a1fdf..a599bc0 100644 --- a/src/BlazorDB/StorageContext.cs +++ b/src/BlazorDB/StorageContext.cs @@ -6,6 +6,19 @@ public class StorageContext : IStorageContext { private IStorageManager StorageManager { get; set; } = new StorageManager(); // Dep injection not working right now + public void LogToConsole() + { + Logger.StartContextType(GetType(), false); + var storageSets = StorageManagerUtil.GetStorageSets(GetType()); + foreach (var prop in storageSets) + { + var storageSet = prop.GetValue(this); + var method = storageSet.GetType().GetMethod("LogToConsole"); + method.Invoke(storageSet, new object[]{}); + } + Logger.EndGroup(); + } + public int SaveChanges() { return StorageManager.SaveContextToLocalStorage(this); diff --git a/src/BlazorDB/StorageSet.cs b/src/BlazorDB/StorageSet.cs index 121be53..2dfc757 100644 --- a/src/BlazorDB/StorageSet.cs +++ b/src/BlazorDB/StorageSet.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; namespace BlazorDB { @@ -11,6 +10,11 @@ public class StorageSet : IList where TModel : class private string StorageContextTypeName { get; set; } private IList List { get; set; } = new List(); + public void LogToConsole() + { + Logger.LogStorageSetToConsole(GetType(), List); + } + public TModel this[int index] { get => List[index]; diff --git a/src/FluxorIntegration/Pages/Index.cshtml b/src/FluxorIntegration/Pages/Index.cshtml index d18384d..c95d8de 100644 --- a/src/FluxorIntegration/Pages/Index.cshtml +++ b/src/FluxorIntegration/Pages/Index.cshtml @@ -23,7 +23,9 @@

Movies

- + + + @if (State.Current.Movies.Count > 0) { @@ -148,4 +150,9 @@ await Dispatcher.DispatchAsync(new InsertSeedMoviesAction()); StateHasChanged(); } + + async void OnDebug() + { + await Dispatcher.DispatchAsync(new LogContextAction()); + } } \ No newline at end of file diff --git a/src/FluxorIntegration/Store/Movies/LogContextAction.cs b/src/FluxorIntegration/Store/Movies/LogContextAction.cs new file mode 100644 index 0000000..a651e8b --- /dev/null +++ b/src/FluxorIntegration/Store/Movies/LogContextAction.cs @@ -0,0 +1,8 @@ +using Blazor.Fluxor; + +namespace FluxorIntegration.Store.Movies +{ + public class LogContextAction : IAction + { + } +} diff --git a/src/FluxorIntegration/Store/Movies/LogContextReducer.cs b/src/FluxorIntegration/Store/Movies/LogContextReducer.cs new file mode 100644 index 0000000..c57038a --- /dev/null +++ b/src/FluxorIntegration/Store/Movies/LogContextReducer.cs @@ -0,0 +1,20 @@ +using Blazor.Fluxor; +using FluxorIntegration.Models; + +namespace FluxorIntegration.Store.Movies +{ + public class LogContextReducer : IReducer + { + private Context Context { get; set; } + + public LogContextReducer(Context context) + { + Context = context; + } + public MovieState Reduce(MovieState state, LogContextAction action) + { + Context.LogToConsole(); + return state; + } + } +}