Skip to content

Commit d598559

Browse files
committed
Add persistent disk storage.
1 parent 0d13726 commit d598559

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

SimpleList/App.xaml.cs

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
using System;
88
using System.Reflection;
99

10-
// To learn more about WinUI, the WinUI project structure,
11-
// and more about our project templates, see: http://aka.ms/winui-project-info.
12-
1310
namespace SimpleList
1411
{
1512
/// <summary>

SimpleList/Pages/CloudPage.xaml.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
using SimpleList.ViewModels;
44
using SimpleList.Views;
55
using System;
6-
using System.Collections.Generic;
7-
using System.Collections.ObjectModel;
8-
using Windows.ApplicationModel.DataTransfer;
9-
using Windows.Storage;
10-
using CommunityToolkit.Mvvm.DependencyInjection;
11-
using System.Linq;
12-
using System.Threading.Tasks;
13-
using Windows.System;
14-
using SimpleList.Models;
6+
using System.Diagnostics.CodeAnalysis;
157

168
namespace SimpleList.Pages
179
{
1810
public sealed partial class CloudPage : Page
1911
{
12+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
2013
public CloudPage()
2114
{
2215
InitializeComponent();
2316
DataContext = new CloudViewModel();
17+
Loaded += async (sender, args) =>
18+
{
19+
await (DataContext as CloudViewModel).LoadDrivesFromDisk();
20+
};
2421
}
2522

2623
private async void ShowCreateDriveDialogAsync(object sender, RoutedEventArgs e)

SimpleList/SimpleList.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<AssemblyName>SimpleList</AssemblyName>
2929
<Authors>Thawne</Authors>
3030
<PackageProjectUrl>https://github.com/aiguoli/SimpleList</PackageProjectUrl>
31-
<AssemblyVersion>1.11.1</AssemblyVersion>
31+
<AssemblyVersion>1.11.2</AssemblyVersion>
3232
<ApplicationIcon>Assets\favicon.ico</ApplicationIcon>
3333
<PackageIcon>128.png</PackageIcon>
3434
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

SimpleList/ViewModels/CloudViewModel.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
using CommunityToolkit.Mvvm.ComponentModel;
22
using System.Collections.ObjectModel;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.IO;
35
using System.Linq;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
48

59
namespace SimpleList.ViewModels
610
{
711
public partial class CloudViewModel : ObservableObject
812
{
13+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
914
public CloudViewModel()
1015
{
16+
Drives.CollectionChanged += async (sender, args) =>
17+
{
18+
await SaveDrivesToDisk();
19+
};
1120
}
1221

1322
public void AddDrive(DriveViewModel drive)
@@ -20,6 +29,30 @@ public DriveViewModel GetDrive(string name)
2029
return Drives.FirstOrDefault(d => d.DisplayName == name);
2130
}
2231

23-
public ObservableCollection<DriveViewModel> Drives { get; set; } = new();
32+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
33+
private async Task SaveDrivesToDisk()
34+
{
35+
string jsonData = JsonSerializer.Serialize(Drives);
36+
string cachePath = Path.Combine(Directory.GetCurrentDirectory(), "cache");
37+
Directory.CreateDirectory(cachePath);
38+
await File.WriteAllTextAsync(Path.Combine(cachePath, "drives.json"), jsonData);
39+
}
40+
41+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)")]
42+
public async Task LoadDrivesFromDisk()
43+
{
44+
if (File.Exists(cacheFilePath))
45+
{
46+
string jsonData = await File.ReadAllTextAsync(cacheFilePath);
47+
ObservableCollection<DriveViewModel> drives = JsonSerializer.Deserialize<ObservableCollection<DriveViewModel>>(jsonData);
48+
foreach (DriveViewModel drive in drives)
49+
{
50+
Drives.Add(drive);
51+
}
52+
}
53+
}
54+
55+
private string cacheFilePath = Path.Combine(Directory.GetCurrentDirectory(), "cache", "drives.json");
56+
[ObservableProperty] private ObservableCollection<DriveViewModel> drives = new();
2457
}
2558
}

0 commit comments

Comments
 (0)