Skip to content

Commit

Permalink
(#156) Added new TodoItemViewModel (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall authored Nov 22, 2024
1 parent ccca249 commit 74df80e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
</PropertyGroup>

<ItemGroup>
<!--
The following packages are required if you are not linking source
<PackageReference Include="CommunityToolkit.Datasync.Server" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.Datasync.Server.EntityFrameworkCore" Version="8.0.0" />
-->
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PackageReference Include="CommunityToolkit.Datasync.Server" Version="8.0.4" />
<PackageReference Include="CommunityToolkit.Datasync.Server.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 4 additions & 1 deletion samples/todoapp/TodoApp.WinUI3/Database/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#define OFFLINESYNC_ENABLED

using CommunityToolkit.Datasync.Client.Http;
using CommunityToolkit.Datasync.Client.Offline;
using Microsoft.EntityFrameworkCore;
Expand All @@ -14,14 +16,15 @@
namespace TodoApp.WinUI3.Database;

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

//protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
//{
// HttpClientOptions clientOptions = new()
// {
// Endpoint = new Uri("https://YOURSITEHERE.azurewebsites.net/"),
// Endpoint = new Uri("https://app-qhvxwauvecrtg.azurewebsites.net/"),
// HttpPipeline = [new LoggingHandler()]
// };
// _ = optionsBuilder.UseHttpClientOptions(clientOptions);
Expand Down
4 changes: 2 additions & 2 deletions samples/todoapp/TodoApp.WinUI3/TodoApp.WinUI3.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
Expand Down Expand Up @@ -33,7 +33,7 @@
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Behaviors" Version="7.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
Expand Down
46 changes: 46 additions & 0 deletions samples/todoapp/TodoApp.WinUI3/ViewModels/TodoItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.Mvvm.ComponentModel;
using System;
using TodoApp.WinUI3.Database;

namespace TodoApp.WinUI3.ViewModels;

public partial class TodoItemViewModel(TodoItem todoItem) : ObservableObject
{
public readonly TodoItem _todoItem = todoItem;

public string Title
{
get => this._todoItem.Title;
set => SetProperty(this._todoItem.Title, value, this._todoItem, (item, value) => item.Title = value);
}

public bool IsComplete
{
get => this._todoItem.IsComplete;
set => SetProperty(this._todoItem.IsComplete, value, this._todoItem, (item, value) => item.IsComplete = value);
}

public string Version
{
get => this._todoItem.Version;
set => SetProperty(this._todoItem.Version, value, this._todoItem, (item, value) => item.Version = value);
}

public string Id => _todoItem.Id;

public DateTimeOffset? UpdatedAt
{
get => this._todoItem.UpdatedAt;
set => SetProperty(this._todoItem.UpdatedAt, value, this._todoItem, (item, value) => item.UpdatedAt = value);
}

public bool Deleted
{
get => this._todoItem.Deleted;
set => SetProperty(this._todoItem.Deleted, value, this._todoItem, (item, value) => item.Deleted = value);
}
}
12 changes: 7 additions & 5 deletions samples/todoapp/TodoApp.WinUI3/ViewModels/TodoListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TodoApp.WinUI3.Database;
Expand All @@ -25,7 +26,7 @@ public partial class TodoListViewModel(AppDbContext service) : ObservableRecipie
private bool isRefreshing;

[ObservableProperty]
private ConcurrentObservableCollection<TodoItem> items = [];
private ConcurrentObservableCollection<TodoItemViewModel> items = [];

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

// Add the item to the end of the list.
Items.Add(addition);
Items.Add(new TodoItemViewModel(addition));

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

// Update the item in the list
_ = Items.ReplaceIf(x => x.Id == itemId, item);
_ = Items.ReplaceIf(x => x.Id == itemId, new TodoItemViewModel(item));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -106,10 +107,11 @@ public async Task RefreshItemsAsync(CancellationToken cancellationToken = defaul
await service.SynchronizeAsync(cancellationToken);

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

// Replace all the items in the collection.
Items.ReplaceAll(itemsFromDatabase);

Items.ReplaceAll(itemsFromDatabase.Select(item => new TodoItemViewModel(item)));
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion samples/todoapp/TodoApp.WinUI3/Views/TodoListPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
Command="{Binding ViewModel.EditItemCommand, ElementName=ThisPage}"
CommandParameter="{Binding Id}"
Content="{Binding Title}"
IsChecked="{Binding IsComplete}" />
IsChecked="{Binding IsComplete, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Expand Down

0 comments on commit 74df80e

Please sign in to comment.