Skip to content

Commit 3f7ddeb

Browse files
committed
Fixing AOT things
1 parent b1ac727 commit 3f7ddeb

8 files changed

Lines changed: 29 additions & 52 deletions

File tree

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList.Android/AdvancedToDoList.Android.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
1111
<AndroidPackageFormat>apk</AndroidPackageFormat>
1212
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
13+
<RunAOTCompilation>true</RunAOTCompilation>
14+
<PublishTrimmed>true</PublishTrimmed>
1315
</PropertyGroup>
1416

1517
<ItemGroup>

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList.Browser/AdvancedToDoList.Browser.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Avalonia.Browser" />
14-
<!-- Use WASM-safe SQLite provider/lib and exclude native e_sqlite3 -->
1514
</ItemGroup>
1615

1716
<ItemGroup>

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

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1+
12
using System;
23
using System.Collections.Generic;
34
using System.Data;
45
using System.IO;
6+
using System.Linq;
7+
using System.Runtime.CompilerServices;
58
using System.Threading.Tasks;
69
using AdvancedToDoList.Models;
710
using AdvancedToDoList.Services;
811
using Avalonia.Controls;
912
using Dapper;
1013
using Microsoft.Data.Sqlite;
1114

15+
[module: DapperAot]
16+
1217
namespace AdvancedToDoList.Helper;
1318

14-
[DapperAot]
1519
public static class DataBaseHelper
1620
{
17-
static DataBaseHelper()
18-
{
19-
// Register a type handler to map between SQLite INTEGER (Int64) and our Priority enum
20-
SqlMapper.AddTypeHandler(new PriorityTypeHandler());
21-
}
22-
2321
private static bool _initialized;
2422

2523
internal static async Task<SqliteConnection> GetOpenConnection()
@@ -96,21 +94,25 @@ public static async Task<IEnumerable<ToDoItem>> GetToDoItemsAsync(bool loadAlsoC
9694
Console.WriteLine("GetToDoItemsAsync");
9795
await using var connection = await GetOpenConnection();
9896
const string sql = """
99-
SELECT t.*, c.*
100-
FROM ToDoItem t
101-
LEFT JOIN Category c ON t.CategoryId = c.Id
102-
WHERE t.Progress < 100 OR @loadAlsoCompletedItems;
97+
SELECT *
98+
FROM ToDoItem
99+
WHERE Progress < 100 OR @loadAlsoCompletedItems;
103100
""";
104101

105-
return await connection.QueryAsync<ToDoItem, Category, ToDoItem>(
102+
var toDoItems = await connection.QueryAsync<ToDoItem>(
106103
sql,
107-
(toDoItem, category) =>
108-
{
109-
toDoItem.Category = category;
110-
return toDoItem;
111-
},
112-
splitOn: "Id",
113-
param: new {loadAlsoCompletedItems});
104+
new {loadAlsoCompletedItems});
105+
106+
var categories = await connection.QueryAsync<Category>("SELECT * FROM Category");
107+
var categoriesDict = new Dictionary<int, Category>(
108+
categories.Select(x => new KeyValuePair<int, Category>(x.Id ?? -1, x)));
109+
110+
foreach (var item in toDoItems)
111+
{
112+
item.Category = categoriesDict.GetValueOrDefault(item.CategoryId ?? -1);
113+
}
114+
115+
return toDoItems;
114116
}
115117

116118
private static async Task EnsureInitializedAsync(SqliteConnection connection)
@@ -184,29 +186,4 @@ INSERT INTO ToDoItem (Id, CategoryId, Title, Priority, Description, DueDate, Pro
184186
"""
185187
);
186188
}
187-
}
188-
189-
public class PriorityTypeHandler : SqlMapper.TypeHandler<Priority>
190-
{
191-
public override void SetValue(IDbDataParameter parameter, Priority value)
192-
{
193-
// Store enum as integer value in SQLite
194-
parameter.Value = (int)value;
195-
}
196-
197-
public override Priority Parse(object value)
198-
{
199-
// SQLite returns INTEGER as Int64 by default; handle common representations robustly
200-
return value switch
201-
{
202-
null => default,
203-
DBNull => default,
204-
long l => (Priority)(int)l,
205-
int i => (Priority)i,
206-
short s => (Priority)s,
207-
byte b => (Priority)b,
208-
string str when int.TryParse(str, out var parsed) => (Priority)parsed,
209-
_ => default
210-
};
211-
}
212189
}

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Models/Category.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace AdvancedToDoList.Models;
66

7-
[DapperAot]
87
public record Category
98
{
109
/// <summary>

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/Models/ToDoItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace AdvancedToDoList.Models;
77

8-
[DapperAot]
98
public class ToDoItem
109
{
1110
/// <summary>
@@ -32,7 +31,7 @@ public class ToDoItem
3231
/// <summary>
3332
/// Gets or sets the Priority of the ToDoItem. The default value is Medium.
3433
/// </summary>
35-
public Priority Priority { get; set; } = Priority.Medium;
34+
public int Priority { get; set; } = (int)Models.Priority.Medium;
3635

3736
/// <summary>
3837
/// Gets or sets the Description of the ToDoItem. This property is optional.

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/ViewModels/ManageToDoItemsViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public ManageToDoItemsViewModel()
6161
private async Task LoadDataAsync()
6262
{
6363
var toDoItems = await DataBaseHelper.GetToDoItemsAsync(ShowAlsoCompletedItems);
64+
6465
_toDoItemsSourceCache.AddOrUpdate(toDoItems.Select(x => new ToDoItemViewModel(x)));
6566
}
6667

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/ViewModels/ToDoItemViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ToDoItemViewModel(ToDoItem toDoItem)
2424
? new CategoryViewModel(toDoItem.Category)
2525
: CategoryViewModel.Empty;
2626
Title = toDoItem.Title;
27-
Priority = toDoItem.Priority;
27+
Priority = (Priority)toDoItem.Priority;
2828
Description = toDoItem.Description;
2929
DueDate = toDoItem.DueDate;
3030
Progress = toDoItem.Progress;
@@ -142,7 +142,7 @@ private async Task SetProgressAsync(int value)
142142
CategoryId = Category.Id,
143143
Category = Category.ToCategory(),
144144
Progress = Progress,
145-
Priority = Priority,
145+
Priority = (int)Priority,
146146
DueDate = DueDate,
147147
CreatedDate = CreatedDate,
148148
CompletedDate = CompletedDate,

src/Avalonia.Samples/CompleteApps/AdvancedToDoList/AdvancedToDoList/ViewModels/_DesignData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public static class _DesignData
77
{
88
static _DesignData()
99
{
10-
var categories = DataBaseHelper.GetCategoriesAsync().Result.ToArray();
10+
var categories = DataBaseHelper.GetCategoriesAsync().Result;
1111
EditCategoryViewModel = new EditCategoryViewModel(new CategoryViewModel(categories.First()));
1212

13-
var toDoItems = DataBaseHelper.GetToDoItemsAsync().Result.ToArray();
13+
var toDoItems = DataBaseHelper.GetToDoItemsAsync().Result;
1414
EditToDoItemViewModel = new EditToDoItemViewModel(new ToDoItemViewModel(toDoItems.First()), categories.Select(x => new CategoryViewModel(x)).ToList());
1515
}
1616

0 commit comments

Comments
 (0)