Skip to content

Commit 17f1251

Browse files
Merge pull request #44 from CommunityToolkit/update/uwp-sample
Sample app updates
2 parents 5691816 + 57f5aa4 commit 17f1251

19 files changed

+100
-90
lines changed

samples/MvvmSample.Core/Models/Post.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,62 @@
44

55
using System.Collections.Generic;
66
using System.Linq;
7-
using Newtonsoft.Json;
7+
using System.Text.Json.Serialization;
88

99
namespace MvvmSample.Core.Models
1010
{
1111
/// <summary>
1212
/// A class for a query for posts in a given subreddit.
1313
/// </summary>
14-
public class PostsQueryResponse
14+
public sealed class PostsQueryResponse
1515
{
1616
/// <summary>
1717
/// Gets or sets the listing data for the response.
1818
/// </summary>
19-
[JsonProperty("data")]
20-
public PostListing Data { get; set; }
19+
[JsonPropertyName("data")]
20+
public PostListing? Data { get; set; }
2121
}
2222

2323
/// <summary>
2424
/// A class for a Reddit listing of posts.
2525
/// </summary>
26-
public class PostListing
26+
public sealed class PostListing
2727
{
2828
/// <summary>
2929
/// Gets or sets the items in this listing.
3030
/// </summary>
31-
[JsonProperty("children")]
32-
public IList<PostData> Items { get; set; }
31+
[JsonPropertyName("children")]
32+
public IList<PostData>? Items { get; set; }
3333
}
3434

3535
/// <summary>
3636
/// A wrapping class for a post.
3737
/// </summary>
38-
public class PostData
38+
public sealed class PostData
3939
{
4040
/// <summary>
4141
/// Gets or sets the <see cref="Post"/> instance.
4242
/// </summary>
43-
[JsonProperty("data")]
44-
public Post Data { get; set; }
43+
[JsonPropertyName("data")]
44+
public Post? Data { get; set; }
4545
}
4646

4747
/// <summary>
4848
/// A simple model for a Reddit post.
4949
/// </summary>
50-
public class Post
50+
public sealed class Post
5151
{
5252
/// <summary>
5353
/// Gets or sets the title of the post.
5454
/// </summary>
55-
[JsonProperty("title")]
56-
public string Title { get; set; }
55+
[JsonPropertyName("title")]
56+
public string? Title { get; set; }
5757

5858
/// <summary>
5959
/// Gets or sets the URL to the post thumbnail, if present.
6060
/// </summary>
61-
[JsonProperty("thumbnail")]
62-
public string Thumbnail { get; set; }
61+
[JsonPropertyName("thumbnail")]
62+
public string? Thumbnail { get; set; }
6363

6464
/// <summary>
6565
/// Gets the text of the post.

samples/MvvmSample.Core/MvvmSample.Core.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>9.0</LangVersion>
6+
<Nullable>enable</Nullable>
67
<UserSecretsId>4e66f7b4-01a8-4f00-8733-4ae6a08c741f</UserSecretsId>
78
</PropertyGroup>
89

@@ -43,9 +44,9 @@
4344
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4445
</Content>
4546
</ItemGroup>
46-
<ItemGroup>
47-
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.0-preview5" />
48-
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.1.0" />
49-
<PackageReference Include="Refit" Version="5.2.1" />
50-
</ItemGroup>
47+
<ItemGroup>
48+
<PackageReference Include="CommunityToolkit.Mvvm" Version="7.0.3" />
49+
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.1.0" />
50+
<PackageReference Include="Refit" Version="6.0.38" />
51+
</ItemGroup>
5152
</Project>

samples/MvvmSample.Core/Services/ISettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public interface ISettingsService
2626
/// <typeparam name="T">The type of the object to retrieve.</typeparam>
2727
/// <param name="key">The key associated to the requested object.</param>
2828
[Pure]
29-
T GetValue<T>(string key);
29+
T? GetValue<T>(string key);
3030
}
3131
}

samples/MvvmSample.Core/ViewModels/AsyncRelayCommandPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Threading.Tasks;
6-
using Microsoft.Toolkit.Mvvm.Input;
6+
using CommunityToolkit.Mvvm.Input;
77

88
namespace MvvmSample.Core.ViewModels
99
{

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
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-
using Microsoft.Toolkit.Mvvm.ComponentModel;
6-
using Microsoft.Toolkit.Mvvm.Input;
7-
using Microsoft.Toolkit.Mvvm.Messaging;
8-
using Microsoft.Toolkit.Mvvm.Messaging.Messages;
95
using System.Windows.Input;
6+
using CommunityToolkit.Mvvm.ComponentModel;
7+
using CommunityToolkit.Mvvm.Input;
8+
using CommunityToolkit.Mvvm.Messaging;
9+
using CommunityToolkit.Mvvm.Messaging.Messages;
1010

1111
namespace MvvmSample.Core.ViewModels
1212
{
@@ -21,9 +21,9 @@ public MessengerPageViewModel()
2121
public ICommand RequestCurrentUsernameCommand { get; }
2222
public ICommand ResetCurrentUsernameCommand { get; }
2323

24-
public UserSenderViewModel SenderViewModel { get; } = new UserSenderViewModel();
24+
public UserSenderViewModel SenderViewModel { get; } = new();
2525

26-
public UserReceiverViewModel ReceiverViewModel { get; } = new UserReceiverViewModel();
26+
public UserReceiverViewModel ReceiverViewModel { get; } = new();
2727

2828
// Simple viewmodel for a module sending a username message
2929
public class UserSenderViewModel : ObservableRecipient
@@ -73,9 +73,9 @@ protected override void OnActivated()
7373
}
7474
}
7575

76-
private string username;
76+
private string? username;
7777

78-
public string Username
78+
public string? Username
7979
{
8080
get => username;
8181
private set => SetProperty(ref username, value);

samples/MvvmSample.Core/ViewModels/ObservableObjectPageViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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-
using Microsoft.Toolkit.Mvvm.Input;
65
using System.Threading.Tasks;
76
using System.Windows.Input;
7+
using CommunityToolkit.Mvvm.Input;
88

99
namespace MvvmSample.Core.ViewModels
1010
{
@@ -20,23 +20,23 @@ public ObservableObjectPageViewModel()
2020
/// </summary>
2121
public ICommand ReloadTaskCommand { get; }
2222

23-
private string name;
23+
private string? name;
2424

2525
/// <summary>
2626
/// Gets or sets the name to display.
2727
/// </summary>
28-
public string Name
28+
public string? Name
2929
{
3030
get => name;
3131
set => SetProperty(ref name, value);
3232
}
3333

34-
private TaskNotifier myTask;
34+
private TaskNotifier? myTask;
3535

3636
/// <summary>
3737
/// Gets or sets the name to display.
3838
/// </summary>
39-
public Task MyTask
39+
public Task? MyTask
4040
{
4141
get => myTask;
4242
private set => SetPropertyAndNotifyOnCompletion(ref myTask, value);

samples/MvvmSample.Core/ViewModels/RelayCommandPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Windows.Input;
6-
using Microsoft.Toolkit.Mvvm.Input;
6+
using CommunityToolkit.Mvvm.Input;
77

88
namespace MvvmSample.Core.ViewModels
99
{

samples/MvvmSample.Core/ViewModels/SamplePageViewModel.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Threading.Tasks;
8-
using Microsoft.Toolkit.Mvvm.ComponentModel;
9-
using Microsoft.Toolkit.Mvvm.DependencyInjection;
10-
using Microsoft.Toolkit.Mvvm.Input;
8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.DependencyInjection;
10+
using CommunityToolkit.Mvvm.Input;
1111
using MvvmSample.Core.Helpers;
1212
using MvvmSample.Core.Services;
1313

@@ -18,7 +18,6 @@ namespace MvvmSample.Core.ViewModels
1818
/// </summary>
1919
public class SamplePageViewModel : ObservableObject
2020
{
21-
private IReadOnlyDictionary<string, string> texts;
2221
/// <summary>
2322
/// The <see cref="IFilesService"/> instance currently in use.
2423
/// </summary>
@@ -34,7 +33,9 @@ public SamplePageViewModel()
3433
/// </summary>
3534
public IAsyncRelayCommand<string> LoadDocsCommand { get; }
3635

37-
public IReadOnlyDictionary<string, string> Texts
36+
private IReadOnlyDictionary<string, string>? texts;
37+
38+
public IReadOnlyDictionary<string, string>? Texts
3839
{
3940
get => texts;
4041
set => SetProperty(ref texts, value);
@@ -47,15 +48,17 @@ public IReadOnlyDictionary<string, string> Texts
4748
/// <returns>The text of the specified paragraph, or <see langword="null"/>.</returns>
4849
public string GetParagraph(string key)
4950
{
50-
return Texts != null && Texts.TryGetValue(key, out var value) ? value : string.Empty;
51+
return Texts is not null && Texts.TryGetValue(key, out var value) ? value : string.Empty;
5152
}
5253

5354
/// <summary>
5455
/// Implements the logic for <see cref="LoadDocsCommand"/>.
5556
/// </summary>
5657
/// <param name="name">The name of the docs file to load.</param>
57-
private async Task LoadDocsAsync(string name)
58+
private async Task LoadDocsAsync(string? name)
5859
{
60+
if (name is null) return;
61+
5962
// Skip if the loading has already started
6063
if (!(LoadDocsCommand.ExecutionTask is null)) return;
6164

samples/MvvmSample.Core/ViewModels/Widgets/PostWidgetViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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-
using Microsoft.Toolkit.Mvvm.ComponentModel;
6-
using Microsoft.Toolkit.Mvvm.Messaging;
7-
using Microsoft.Toolkit.Mvvm.Messaging.Messages;
5+
using CommunityToolkit.Mvvm.ComponentModel;
6+
using CommunityToolkit.Mvvm.Messaging;
7+
using CommunityToolkit.Mvvm.Messaging.Messages;
88
using MvvmSample.Core.Models;
99

1010
namespace MvvmSample.Core.ViewModels.Widgets
@@ -14,12 +14,12 @@ namespace MvvmSample.Core.ViewModels.Widgets
1414
/// </summary>
1515
public sealed class PostWidgetViewModel : ObservableRecipient, IRecipient<PropertyChangedMessage<Post>>
1616
{
17-
private Post post;
17+
private Post? post;
1818

1919
/// <summary>
2020
/// Gets the currently selected post, if any.
2121
/// </summary>
22-
public Post Post
22+
public Post? Post
2323
{
2424
get => post;
2525
private set => SetProperty(ref post, value);

samples/MvvmSample.Core/ViewModels/Widgets/SubredditWidgetViewModel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
77
using System.Threading.Tasks;
8-
using Microsoft.Toolkit.Mvvm.ComponentModel;
9-
using Microsoft.Toolkit.Mvvm.DependencyInjection;
10-
using Microsoft.Toolkit.Mvvm.Input;
8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.DependencyInjection;
10+
using CommunityToolkit.Mvvm.Input;
1111
using MvvmSample.Core.Models;
1212
using MvvmSample.Core.Services;
1313
using Nito.AsyncEx;
@@ -32,7 +32,7 @@ public sealed class SubredditWidgetViewModel : ObservableRecipient
3232
/// <summary>
3333
/// An <see cref="AsyncLock"/> instance to avoid concurrent requests.
3434
/// </summary>
35-
private readonly AsyncLock LoadingLock = new AsyncLock();
35+
private readonly AsyncLock LoadingLock = new();
3636

3737
/// <summary>
3838
/// Creates a new <see cref="SubredditWidgetViewModel"/> instance.
@@ -52,7 +52,7 @@ public SubredditWidgetViewModel()
5252
/// <summary>
5353
/// Gets the collection of loaded posts.
5454
/// </summary>
55-
public ObservableCollection<Post> Posts { get; } = new ObservableCollection<Post>();
55+
public ObservableCollection<Post> Posts { get; } = new();
5656

5757
/// <summary>
5858
/// Gets the collection of available subreddits to pick from.
@@ -83,12 +83,12 @@ public string SelectedSubreddit
8383
}
8484
}
8585

86-
private Post selectedPost;
86+
private Post? selectedPost;
8787

8888
/// <summary>
89-
/// Gets or sets the currently selected subreddit.
89+
/// Gets or sets the currently selected post, if any.
9090
/// </summary>
91-
public Post SelectedPost
91+
public Post? SelectedPost
9292
{
9393
get => selectedPost;
9494
set => SetProperty(ref selectedPost, value, true);
@@ -107,9 +107,9 @@ private async Task LoadPostsAsync()
107107

108108
Posts.Clear();
109109

110-
foreach (var item in response.Data.Items)
110+
foreach (var item in response.Data!.Items!)
111111
{
112-
Posts.Add(item.Data);
112+
Posts.Add(item.Data!);
113113
}
114114
}
115115
catch

0 commit comments

Comments
 (0)