-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBaseRoamingSettingsDataStore.cs
More file actions
242 lines (207 loc) · 7.44 KB
/
BaseRoamingSettingsDataStore.cs
File metadata and controls
242 lines (207 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.Storage;
namespace CommunityToolkit.Uwp.Graph.Helpers.RoamingSettings
{
/// <summary>
/// A base class for easily building roaming settings helper implementations.
/// </summary>
public abstract class BaseRoamingSettingsDataStore : IRoamingSettingsDataStore
{
/// <inheritdoc />
public EventHandler SyncCompleted { get; set; }
/// <inheritdoc />
public EventHandler SyncFailed { get; set; }
/// <inheritdoc />
public bool AutoSync { get; }
/// <inheritdoc />
public string Id { get; }
/// <inheritdoc />
public string UserId { get; }
/// <inheritdoc />
public IDictionary<string, object> Cache { get; private set; }
/// <summary>
/// Gets an object serializer for converting objects in the data store.
/// </summary>
protected IObjectSerializer Serializer { get; }
/// <summary>
/// Initializes a new instance of the <see cref="BaseRoamingSettingsDataStore"/> class.
/// </summary>
/// <param name="userId">The id of the target Graph user.</param>
/// <param name="dataStoreId">A unique id for the data store.</param>
/// <param name="objectSerializer">An IObjectSerializer used for serializing objects.</param>
/// <param name="autoSync">Determines if the data store should sync for every interaction.</param>
public BaseRoamingSettingsDataStore(string userId, string dataStoreId, IObjectSerializer objectSerializer, bool autoSync = true)
{
AutoSync = autoSync;
Id = dataStoreId;
UserId = userId;
Serializer = objectSerializer;
Cache = null;
}
/// <summary>
/// Create a new instance of the data storage container.
/// </summary>
/// <returns>A task.</returns>
public abstract Task Create();
/// <summary>
/// Delete the instance of the data storage container.
/// </summary>
/// <returns>A task.</returns>
public abstract Task Delete();
/// <inheritdoc />
public bool KeyExists(string key)
{
return Cache != null && Cache.ContainsKey(key);
}
/// <inheritdoc />
public bool KeyExists(string compositeKey, string key)
{
if (KeyExists(compositeKey))
{
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Cache[compositeKey];
if (composite != null)
{
return composite.ContainsKey(key);
}
}
return false;
}
/// <inheritdoc />
public T Read<T>(string key, T @default = default)
{
if (Cache != null && Cache.TryGetValue(key, out object value))
{
try
{
return Serializer.Deserialize<T>((string)value);
}
catch
{
// Primitive types can't be deserialized.
return (T)Convert.ChangeType(value, typeof(T));
}
}
return @default;
}
/// <inheritdoc />
public T Read<T>(string compositeKey, string key, T @default = default)
{
if (Cache != null)
{
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Cache[compositeKey];
if (composite != null)
{
object value = composite[key];
if (value != null)
{
try
{
return Serializer.Deserialize<T>((string)value);
}
catch
{
// Primitive types can't be deserialized.
return (T)Convert.ChangeType(value, typeof(T));
}
}
}
}
return @default;
}
/// <inheritdoc />
public void Save<T>(string key, T value)
{
InitCache();
// Skip serialization for primitives.
if (typeof(T) == typeof(object) || Type.GetTypeCode(typeof(T)) != TypeCode.Object)
{
// Update the cache
Cache[key] = value;
}
else
{
// Update the cache
Cache[key] = Serializer.Serialize(value);
}
if (AutoSync)
{
// Update the remote
Task.Run(() => Sync());
}
}
/// <inheritdoc />
public void Save<T>(string compositeKey, IDictionary<string, T> values)
{
InitCache();
if (KeyExists(compositeKey))
{
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Cache[compositeKey];
foreach (KeyValuePair<string, T> setting in values.ToList())
{
if (composite.ContainsKey(setting.Key))
{
composite[setting.Key] = Serializer.Serialize(setting.Value);
}
else
{
composite.Add(setting.Key, Serializer.Serialize(setting.Value));
}
}
// Update the cache
Cache[compositeKey] = composite;
if (AutoSync)
{
// Update the remote
Task.Run(() => Sync());
}
}
else
{
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
foreach (KeyValuePair<string, T> setting in values.ToList())
{
composite.Add(setting.Key, Serializer.Serialize(setting.Value));
}
// Update the cache
Cache[compositeKey] = composite;
if (AutoSync)
{
// Update the remote
Task.Run(() => Sync());
}
}
}
/// <inheritdoc />
public abstract Task<bool> FileExistsAsync(string filePath);
/// <inheritdoc />
public abstract Task<T> ReadFileAsync<T>(string filePath, T @default = default);
/// <inheritdoc />
public abstract Task<StorageFile> SaveFileAsync<T>(string filePath, T value);
/// <inheritdoc />
public abstract Task Sync();
/// <summary>
/// Initialize the internal cache.
/// </summary>
protected void InitCache()
{
if (Cache == null)
{
Cache = new Dictionary<string, object>();
}
}
/// <summary>
/// Delete the internal cache.
/// </summary>
protected void DeleteCache()
{
Cache = null;
}
}
}