Skip to content

Commit 5ad4afe

Browse files
committed
feature: finisied the history load and save
1 parent 76d43dd commit 5ad4afe

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

Avalonia.CpuLimiter/App.axaml.cs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
using Avalonia.CpuLimiter.Services;
44
using Avalonia.CpuLimiter.ViewModels;
55
using Avalonia.CpuLimiter.Views;
6+
using Avalonia.CpuLimiter;
67
using Avalonia.Markup.Xaml;
78
using Microsoft.Extensions.DependencyInjection;
89
using System;
10+
using System.Collections.Generic;
911
using System.Globalization;
12+
using System.Linq;
13+
using System.Threading.Tasks;
14+
using Avalonia.CpuLimiter.Models;
1015

1116
namespace Avalonia.CpuLimiter
1217
{
@@ -17,26 +22,35 @@ public override void Initialize()
1722
AvaloniaXamlLoader.Load(this);
1823
}
1924

20-
public override void OnFrameworkInitializationCompleted()
25+
26+
private readonly MainWindowViewModel _mainWindowViewModel = new MainWindowViewModel();
27+
28+
public override async void OnFrameworkInitializationCompleted()
2129
{
22-
Assets.Resources.Culture = new CultureInfo("zh-hans-cn");
30+
Lang.Resources.Culture = new CultureInfo("zh-hans-cn");
31+
32+
2333
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
2434
{
2535
desktop.MainWindow = new MainWindow
2636
{
27-
DataContext = new MainWindowViewModel(),
37+
DataContext = _mainWindowViewModel
2838
};
2939

3040
var services = new ServiceCollection();
3141

3242
services.AddSingleton<IFilesService>(x => new FilesService(desktop.MainWindow));
3343

3444
Services = services.BuildServiceProvider();
35-
}
36-
3745

46+
desktop.ShutdownRequested += DesktopOnShutdownRequested;
47+
}
3848

3949
base.OnFrameworkInitializationCompleted();
50+
51+
// init and load from config.json
52+
53+
await InitMainWindowViewModel();
4054
}
4155

4256
// services provider
@@ -45,5 +59,44 @@ public override void OnFrameworkInitializationCompleted()
4559
public new static App? Current => Application.Current as App;
4660

4761
public IServiceProvider? Services { get; private set; }
62+
63+
private bool _canClose;
64+
65+
// save config to local json file
66+
// handler to handler the close app event is triggered.
67+
private async void DesktopOnShutdownRequested(object? sender, ShutdownRequestedEventArgs e)
68+
{
69+
e.Cancel = !_canClose;
70+
71+
if(!_canClose)
72+
{
73+
IEnumerable<HistoryItem> itemToSave = _mainWindowViewModel.HistoryItems.Select(item => item.GetHistoryItem());
74+
75+
await HistoryItemFileService.SaveHistoryToFileAsync(itemToSave);
76+
77+
_canClose = true;
78+
79+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
80+
{
81+
desktop.Shutdown();
82+
}
83+
}
84+
}
85+
86+
87+
// load config from json
88+
89+
private async Task InitMainWindowViewModel()
90+
{
91+
IEnumerable<HistoryItem>? loadItem = await HistoryItemFileService.LoadHistoryFromFileAsync();
92+
93+
if (loadItem != null)
94+
{
95+
foreach (HistoryItem item in loadItem)
96+
{
97+
_mainWindowViewModel.HistoryItems.Add(new HistoryItemViewModel(item));
98+
}
99+
}
100+
}
48101
}
49102
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.Json;
5+
using System.Threading.Tasks;
6+
using Avalonia.CpuLimiter.Models;
7+
8+
namespace Avalonia.CpuLimiter.Services;
9+
10+
public class HistoryItemFileService
11+
{
12+
private static string _configPath =
13+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Avalonia.CpuLimiter", "history.json");
14+
15+
16+
// the save config service should with save code in App.axaml.cs
17+
public static async Task SaveHistoryToFileAsync(IEnumerable<HistoryItem>? historyItems)
18+
{
19+
if(!Directory.Exists(Path.GetDirectoryName(_configPath)))
20+
Directory.CreateDirectory(Path.GetDirectoryName(_configPath)!);
21+
22+
using (FileStream fs = File.Open(_configPath, FileMode.Create))
23+
{
24+
await JsonSerializer.SerializeAsync(fs, historyItems, _jsonSerializerOptions);
25+
}
26+
}
27+
28+
29+
// this load config service should work with load code in App.axaml.cs
30+
31+
public static async Task<IEnumerable<HistoryItem>?> LoadHistoryFromFileAsync()
32+
{
33+
try
34+
{
35+
using (FileStream fs = File.OpenRead(_configPath))
36+
{
37+
return await JsonSerializer.DeserializeAsync<IEnumerable<HistoryItem>>(fs ,_jsonSerializerOptions);
38+
}
39+
}
40+
catch (Exception e) when (e is FileNotFoundException or DirectoryNotFoundException)
41+
{
42+
Console.WriteLine(e);
43+
return null;
44+
}
45+
}
46+
47+
private static JsonSerializerOptions _jsonSerializerOptions = new()
48+
{
49+
WriteIndented = true
50+
};
51+
52+
}

0 commit comments

Comments
 (0)