Skip to content

Commit e841b2d

Browse files
committed
(#341) Corrections to tutorial code
1 parent a2748bd commit e841b2d

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

docs/tutorial/client/part-1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,4 @@ In the [next tutorial](./part-2.md), I'm going to talk about authenticating clie
216216
[todomvc]: https://github.com/CommunityToolkit/Datasync/tree/main/samples/todoapp-mvc
217217
[todoapp]: https://github.com/CommunityToolkit/Datasync/tree/main/samples/todoapp-tutorial
218218
[CommunityToolkit.MVVM]: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/
219-
[CommunityToolkit.Datasync.Client]: https://www.nuget.org/packages/CommunityToolkit.Datasync.Client#readme-body-tab
219+
[CommunityToolkit.Datasync.Client]: https://www.nuget.org/packages/CommunityToolkit.Datasync.Client#readme-body-tab

samples/todoapp-tutorial/ClientApp/App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ClientApp.Interfaces;
1+
using ClientApp.Interfaces;
22
using ClientApp.Services;
33
using ClientApp.ViewModels;
44
using Microsoft.Extensions.DependencyInjection;
@@ -29,7 +29,7 @@ private void InitializeApplication()
2929
{
3030
using IServiceScope scope = Services.CreateScope();
3131
IAppInitializer initializer = scope.ServiceProvider.GetRequiredService<IAppInitializer>();
32-
initializer.Initialize();
32+
_ = initializer.Initialize();
3333
}
3434

3535
/// <summary>

samples/todoapp-tutorial/ClientApp/Converters/BooleanToImageConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
15
using System.Globalization;
26
using System.Windows.Data;
37
using System.Windows.Media.Imaging;

samples/todoapp-tutorial/ClientApp/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Window x:Class="ClientApp.MainWindow"
1+
<Window x:Class="ClientApp.MainWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 ClientApp.Interfaces;
6+
using ClientApp.Models;
7+
using CommunityToolkit.Mvvm.ComponentModel;
8+
using CommunityToolkit.Mvvm.Input;
9+
using System.ComponentModel;
10+
11+
namespace ClientApp.ViewModels;
12+
13+
public partial class TodoListViewModel(ITodoService todoService, IAlertService alertService) : ObservableRecipient
14+
{
15+
[ObservableProperty]
16+
private bool isRefreshing;
17+
18+
[ObservableProperty]
19+
private BindingList<TodoItem> items = [];
20+
21+
[ObservableProperty]
22+
private string addItemTitle = string.Empty;
23+
24+
[RelayCommand]
25+
public async Task AddItemAsync(CancellationToken cancellationToken = default)
26+
{
27+
try
28+
{
29+
var addition = await todoService.AddTodoItemAsync(AddItemTitle, cancellationToken);
30+
Items.Add(addition);
31+
AddItemTitle = string.Empty;
32+
}
33+
catch (Exception ex)
34+
{
35+
await alertService.ShowErrorAlertAsync("Error adding item", ex.Message);
36+
}
37+
}
38+
39+
[RelayCommand]
40+
public async Task UpdateItemAsync(TodoItem item, CancellationToken cancellationToken = default)
41+
{
42+
try
43+
{
44+
TodoItem? storedItem = await todoService.GetTodoItemAsync(item.Id, cancellationToken);
45+
if (storedItem is not null)
46+
{
47+
storedItem.IsComplete = !storedItem.IsComplete;
48+
var replacedItem = await todoService.ReplaceTodoItemAsync(storedItem, cancellationToken);
49+
var idx = Items.IndexOf(item);
50+
Items[idx] = replacedItem;
51+
}
52+
else
53+
{
54+
await alertService.ShowErrorAlertAsync("Item not found", "The item was not found in the database.");
55+
}
56+
}
57+
catch (Exception ex)
58+
{
59+
await alertService.ShowErrorAlertAsync("Error updating item", ex.Message);
60+
}
61+
}
62+
63+
[RelayCommand]
64+
public async Task RefreshItemsAsync(CancellationToken cancellationToken = default)
65+
{
66+
try
67+
{
68+
IsRefreshing = true;
69+
List<TodoItem> dbItems = await todoService.GetAllTodoItemsAsync(cancellationToken);
70+
Items.Clear();
71+
foreach (var dbItem in dbItems)
72+
{
73+
Items.Add(dbItem);
74+
}
75+
}
76+
catch (Exception ex)
77+
{
78+
await alertService.ShowErrorAlertAsync("Error refreshing items", ex.Message);
79+
}
80+
finally
81+
{
82+
IsRefreshing = false;
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)