-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from FaithBeam/custom-theme
Move Theme Values into Db
- Loading branch information
Showing
15 changed files
with
286 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Riok.Mapperly.Abstractions; | ||
using YMouseButtonControl.Core.ViewModels.Models; | ||
using YMouseButtonControl.DataAccess.Models; | ||
|
||
namespace YMouseButtonControl.Core.Mappings; | ||
|
||
[Mapper] | ||
public static partial class ThemeMapper | ||
{ | ||
public static partial ThemeVm Map(Theme? theme); | ||
|
||
public static partial Theme Map(ThemeVm? vm); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using YMouseButtonControl.Core.Mappings; | ||
using YMouseButtonControl.Core.ViewModels.Models; | ||
using YMouseButtonControl.DataAccess.Context; | ||
using YMouseButtonControl.DataAccess.Models; | ||
using YMouseButtonControl.DataAccess.Queries; | ||
|
||
namespace YMouseButtonControl.Core.Repositories; | ||
|
||
public class ThemeRepository(YMouseButtonControlDbContext ctx, ThemeQueries queries) | ||
: IRepository<Theme, ThemeVm> | ||
{ | ||
private const string TblName = "Themes"; | ||
|
||
public int Add(ThemeVm vm) | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return conn.Query<int>(queries.Add(), ThemeMapper.Map(vm)).Single(); | ||
} | ||
|
||
public Task<int> AddAsync(ThemeVm vm) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public int Delete(ThemeVm vm) | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return conn.Execute(queries.DeleteById(TblName), new { ThemeMapper.Map(vm).Id }); | ||
} | ||
|
||
public Task<int> DeleteAsync(ThemeVm vm) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public IEnumerable<ThemeVm> GetAll() | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return conn.Query<Theme>(queries.GetAll(TblName)).Select(ThemeMapper.Map); | ||
} | ||
|
||
public Task<IEnumerable<ThemeVm>> GetAllAsync() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ThemeVm? GetById(int id) | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return ThemeMapper.Map( | ||
conn.QueryFirstOrDefault<Theme>(queries.GetById(TblName), new { Id = id }) | ||
); | ||
} | ||
|
||
public Task<ThemeVm?> GetByIdAsync(int id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ThemeVm? GetByName(string name) | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return ThemeMapper.Map( | ||
conn.QuerySingleOrDefault<Theme>(queries.GetByName(TblName), new { Name = name }) | ||
); | ||
} | ||
|
||
public int Update(ThemeVm vm) | ||
{ | ||
using var conn = ctx.CreateConnection(); | ||
return conn.Execute(queries.Update(), ThemeMapper.Map(vm)); | ||
} | ||
|
||
public Task<int> UpdateAsync(ThemeVm vm) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
179 changes: 100 additions & 79 deletions
179
YMouseButtonControl.Core/Services/Theme/ThemeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,146 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avalonia; | ||
using Avalonia.Media; | ||
using Avalonia.Styling; | ||
using ReactiveUI; | ||
using YMouseButtonControl.Core.Repositories; | ||
using YMouseButtonControl.Core.Services.Settings; | ||
using YMouseButtonControl.Core.ViewModels.Models; | ||
using YMouseButtonControl.DataAccess.Models; | ||
|
||
namespace YMouseButtonControl.Core.Services.Theme; | ||
|
||
public interface IThemeService | ||
{ | ||
IBrush HighlightLight { get; set; } | ||
IBrush HighlightDark { get; set; } | ||
IBrush BackgroundDark { get; set; } | ||
IBrush BackgroundLight { get; set; } | ||
IBrush CurBackground { get; set; } | ||
IBrush CurHighlight { get; set; } | ||
public IBrush Background { get; } | ||
public IBrush Highlight { get; } | ||
ThemeVariant ThemeVariant { get; } | ||
List<ThemeVm> Themes { get; } | ||
} | ||
|
||
public class ThemeService : ReactiveObject, IThemeService | ||
{ | ||
private readonly IRepository<DataAccess.Models.Theme, ThemeVm> _themeRepo; | ||
private readonly SettingIntVm _themeSetting; | ||
private IBrush _highlightLight = Brushes.Yellow; | ||
private IBrush _highlightDark = Brush.Parse("#3700b3"); | ||
private IBrush _backgroundDark = Brushes.Black; | ||
private IBrush _backgroundLight = Brushes.White; | ||
private IBrush _curBackground; | ||
private IBrush _curHighlight; | ||
private ThemeVariant _themeVariant; | ||
|
||
public ThemeService(ISettingsService settingsService) | ||
private readonly ThemeVm _themeVm; | ||
private IBrush _background; | ||
private IBrush _highlight; | ||
private readonly ThemeVariant _themeVariant; | ||
|
||
public ThemeService( | ||
IRepository<DataAccess.Models.Theme, ThemeVm> themeRepo, | ||
ISettingsService settingsService | ||
) | ||
{ | ||
_themeRepo = themeRepo; | ||
_themeSetting = | ||
settingsService.GetSetting("Theme") as SettingIntVm | ||
?? throw new Exception("Error retrieving theme setting"); | ||
_curBackground = GetCurrentThemeBackground(); | ||
_curHighlight = GetCurrentThemeHighlight(); | ||
_themeVm = | ||
_themeRepo.GetById(_themeSetting.IntValue) | ||
?? throw new Exception($"Error retrieving theme with id: {_themeSetting.IntValue}"); | ||
_themeVariant = GetThemeVariant(); | ||
_background = GetBackground(); | ||
_highlight = GetHighlight(); | ||
} | ||
|
||
public IBrush HighlightLight | ||
{ | ||
get => _highlightLight; | ||
set => this.RaiseAndSetIfChanged(ref _highlightLight, value); | ||
} | ||
|
||
public IBrush HighlightDark | ||
{ | ||
get => _highlightDark; | ||
set => this.RaiseAndSetIfChanged(ref _highlightDark, value); | ||
} | ||
public List<ThemeVm> Themes => [.. _themeRepo.GetAll().OrderBy(x => x.Id)]; | ||
|
||
public IBrush BackgroundDark | ||
{ | ||
get => _backgroundDark; | ||
set => this.RaiseAndSetIfChanged(ref _backgroundDark, value); | ||
} | ||
|
||
public IBrush BackgroundLight | ||
{ | ||
get => _backgroundLight; | ||
set => this.RaiseAndSetIfChanged(ref _backgroundLight, value); | ||
} | ||
public ThemeVariant ThemeVariant => _themeVariant; | ||
|
||
public IBrush CurBackground | ||
public IBrush Background | ||
{ | ||
get => _curBackground; | ||
set => this.RaiseAndSetIfChanged(ref _curBackground, value); | ||
get => _background; | ||
set => this.RaiseAndSetIfChanged(ref _background, value); | ||
} | ||
|
||
public IBrush CurHighlight | ||
public IBrush Highlight | ||
{ | ||
get => _curHighlight; | ||
set => this.RaiseAndSetIfChanged(ref _curHighlight, value); | ||
get => _highlight; | ||
set => this.RaiseAndSetIfChanged(ref _highlight, value); | ||
} | ||
|
||
public ThemeVariant ThemeVariant => _themeVariant; | ||
|
||
private ThemeVariant GetThemeVariant() | ||
private IBrush GetBackground() | ||
{ | ||
var theme = (ThemeEnum)_themeSetting.IntValue; | ||
return theme switch | ||
// Background is of the form #aarrggbb | ||
if (_themeVm.Background.StartsWith('#')) | ||
{ | ||
ThemeEnum.Default => ThemeVariant.Default, | ||
ThemeEnum.Light => ThemeVariant.Light, | ||
ThemeEnum.Dark => ThemeVariant.Dark, | ||
_ => throw new ArgumentOutOfRangeException($"Invalid theme {theme}"), | ||
}; | ||
return Brush.Parse(_themeVm.Background); | ||
} | ||
|
||
// Background is an avalonia resource like SystemAltHighColor | ||
if ( | ||
Application.Current!.TryGetResource( | ||
_themeVm.Background, | ||
Application.Current.ActualThemeVariant, | ||
out var backgroundBrush | ||
) | ||
) | ||
{ | ||
if (backgroundBrush is null) | ||
{ | ||
throw new Exception("Error retrieving background brush"); | ||
} | ||
var bbStr = backgroundBrush.ToString(); | ||
if (string.IsNullOrWhiteSpace(bbStr)) | ||
{ | ||
throw new Exception("Error retrieving background brush"); | ||
} | ||
var brush = Brush.Parse(bbStr); | ||
return brush; | ||
} | ||
|
||
// Background may be a color like White, Black, etc. | ||
return Brush.Parse(_themeVm.Background); | ||
} | ||
|
||
private IBrush GetCurrentThemeBackground() | ||
private IBrush GetHighlight() | ||
{ | ||
var theme = (ThemeEnum)_themeSetting.IntValue; | ||
|
||
return theme switch | ||
// Highlight is of the form #aarrggbb | ||
if (_themeVm.Highlight.StartsWith('#')) | ||
{ | ||
ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Light => | ||
_backgroundLight, | ||
ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Dark => | ||
_backgroundDark, | ||
ThemeEnum.Light => _backgroundLight, | ||
ThemeEnum.Dark => _backgroundDark, | ||
_ => throw new ArgumentOutOfRangeException($"Unknown theme {theme}"), | ||
}; | ||
return Brush.Parse(_themeVm.Highlight); | ||
} | ||
|
||
// Highlight is an avalonia resource like SystemAltHighColor | ||
if ( | ||
Application.Current!.TryGetResource( | ||
_themeVm.Highlight, | ||
Application.Current.ActualThemeVariant, | ||
out var highlightBrush | ||
) | ||
) | ||
{ | ||
if (highlightBrush is null) | ||
{ | ||
throw new Exception("Error retrieving highlight brush"); | ||
} | ||
var bbStr = highlightBrush.ToString(); | ||
if (string.IsNullOrWhiteSpace(bbStr)) | ||
{ | ||
throw new Exception("Error retrieving highlight brush"); | ||
} | ||
var brush = Brush.Parse(bbStr); | ||
return brush; | ||
} | ||
|
||
// Highlight may be a color like White, Black, etc. | ||
return Brush.Parse(_themeVm.Highlight); | ||
} | ||
|
||
private IBrush GetCurrentThemeHighlight() | ||
private ThemeVariant GetThemeVariant() | ||
{ | ||
var theme = (ThemeEnum)_themeSetting.IntValue; | ||
|
||
return theme switch | ||
return _themeSetting.IntValue switch | ||
{ | ||
ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Light => | ||
_highlightLight, | ||
ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Dark => | ||
_highlightDark, | ||
ThemeEnum.Light => _highlightLight, | ||
ThemeEnum.Dark => _highlightDark, | ||
_ => throw new Exception($"Unknown theme {theme}"), | ||
1 => Application.Current!.ActualThemeVariant == ThemeVariant.Light | ||
? ThemeVariant.Light | ||
: ThemeVariant.Dark, | ||
2 => ThemeVariant.Light, | ||
3 => ThemeVariant.Dark, | ||
_ => throw new ArgumentOutOfRangeException( | ||
$"Invalid theme id: {_themeSetting.IntValue}" | ||
), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.