Skip to content

Commit b2f4375

Browse files
authored
Merge pull request MicaApps#93 from MicaApps/gan601
启动时尝试建库, 5.19 graph降到5.11版本解决莫名其妙的空引用异常, DefaultFolderTaskAsync返回Task
2 parents d929a40 + 3402f5e commit b2f4375

File tree

6 files changed

+67
-117
lines changed

6 files changed

+67
-117
lines changed

Mail/App.xaml.cs

Lines changed: 29 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
using System;
2-
using System.Data;
3-
using System.Diagnostics;
4-
using Windows.ApplicationModel;
5-
using Windows.ApplicationModel.Activation;
6-
using Windows.ApplicationModel.Core;
7-
using Windows.Storage;
8-
using Windows.UI.Xaml;
9-
using Windows.UI.Xaml.Controls;
10-
using Chloe;
1+
using Chloe;
112
using Chloe.Infrastructure;
123
using Mail.Extensions;
134
using Mail.Pages;
@@ -16,104 +7,65 @@
167
using Microsoft.Extensions.Caching.Memory;
178
using Microsoft.Extensions.DependencyInjection;
189
using Microsoft.Toolkit.Uwp.Helpers;
10+
using System;
11+
using System.IO;
12+
using System.Threading.Tasks;
13+
using Windows.ApplicationModel;
14+
using Windows.ApplicationModel.Activation;
15+
using Windows.ApplicationModel.Core;
16+
using Windows.Storage;
17+
using Windows.UI.Xaml;
1918

2019
namespace Mail
2120
{
2221
sealed partial class App : Application
2322
{
24-
public static IServiceProvider Services = null!;
23+
public static IServiceProvider Services { get; private set; }
2524

2625
public App()
2726
{
2827
InitializeComponent();
29-
var services = new ServiceCollection();
30-
RegisterServices(services);
31-
Services = services.BuildServiceProvider();
28+
Services = RegisterServices();
29+
}
3230

33-
Suspending += OnSuspending;
31+
private async Task InitalizeDatabaseAsync()
32+
{
33+
await Services.GetService<IDbContext>().Session.ExecuteReaderAsync(await PathIO.ReadTextAsync(Path.Combine(Package.Current.InstalledPath, "Assets", "sql.sql")));
3434
}
3535

36-
private void RegisterServices(IServiceCollection services)
36+
private IServiceProvider RegisterServices()
3737
{
38-
var connectionString = $"DataSource={ApplicationData.Current.LocalFolder.Path}\\mail.db";
39-
#if DEBUG
40-
Trace.WriteLine($"{nameof(connectionString)}Path: {connectionString}");
41-
#endif
42-
services.AddSingleton<OutlookService, OutlookService>()
43-
.AddSingleton<ICacheService, CacheService>()
44-
.AddSingleton<LocalCacheService>()
45-
.AddSingleton<IMemoryCache>(_ => new MemoryCache(new MemoryCacheOptions()))
46-
.AddTransient<IDbContext>(_ =>
47-
{
48-
var db = new CustomDbContext(new SQLiteConnectionFactory(connectionString));
49-
return db;
50-
})
51-
.BuildServiceProvider();
38+
return new ServiceCollection().AddSingleton<OutlookService, OutlookService>()
39+
.AddSingleton<ICacheService, CacheService>()
40+
.AddSingleton<LocalCacheService>()
41+
.AddSingleton<IMemoryCache>(_ => new MemoryCache(new MemoryCacheOptions()))
42+
.AddTransient<IDbContext>(_ => new CustomDbContext(new DbConnectionFactory(() => new SqliteConnection($"DataSource={ApplicationData.Current.LocalFolder.Path}\\mail.db"))))
43+
.BuildServiceProvider();
5244
}
5345

5446
protected override void OnWindowCreated(WindowCreatedEventArgs args)
5547
{
5648
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
5749
}
5850

59-
protected override void OnLaunched(LaunchActivatedEventArgs e)
51+
protected override async void OnLaunched(LaunchActivatedEventArgs e)
6052
{
61-
OnLaunchedOrActivated(e);
53+
await HandleLaunchedOrActivatedAsync(e);
6254
}
6355

64-
protected override void OnActivated(IActivatedEventArgs args)
56+
protected override async void OnActivated(IActivatedEventArgs args)
6557
{
66-
OnLaunchedOrActivated(args);
58+
await HandleLaunchedOrActivatedAsync(args);
6759
}
6860

69-
private void OnLaunchedOrActivated(IActivatedEventArgs args)
61+
private async Task HandleLaunchedOrActivatedAsync(IActivatedEventArgs args)
7062
{
7163
SystemInformation.Instance.TrackAppUse(args);
7264

73-
if (Window.Current.Content is Frame RootFrame)
74-
{
75-
//Do something on activate
76-
}
77-
else
78-
{
79-
Window.Current.Content = new SplashPage(args.SplashScreen,
80-
args.PreviousExecutionState == ApplicationExecutionState.Terminated);
81-
}
65+
await InitalizeDatabaseAsync();
8266

67+
Window.Current.Content = new SplashPage(args.SplashScreen, args.PreviousExecutionState == ApplicationExecutionState.Terminated);
8368
Window.Current.Activate();
8469
}
85-
86-
private void OnSuspending(object sender, SuspendingEventArgs e)
87-
{
88-
var Deferral = e.SuspendingOperation.GetDeferral();
89-
90-
try
91-
{
92-
//TODO: 保存应用程序状态并停止任何后台活动
93-
}
94-
catch (Exception)
95-
{
96-
}
97-
finally
98-
{
99-
Deferral.Complete();
100-
}
101-
}
102-
}
103-
}
104-
105-
public class SQLiteConnectionFactory : IDbConnectionFactory
106-
{
107-
private readonly string _connString;
108-
109-
public SQLiteConnectionFactory(string connString)
110-
{
111-
_connString = connString;
112-
}
113-
114-
public IDbConnection CreateConnection()
115-
{
116-
var conn = new SqliteConnection(_connString);
117-
return conn;
11870
}
11971
}

Mail/sql.sql renamed to Mail/Assets/sql.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
create table main.MailFolderData
1+
create table if not exists main.MailFolderData
22
(
33
Id NVARCHAR(255) not null
44
primary key,
@@ -11,7 +11,7 @@
1111
ChildFolderCount INTEGER not null
1212
);
1313

14-
create table main.MailMessageContentData
14+
create table if not exists main.MailMessageContentData
1515
(
1616
MessageId NVARCHAR(255) not null
1717
primary key,
@@ -20,7 +20,7 @@ create table main.MailMessageContentData
2020
ContentType MEDIUMINT not null
2121
);
2222

23-
create table main.MailMessageData
23+
create table if not exists main.MailMessageData
2424
(
2525
MessageId NVARCHAR(255) not null
2626
primary key,
@@ -30,7 +30,7 @@ create table main.MailMessageData
3030
SentTime DATETIME
3131
);
3232

33-
create table main.MailMessageRecipientData
33+
create table if not exists main.MailMessageRecipientData
3434
(
3535
Id integer not null
3636
constraint MailMessageRecipientData_pk
@@ -41,3 +41,5 @@ create table main.MailMessageRecipientData
4141
RecipientType MEDIUMINT not null
4242
);
4343

44+
create index if not exists MailMessageRecipientData_MessageId_index
45+
on MailMessageRecipientData (MessageId);

Mail/Extensions/CustomDbContext.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Data;
3-
using System.Threading.Tasks;
4-
using Chloe.Infrastructure;
1+
using Chloe.Infrastructure;
52
using Chloe.SQLite;
63
using Mail.Services.Data.Enums;
4+
using System;
5+
using System.Data;
6+
using System.Threading.Tasks;
77

88
namespace Mail.Extensions;
99

@@ -17,20 +17,22 @@ public class CustomDbContext : SQLiteContext
1717
{
1818
public CustomDbContext(IDbConnectionFactory dbConnectionFactory) : base(dbConnectionFactory)
1919
{
20+
2021
}
2122

22-
public CustomDbContext(IDbConnectionFactory dbConnectionFactory, bool concurrencyMode) : base(dbConnectionFactory,
23-
concurrencyMode)
23+
public CustomDbContext(IDbConnectionFactory dbConnectionFactory, bool concurrencyMode) : base(dbConnectionFactory, concurrencyMode)
2424
{
25+
2526
}
2627

2728
public CustomDbContext(Func<IDbConnection> dbConnectionFactory) : base(dbConnectionFactory)
2829
{
30+
2931
}
3032

31-
public CustomDbContext(Func<IDbConnection> dbConnectionFactory, bool concurrencyMode) : base(dbConnectionFactory,
32-
concurrencyMode)
33+
public CustomDbContext(Func<IDbConnection> dbConnectionFactory, bool concurrencyMode) : base(dbConnectionFactory, concurrencyMode)
3334
{
35+
3436
}
3537

3638
protected override Task<int> Delete<TEntity>(TEntity entity, string table, bool async)

Mail/Mail.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
<Content Include="Assets\Wide310x150Logo.scale-125.png" />
243243
<Content Include="Assets\Wide310x150Logo.scale-150.png" />
244244
<Content Include="Assets\Wide310x150Logo.scale-400.png" />
245-
<None Include="sql.sql"/>
245+
<Content Include="Assets\sql.sql"/>
246246
<Content Include="Properties\Default.rd.xml" />
247247
<Content Include="Assets\LockScreenLogo.scale-200.png" />
248248
<Content Include="Assets\SplashScreen.scale-200.png" />

Mail/Pages/MailFolderDetailsPage.xaml.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Windows.Storage;
99
using Windows.Storage.Pickers;
1010
using Windows.UI;
11-
using Windows.UI.Core;
1211
using Windows.UI.Xaml;
1312
using Windows.UI.Xaml.Controls;
1413
using Windows.UI.Xaml.Input;
@@ -24,6 +23,7 @@
2423
using Microsoft.Toolkit.Uwp.Helpers;
2524
using Microsoft.Toolkit.Uwp.UI.Controls;
2625
using Nito.AsyncEx;
26+
using Windows.UI.Core;
2727

2828
namespace Mail.Pages
2929
{
@@ -262,21 +262,15 @@ private async void ListDetailsView_Tapped(object sender, TappedRoutedEventArgs e
262262
}
263263
}
264264

265-
private readonly Regex Rgx = new("cid:[^\"]+");
266-
267265
private async Task LoadImageAndCacheAsync(MailMessageListDetailViewModel model, WebView browser)
268266
{
269-
var attachmentFileList = await Service.GetMailAttachmentFileAsync(model).ToListAsync();
267+
IReadOnlyList<MailMessageFileAttachmentData> attachmentFileList = await Service.GetMailAttachmentFileAsync(model).ToListAsync();
270268

271269
if (browser.DataContext is MailMessageListDetailViewModel context)
272270
{
273271
if (context.Id.Equals(model.Id))
274272
{
275-
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
276-
browser.NavigateToString(ConvertContentTheme(
277-
Rgx.Replace(model.Content,
278-
Match => ReplaceHtmlInnerImageCidToBase64(Match, attachmentFileList)),
279-
model.ContentType == MailMessageContentType.Text)));
273+
browser.NavigateToString(ConvertContentTheme(new Regex("cid:[^\"]+").Replace(model.Content, Match => ReplaceHtmlInnerImageCidToBase64(Match, attachmentFileList)), model.ContentType == MailMessageContentType.Text));
280274
}
281275
}
282276
}

Mail/Services/OutlookService.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
4-
using System.Diagnostics;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Runtime.CompilerServices;
8-
using System.Threading;
9-
using System.Threading.Tasks;
10-
using Windows.Storage;
11-
using Windows.Storage.FileProperties;
121
using CommunityToolkit.Authentication;
132
using Mail.Extensions.Graph;
143
using Mail.Models;
@@ -26,6 +15,17 @@
2615
using Microsoft.IdentityModel.Tokens;
2716
using Microsoft.Toolkit.Uwp.Helpers;
2817
using Newtonsoft.Json;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Collections.ObjectModel;
21+
using System.Diagnostics;
22+
using System.IO;
23+
using System.Linq;
24+
using System.Runtime.CompilerServices;
25+
using System.Threading;
26+
using System.Threading.Tasks;
27+
using Windows.Storage;
28+
using Windows.Storage.FileProperties;
2929

3030
namespace Mail.Services
3131
{
@@ -114,7 +114,6 @@ async IAsyncEnumerable<MailMessageData> IMailService.IFocusFilterSupport.GetMail
114114
}
115115
}
116116

117-
118117
private async Task<MailMessageData> GenAndSaveMailMessageDataAsync(string RootFolderId, Message message,
119118
string type = "Focused")
120119
{
@@ -143,7 +142,7 @@ private async Task<MailMessageData> GenAndSaveMailMessageDataAsync(string RootFo
143142

144143
private GraphServiceClient GetClient() => Client ??= Provider.GetClient();
145144

146-
private async Task<MailFolderCollectionResponse?> DefaultFolderTaskAsync(string name,
145+
private async Task DefaultFolderTaskAsync(string name,
147146
CancellationToken CancellationToken)
148147
{
149148
try
@@ -164,10 +163,8 @@ private async Task<MailMessageData> GenAndSaveMailMessageDataAsync(string RootFo
164163
}
165164
catch (Exception e)
166165
{
167-
Trace.WriteLine(e);
166+
Trace.WriteLine($"{nameof(DefaultFolderTaskAsync)}: {e}");
168167
}
169-
170-
return null;
171168
}
172169

173170
public override async IAsyncEnumerable<MailFolderData> GetMailSuperFoldersAsync(
@@ -262,7 +259,6 @@ private async Task LoadMailChildFolderAsync(MailFolderData ParentMailFolder,
262259
}
263260
}
264261

265-
266262
public override async Task<MailFolderData> GetMailFolderDetailAsync(string RootFolderId,
267263
CancellationToken CancelToken = default)
268264
{
@@ -478,7 +474,11 @@ public override async Task<bool> MailForwardAsync(MailMessageListDetailViewModel
478474
if (message is null) return false;
479475

480476
await me.Messages[Model.Id].Forward.PostAsync(new ForwardPostRequestBody
481-
{ ToRecipients = message.ToRecipients, Comment = ForwardContent });
477+
{
478+
Comment = ForwardContent,
479+
ToRecipients = message.ToRecipients
480+
});
481+
482482
return true;
483483
}
484484

0 commit comments

Comments
 (0)