Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMysteriousStranger90 committed Dec 26, 2024
1 parent 8f853da commit b4e01d4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
78 changes: 60 additions & 18 deletions WhisperFTPApp/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using WhisperFTPApp.Data;
using WhisperFTPApp.Extensions;
Expand All @@ -19,11 +21,10 @@ public override void Initialize()
{
AvaloniaXamlLoader.Load(this);

var localization = LocalizationService.Instance;
try
{
StaticFileLogger.LogInformation("Setting initial language...");
localization.SetLanguage("en");
LocalizationService.Instance.SetLanguage("en");
StaticFileLogger.LogInformation("Language set successfully");
}
catch (Exception ex)
Expand All @@ -34,25 +35,66 @@ public override void Initialize()

public override void OnFrameworkInitializationCompleted()
{
var collection = new ServiceCollection();
collection.AddCommonServices();
collection.AddCommonViewModels();
collection.AddCommonWindows();

var serviceProvider = collection.BuildServiceProvider();
var mainWindow = serviceProvider.GetRequiredService<MainWindow>();

using (var scope = serviceProvider.CreateScope())
try
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
context.Database.EnsureCreated();
}
BindingPlugins.DataValidators.RemoveAt(0);

var collection = new ServiceCollection();

// Configure SQLite with specific options
collection.AddDbContext<AppDbContext>(
options =>
{
options.UseSqlite("Data Source=DatabaseWhisperFTPApp.db",
sqliteOptions => { sqliteOptions.CommandTimeout(30); });
}, ServiceLifetime.Singleton);

collection.AddCommonServices();
collection.AddCommonViewModels();
collection.AddCommonWindows();

var serviceProvider = collection.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
try
{
StaticFileLogger.LogInformation("Initializing database...");
context.Database.EnsureCreated();

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
// Force connection close
context.Database.GetDbConnection().Close();

StaticFileLogger.LogInformation("Database initialized successfully");
}
catch (Exception dbEx)
{
StaticFileLogger.LogError($"Database initialization failed: {dbEx}");
throw;
}
}

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = serviceProvider.GetRequiredService<MainWindow>();
desktop.ShutdownRequested += (s, e) =>
{
// Cleanup on shutdown
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
context.Database.GetDbConnection().Close();
};

StaticFileLogger.LogInformation("Application initialized successfully");
}

base.OnFrameworkInitializationCompleted();
}
catch (Exception ex)
{
desktop.MainWindow = mainWindow;
StaticFileLogger.LogError($"Application startup failed: {ex.Message}\nStackTrace: {ex.StackTrace}");
throw;
}

base.OnFrameworkInitializationCompleted();
}
}
Binary file modified WhisperFTPApp/DatabaseWhisperFTPApp.db
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
migrationBuilder.InsertData(
table: "FtpConnections",
columns: new[] { "Id", "Address", "LastUsed", "Name", "Password", "Username" },
values: new object[] { 1, "ftp://demo.wftpserver.com", new DateTime(2024, 12, 25, 16, 6, 48, 764, DateTimeKind.Local).AddTicks(9433), "ftp://demo.wftpserver.com", "demo", "demo" });
values: new object[] { 1, "ftp://demo.wftpserver.com", new DateTime(2024, 12, 26, 14, 15, 4, 680, DateTimeKind.Local).AddTicks(908), "ftp://demo.wftpserver.com", "demo", "demo" });

migrationBuilder.InsertData(
table: "Settings",
Expand Down
2 changes: 1 addition & 1 deletion WhisperFTPApp/Migrations/AppDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
Id = 1,
Address = "ftp://demo.wftpserver.com",
LastUsed = new DateTime(2024, 12, 25, 16, 6, 48, 764, DateTimeKind.Local).AddTicks(9433),
LastUsed = new DateTime(2024, 12, 26, 14, 15, 4, 680, DateTimeKind.Local).AddTicks(908),
Name = "ftp://demo.wftpserver.com",
Password = "demo",
Username = "demo"
Expand Down
2 changes: 2 additions & 0 deletions WhisperFTPApp/WhisperFTPApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<InformationalVersion>1.0.0.0</InformationalVersion>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,6 +21,7 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.2"/>
<PackageReference Include="Avalonia.Desktop" Version="11.0.2"/>
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.2" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.2"/>
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.2"/>
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
Expand Down

0 comments on commit b4e01d4

Please sign in to comment.