Browse Source

Initialize lite db after librarydir set

pull/14/head
Ionite 1 year ago
parent
commit
96f539281e
No known key found for this signature in database
  1. 21
      StabilityMatrix/App.xaml.cs
  2. 18
      StabilityMatrix/Database/LiteDbContext.cs
  3. 5
      StabilityMatrix/Helper/ISettingsManager.cs
  4. 24
      StabilityMatrix/Helper/SettingsManager.cs
  5. 8
      StabilityMatrix/Models/Configs/DebugOptions.cs
  6. 5
      StabilityMatrix/SelectInstallLocationsDialog.xaml.cs
  7. 24
      StabilityMatrix/ViewModels/MainWindowViewModel.cs
  8. 4
      StabilityMatrix/appsettings.Development.json

21
StabilityMatrix/App.xaml.cs

@ -17,6 +17,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
@ -225,21 +226,23 @@ namespace StabilityMatrix
client.Credentials = new Credentials(githubApiKey);
return client;
});
serviceCollection.AddMemoryCache();
serviceCollection.AddSingleton<IGithubApiCache, GithubApiCache>();
serviceCollection.AddSingleton<ILiteDbContext>(provider =>
// Database
serviceCollection.AddSingleton<ILiteDbContext, LiteDbContext>(provider =>
{
var liteDbContext = new LiteDbContext();
// Register to initialize the database when LibraryDir is set
var settingsManager = provider.GetRequiredService<ISettingsManager>();
// Setup LiteDb
var connectionString = Config["TempDatabase"] switch
settingsManager.LibraryDirChanged += (_, libraryDir) =>
{
"True" => ":temp:",
_ => settingsManager.DatabasePath
liteDbContext.Initialize(libraryDir);
};
return new LiteDbContext(connectionString);
return liteDbContext;
});
// Caches
serviceCollection.AddMemoryCache();
serviceCollection.AddSingleton<IGithubApiCache, GithubApiCache>();
// Configure Refit and Polly
var defaultRefitSettings = new RefitSettings

18
StabilityMatrix/Database/LiteDbContext.cs

@ -13,7 +13,16 @@ namespace StabilityMatrix.Database;
public class LiteDbContext : ILiteDbContext
{
public LiteDatabaseAsync Database { get; }
private LiteDatabaseAsync? database;
public LiteDatabaseAsync Database
{
get
{
if (database is null) throw new InvalidOperationException("Database not set before access");
return database;
}
private set => database = value;
}
// Notification events
public event EventHandler? CivitModelsChanged;
@ -23,7 +32,10 @@ public class LiteDbContext : ILiteDbContext
public ILiteCollectionAsync<CivitModelVersion> CivitModelVersions => Database.GetCollection<CivitModelVersion>("CivitModelVersions");
public ILiteCollectionAsync<CivitModelQueryCacheEntry> CivitModelQueryCache => Database.GetCollection<CivitModelQueryCacheEntry>("CivitModelQueryCache");
public LiteDbContext(string connectionString)
/// <summary>
/// Loads the database from the specified connection string.
/// </summary>
public void Initialize(string connectionString)
{
Database = new LiteDatabaseAsync(connectionString);
@ -94,7 +106,7 @@ public class LiteDbContext : ILiteDbContext
public void Dispose()
{
Database.Dispose();
database?.Dispose();
GC.SuppressFinalize(this);
}
}

5
StabilityMatrix/Helper/ISettingsManager.cs

@ -8,8 +8,11 @@ namespace StabilityMatrix.Helper;
public interface ISettingsManager
{
Settings Settings { get; }
// Events
event EventHandler<bool>? ModelBrowserNsfwEnabledChanged;
event EventHandler<string>? LibraryDirChanged;
// Library settings
bool IsPortableMode { get; }
string LibraryDir { get; }

24
StabilityMatrix/Helper/SettingsManager.cs

@ -23,9 +23,25 @@ public class SettingsManager : ISettingsManager
private readonly string? originalEnvPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
// Library properties
public bool IsPortableMode { get; set; }
public string LibraryDir { get; set; } = string.Empty;
public bool IsPortableMode { get; private set; }
private string? libraryDir;
public string LibraryDir
{
get
{
if (string.IsNullOrWhiteSpace(libraryDir))
{
throw new InvalidOperationException("LibraryDir is not set");
}
return libraryDir;
}
private set
{
libraryDir = value;
LibraryDirChanged?.Invoke(this, value);
}
}
// Dynamic paths from library
public string DatabasePath => Path.Combine(LibraryDir, "StabilityMatrix.db");
private string SettingsPath => Path.Combine(LibraryDir, "settings.json");
@ -33,7 +49,9 @@ public class SettingsManager : ISettingsManager
public Settings Settings { get; private set; } = new();
// Events
public event EventHandler<bool>? ModelBrowserNsfwEnabledChanged;
public event EventHandler<string>? LibraryDirChanged;
/// <summary>
/// Attempts to locate and set the library path

8
StabilityMatrix/Models/Configs/DebugOptions.cs

@ -2,5 +2,13 @@
public class DebugOptions
{
/// <summary>
/// Sets up LiteDB to use a temporary database file on each run
/// </summary>
public bool TempDatabase { get; set; }
/// <summary>
/// Always show the one-click install page on launch
/// </summary>
public bool ShowOneClickInstall { get; set; }
}

5
StabilityMatrix/SelectInstallLocationsDialog.xaml.cs

@ -7,18 +7,15 @@ namespace StabilityMatrix;
public partial class SelectInstallLocationsDialog : ContentDialog
{
private readonly SelectInstallLocationsViewModel viewModel;
public SelectInstallLocationsDialog(IContentDialogService dialogService, SelectInstallLocationsViewModel viewModel) : base(
dialogService.GetContentPresenter())
{
this.viewModel = viewModel;
InitializeComponent();
DataContext = viewModel;
}
private void ContinueButton_OnClick(object sender, RoutedEventArgs e)
{
Hide();
Hide(ContentDialogResult.Primary);
}
}

24
StabilityMatrix/ViewModels/MainWindowViewModel.cs

@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -13,6 +12,7 @@ using StabilityMatrix.Helper;
using StabilityMatrix.Models.Configs;
using StabilityMatrix.Services;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls.ContentDialogControl;
using Wpf.Ui.Controls.Window;
using EventManager = StabilityMatrix.Helper.EventManager;
@ -25,13 +25,17 @@ public partial class MainWindowViewModel : ObservableObject
private readonly INotificationBarService notificationBarService;
private readonly DebugOptions debugOptions;
public MainWindowViewModel(ISettingsManager settingsManager, IDialogFactory dialogFactory, INotificationBarService notificationBarService, IOptions<DebugOptions> debugOptions)
public MainWindowViewModel(
ISettingsManager settingsManager,
IDialogFactory dialogFactory,
INotificationBarService notificationBarService,
IOptions<DebugOptions> debugOptions)
{
this.settingsManager = settingsManager;
this.dialogFactory = dialogFactory;
this.notificationBarService = notificationBarService;
this.debugOptions = debugOptions.Value;
// Listen to dev mode event
EventManager.Instance.DevModeSettingChanged += (_, value) => IsTextToImagePageEnabled = value;
}
@ -102,13 +106,17 @@ public partial class MainWindowViewModel : ObservableObject
var isExistingUser = File.Exists(settingsPath);
// need to show dialog to choose library location
while (!settingsManager.TryFindLibrary())
if (!settingsManager.TryFindLibrary())
{
var dialog = dialogFactory.CreateInstallLocationsDialog();
dialog.IsPrimaryButtonEnabled = false;
dialog.IsSecondaryButtonEnabled = false;
dialog.IsFooterVisible = false;
await dialog.ShowAsync();
var result = await dialog.ShowAsync();
if (result != ContentDialogResult.Primary)
{
Application.Current.Shutdown();
}
// 1. For portable mode, call settings.SetPortableMode()
var viewModel = (dialog.DataContext as SelectInstallLocationsViewModel)!;
@ -122,6 +130,12 @@ public partial class MainWindowViewModel : ObservableObject
settingsManager.SetLibraryPath(viewModel.DataDirectory);
}
}
// Try to find library again, should be found now
if (!settingsManager.TryFindLibrary())
{
throw new Exception("Could not find library after setting path");
}
}
/// <summary>

4
StabilityMatrix/appsettings.Development.json

@ -1,7 +1,7 @@
{
"GithubApiKey": "",
"TempDatabase": false,
"GithubApiKey": "",
"DebugOptions": {
"TempDatabase": false,
"ShowOneClickInstall": false
}
}

Loading…
Cancel
Save