Skip to content

Commit

Permalink
Expose the start minimized setting in the settings button
Browse files Browse the repository at this point in the history
  • Loading branch information
FaithBeam committed Sep 11, 2024
1 parent 3bbf602 commit 085d8c7
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.ProfilesList;
using YMouseButtonControl.Core.ViewModels.ProfilesList.Features.Add;
using YMouseButtonControl.DataAccess.LiteDb;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using System;
using ReactiveUI;

namespace YMouseButtonControl.Core.DataAccess.Models.Implementations;

public class Setting : IEquatable<Setting>
public class Setting : ReactiveObject, IEquatable<Setting>
{
private string? _value;

public int Id { get; set; }
public required string Name { get; set; }
public string? Value { get; set; }

public string? Value
{
get => _value;
set => this.RaiseAndSetIfChanged(ref _value, value);
}

public bool Equals(Setting? other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace YMouseButtonControl.Core.DataAccess.Repositories;
public interface IRepository<T>
{
T GetById(string id);
T GetById(int id);
IEnumerable<T> GetAll();
void Add(T entity);
void Update(string id, T entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Linq;
using System.Xml.Linq;
using DynamicData;
using ReactiveUI;
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.DataAccess.UnitOfWork;
using YMouseButtonControl.Core.Profiles.Interfaces;

namespace YMouseButtonControl.Core.Profiles.Implementations;

Expand Down Expand Up @@ -33,7 +35,19 @@ public bool IsUnsavedChanges()

public Setting? GetSetting(string name)
{
return _settings.Items.FirstOrDefault(x => x.Name == name);
using var unitOfWork = _unitOfWorkFactory.Create();
var repository = unitOfWork.GetRepository<Setting>();
return repository.GetAll().ToList().FirstOrDefault(x => x.Name == name);
}

public Setting UpdateSetting(int id, string value)
{
using var unitOfWork = _unitOfWorkFactory.Create();
var repository = unitOfWork.GetRepository<Setting>();
var dbSetting = repository.GetById(id);
dbSetting.Value = value;
repository.ApplyAction([dbSetting]);
return dbSetting;
}

private void LoadSettingsFromDb()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using DynamicData;
using YMouseButtonControl.Core.DataAccess.Models.Implementations;

namespace YMouseButtonControl.Core.Profiles.Implementations;
namespace YMouseButtonControl.Core.Profiles.Interfaces;

public interface ISettingsService
{
IObservable<IChangeSet<Setting, int>> Connect();
bool IsUnsavedChanges();
Setting? GetSetting(string name);
Setting UpdateSetting(int id, string value);
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
using System;
using System.Reactive;
using System.Reactive.Linq;
using ReactiveUI;
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.Profiles.Implementations;
using YMouseButtonControl.Core.Profiles.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;

namespace YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;

public class GlobalSettingsDialogViewModel : DialogBase, IGlobalSettingsDialogViewModel
{
private readonly ISettingsService _settingsService;
private Setting _startMinimized;
private readonly ObservableAsPropertyHelper<bool>? _applyIsExec;

public GlobalSettingsDialogViewModel(ISettingsService settingsService)
{
_settingsService = settingsService;

_startMinimized =
_settingsService.GetSetting("StartMinimized")
settingsService.GetSetting("StartMinimized")
?? throw new Exception($"Error retrieving StartMinimized setting");

var startMinimizedChanged = this.WhenAnyValue(
x => x.StartMinimized.Value,
selector: val =>
{
var curVal = settingsService.GetSetting("StartMinimized");
if (curVal is null)
{
return true;
}

return curVal.Value != val;
}
);

var applyIsExecObs = this.WhenAnyValue(x => x.AppIsExec);
var canSave = startMinimizedChanged.Merge(applyIsExecObs);
ApplyCommand = ReactiveCommand.Create(
() =>
{
settingsService.UpdateSetting(StartMinimized.Id, StartMinimized.Value!);
},
canSave
);
_applyIsExec = ApplyCommand.IsExecuting.ToProperty(this, x => x.AppIsExec);
}

public Setting StartMinimized
{
get => _startMinimized;
set => this.RaiseAndSetIfChanged(ref _startMinimized, value);
}

public ReactiveCommand<Unit, Unit> ApplyCommand { get; init; }

public bool AppIsExec => _applyIsExec?.Value ?? false;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using YMouseButtonControl.Core.DataAccess.Models.Implementations;

namespace YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;
namespace YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;

public interface IGlobalSettingsDialogViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Reactive;
using ReactiveUI;
using YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;

namespace YMouseButtonControl.Core.ViewModels.MainWindow;

Expand All @@ -12,6 +14,6 @@ public interface IMainWindowViewModel
ReactiveCommand<Unit, Unit> ApplyCommand { get; }
ReactiveCommand<Unit, Unit> CloseCommand { get; }
ReactiveCommand<Unit, Unit> SettingsCommand { get; }
Interaction<Unit, Unit> ShowSettingsDialogInteraction { get; }
Interaction<IGlobalSettingsDialogViewModel, Unit> ShowSettingsDialogInteraction { get; }
string? ProfileName { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.Profiles.Interfaces;
using YMouseButtonControl.Core.ViewModels.Implementations;
using YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.MainWindow.Features.Apply;

namespace YMouseButtonControl.Core.ViewModels.MainWindow;
Expand All @@ -20,6 +22,7 @@ public class MainWindowViewModel : ViewModelBase, IMainWindowViewModel

private readonly IProfilesService _ps;
private readonly IProfilesListViewModel _profilesListViewModel;
private readonly IGlobalSettingsDialogViewModel _globalSettingsDialogViewModel;
private string? _profileName;

#endregion
Expand All @@ -31,15 +34,17 @@ public MainWindowViewModel(
ILayerViewModel layerViewModel,
IProfilesListViewModel profilesListViewModel,
IProfilesInformationViewModel profilesInformationViewModel,
IGlobalSettingsDialogViewModel globalSettingsDialogViewModel,
IApply apply
)
{
_profilesListViewModel = profilesListViewModel;
_globalSettingsDialogViewModel = globalSettingsDialogViewModel;
_ps = ps;
LayerViewModel = layerViewModel;
ProfilesInformationViewModel = profilesInformationViewModel;
SettingsCommand = ReactiveCommand.CreateFromTask(ShowSettingsDialogAsync);
ShowSettingsDialogInteraction = new Interaction<Unit, Unit>();
ShowSettingsDialogInteraction = new Interaction<IGlobalSettingsDialogViewModel, Unit>();
CloseCommand = ReactiveCommand.Create(() =>
{
if (
Expand Down Expand Up @@ -75,7 +80,7 @@ is IClassicDesktopStyleApplicationLifetime lifetime
public ReactiveCommand<Unit, Unit> CloseCommand { get; }
public ReactiveCommand<Unit, Unit> SettingsCommand { get; }

public Interaction<Unit, Unit> ShowSettingsDialogInteraction { get; }
public Interaction<IGlobalSettingsDialogViewModel, Unit> ShowSettingsDialogInteraction { get; }

public string? ProfileName
{
Expand All @@ -87,7 +92,7 @@ public string? ProfileName

private async Task ShowSettingsDialogAsync()
{
await ShowSettingsDialogInteraction.Handle(Unit.Default);
await ShowSettingsDialogInteraction.Handle(_globalSettingsDialogViewModel);
}

private void OnProfileChanged(Profile profile) => ProfileName = profile.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using ReactiveUI;
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.Profiles.Interfaces;
using YMouseButtonControl.Core.ViewModels.Implementations;
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.ProfilesList.Features.Add;

namespace YMouseButtonControl.Core.ViewModels.Implementations;
namespace YMouseButtonControl.Core.ViewModels.ProfilesList;

public class ProfilesListViewModel : ViewModelBase, IProfilesListViewModel
{
Expand Down
2 changes: 2 additions & 0 deletions YMouseButtonControl.DataAccess.LiteDb/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Repository<T>(ILiteCollection<T> collection) : IRepository<T>
{
public T GetById(string id) => collection.FindById(id);

public T GetById(int id) => collection.FindById(id);

public IEnumerable<T> GetAll() => collection.FindAll();

public void Add(T entity) => collection.Insert(entity);
Expand Down
1 change: 1 addition & 0 deletions YMouseButtonControl/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using YMouseButtonControl.Configuration;
using YMouseButtonControl.Core;
using YMouseButtonControl.Core.Profiles.Implementations;
using YMouseButtonControl.Core.Profiles.Interfaces;
using YMouseButtonControl.Core.Services.BackgroundTasks;
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.MainWindow;
Expand Down
32 changes: 32 additions & 0 deletions YMouseButtonControl/Converters/SettingValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;

namespace YMouseButtonControl.Converters;

public class SettingValueConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not string)
{
return null;
}

if (value is not string valStr)
{
return null;
}
return bool.Parse(valStr);
}

public object? ConvertBack(
object? value,
Type targetType,
object? parameter,
CultureInfo culture
)
{
return value?.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using YMouseButtonControl.Core.ViewModels.Interfaces;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.MainWindow;
using YMouseButtonControl.Core.ViewModels.ProfilesList;
using YMouseButtonControl.Core.ViewModels.Services;

namespace YMouseButtonControl.DependencyInjection;
Expand Down
21 changes: 16 additions & 5 deletions YMouseButtonControl/Views/Dialogs/GlobalSettingsDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dialogs="clr-namespace:YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;assembly=YMouseButtonControl.Core"
xmlns:converters="clr-namespace:YMouseButtonControl.Converters"
WindowStartupLocation="CenterOwner"
Width="800"
Height="600"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="YMouseButtonControl.Views.Dialogs.GlobalSettingsDialog"
Title="Global Settings">
<Design.DataContext>
<dialogs:GlobalSettingsDialogViewModel />
</Design.DataContext>
<CheckBox Content="Start Minimized" IsChecked="{Binding StartMinimized.Value}" />
</Window>
<Design.DataContext>
<dialogs:GlobalSettingsDialogViewModel />
</Design.DataContext>

<Window.Resources>
<converters:SettingValueConverter x:Key="SettingValueConverter" />
</Window.Resources>

<StackPanel>
<CheckBox Content="Start Minimized"
IsChecked="{Binding StartMinimized.Value, Converter={StaticResource SettingValueConverter}}" />
<Button Content="Apply" Command="{Binding ApplyCommand}" />
<Button Content="Cancel" Click="Cancel_OnClick"></Button>
</StackPanel>
</Window>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace YMouseButtonControl.Views.Dialogs;
Expand All @@ -10,4 +11,9 @@ public GlobalSettingsDialog()
{
InitializeComponent();
}

private void Cancel_OnClick(object? sender, RoutedEventArgs e)
{
Close();
}
}
5 changes: 4 additions & 1 deletion YMouseButtonControl/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.ReactiveUI;
using ReactiveUI;
using YMouseButtonControl.Core.ViewModels.Implementations.Dialogs;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.MainWindow;
using YMouseButtonControl.Core.Views;
using YMouseButtonControl.Views.Dialogs;
Expand Down Expand Up @@ -33,7 +34,9 @@ public MainWindow()
#endif
}

private async Task ShowGlobalSettingsDialog(IInteractionContext<Unit, Unit> context)
private async Task ShowGlobalSettingsDialog(
IInteractionContext<IGlobalSettingsDialogViewModel, Unit> context
)
{
var dialog = new GlobalSettingsDialog { DataContext = context.Input };
await dialog.ShowDialog<IGlobalSettingsDialogViewModel?>(
Expand Down
3 changes: 2 additions & 1 deletion YMouseButtonControl/Views/ProfilesListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:implementations="clr-namespace:YMouseButtonControl.Core.ViewModels.Implementations;assembly=YMouseButtonControl.Core"
xmlns:models="clr-namespace:YMouseButtonControl.Core.DataAccess.Models.Implementations;assembly=YMouseButtonControl.Core"
xmlns:profilesList="clr-namespace:YMouseButtonControl.Core.ViewModels.ProfilesList;assembly=YMouseButtonControl.Core"
mc:Ignorable="d"
d:DesignWidth="800"
d:DesignHeight="450"
Expand All @@ -12,7 +13,7 @@
x:Class="YMouseButtonControl.Views.ProfilesListView">

<Design.DataContext>
<implementations:ProfilesListViewModel />
<profilesList:ProfilesListViewModel />
</Design.DataContext>

<Grid RowDefinitions="*,Auto">
Expand Down
1 change: 1 addition & 0 deletions YMouseButtonControl/Views/ProfilesListView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.ViewModels.Implementations;
using YMouseButtonControl.Core.ViewModels.Interfaces.Dialogs;
using YMouseButtonControl.Core.ViewModels.ProfilesList;
using YMouseButtonControl.Views.Dialogs;

namespace YMouseButtonControl.Views;
Expand Down

0 comments on commit 085d8c7

Please sign in to comment.