From 86adae19c1be72e31313c7f093715f8cb73fb689 Mon Sep 17 00:00:00 2001 From: Mitchel Sellers Date: Fri, 17 May 2019 23:30:08 -0500 Subject: [PATCH] ThemeService Improvements Centralized the code used to determine the available themes, panes, and containers. This isn't the most "ideal" way to handle this. However, it does improve the management of the code by centralizing the logic for theme selection. Future PR's development might improve this more. --- .../Modules/Admin/ModuleSettings/Index.razor | 9 +--- Oqtane.Client/Modules/Admin/Pages/Add.razor | 15 ++----- .../Modules/Admin/Pages/Delete.razor | 13 +----- Oqtane.Client/Modules/Admin/Pages/Edit.razor | 13 +----- Oqtane.Client/Services/ISkinService.cs | 11 ----- Oqtane.Client/Services/IthemeService.cs | 14 +++++++ Oqtane.Client/Services/ThemeService.cs | 42 ++++++++++++++++++- .../Themes/Controls/ControlPanel.razor | 9 +--- 8 files changed, 64 insertions(+), 62 deletions(-) delete mode 100644 Oqtane.Client/Services/ISkinService.cs create mode 100644 Oqtane.Client/Services/IthemeService.cs diff --git a/Oqtane.Client/Modules/Admin/ModuleSettings/Index.razor b/Oqtane.Client/Modules/Admin/ModuleSettings/Index.razor index c323d4a9..6454aaab 100644 --- a/Oqtane.Client/Modules/Admin/ModuleSettings/Index.razor +++ b/Oqtane.Client/Modules/Admin/ModuleSettings/Index.razor @@ -80,14 +80,7 @@ protected override async Task OnInitAsync() { title = ModuleState.Title; - List Themes = await ThemeService.GetThemesAsync(); - foreach (Theme theme in Themes) - { - foreach (string container in theme.ContainerControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - containers.Add(container, theme.Name + " - " + @Utilities.GetTypeNameClass(container)); - } - } + containers = ThemeService.CalculateSelectableContainers(await ThemeService.GetThemesAsync()); containertype = ModuleState.ContainerType; viewpermissions = ModuleState.ViewPermissions; editpermissions = ModuleState.EditPermissions; diff --git a/Oqtane.Client/Modules/Admin/Pages/Add.razor b/Oqtane.Client/Modules/Admin/Pages/Add.razor index 91371a4e..636b77ae 100644 --- a/Oqtane.Client/Modules/Admin/Pages/Add.razor +++ b/Oqtane.Client/Modules/Admin/Pages/Add.razor @@ -134,18 +134,9 @@ protected override async Task OnInitAsync() { - List Themes = await ThemeService.GetThemesAsync(); - foreach (Theme theme in Themes) - { - foreach (string themecontrol in theme.ThemeControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - themes.Add(themecontrol, theme.Name + " - " + @Utilities.GetTypeNameClass(themecontrol)); - } - foreach (string panelayout in theme.PaneLayouts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - panelayouts.Add(panelayout, theme.Name + " - " + @Utilities.GetTypeNameClass(panelayout)); - } - } + var Themes = await ThemeService.GetThemesAsync(); + themes = ThemeService.CalculateSelectableThemes(Themes); + panelayouts = ThemeService.CalculateSelectablePaneLayouts(Themes); } private async Task SavePage() diff --git a/Oqtane.Client/Modules/Admin/Pages/Delete.razor b/Oqtane.Client/Modules/Admin/Pages/Delete.razor index e3d04920..156b6d96 100644 --- a/Oqtane.Client/Modules/Admin/Pages/Delete.razor +++ b/Oqtane.Client/Modules/Admin/Pages/Delete.razor @@ -136,17 +136,8 @@ protected override async Task OnInitAsync() { List Themes = await ThemeService.GetThemesAsync(); - foreach (Theme theme in Themes) - { - foreach (string themecontrol in theme.ThemeControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - themes.Add(themecontrol, theme.Name + " - " + @Utilities.GetTypeNameClass(themecontrol)); - } - foreach (string panelayout in theme.PaneLayouts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - panelayouts.Add(panelayout, theme.Name + " - " + @Utilities.GetTypeNameClass(panelayout)); - } - } + themes = ThemeService.CalculateSelectableThemes(Themes); + panelayouts = ThemeService.CalculateSelectablePaneLayouts(Themes); PageId = Int32.Parse(PageState.QueryString["id"]); Page p = PageState.Pages.Where(item => item.PageId == PageId).FirstOrDefault(); diff --git a/Oqtane.Client/Modules/Admin/Pages/Edit.razor b/Oqtane.Client/Modules/Admin/Pages/Edit.razor index 9a1abda6..af201b9e 100644 --- a/Oqtane.Client/Modules/Admin/Pages/Edit.razor +++ b/Oqtane.Client/Modules/Admin/Pages/Edit.razor @@ -136,17 +136,8 @@ protected override async Task OnInitAsync() { List Themes = await ThemeService.GetThemesAsync(); - foreach (Theme theme in Themes) - { - foreach (string themeControl in theme.ThemeControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - themes.Add(themeControl, theme.Name + " - " + @Utilities.GetTypeNameClass(themeControl)); - } - foreach (string panelayout in theme.PaneLayouts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - panelayouts.Add(panelayout, theme.Name + " - " + @Utilities.GetTypeNameClass(panelayout)); - } - } + themes = ThemeService.CalculateSelectableThemes(Themes); + panelayouts = ThemeService.CalculateSelectablePaneLayouts(Themes); PageId = Int32.Parse(PageState.QueryString["id"]); Page p = PageState.Pages.Where(item => item.PageId == PageId).FirstOrDefault(); diff --git a/Oqtane.Client/Services/ISkinService.cs b/Oqtane.Client/Services/ISkinService.cs deleted file mode 100644 index 413960f9..00000000 --- a/Oqtane.Client/Services/ISkinService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Oqtane.Models; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace Oqtane.Services -{ - public interface IThemeService - { - Task> GetThemesAsync(); - } -} diff --git a/Oqtane.Client/Services/IthemeService.cs b/Oqtane.Client/Services/IthemeService.cs new file mode 100644 index 00000000..1a2ec875 --- /dev/null +++ b/Oqtane.Client/Services/IthemeService.cs @@ -0,0 +1,14 @@ +using Oqtane.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Oqtane.Services +{ + public interface IThemeService + { + Task> GetThemesAsync(); + Dictionary CalculateSelectableThemes(List themes); + Dictionary CalculateSelectablePaneLayouts(List themes); + Dictionary CalculateSelectableContainers(List themes); + } +} diff --git a/Oqtane.Client/Services/ThemeService.cs b/Oqtane.Client/Services/ThemeService.cs index 0cccd098..ca67509e 100644 --- a/Oqtane.Client/Services/ThemeService.cs +++ b/Oqtane.Client/Services/ThemeService.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using System.Reflection; using System; +using Oqtane.Shared; namespace Oqtane.Services { @@ -21,7 +22,7 @@ namespace Oqtane.Services this.http = http; apiurl = CreateApiUrl(urihelper.GetAbsoluteUri(), "Theme"); } - + //TODO: Implement Caching or otherwise on this, and other calls within this class public async Task> GetThemesAsync() { if (themes == null) @@ -56,5 +57,44 @@ namespace Oqtane.Services } return themes.OrderBy(item => item.Name).ToList(); } + + public Dictionary CalculateSelectableThemes(List themes) + { + var selectableThemes = new Dictionary(); + foreach (Theme theme in themes) + { + foreach (string themecontrol in theme.ThemeControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + { + selectableThemes.Add(themecontrol, theme.Name + " - " + Utilities.GetTypeNameClass(themecontrol)); + } + } + return selectableThemes; + } + + public Dictionary CalculateSelectablePaneLayouts(List themes) + { + var selectablePaneLayouts = new Dictionary(); + foreach (Theme theme in themes) + { + foreach (string panelayout in theme.PaneLayouts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + { + selectablePaneLayouts.Add(panelayout, theme.Name + " - " + @Utilities.GetTypeNameClass(panelayout)); + } + } + return selectablePaneLayouts; + } + + public Dictionary CalculateSelectableContainers(List themes) + { + var selectableContainers = new Dictionary(); + foreach (Theme theme in themes) + { + foreach (string container in theme.ContainerControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + { + selectableContainers.Add(container, theme.Name + " - " + @Utilities.GetTypeNameClass(container)); + } + } + return selectableContainers; + } } } diff --git a/Oqtane.Client/Themes/Controls/ControlPanel.razor b/Oqtane.Client/Themes/Controls/ControlPanel.razor index f87698b3..fde455a4 100644 --- a/Oqtane.Client/Themes/Controls/ControlPanel.razor +++ b/Oqtane.Client/Themes/Controls/ControlPanel.razor @@ -84,14 +84,7 @@ { //TODO: Move this to shared component. This is used in this control Add, Edit, and Delete controls as well moduledefinitions = await ModuleDefinitionService.GetModuleDefinitionsAsync(); - List themes = await ThemeService.GetThemesAsync(); - foreach (Theme theme in themes) - { - foreach (string container in theme.ContainerControls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - containers.Add(container, theme.Name + " - " + @Utilities.GetTypeNameClass(container)); - } - } + containers = ThemeService.CalculateSelectableContainers(await ThemeService.GetThemesAsync()); List modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId, Constants.PageManagementModule); pagemanagementmoduleid = modules.FirstOrDefault().ModuleId; if (UserService.IsAuthorized(PageState.User, PageState.Page.EditPermissions))