88using Microsoft . EntityFrameworkCore ;
99using System ;
1010using System . Collections . Generic ;
11+ using System . Linq ;
1112using System . Threading ;
1213using System . Threading . Tasks ;
1314using 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 {
0 commit comments