Skip to content

Added more verbose logging and moved database instantiation to MainWindow.xaml.cs #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demos/WPF/App.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Application x:Class="PowersyncDotnetTodoList.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:PowersyncDotnetTodoList.Converters">
xmlns:converters="clr-namespace:PowersyncDotnetTodoList.Converters"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application.Resources>
<converters:BoolToStatusConverter x:Key="BoolToStatusConverter"/>
<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
Expand Down
56 changes: 44 additions & 12 deletions demos/WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,41 @@ public partial class App : Application
{
public static IServiceProvider? Services { get; private set; }

protected override async void OnStartup(StartupEventArgs e)
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

// Handle async initialization synchronously or with proper error handling
try
{
InitializeApplicationAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
MessageBox.Show(
$"Application startup failed: {ex.Message}\n\nFull error: {ex}",
"Startup Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);

// Shut down the application
Current.Shutdown(1);
return;
}
}

private async Task InitializeApplicationAsync()
{
var services = new ServiceCollection();
ConfigureServices(services);

// Build the service provider
Services = services.BuildServiceProvider();

// Initialize the database and connector
var db = Services.GetRequiredService<PowerSyncDatabase>();
var connector = Services.GetRequiredService<PowerSyncConnector>();
await db.Init();
await db.Connect(connector);
await db.WaitForFirstSync();

var mainWindow = Services.GetRequiredService<MainWindow>();

var navigationService = Services.GetRequiredService<INavigationService>();
navigationService.Navigate<TodoListViewModel>();

navigationService.Navigate<TodoListViewModel>();
mainWindow.Show();
}

Expand Down Expand Up @@ -67,7 +80,10 @@ private void ConfigureServices(IServiceCollection services)
);

// Register PowerSyncConnector
services.AddSingleton<PowerSyncConnector>();
services.AddSingleton<PowerSyncConnector>(sp =>
{
return new PowerSyncConnector();
});

// Register ViewModels and Views
services.AddTransient<TodoListViewModel>();
Expand All @@ -86,5 +102,21 @@ private void ConfigureServices(IServiceCollection services)
return new NavigationService(mainWindow.MainFrame, sp);
});
}

// Add global exception handler
private void Application_DispatcherUnhandledException(
object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e
)
{
MessageBox.Show(
$"An unhandled exception occurred: {e.Exception.Message}\n\nFull error: {e.Exception}",
"Unhandled Exception",
MessageBoxButton.OK,
MessageBoxImage.Error
);

e.Handled = true; // Prevent application crash
}
}
}
40 changes: 37 additions & 3 deletions demos/WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
using System.Windows;
using PowerSync.Common.Client;
using PowersyncDotnetTodoList.Services;
using PowersyncDotnetTodoList.ViewModels;

namespace PowersyncDotnetTodoList;

public partial class MainWindow : Window
{
public MainWindow(MainWindowViewModel viewModel)
private readonly MainWindowViewModel _viewModel;
private readonly PowerSyncConnector _connector;
private readonly PowerSyncDatabase _db;

public MainWindow(
MainWindowViewModel viewModel,
PowerSyncConnector connector,
PowerSyncDatabase db
)
{
InitializeComponent();

this.DataContext = viewModel;
_viewModel = viewModel;
_connector = connector;
_db = db;

this.DataContext = _viewModel;

// Start the async initialization
InitializeAsync();
}

private async void InitializeAsync()
{
try
{
await _db.Init();
await _db.Connect(_connector);
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to initialize database: {ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
}
}
16 changes: 16 additions & 0 deletions demos/WPF/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net9.0-windows\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net9.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>false</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
34 changes: 26 additions & 8 deletions demos/WPF/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,35 @@ public bool Connected
#region Constructor
public MainWindowViewModel(PowerSyncDatabase db)
{
_db = db;
// Set up the listener to track the status changes
_db.RunListener(
(update) =>
try
{

if (db == null)
{
if (update.StatusChanged != null)
Console.WriteLine("ERROR: PowerSyncDatabase is null!");
throw new ArgumentNullException(nameof(db));
}

_db = db;
Console.WriteLine("PowerSyncDatabase assigned successfully");
_db.RunListener(
(update) =>
{
Connected = update.StatusChanged.Connected;
Console.WriteLine(
$"Listener callback triggered: StatusChanged = {update.StatusChanged?.Connected}"
);
if (update.StatusChanged != null)
{
Connected = update.StatusChanged.Connected;
}
}
}
);
);
}
catch (Exception ex)
{
Console.WriteLine($"ERROR in MainWindowViewModel constructor: {ex}");
throw;
}
}
#endregion
}
Expand Down
6 changes: 6 additions & 0 deletions demos/WPF/WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<ItemGroup>
<ProjectReference Include="..\..\PowerSync\Powersync.Common\PowerSync.Common.csproj" />
</ItemGroup>

<ItemGroup>
<None Update=".env">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>