Skip to content

Commit

Permalink
refactor: migrate from ini app config to json, add theming and config…
Browse files Browse the repository at this point in the history
… services

Signed-off-by: Russell Camo <[email protected]>
  • Loading branch information
russkyc committed Aug 28, 2023
1 parent 1e31cf4 commit b13ceac
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 69 deletions.
36 changes: 36 additions & 0 deletions GroomWise.Application/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using GroomWise.Domain.Enums;
using GroomWise.Infrastructure.Authentication.Enums;
using GroomWise.Infrastructure.Authentication.Interfaces;
using GroomWise.Infrastructure.Configuration.Interfaces;
using GroomWise.Infrastructure.IoC.Interfaces;
using GroomWise.Infrastructure.Navigation.Interfaces;
using GroomWise.Infrastructure.Theming.Interfaces;
using Injectio.Attributes;
using MvvmGen;
using MvvmGen.ViewModels;
Expand All @@ -19,11 +21,16 @@ namespace GroomWise.Application.ViewModels;
[Inject(typeof(IDialogFactory))]
[Inject(typeof(INavigationService))]
[Inject(typeof(IAppServicesContainer))]
[Inject(typeof(IConfigurationService))]
[Inject(typeof(IThemeManagerService))]
[Inject(typeof(DashboardViewModel))]
[ViewModel]
[RegisterSingleton]
public partial class AppViewModel
{
[Property]
private IConfigurationService _configuration;

[Property]
private ViewModelBase _pageContext;

Expand All @@ -33,6 +40,7 @@ public partial class AppViewModel
partial void OnInitialize()
{
PageContext = DashboardViewModel;
Configuration = ConfigurationService;
AuthenticatedUserRoles = AuthenticationService.GetSession()?.Roles!;
}

Expand Down Expand Up @@ -74,4 +82,32 @@ private async Task NavigateToPage(object param)
}
});
}

[Command]
public async Task SetDarkTheme(object param)
{
await Task.Run(() =>
{
if (param is bool useDarkTheme)
{
Configuration.DarkMode = true;
OnPropertyChanged(nameof(Configuration.DarkMode));
ThemeManagerService.SetDarkTheme(useDarkTheme);
}
});
}

[Command]
public async Task SetColorTheme(object param)
{
await Task.Run(() =>
{
if (param is string themeId)
{
Configuration.ColorTheme = themeId;
OnPropertyChanged(nameof(Configuration.ColorTheme));
ThemeManagerService.SetColorTheme(themeId);
}
});
}
}
16 changes: 15 additions & 1 deletion GroomWise.Application/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Infrastructure.Configuration.Interfaces;
using GroomWise.Infrastructure.Theming.Interfaces;
using Injectio.Attributes;
using MvvmGen;

namespace GroomWise.Application.ViewModels;

[ViewModel]
[Inject(typeof(IThemeManagerService))]
[Inject(typeof(IConfigurationService))]
[RegisterSingleton]
public partial class SettingsViewModel { }
public partial class SettingsViewModel
{
[Property]
private IConfigurationService _configuration;

partial void OnInitialize()
{
Configuration = ConfigurationService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using System.ComponentModel;
using GroomWise.Infrastructure.Configuration.Interfaces;
using Injectio.Attributes;
using Russkyc.Configuration;

namespace GroomWise.Infrastructure.Configuration;

[RegisterSingleton<IConfigurationService, ConfigurationService>]
public class ConfigurationService : ConfigProvider, IConfigurationService
{
public ConfigurationService(string path)
: base(path) { }
public ConfigurationService()
: base("appconfig.json") { }

public bool DarkMode
{
Expand Down
2 changes: 2 additions & 0 deletions GroomWise.Infrastructure/GroomWise.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@





</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

namespace GroomWise.Infrastructure.Theming.Interfaces;

public interface IThemeManagerService
{
void SetDarkTheme(bool useDarkTheme);
void SetColorTheme(string themeId);
}
34 changes: 17 additions & 17 deletions GroomWise.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

using System.Threading;
using GroomWise.Application.Enums;
using GroomWise.Infrastructure.Configuration.Interfaces;
using GroomWise.Infrastructure.IoC.Interfaces;
using GroomWise.Infrastructure.Navigation.Interfaces;
using GroomWise.Infrastructure.Theming.Interfaces;
using GroomWise.Views;
using GroomWise.Views.Dialogs;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -25,39 +27,37 @@ public App()
{
InitializeComponent();

var services = new ServiceCollection()
var container = new ServiceCollection()
.AddGroomWiseInfrastructure()
.AddGroomWiseApplication()
.AddGroomWise();
.AddGroomWise()
.BuildServiceProvider();

var container = services.BuildServiceProvider();
var scope = container.GetService<IAppServicesContainer>();
scope?.AddContainer(container);

var main = scope?.GetService<MainView>();
var login = scope?.GetService<LoginView>();
var navigation = scope?.GetService<INavigationService>();

Current.MainWindow = login;

Current.Dispatcher.BeginInvoke(() =>
Dispatcher.BeginInvoke(() =>
{
navigation?.Initialize(SynchronizationContext.Current!, login!);
navigation?.Add(AppViews.Login, login!);
navigation?.Add(AppViews.Main, main!);
});
MainWindow?.Show();
}

/*protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Load Theme Defaults
var config = scope?.GetService<IConfigurationService>();
var themeManager = scope?.GetService<IThemeManagerService>();

// Set the maximum amount of memory that the application can use to 2 GB for x86 and 4 GB for x64.
IntPtr maxWorkingSet = new IntPtr(
(IntPtr.Size == 4) ? (int)(1.5 * 1024 * 1024 * 1024) : 4L * 1024L * 1024L * 1024L
);
if (config is not null && themeManager is not null)
{
themeManager.SetDarkTheme(config.DarkMode);
themeManager.SetColorTheme(config.ColorTheme);
}

Process.GetCurrentProcess().MaxWorkingSet = maxWorkingSet;
}*/
MainWindow = login;
MainWindow?.Show();
}
}
25 changes: 25 additions & 0 deletions GroomWise.WPF/Converters/PageContextToSelectionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using System;
using System.Globalization;
using System.Windows.Data;

namespace GroomWise.Converters;

public class PageContextToSelectionConverter : IValueConverter
{
public static PageContextToSelectionConverter Instance = new();

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.GetType() == (Type)parameter;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
6 changes: 2 additions & 4 deletions GroomWise.WPF/GroomWise.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@
<None Update="data.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="appconfig.ini">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="appconfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions GroomWise.WPF/Services/ThemeManagerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Infrastructure.Theming.Interfaces;
using Injectio.Attributes;
using org.russkyc.moderncontrols.Helpers;

namespace GroomWise.Services;

[RegisterSingleton]
public class ThemeManagerService : IThemeManagerService
{
public void SetDarkTheme(bool useDarkTheme)
{
ThemeManager.Instance.SetBaseTheme(useDarkTheme ? "Dark" : "Light");
}

public void SetColorTheme(string themeId)
{
ThemeManager.Instance.SetColorTheme(themeId);
}
}
12 changes: 10 additions & 2 deletions GroomWise.WPF/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
x:Class="GroomWise.Views.MainView"
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:GroomWise.Converters"
xmlns:d="http:https://schemas.microsoft.com/expression/blend/2008"
xmlns:icons="clr-namespace:Material.Icons.WPF;assembly=Material.Icons.WPF"
xmlns:mc="http:https://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -100,13 +101,13 @@
Margin="5"
Padding="6"
CheckedForeground="{DynamicResource inverted-lighten-3}"
Command="{Binding SwitchBaseThemeCommand}"
Command="{Binding SetDarkThemeCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
Content="{icons:MaterialIconExt Kind=Lightbulb}"
CornerRadius="5"
DefaultForeground="{DynamicResource fg-600}"
HoverForeground="{DynamicResource fg-500}"
IsChecked="{Binding ThemeManagerService.DarkMode, UpdateSourceTrigger=PropertyChanged}" />
IsChecked="{Binding Configuration.DarkMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Grid>
</russkyc:ModernWindow.TitleBarTemplate>
Expand All @@ -127,38 +128,45 @@
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt Home}"
PageContext="{x:Type viewModels:DashboardViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:DashboardViewModel}, Mode=OneWay}"
Tooltip="Home" />
<templates:NavItemTemplate
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt Event}"
PageContext="{x:Type viewModels:AppointmentViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:AppointmentViewModel}, Mode=OneWay}"
Tooltip="Appointments" />
<templates:NavItemTemplate
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt Shower}"
PageContext="{x:Type viewModels:GroomingServiceViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:GroomingServiceViewModel}, Mode=OneWay}"
Tooltip="Services" />
<templates:NavItemTemplate
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt Person}"
PageContext="{x:Type viewModels:CustomerViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:CustomerViewModel}, Mode=OneWay}"
Tooltip="Customers" />
<templates:NavItemTemplate
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt PeopleGroup}"
PageContext="{x:Type viewModels:EmployeeViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:EmployeeViewModel}, Mode=OneWay}"
Tooltip="Employees" />
<templates:NavItemTemplate
Margin="8,8,8,0"
Icon="{icons:MaterialIconExt Pets}"
PageContext="{x:Type viewModels:PetViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:PetViewModel}, Mode=OneWay}"
Tooltip="Pets" />
</StackPanel>
<templates:NavItemTemplate
Margin="8"
VerticalAlignment="Bottom"
Icon="{icons:MaterialIconExt Settings}"
PageContext="{x:Type viewModels:SettingsViewModel}"
Selected="{Binding PageContext, Converter={x:Static converters:PageContextToSelectionConverter.Instance}, ConverterParameter={x:Type viewModels:SettingsViewModel}, Mode=OneWay}"
Tooltip="Settings" />
</Grid>
<ContentControl Grid.Column="1" Content="{Binding PageContext}">
Expand Down
2 changes: 1 addition & 1 deletion GroomWise.WPF/Views/Templates/NavItemTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
GroupName="NavItemGroup"
HoverBackground="{DynamicResource bg-200}"
HoverForeground="{DynamicResource bg-600}"
IsChecked="{Binding Selected, UpdateSourceTrigger=PropertyChanged}"
IsChecked="{Binding Selected, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType=templates:NavItemTemplate}}"
PressedForeground="{DynamicResource inverted-default}"
ToolTip="{Binding Tooltip, RelativeSource={RelativeSource FindAncestor, AncestorType=templates:NavItemTemplate}}" />
</Grid>
Expand Down
3 changes: 3 additions & 0 deletions GroomWise.WPF/Views/Templates/NavItemTemplate.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public NavItemTemplate()
[DependencyProperty(typeof(string))]
public static readonly DependencyProperty TooltipProperty;

[DependencyProperty(typeof(bool))]
public static readonly DependencyProperty SelectedProperty;

[DependencyProperty(typeof(Type))]
public static readonly DependencyProperty PageContextProperty;
}
Loading

0 comments on commit b13ceac

Please sign in to comment.