Skip to content

Commit 74df80e

Browse files
authored
(#156) Added new TodoItemViewModel (#157)
1 parent ccca249 commit 74df80e

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed

samples/datasync-server/src/Sample.Datasync.Server/Sample.Datasync.Server.csproj

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<!--
11-
The following packages are required if you are not linking source
12-
13-
<PackageReference Include="CommunityToolkit.Datasync.Server" Version="8.0.0" />
14-
<PackageReference Include="CommunityToolkit.Datasync.Server.EntityFrameworkCore" Version="8.0.0" />
15-
16-
-->
17-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
18-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
10+
<PackageReference Include="CommunityToolkit.Datasync.Server" Version="8.0.4" />
11+
<PackageReference Include="CommunityToolkit.Datasync.Server.EntityFrameworkCore" Version="8.0.4" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.11">
1914
<PrivateAssets>all</PrivateAssets>
2015
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2116
</PackageReference>

samples/todoapp/TodoApp.WinUI3/Database/AppDbContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#define OFFLINESYNC_ENABLED
6+
57
using CommunityToolkit.Datasync.Client.Http;
68
using CommunityToolkit.Datasync.Client.Offline;
79
using Microsoft.EntityFrameworkCore;
@@ -14,14 +16,15 @@
1416
namespace TodoApp.WinUI3.Database;
1517

1618
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
19+
// public class AppDbContext(DbContextOptions<AppDbContext> options) : OfflineDbContext(options)
1720
{
1821
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
1922

2023
//protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
2124
//{
2225
// HttpClientOptions clientOptions = new()
2326
// {
24-
// Endpoint = new Uri("https://YOURSITEHERE.azurewebsites.net/"),
27+
// Endpoint = new Uri("https://app-qhvxwauvecrtg.azurewebsites.net/"),
2528
// HttpPipeline = [new LoggingHandler()]
2629
// };
2730
// _ = optionsBuilder.UseHttpClientOptions(clientOptions);

samples/todoapp/TodoApp.WinUI3/TodoApp.WinUI3.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>WinExe</OutputType>
44
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
@@ -33,7 +33,7 @@
3333
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.0.240109" />
3434
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.0.240109" />
3535
<PackageReference Include="CommunityToolkit.WinUI.UI.Behaviors" Version="7.1.2" />
36-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
36+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
3737
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
3838
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1" />
3939
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.Mvvm.ComponentModel;
6+
using System;
7+
using TodoApp.WinUI3.Database;
8+
9+
namespace TodoApp.WinUI3.ViewModels;
10+
11+
public partial class TodoItemViewModel(TodoItem todoItem) : ObservableObject
12+
{
13+
public readonly TodoItem _todoItem = todoItem;
14+
15+
public string Title
16+
{
17+
get => this._todoItem.Title;
18+
set => SetProperty(this._todoItem.Title, value, this._todoItem, (item, value) => item.Title = value);
19+
}
20+
21+
public bool IsComplete
22+
{
23+
get => this._todoItem.IsComplete;
24+
set => SetProperty(this._todoItem.IsComplete, value, this._todoItem, (item, value) => item.IsComplete = value);
25+
}
26+
27+
public string Version
28+
{
29+
get => this._todoItem.Version;
30+
set => SetProperty(this._todoItem.Version, value, this._todoItem, (item, value) => item.Version = value);
31+
}
32+
33+
public string Id => _todoItem.Id;
34+
35+
public DateTimeOffset? UpdatedAt
36+
{
37+
get => this._todoItem.UpdatedAt;
38+
set => SetProperty(this._todoItem.UpdatedAt, value, this._todoItem, (item, value) => item.UpdatedAt = value);
39+
}
40+
41+
public bool Deleted
42+
{
43+
get => this._todoItem.Deleted;
44+
set => SetProperty(this._todoItem.Deleted, value, this._todoItem, (item, value) => item.Deleted = value);
45+
}
46+
}

samples/todoapp/TodoApp.WinUI3/ViewModels/TodoListViewModel.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.EntityFrameworkCore;
99
using System;
1010
using System.Collections.Generic;
11+
using System.Linq;
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314
using TodoApp.WinUI3.Database;
@@ -25,7 +26,7 @@ public partial class TodoListViewModel(AppDbContext service) : ObservableRecipie
2526
private bool isRefreshing;
2627

2728
[ObservableProperty]
28-
private ConcurrentObservableCollection<TodoItem> items = [];
29+
private ConcurrentObservableCollection<TodoItemViewModel> items = [];
2930

3031
[ObservableProperty]
3132
private string title = string.Empty;
@@ -47,7 +48,7 @@ public async Task AddItemAsync(CancellationToken cancellationToken = default)
4748
_ = await service.SaveChangesAsync(cancellationToken);
4849

4950
// Add the item to the end of the list.
50-
Items.Add(addition);
51+
Items.Add(new TodoItemViewModel(addition));
5152

5253
// Update the title field ready for ext insertion.
5354
Title = string.Empty;
@@ -77,7 +78,7 @@ public async Task EditItemAsync(string itemId, CancellationToken cancellationTok
7778
_ = await service.SaveChangesAsync(cancellationToken);
7879

7980
// Update the item in the list
80-
_ = Items.ReplaceIf(x => x.Id == itemId, item);
81+
_ = Items.ReplaceIf(x => x.Id == itemId, new TodoItemViewModel(item));
8182
}
8283
catch (Exception ex)
8384
{
@@ -106,10 +107,11 @@ public async Task RefreshItemsAsync(CancellationToken cancellationToken = defaul
106107
await service.SynchronizeAsync(cancellationToken);
107108

108109
// Pull all items from the database.
109-
IEnumerable<TodoItem> itemsFromDatabase = await service.TodoItems.ToListAsync(cancellationToken);
110+
IEnumerable<TodoItem> itemsFromDatabase = await service.TodoItems.OrderBy(item => item.Id).ToListAsync(cancellationToken);
110111

111112
// Replace all the items in the collection.
112-
Items.ReplaceAll(itemsFromDatabase);
113+
114+
Items.ReplaceAll(itemsFromDatabase.Select(item => new TodoItemViewModel(item)));
113115
}
114116
catch (Exception ex)
115117
{

samples/todoapp/TodoApp.WinUI3/Views/TodoListPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
Command="{Binding ViewModel.EditItemCommand, ElementName=ThisPage}"
5353
CommandParameter="{Binding Id}"
5454
Content="{Binding Title}"
55-
IsChecked="{Binding IsComplete}" />
55+
IsChecked="{Binding IsComplete, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
5656
</DataTemplate>
5757
</ListView.ItemTemplate>
5858
</ListView>

0 commit comments

Comments
 (0)