From 9e048751ae0833254b4de2a152f84b5f6edeece6 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 24 Jul 2024 11:44:13 +0800 Subject: [PATCH] refactor: move non-observable object from `ViewModels` to `Models` * ViewModels.MergeMode -> Models.MergeMode * ViewModels.Notification -> Models.Notification * ViewModels.ResetMode -> Models.ResetMode * use `int` instead of `ViewModels.CountSelectedCommits` --- src/Models/MergeMode.cs | 24 +++++++++++++++ src/{ViewModels => Models}/Notification.cs | 2 +- src/Models/ResetMode.cs | 27 ++++++++++++++++ src/ViewModels/Histories.cs | 7 +---- src/ViewModels/Launcher.cs | 4 +-- src/ViewModels/LauncherPage.cs | 4 +-- src/ViewModels/Merge.cs | 33 ++------------------ src/ViewModels/Reset.cs | 36 ++-------------------- src/Views/Histories.axaml | 4 +-- src/Views/LauncherPage.axaml | 3 +- src/Views/LauncherPage.axaml.cs | 2 +- src/Views/Merge.axaml | 5 +-- src/Views/Reset.axaml | 5 +-- 13 files changed, 74 insertions(+), 82 deletions(-) create mode 100644 src/Models/MergeMode.cs rename src/{ViewModels => Models}/Notification.cs (87%) create mode 100644 src/Models/ResetMode.cs diff --git a/src/Models/MergeMode.cs b/src/Models/MergeMode.cs new file mode 100644 index 00000000..23ace5c5 --- /dev/null +++ b/src/Models/MergeMode.cs @@ -0,0 +1,24 @@ +namespace SourceGit.Models +{ + public class MergeMode + { + public static readonly MergeMode[] Supported = + [ + new MergeMode("Default", "Fast-forward if possible", ""), + new MergeMode("No Fast-forward", "Always create a merge commit", "--no-ff"), + new MergeMode("Squash", "Use '--squash'", "--squash"), + new MergeMode("Don't commit", "Merge without commit", "--no-commit"), + ]; + + public string Name { get; set; } + public string Desc { get; set; } + public string Arg { get; set; } + + public MergeMode(string n, string d, string a) + { + Name = n; + Desc = d; + Arg = a; + } + } +} diff --git a/src/ViewModels/Notification.cs b/src/Models/Notification.cs similarity index 87% rename from src/ViewModels/Notification.cs rename to src/Models/Notification.cs index e54c3790..2261e327 100644 --- a/src/ViewModels/Notification.cs +++ b/src/Models/Notification.cs @@ -1,4 +1,4 @@ -namespace SourceGit.ViewModels +namespace SourceGit.Models { public class Notification { diff --git a/src/Models/ResetMode.cs b/src/Models/ResetMode.cs new file mode 100644 index 00000000..bf608149 --- /dev/null +++ b/src/Models/ResetMode.cs @@ -0,0 +1,27 @@ +using Avalonia.Media; + +namespace SourceGit.Models +{ + public class ResetMode + { + public static readonly ResetMode[] Supported = + [ + new ResetMode("Soft", "Keep all changes. Stage differences", "--soft", Brushes.Green), + new ResetMode("Mixed", "Keep all changes. Unstage differences", "--mixed", Brushes.Orange), + new ResetMode("Hard", "Discard all changes", "--hard", Brushes.Red), + ]; + + public string Name { get; set; } + public string Desc { get; set; } + public string Arg { get; set; } + public IBrush Color { get; set; } + + public ResetMode(string n, string d, string a, IBrush b) + { + Name = n; + Desc = d; + Arg = a; + Color = b; + } + } +} diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index e66f1415..59c03324 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -9,11 +9,6 @@ using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels { - public class CountSelectedCommits - { - public int Count { get; set; } - } - public class Histories : ObservableObject { public bool IsLoading @@ -143,7 +138,7 @@ namespace SourceGit.ViewModels else { _repo.SearchResultSelectedCommit = null; - DetailContext = new CountSelectedCommits() { Count = commits.Count }; + DetailContext = commits.Count; } } diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 19c328c4..71584098 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -38,7 +38,7 @@ namespace SourceGit.ViewModels var root = new Commands.QueryRepositoryRootPath(startupRepo).Result(); if (string.IsNullOrEmpty(root)) { - Pages[0].Notifications.Add(new Notification + Pages[0].Notifications.Add(new Models.Notification { IsError = true, Message = $"Given path: '{startupRepo}' is NOT a valid repository!" @@ -272,7 +272,7 @@ namespace SourceGit.ViewModels public void DispatchNotification(string pageId, string message, bool isError) { - var notification = new Notification() + var notification = new Models.Notification() { IsError = isError, Message = message, diff --git a/src/ViewModels/LauncherPage.cs b/src/ViewModels/LauncherPage.cs index 27d84d10..65d5f7a5 100644 --- a/src/ViewModels/LauncherPage.cs +++ b/src/ViewModels/LauncherPage.cs @@ -18,11 +18,11 @@ namespace SourceGit.ViewModels set => SetProperty(ref _data, value); } - public AvaloniaList Notifications + public AvaloniaList Notifications { get; set; - } = new AvaloniaList(); + } = new AvaloniaList(); public LauncherPage() { diff --git a/src/ViewModels/Merge.cs b/src/ViewModels/Merge.cs index 37b537fc..48af8722 100644 --- a/src/ViewModels/Merge.cs +++ b/src/ViewModels/Merge.cs @@ -1,22 +1,7 @@ -using System.Collections.Generic; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace SourceGit.ViewModels { - public class MergeMode - { - public string Name { get; set; } - public string Desc { get; set; } - public string Arg { get; set; } - - public MergeMode(string n, string d, string a) - { - Name = n; - Desc = d; - Arg = a; - } - } - public class Merge : Popup { public string Source @@ -31,13 +16,7 @@ namespace SourceGit.ViewModels private set; } - public List Modes - { - get; - private set; - } - - public MergeMode SelectedMode + public Models.MergeMode SelectedMode { get; set; @@ -48,13 +27,7 @@ namespace SourceGit.ViewModels _repo = repo; Source = source; Into = into; - Modes = new List() { - new MergeMode("Default", "Fast-forward if possible", ""), - new MergeMode("No Fast-forward", "Always create a merge commit", "--no-ff"), - new MergeMode("Squash", "Use '--squash'", "--squash"), - new MergeMode("Don't commit", "Merge without commit", "--no-commit"), - }; - SelectedMode = Modes[0]; + SelectedMode = Models.MergeMode.Supported[0]; View = new Views.Merge() { DataContext = this }; } diff --git a/src/ViewModels/Reset.cs b/src/ViewModels/Reset.cs index 30b89137..ba3a3794 100644 --- a/src/ViewModels/Reset.cs +++ b/src/ViewModels/Reset.cs @@ -1,26 +1,7 @@ -using System.Collections.Generic; -using System.Threading.Tasks; - -using Avalonia.Media; +using System.Threading.Tasks; namespace SourceGit.ViewModels { - public class ResetMode - { - public string Name { get; set; } - public string Desc { get; set; } - public string Arg { get; set; } - public IBrush Color { get; set; } - - public ResetMode(string n, string d, string a, IBrush b) - { - Name = n; - Desc = d; - Arg = a; - Color = b; - } - } - public class Reset : Popup { public Models.Branch Current @@ -35,13 +16,7 @@ namespace SourceGit.ViewModels private set; } - public List Modes - { - get; - private set; - } - - public ResetMode SelectedMode + public Models.ResetMode SelectedMode { get; set; @@ -52,12 +27,7 @@ namespace SourceGit.ViewModels _repo = repo; Current = current; To = to; - Modes = new List() { - new ResetMode("Soft", "Keep all changes. Stage differences", "--soft", Brushes.Green), - new ResetMode("Mixed", "Keep all changes. Unstage differences", "--mixed", Brushes.Orange), - new ResetMode("Hard", "Discard all changes", "--hard", Brushes.Red), - }; - SelectedMode = Modes[0]; + SelectedMode = Models.ResetMode.Supported[0]; View = new Views.Reset() { DataContext = this }; } diff --git a/src/Views/Histories.axaml b/src/Views/Histories.axaml index 3f160d60..74818238 100644 --- a/src/Views/Histories.axaml +++ b/src/Views/Histories.axaml @@ -215,7 +215,7 @@ - + + Text="{Binding Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/> diff --git a/src/Views/LauncherPage.axaml b/src/Views/LauncherPage.axaml index f4cc3f8d..dda5b7ef 100644 --- a/src/Views/LauncherPage.axaml +++ b/src/Views/LauncherPage.axaml @@ -2,6 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:m="using:SourceGit.Models" xmlns:v="using:SourceGit.Views" xmlns:vm="using:SourceGit.ViewModels" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" @@ -86,7 +87,7 @@ - + diff --git a/src/Views/LauncherPage.axaml.cs b/src/Views/LauncherPage.axaml.cs index 17331ec0..44a5351b 100644 --- a/src/Views/LauncherPage.axaml.cs +++ b/src/Views/LauncherPage.axaml.cs @@ -34,7 +34,7 @@ namespace SourceGit.Views private void OnDismissNotification(object sender, RoutedEventArgs e) { - if (sender is Button { DataContext: ViewModels.Notification notice } && + if (sender is Button { DataContext: Models.Notification notice } && DataContext is ViewModels.LauncherPage page) page.Notifications.Remove(notice); diff --git a/src/Views/Merge.axaml b/src/Views/Merge.axaml index 8ab08c2a..b63bfc04 100644 --- a/src/Views/Merge.axaml +++ b/src/Views/Merge.axaml @@ -2,6 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:m="using:SourceGit.Models" xmlns:vm="using:SourceGit.ViewModels" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.Merge" @@ -36,10 +37,10 @@ - + diff --git a/src/Views/Reset.axaml b/src/Views/Reset.axaml index 238a7849..3c9a78f8 100644 --- a/src/Views/Reset.axaml +++ b/src/Views/Reset.axaml @@ -2,6 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:m="using:SourceGit.Models" xmlns:vm="using:SourceGit.ViewModels" xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" @@ -38,10 +39,10 @@ - +