< Summary

Information
Class: MyNet.UI.ViewModels.Shell.Chrome.ShellThemeViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Shell/Chrome/ShellThemeViewModel.cs
Tag: 323_28699572109
Line coverage
61%
Covered lines: 16
Uncovered lines: 10
Coverable lines: 26
Total lines: 98
Line coverage: 61.5%
Branch coverage
50%
Covered branches: 6
Total branches: 12
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
get_CurrentTheme()100%210%
set_IsDark(...)66.66%7671.42%
OnThemeServiceThemeChanged(...)0%620%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Shell/Chrome/ShellThemeViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ShellThemeViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reactive.Disposables;
 9using System.Windows.Input;
 10using MyNet.UI.Commands;
 11using MyNet.UI.Theming;
 12using MyNet.Utilities.Suspending;
 13
 14namespace MyNet.UI.ViewModels.Shell.Chrome;
 15
 16/// <summary>
 17/// Shell chrome for quick light/dark theme base switching.
 18/// Detailed theme colors remain in preferences display settings.
 19/// </summary>
 20public sealed class ShellThemeViewModel : ViewModelBase
 21{
 22    private readonly IThemeService _themeService;
 23    private readonly IThemeBaseRegistry _themeBaseRegistry;
 2724    private readonly Suspender _themeSyncSuspender = new();
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="ShellThemeViewModel"/> class.
 28    /// </summary>
 2729    public ShellThemeViewModel(
 2730        IThemeService themeService,
 2731        IThemeBaseRegistry themeBaseRegistry,
 2732        ICommandFactory? commandFactory = null)
 33    {
 2734        _themeService = themeService ?? throw new ArgumentNullException(nameof(themeService));
 2735        _themeBaseRegistry = themeBaseRegistry ?? throw new ArgumentNullException(nameof(themeBaseRegistry));
 36
 2737        var commands = commandFactory.GetOrDefault();
 38        IsDarkCommand = commands.Create(() => IsDark = true);
 39        IsLightCommand = commands.Create(() => IsDark = false);
 40        ChangeThemeCommand = commands.CreateRequired<IThemeBase>(themeBase => _themeService.ApplyBaseTheme(themeBase));
 41
 2742        IsDark = _themeService.CurrentTheme.Base.IsDark;
 43
 2744        _themeService.ThemeChanged += OnThemeServiceThemeChanged;
 45        Disposables.Add(Disposable.Create(() => _themeService.ThemeChanged -= OnThemeServiceThemeChanged));
 2746    }
 47
 048    public IThemeBase CurrentTheme => _themeService.CurrentTheme.Base;
 49
 50    /// <summary>
 51    /// Gets or sets a value indicating whether the dark theme base is active.
 52    /// </summary>
 53    public bool IsDark
 54    {
 55        get;
 56        set
 57        {
 3058            if (_themeSyncSuspender.IsSuspended)
 59            {
 060                SetProperty(ref field, value);
 061                return;
 62            }
 63
 3064            if (!SetProperty(ref field, value))
 2765                return;
 66
 367            _themeService.ApplyBaseTheme(value ? _themeBaseRegistry.Dark : _themeBaseRegistry.Light);
 368        }
 69    }
 70
 71    /// <summary>
 72    /// Gets the command that switches to the dark theme base.
 73    /// </summary>
 74    public ICommand IsDarkCommand { get; }
 75
 76    /// <summary>
 77    /// Gets the command that switches to the light theme base.
 78    /// </summary>
 79    public ICommand IsLightCommand { get; }
 80
 81    /// <summary>
 82    /// Gets the command that toggles between light and dark theme bases.
 83    /// </summary>
 84    public ICommand ChangeThemeCommand { get; }
 85
 86    private void OnThemeServiceThemeChanged(object? sender, ThemeChangedEventArgs e)
 87    {
 088        var isDark = e.CurrentTheme.Base.IsDark;
 089        if (IsDark == isDark)
 090            return;
 91
 092        using (_themeSyncSuspender.Suspend())
 093            IsDark = isDark;
 94
 095        NotifyPropertyChanged(nameof(CurrentTheme));
 096    }
 97}
 98