Skip to content

Commit d4b284d

Browse files
committed
Adding some comments to the code.
1 parent ea26d6a commit d4b284d

11 files changed

Lines changed: 181 additions & 38 deletions

File tree

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Assets/Icons.axaml

Lines changed: 17 additions & 5 deletions
Large diffs are not rendered by default.

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/DataTemplates/ToDoItemPriorityIconTemplateSelector.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66

77
namespace AdvancedToDoList.DataTemplates;
88

9+
/// <summary>
10+
/// This class is a helper to resolve the symbol-representation of our <see cref="Priority"/>-enum
11+
/// </summary>
912
public class ToDoItemPriorityIconTemplateSelector : IDataTemplate
1013
{
14+
/// <summary>
15+
/// Gets the default instance of this class
16+
/// </summary>
1117
public static ToDoItemPriorityIconTemplateSelector Instance { get; } = new ToDoItemPriorityIconTemplateSelector();
1218

19+
/// <inheritdoc />
1320
public Control? Build(object? param)
1421
{
1522
var priority = param as Priority?;
@@ -38,15 +45,16 @@ public class ToDoItemPriorityIconTemplateSelector : IDataTemplate
3845
new PathIcon()
3946
{
4047
Data = ResourcesHelper.GetAppResource<Geometry>("PhosphorIcons.ArrowCircleUpRight"),
41-
Foreground = new SolidColorBrush(Colors.Red),
48+
Foreground = Brushes.Red,
4249
Width = fontSize,
4350
Height = fontSize
4451
},
4552

4653
_ => null
4754
};
48-
}
49-
55+
}
56+
57+
/// <inheritdoc />
5058
public bool Match(object? data)
5159
{
5260
return data is Priority;

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/DataTemplates/TodoItemStatusTemplateSelector.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66

77
namespace AdvancedToDoList.DataTemplates;
88

9+
/// <summary>
10+
/// This class is a helper to resolve the symbol for the <see cref="ToDoItemStatus"/>.
11+
/// </summary>
912
public class ToDoItemStatusTemplateSelector : IDataTemplate
1013
{
14+
/// <summary>
15+
/// Gets the default instance of this class
16+
/// </summary>
1117
public static ToDoItemStatusTemplateSelector Instance { get; } = new ToDoItemStatusTemplateSelector();
12-
18+
19+
/// <inheritdoc />
1320
public Control? Build(object? param)
1421
{
1522
var status = param as ToDoItemStatus?;
@@ -20,7 +27,9 @@ public class ToDoItemStatusTemplateSelector : IDataTemplate
2027
{
2128
Data = ResourcesHelper.GetAppResource<Geometry>("PhosphorIcons.CircleDashedLight"),
2229
Opacity = 0.7,
23-
[ToolTip.TipProperty] = "Pending"
30+
// TIP: If you want your App to be localized, you could also provide this text via App.Resources
31+
// or any other localization provider.
32+
[ToolTip.TipProperty] = "Pending"
2433
},
2534

2635
ToDoItemStatus.InProgress =>
@@ -50,7 +59,8 @@ public class ToDoItemStatusTemplateSelector : IDataTemplate
5059
_ => null
5160
};
5261
}
53-
62+
63+
/// <inheritdoc />
5464
public bool Match(object? data)
5565
{
5666
return data is ToDoItemStatus;

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/ColorHelper.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
namespace AdvancedToDoList.Helper;
55

6-
public class ColorHelper
6+
/// <summary>
7+
/// An internal helper class for the <see cref="Avalonia.Media.Color"/>-struct
8+
/// </summary>
9+
internal static class ColorHelper
710
{
8-
public static Color GetRandomColor()
11+
/// <summary>
12+
/// Creates a Random Color using RGB
13+
/// </summary>
14+
/// <returns>The random Color</returns>
15+
internal static Color GetRandomColor()
916
{
1017
return new Color(
1118
255,

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/DataBaseDto.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
namespace AdvancedToDoList.Helper;
44

5+
/// <summary>
6+
/// This is Data-Transfer-Object used to convert the DataBase from and to JSON-representation.
7+
/// </summary>
8+
/// <seealso href="https://en.wikipedia.org/wiki/Data_transfer_object"/>
59
public class DataBaseDto
610
{
11+
/// <summary>
12+
/// Gets or sets a collection of available Categories.
13+
/// </summary>
714
public Category[]? Categories { get; set; }
815

16+
/// <summary>
17+
/// Gets or sets a collection of available ToDoItems.
18+
/// </summary>
919
public ToDoItem[]? ToDoItems { get; set; }
1020
}

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/DataBaseHelper.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,37 @@
1212
using Microsoft.Data.Sqlite;
1313
using Microsoft.Extensions.DependencyInjection;
1414

15+
// Needed to make Dapper AOT-friendly.
16+
// See: https://aot.dapperlib.dev for usage details
1517
[module: DapperAot]
1618

1719
namespace AdvancedToDoList.Helper;
1820

21+
/// <summary>
22+
/// This is a helper class for working with our DataBase.
23+
/// </summary>
1924
public static class DataBaseHelper
2025
{
26+
// A flag that indicates if the DB is yet initialized.
2127
private static bool _initialized;
2228

29+
/// <summary>
30+
/// Opens a new <see cref="SqliteConnection"/> and opens it for usage.
31+
/// </summary>
32+
/// <remarks>
33+
/// Ensure the connection is disposed of after use.
34+
/// </remarks>
35+
/// <returns>The open connection.</returns>
2336
internal static async Task<SqliteConnection> GetOpenConnectionAsync()
2437
{
25-
if (Design.IsDesignMode)
26-
{
27-
Console.WriteLine(App.Services.GetService<IDbService>() is not null ? "Service found" : "Service not found");
28-
}
29-
38+
// Get the DB-service to resolve the DB per platfrom correctly.
3039
var dbService = App.Services.GetRequiredService<IDbService>();
31-
40+
3241
var dbPath = dbService.GetDatabasePath();
3342
var dbSource = $"Data Source='{dbPath}'";
3443

3544
// Ensure the directory exists for the database file
36-
string? dir = Path.GetDirectoryName(dbPath);
45+
var dir = Path.GetDirectoryName(dbPath);
3746
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
3847
{
3948
Directory.CreateDirectory(dir);
@@ -42,34 +51,50 @@ internal static async Task<SqliteConnection> GetOpenConnectionAsync()
4251
var connection = new SqliteConnection(dbSource);
4352
await connection.OpenAsync();
4453

54+
// make sure the necessary DB-schema is created.
4555
await EnsureInitializedAsync(connection);
46-
Console.WriteLine($"Opened database at {dbPath}");
4756

4857
return connection;
4958
}
5059

51-
60+
/// <summary>
61+
/// Gets all available Categories from the DB.
62+
/// </summary>
63+
/// <returns>the loaded Categories</returns>
5264
public static async Task<IEnumerable<Category>> GetCategoriesAsync()
5365
{
5466
await using var connection = await GetOpenConnectionAsync();
5567
return (await connection.QueryAsync<Category>("SELECT * FROM Category"));
5668
}
5769

70+
/// <summary>
71+
/// Gets all available ToDoItems from the DB, filtered by its status.
72+
/// </summary>
73+
/// <param name="loadAlsoCompletedItems">If true, also loads items that are marked as compleded (Progess = 100 %). The default is false.</param>
74+
/// <returns>the loaded ToDoItems</returns>
5875
public static async Task<IEnumerable<ToDoItem>> GetToDoItemsAsync(bool loadAlsoCompletedItems = false)
5976
{
60-
Console.WriteLine("GetToDoItemsAsync");
6177
await using var connection = await GetOpenConnectionAsync();
78+
// The trick here is to pass @loadAlsoCompletedItems as a parameter.
79+
// If it is true, the condition will always be true.
80+
// The alternative would be to write different SQL queries.
6281
const string sql = """
6382
SELECT *
6483
FROM ToDoItem
65-
WHERE Progress < 100 OR @loadAlsoCompletedItems;
84+
WHERE @loadAlsoCompletedItems OR Progress < 100;
6685
""";
6786

87+
// store the items into an array
6888
var toDoItems =
6989
(await connection.QueryAsync<ToDoItem>(sql,
7090
new {loadAlsoCompletedItems}))
7191
.ToArray();
7292

93+
// map the categories.
94+
// Dapper could also to it directly in the query.
95+
// However, this would need reflection and NativeAOT would not work properly.
96+
// Thus, we will map the categories to CategoryId on our own.
97+
7398
var categories = await connection.QueryAsync<Category>("SELECT * FROM Category");
7499
var categoriesDict = new Dictionary<long, Category>(
75100
categories.Select(x => new KeyValuePair<long, Category>(x.Id ?? -1, x)));
@@ -86,7 +111,7 @@ FROM ToDoItem
86111
/// Ensures that all tables are created and the database is ready to be used.
87112
/// </summary>
88113
/// <param name="connection">the connection to use</param>
89-
/// <param name="force">the creating will be skipped by default, unless you set this parameter to true</param>
114+
/// <param name="force">the creating will be skipped by default, unless you set this parameter to true.</param>
90115
internal static async Task EnsureInitializedAsync(SqliteConnection connection, bool force = false)
91116
{
92117
if (_initialized && !force) return;
@@ -121,6 +146,7 @@ Color TEXT NULL
121146

122147
Console.WriteLine("Created Category table.");
123148

149+
// Populate some data for the designer if it has none yet.
124150
if (Design.IsDesignMode)
125151
{
126152
var categoryCount = await connection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM Category");
@@ -142,13 +168,17 @@ Color TEXT NULL
142168
/// </summary>
143169
/// <remarks>
144170
/// Wasm uses IndexedDB to store the data. This needs to be synced.
145-
/// This helper will do it for us.
171+
/// This helper will do it for us. If you don't need the WASM-target, this can be omitted.
146172
/// </remarks>
147173
public static async Task SyncUnderlyingDatabaseAsync()
148174
{
149175
await App.Services.GetRequiredService<IDbService>().SaveAsync();
150176
}
151177

178+
/// <summary>
179+
/// Adds some sample data for the designer.
180+
/// </summary>
181+
/// <param name="connection">The connection to use.</param>
152182
private static async Task AddSampleDataAsync(SqliteConnection connection)
153183
{
154184

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/FileHelper.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
namespace AdvancedToDoList.Helper;
44

5-
public static class FileHelper
5+
/// <summary>
6+
/// An internal helper for working with File-I/O.
7+
/// </summary>
8+
internal static class FileHelper
69
{
7-
public static FilePickerFileType JsonFileType { get; } = new FilePickerFileType("Json file")
10+
/// <summary>
11+
/// Gets the <see cref="FilePickerFileType"/> that represents a JSON file.
12+
/// </summary>
13+
/// <seealso href="https://developer.apple.com/documentation/uniformtypeidentifiers"/>
14+
/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types"/>
15+
internal static FilePickerFileType JsonFileType { get; } = new FilePickerFileType("Json file")
816
{
917
Patterns = ["*.json"],
1018
AppleUniformTypeIdentifiers = ["public.json"],

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/JsonContextHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace AdvancedToDoList.Helper;
99

10+
/// <summary>
11+
/// This class is the <see cref="JsonSerializerContext"/> which is needed to make the serialization
12+
/// AOT and trimming friendly.
13+
/// </summary>
14+
/// <see href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation"/>
1015
[JsonSerializable(typeof(DataBaseDto))]
1116
[JsonSerializable(typeof(Settings))]
1217
[JsonSerializable(typeof(Category[]))]
@@ -21,6 +26,9 @@ public partial class JsonContextHelper : JsonSerializerContext
2126
{
2227
}
2328

29+
/// <summary>
30+
/// This class converts a <see cref="Color"/> from and to JSON.
31+
/// </summary>
2432
public class JsonColorConverter : JsonConverter<Color>
2533
{
2634
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Helper/ResourcesHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44

55
namespace AdvancedToDoList.Helper;
66

7+
/// <summary>
8+
/// This is an interal helper class to work with App-resources.
9+
/// </summary>
710
internal static class ResourcesHelper
811
{
12+
/// <summary>
13+
/// This method will look up all App-resources and return the found value or the default if nothing was found.
14+
/// </summary>
15+
/// <param name="resourceKey">The resource key to lookup.</param>
16+
/// <typeparam name="T">The expected return type.</typeparam>
17+
/// <returns>the found value if present, otherwise its default value.</returns>
918
internal static T? GetAppResource<T>(object resourceKey)
1019
{
1120
var found = Application.Current!.TryFindResource(resourceKey, out var resource);
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
using System;
1+
using CommunityToolkit.Mvvm.Messaging;
22

33
namespace AdvancedToDoList.Messages;
44

5+
/// <summary>
6+
/// A class that repsents a message for communication between different ViewModels.
7+
/// It notifies about changed objects that should update.
8+
/// </summary>
9+
/// <param name="affectedItems">the updated items</param>
10+
/// <typeparam name="T">the type of the updated items</typeparam>
11+
/// <remarks>Used via <see cref="WeakReferenceMessenger"/>.</remarks>
512
public class UpdateDataRequest<T>(params T[] affectedItems)
613
{
14+
/// <summary>
15+
/// Gets the items that were updated.
16+
/// </summary>
717
public T[] ItemsAffected => affectedItems;
818
}

0 commit comments

Comments
 (0)