Skip to content
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

Added WPF example - non-working #75

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
36 changes: 18 additions & 18 deletions samples/datasync-server/Sample.Datasync.Server.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Datasync.Server", "s
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "library", "library", "{95358590-6440-469A-8A6A-6ACC47F52966}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server", "..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server\CommunityToolkit.Datasync.Server.csproj", "{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server.Abstractions", "..\..\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj", "{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server.Abstractions", "..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj", "{CECB1B21-46C7-4197-B8D2-EC54D0AF8145}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server", "..\..\src\CommunityToolkit.Datasync.Server\CommunityToolkit.Datasync.Server.csproj", "{DEC37ED1-B52A-4287-8F63-8210328AFF70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server.EntityFrameworkCore", "..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj", "{8CDD3736-4B8D-43F5-8F9A-222210D73DAC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server.EntityFrameworkCore", "..\..\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj", "{2086DD5C-C7C1-4957-B667-847C5FEE832C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -25,26 +25,26 @@ Global
{67A76156-0033-4085-86BE-558DC28688A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67A76156-0033-4085-86BE-558DC28688A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67A76156-0033-4085-86BE-558DC28688A6}.Release|Any CPU.Build.0 = Release|Any CPU
{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25}.Release|Any CPU.Build.0 = Release|Any CPU
{CECB1B21-46C7-4197-B8D2-EC54D0AF8145}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CECB1B21-46C7-4197-B8D2-EC54D0AF8145}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CECB1B21-46C7-4197-B8D2-EC54D0AF8145}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CECB1B21-46C7-4197-B8D2-EC54D0AF8145}.Release|Any CPU.Build.0 = Release|Any CPU
{8CDD3736-4B8D-43F5-8F9A-222210D73DAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CDD3736-4B8D-43F5-8F9A-222210D73DAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CDD3736-4B8D-43F5-8F9A-222210D73DAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CDD3736-4B8D-43F5-8F9A-222210D73DAC}.Release|Any CPU.Build.0 = Release|Any CPU
{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6}.Release|Any CPU.Build.0 = Release|Any CPU
{DEC37ED1-B52A-4287-8F63-8210328AFF70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEC37ED1-B52A-4287-8F63-8210328AFF70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEC37ED1-B52A-4287-8F63-8210328AFF70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEC37ED1-B52A-4287-8F63-8210328AFF70}.Release|Any CPU.Build.0 = Release|Any CPU
{2086DD5C-C7C1-4957-B667-847C5FEE832C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2086DD5C-C7C1-4957-B667-847C5FEE832C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2086DD5C-C7C1-4957-B667-847C5FEE832C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2086DD5C-C7C1-4957-B667-847C5FEE832C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{67A76156-0033-4085-86BE-558DC28688A6} = {4B8E4DD5-C2CA-4729-A10B-8A3C993018EC}
{D42DBA41-28AA-4B6D-83A4-A5B839EC8B25} = {95358590-6440-469A-8A6A-6ACC47F52966}
{CECB1B21-46C7-4197-B8D2-EC54D0AF8145} = {95358590-6440-469A-8A6A-6ACC47F52966}
{8CDD3736-4B8D-43F5-8F9A-222210D73DAC} = {95358590-6440-469A-8A6A-6ACC47F52966}
{54E9A0B2-A0B0-4CB1-8FAD-11DB9E4535A6} = {95358590-6440-469A-8A6A-6ACC47F52966}
{DEC37ED1-B52A-4287-8F63-8210328AFF70} = {95358590-6440-469A-8A6A-6ACC47F52966}
{2086DD5C-C7C1-4957-B667-847C5FEE832C} = {95358590-6440-469A-8A6A-6ACC47F52966}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Sample.Datasync.Server.Db;
public class TodoItem : EntityTableData
{
[Required, MinLength(1)]
public string Text { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;

public bool IsComplete { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\..\..\..\..\CommunityToolkit\Datasync\src\CommunityToolkit.Datasync.Server\CommunityToolkit.Datasync.Server.csproj" />
<ProjectReference Include="..\..\..\..\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\..\..\src\CommunityToolkit.Datasync.Server\CommunityToolkit.Datasync.Server.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions samples/todoapp/Samples.TodoApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoApp.WinUI3", "TodoApp.W
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Client", "..\..\src\CommunityToolkit.Datasync.Client\CommunityToolkit.Datasync.Client.csproj", "{2AC73FBE-9E76-4702-B551-B5884383CC68}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoApp.WPF", "TodoApp.WPF\TodoApp.WPF.csproj", "{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Datasync.Server", "..\datasync-server\src\Sample.Datasync.Server\Sample.Datasync.Server.csproj", "{E67734DD-B397-4A65-AA50-D62F37EF05DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -61,6 +65,38 @@ Global
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.Build.0 = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.ActiveCfg = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.Build.0 = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|ARM64.Build.0 = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|x64.ActiveCfg = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|x64.Build.0 = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|x86.ActiveCfg = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Debug|x86.Build.0 = Debug|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|Any CPU.Build.0 = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|ARM64.ActiveCfg = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|ARM64.Build.0 = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|x64.ActiveCfg = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|x64.Build.0 = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|x86.ActiveCfg = Release|Any CPU
{410D4BBD-5ED7-4BC0-A2CF-547A4784732F}.Release|x86.Build.0 = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|ARM64.Build.0 = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|x64.ActiveCfg = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|x64.Build.0 = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|x86.ActiveCfg = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|x86.Build.0 = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|Any CPU.Build.0 = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|ARM64.ActiveCfg = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|ARM64.Build.0 = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|x64.ActiveCfg = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|x64.Build.0 = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|x86.ActiveCfg = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 9 additions & 0 deletions samples/todoapp/TodoApp.WPF/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="TodoApp.WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TodoApp.WPF"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
85 changes: 85 additions & 0 deletions samples/todoapp/TodoApp.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using TodoApp.WPF.Database;
using TodoApp.WPF.ViewModels;

namespace TodoApp.WPF;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, IDisposable
{
private readonly SqliteConnection dbConnection;

/// <summary>
/// The IoC service provider
/// </summary>
public IServiceProvider Services { get; }

public App()
{
// Create the connection to the SQLite database
this.dbConnection = new SqliteConnection("Data Source=:memory:");
this.dbConnection.Open();

// Create the IoC Services provider.
Services = new ServiceCollection()
.AddTransient<TodoListViewModel>()
.AddScoped<IDbInitializer, DbContextInitializer>()
.AddDbContext<AppDbContext>(options => options.UseSqlite(this.dbConnection))
.BuildServiceProvider();

// Initialize the database
InitializeDatabase();
}

private void InitializeDatabase()
{
// using IServiceScope scope = Ioc.Default.CreateScope();
using IServiceScope scope = Services.CreateScope();
IDbInitializer initializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
initializer.Initialize();
}

/// <summary>
/// A helper method for getting a service from the services collection.
/// </summary>
/// <remarks>
/// You can see this in action in the <see cref="TodoListPage"/> class.
/// </remarks>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>An instance of the service</returns>
public static TService GetRequiredService<TService>() where TService : notnull
=> ((App)App.Current).Services.GetRequiredService<TService>();

#region IDisposable
private bool hasDisposed;

protected virtual void Dispose(bool disposing)
{
if (!this.hasDisposed)
{
if (disposing)
{
this.dbConnection.Close();
}

this.hasDisposed = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
#endregion
}

14 changes: 14 additions & 0 deletions samples/todoapp/TodoApp.WPF/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
57 changes: 57 additions & 0 deletions samples/todoapp/TodoApp.WPF/Database/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.Client.Http;
using CommunityToolkit.Datasync.Client.Offline;
using Microsoft.EntityFrameworkCore;
using TodoApp.WPF.Services;

namespace TodoApp.WPF.Database;

public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
public DbSet<TodoItem> TodoItems => Set<TodoItem>();

// protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
// {
// HttpClientOptions clientOptions = new()
// {
// Endpoint = new Uri("https://Y.azurewebsites.net/"),
// HttpPipeline = [new LoggingHandler()]
// };
// _ = optionsBuilder.UseHttpClientOptions(clientOptions);
// }

public async Task SynchronizeAsync(CancellationToken cancellationToken = default)
{
// PushResult pushResult = await this.PushAsync(cancellationToken);
// if (!pushResult.IsSuccessful)
// {
// throw new ApplicationException($"Push failed: {pushResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
// }

// PullResult pullResult = await this.PullAsync(cancellationToken);
// if (!pullResult.IsSuccessful)
// {
// throw new ApplicationException($"Pull failed: {pullResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
// }
}
}

/// <summary>
/// Use this class to initialize the database. In this sample, we just create
/// the database using <see cref="DatabaseFacade.EnsureCreated"/>. However, you
/// may want to use migrations.
/// </summary>
/// <param name="context">The context for the database.</param>
public class DbContextInitializer(AppDbContext context) : IDbInitializer
{
/// <inheritdoc />
public void Initialize()
=> context.Database.EnsureCreated();

/// <inheritdoc />
public Task InitializeAsync(CancellationToken cancellationToken = default)
=> context.Database.EnsureCreatedAsync(cancellationToken);
}
23 changes: 23 additions & 0 deletions samples/todoapp/TodoApp.WPF/Database/IDbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace TodoApp.WPF.Database;

/// <summary>
/// An interface to initialize a database.
/// </summary>
public interface IDbInitializer
{
/// <summary>
/// Synchronously initialize the database.
/// </summary>
void Initialize();

/// <summary>
/// Asynchronously initialize the database.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that resolves when complete.</returns>
Task InitializeAsync(CancellationToken cancellationToken = default);
}
19 changes: 19 additions & 0 deletions samples/todoapp/TodoApp.WPF/Database/OfflineClientEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.ComponentModel.DataAnnotations;

namespace TodoApp.WPF.Database;

/// <summary>
/// An abstract class for working with offline entities.
/// </summary>
public abstract class OfflineClientEntity
{
[Key]
public string Id { get; set; } = Guid.NewGuid().ToString("N");
public DateTimeOffset? UpdatedAt { get; set; }
public string? Version { get; set; }
public bool Deleted { get; set; }
}
16 changes: 16 additions & 0 deletions samples/todoapp/TodoApp.WPF/Database/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json;

namespace TodoApp.WPF.Database;

public class TodoItem : OfflineClientEntity
{
public string Title { get; set; } = string.Empty;
public bool IsComplete { get; set; } = false;

public override string ToString()
=> JsonSerializer.Serialize(this);
}
Binary file added samples/todoapp/TodoApp.WPF/Images/AddItem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/todoapp/TodoApp.WPF/Images/RefreshItems.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading