Browse Source

Merge branch 'auto-update' of https://github.com/ionite34/StabilityMatrix into auto-update

pull/14/head
Ionite 1 year ago
parent
commit
7d8eb31eaa
No known key found for this signature in database
  1. 9
      StabilityMatrix/App.xaml.cs
  2. 3
      StabilityMatrix/Helper/EventManager.cs
  3. 1
      StabilityMatrix/Helper/ISettingsManager.cs
  4. 6
      StabilityMatrix/Helper/IUpdateHelper.cs
  5. 9
      StabilityMatrix/Helper/SettingsManager.cs
  6. 51
      StabilityMatrix/Helper/UpdateHelper.cs
  7. 8
      StabilityMatrix/MainWindow.xaml
  8. 2
      StabilityMatrix/Models/Settings/GlobalSettings.cs
  9. 2
      StabilityMatrix/Models/Settings/LibrarySettings.cs
  10. 7
      StabilityMatrix/Models/Settings/Settings.cs
  11. 7
      StabilityMatrix/Models/Settings/WindowSettings.cs
  12. 111
      StabilityMatrix/UpdateWindow.xaml
  13. 22
      StabilityMatrix/UpdateWindow.xaml.cs
  14. 24
      StabilityMatrix/ViewModels/MainWindowViewModel.cs
  15. 5
      StabilityMatrix/ViewModels/PackageManagerViewModel.cs
  16. 41
      StabilityMatrix/ViewModels/UpdateWindowViewModel.cs

9
StabilityMatrix/App.xaml.cs

@ -198,6 +198,7 @@ namespace StabilityMatrix
serviceCollection.AddSingleton<LaunchViewModel>();
serviceCollection.AddSingleton<PackageManagerViewModel>();
serviceCollection.AddSingleton<TextToImageViewModel>();
serviceCollection.AddTransient<UpdateWindowViewModel>();
serviceCollection.AddTransient<InstallerViewModel>();
serviceCollection.AddTransient<SelectInstallLocationsViewModel>();
serviceCollection.AddTransient<DataDirectoryMigrationViewModel>();
@ -208,6 +209,7 @@ namespace StabilityMatrix
serviceCollection.Configure<DebugOptions>(Config.GetSection(nameof(DebugOptions)));
serviceCollection.AddSingleton<IUpdateHelper, UpdateHelper>();
serviceCollection.AddSingleton<ISettingsManager, SettingsManager>();
serviceCollection.AddSingleton<BasePackage, A3WebUI>();
serviceCollection.AddSingleton<BasePackage, VladAutomatic>();
@ -343,11 +345,8 @@ namespace StabilityMatrix
var window = serviceProvider.GetRequiredService<MainWindow>();
window.Show();
AutoUpdater.Synchronous = true;
AutoUpdater.DownloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update");
AutoUpdater.ExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update", "StabilityMatrix.exe");
// TODO: make this github url?
AutoUpdater.Start("https://update.danksite.xyz/update.xml");
var updateHelper = serviceProvider.GetRequiredService<IUpdateHelper>();
updateHelper.StartCheckingForUpdates();
}
private void App_OnExit(object sender, ExitEventArgs e)

3
StabilityMatrix/Helper/EventManager.cs

@ -1,4 +1,5 @@
using System;
using AutoUpdaterDotNET;
namespace StabilityMatrix.Helper;
@ -17,10 +18,12 @@ public class EventManager
public event EventHandler? OneClickInstallFinished;
public event EventHandler? TeachingTooltipNeeded;
public event EventHandler<bool>? DevModeSettingChanged;
public event EventHandler<UpdateInfoEventArgs>? UpdateAvailable;
public void OnGlobalProgressChanged(int progress) => GlobalProgressChanged?.Invoke(this, progress);
public void RequestPageChange(Type pageType) => PageChangeRequested?.Invoke(this, pageType);
public void OnInstalledPackagesChanged() => InstalledPackagesChanged?.Invoke(this, EventArgs.Empty);
public void OnOneClickInstallFinished() => OneClickInstallFinished?.Invoke(this, EventArgs.Empty);
public void OnTeachingTooltipNeeded() => TeachingTooltipNeeded?.Invoke(this, EventArgs.Empty);
public void OnDevModeSettingChanged(bool value) => DevModeSettingChanged?.Invoke(this, value);
public void OnUpdateAvailable(UpdateInfoEventArgs args) => UpdateAvailable?.Invoke(this, args);
}

1
StabilityMatrix/Helper/ISettingsManager.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using StabilityMatrix.Models;
using StabilityMatrix.Models.Settings;
using Wpf.Ui.Controls.Window;
namespace StabilityMatrix.Helper;

6
StabilityMatrix/Helper/IUpdateHelper.cs

@ -0,0 +1,6 @@
namespace StabilityMatrix.Helper;
public interface IUpdateHelper
{
void StartCheckingForUpdates();
}

9
StabilityMatrix/Helper/SettingsManager.cs

@ -5,8 +5,10 @@ using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using AutoUpdaterDotNET;
using NLog;
using StabilityMatrix.Models;
using StabilityMatrix.Models.Settings;
using StabilityMatrix.Python;
using Wpf.Ui.Controls.Window;
@ -16,9 +18,10 @@ public class SettingsManager : ISettingsManager
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly ReaderWriterLockSlim FileLock = new();
private static readonly string GlobalSettingsPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix",
"global.json");
private static readonly string GlobalSettingsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix",
"global.json");
private readonly string? originalEnvPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

51
StabilityMatrix/Helper/UpdateHelper.cs

@ -0,0 +1,51 @@
using System;
using System.IO;
using System.Windows.Threading;
using AutoUpdaterDotNET;
using Microsoft.Extensions.Logging;
namespace StabilityMatrix.Helper;
public class UpdateHelper : IUpdateHelper
{
private readonly ILogger<UpdateHelper> logger;
private readonly DispatcherTimer timer = new();
public UpdateHelper(ILogger<UpdateHelper> logger)
{
this.logger = logger;
timer.Interval = TimeSpan.FromMinutes(5);
timer.Tick += (_, _) =>
{
CheckForUpdate();
};
AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
}
public void StartCheckingForUpdates()
{
timer.IsEnabled = true;
timer.Start();
CheckForUpdate();
}
private void CheckForUpdate()
{
AutoUpdater.DownloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update");
AutoUpdater.ExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update", "StabilityMatrix.exe");
// TODO: make this github url?
AutoUpdater.Start("https://update.danksite.xyz/update.xml");
}
private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args.Error == null && args.IsUpdateAvailable)
{
EventManager.Instance.OnUpdateAvailable(args);
}
else
{
logger.LogError(args.Error, "Error while checking for update");
}
}
}

8
StabilityMatrix/MainWindow.xaml

@ -100,6 +100,14 @@
</ui:NavigationView.MenuItems>
<ui:NavigationView.FooterMenuItems>
<!-- Footer menus -->
<ui:NavigationViewItem Content="Update Available"
Visibility="{Binding IsUpdateAvailable, Converter={StaticResource BooleanToVisibilityConverter}}"
Command="{Binding DoUpdateCommand}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="ArrowDownload24"
Foreground="LimeGreen" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem Command="{Binding OpenLinkPatreonCommand}" Content="Become a Patron">
<ui:NavigationViewItem.Icon>
<ui:ImageIcon

2
StabilityMatrix/Models/GlobalSettings.cs → StabilityMatrix/Models/Settings/GlobalSettings.cs

@ -1,4 +1,4 @@
namespace StabilityMatrix.Models;
namespace StabilityMatrix.Models.Settings;
public class GlobalSettings
{

2
StabilityMatrix/Models/LibrarySettings.cs → StabilityMatrix/Models/Settings/LibrarySettings.cs

@ -1,4 +1,4 @@
namespace StabilityMatrix.Models;
namespace StabilityMatrix.Models.Settings;
public class LibrarySettings
{

7
StabilityMatrix/Models/Settings.cs → StabilityMatrix/Models/Settings/Settings.cs

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Wpf.Ui.Controls.Window;
namespace StabilityMatrix.Models;
namespace StabilityMatrix.Models.Settings;
public class Settings
{
@ -30,6 +27,8 @@ public class Settings
SharedFolderType.Lora |
SharedFolderType.LyCORIS;
public WindowSettings? WindowSettings { get; set; }
public InstalledPackage? GetActiveInstalledPackage()
{
return InstalledPackages.FirstOrDefault(x => x.Id == ActiveInstalledPackage);

7
StabilityMatrix/Models/Settings/WindowSettings.cs

@ -0,0 +1,7 @@
namespace StabilityMatrix.Models.Settings;
public class WindowSettings
{
public double Width { get; set; }
public double Height { get; set; }
}

111
StabilityMatrix/UpdateWindow.xaml

@ -0,0 +1,111 @@
<ui:FluentWindow
ExtendsContentIntoTitleBar="True"
Height="700"
Icon="pack://application:,,,/Assets/Icon.ico"
Loaded="UpdateWindow_OnLoaded"
Title="Stability Matrix - Update"
Width="700"
WindowBackdropType="{Binding WindowBackdropType}"
WindowStartupLocation="CenterOwner"
d:DataContext="{d:DesignInstance Type=viewModels:UpdateWindowViewModel,
IsDesignTimeCreatable=True}"
d:DesignHeight="700"
d:DesignWidth="700"
mc:Ignorable="d"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
x:Class="StabilityMatrix.UpdateWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:converters="clr-namespace:StabilityMatrix.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xaml="clr-namespace:MdXaml;assembly=MdXaml">
<ui:FluentWindow.Resources>
<converters:ValueConverterGroup x:Key="InvertAndVisibilitate">
<converters:BoolNegationConverter />
<BooleanToVisibilityConverter />
</converters:ValueConverterGroup>
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
<converters:UriToBitmapConverter x:Key="UriToBitmapConverter" />
<xaml:Markdown
AssetPathRoot="{x:Static system:Environment.CurrentDirectory}"
DocumentStyle="{StaticResource DocumentStyle}"
Heading1Style="{StaticResource H1Style}"
Heading2Style="{StaticResource H2Style}"
Heading3Style="{StaticResource H3Style}"
Heading4Style="{StaticResource H4Style}"
ImageStyle="{StaticResource ImageStyle}"
LinkStyle="{StaticResource LinkStyle}"
SeparatorStyle="{StaticResource SeparatorStyle}"
x:Key="Markdown" />
<xaml:TextToFlowDocumentConverter Markdown="{StaticResource Markdown}" x:Key="TextToFlowDocumentConverter" />
</ui:FluentWindow.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ui:TitleBar Background="{ui:ThemeResource ApplicationBackgroundBrush}">
<ui:TitleBar.Header>
<TextBlock Margin="16,8" Text="Stability Matrix - Update Available" />
</ui:TitleBar.Header>
</ui:TitleBar>
<TextBlock Grid.Row="1"
Text="A new version of Stability Matrix is available!"
HorizontalAlignment="Center"
FontWeight="Thin"
Margin="0,16,0,0"
FontSize="28"/>
<TextBlock Grid.Row="2"
HorizontalAlignment="Center"
FontSize="18"
TextWrapping="Wrap"
TextAlignment="Center"
Margin="16,32,16,0">
<Run Text="Stability Matrix"/>
<Run Text="v"/><Run Text="{Binding UpdateInfo.CurrentVersion, FallbackValue=0.0.0.0}"/>
<Run Text="is now available. You have version"/>
<Run Text="v"/><Run Text="{Binding UpdateInfo.InstalledVersion, FallbackValue=0.0.0.0}"/>
<Run Text="installed. Would you like to download it now?"/>
</TextBlock>
<Border Grid.Row="3"
Margin="32, 16"
CornerRadius="16"
Background="#66000000"/>
<FlowDocumentScrollViewer
Grid.Row="3"
Margin="32,16"
Document="{Binding ReleaseNotes, Converter={StaticResource TextToFlowDocumentConverter}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16">
<ui:Button Content="Remind Me Later"
Margin="0,0,8,0"
FontSize="18"
Appearance="Info"/>
<ui:Button Content="Install Now"
Margin="8,0,0,0"
FontSize="18"
Appearance="Success"/>
</StackPanel>
</Grid>
</ui:FluentWindow>

22
StabilityMatrix/UpdateWindow.xaml.cs

@ -0,0 +1,22 @@
using System.Windows;
using StabilityMatrix.ViewModels;
using Wpf.Ui.Controls.Window;
namespace StabilityMatrix;
public partial class UpdateWindow : FluentWindow
{
private readonly UpdateWindowViewModel viewModel;
public UpdateWindow(UpdateWindowViewModel viewModel)
{
this.viewModel = viewModel;
InitializeComponent();
DataContext = viewModel;
}
private async void UpdateWindow_OnLoaded(object sender, RoutedEventArgs e)
{
await viewModel.OnLoaded();
}
}

24
StabilityMatrix/ViewModels/MainWindowViewModel.cs

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Shell;
using AutoUpdaterDotNET;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Options;
@ -23,17 +24,22 @@ public partial class MainWindowViewModel : ObservableObject
private readonly ISettingsManager settingsManager;
private readonly IDialogFactory dialogFactory;
private readonly INotificationBarService notificationBarService;
private readonly UpdateWindowViewModel updateWindowViewModel;
private readonly DebugOptions debugOptions;
private UpdateInfoEventArgs? updateInfo;
public MainWindowViewModel(
ISettingsManager settingsManager,
IDialogFactory dialogFactory,
INotificationBarService notificationBarService,
INotificationBarService notificationBarService,
UpdateWindowViewModel updateWindowViewModel,
IOptions<DebugOptions> debugOptions)
{
this.settingsManager = settingsManager;
this.dialogFactory = dialogFactory;
this.notificationBarService = notificationBarService;
this.updateWindowViewModel = updateWindowViewModel;
this.debugOptions = debugOptions.Value;
// Listen to dev mode event
@ -52,10 +58,18 @@ public partial class MainWindowViewModel : ObservableObject
[ObservableProperty]
private bool isTextToImagePageEnabled;
[ObservableProperty]
private bool isUpdateAvailable;
public async Task OnLoaded()
{
SetTheme();
EventManager.Instance.GlobalProgressChanged += OnGlobalProgressChanged;
EventManager.Instance.UpdateAvailable += (_, args) =>
{
IsUpdateAvailable = true;
updateInfo = args;
};
// show path selection window if no paths are set
await DoSettingsCheck();
@ -93,6 +107,14 @@ public partial class MainWindowViewModel : ObservableObject
{
ProcessRunner.OpenUrl("https://discord.gg/TUrgfECxHz");
}
[RelayCommand]
private void DoUpdate()
{
updateWindowViewModel.UpdateInfo = updateInfo;
var updateWindow = new UpdateWindow(updateWindowViewModel);
updateWindow.ShowDialog();
}
private async Task DoSettingsCheck()
{

5
StabilityMatrix/ViewModels/PackageManagerViewModel.cs

@ -157,7 +157,7 @@ public partial class PackageManagerViewModel : ObservableObject
[RelayCommand]
private async Task Uninstall()
{
if (SelectedPackage?.Path == null)
if (SelectedPackage?.LibraryPath == null)
{
logger.LogError("No package selected to uninstall");
return;
@ -174,7 +174,8 @@ public partial class PackageManagerViewModel : ObservableObject
{
IsUninstalling = true;
InstallButtonEnabled = false;
var deleteTask = DeleteDirectoryAsync(SelectedPackage.Path);
var deleteTask = DeleteDirectoryAsync(Path.Combine(settingsManager.LibraryDir,
SelectedPackage.LibraryPath));
var taskResult = await snackbarService.TryAsync(deleteTask,
"Some files could not be deleted. Please close any open files in the package directory and try again.");
if (taskResult.IsSuccessful)

41
StabilityMatrix/ViewModels/UpdateWindowViewModel.cs

@ -0,0 +1,41 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using AutoUpdaterDotNET;
using CommunityToolkit.Mvvm.ComponentModel;
using StabilityMatrix.Helper;
using Wpf.Ui.Controls.Window;
namespace StabilityMatrix.ViewModels;
public partial class UpdateWindowViewModel : ObservableObject
{
private readonly ISettingsManager settingsManager;
private readonly IHttpClientFactory httpClientFactory;
public UpdateWindowViewModel(ISettingsManager settingsManager, IHttpClientFactory httpClientFactory)
{
this.settingsManager = settingsManager;
this.httpClientFactory = httpClientFactory;
}
[ObservableProperty] private string? releaseNotes;
public UpdateInfoEventArgs? UpdateInfo { get; set; }
public WindowBackdropType WindowBackdropType => settingsManager.Settings.WindowBackdropType ??
WindowBackdropType.Mica;
public async Task OnLoaded()
{
using var client = httpClientFactory.CreateClient();
var response = await client.GetAsync(UpdateInfo?.ChangelogURL);
if (response.IsSuccessStatusCode)
{
ReleaseNotes = await response.Content.ReadAsStringAsync();
}
else
{
ReleaseNotes = "## Unable to load release notes";
}
}
}
Loading…
Cancel
Save