6
6
using System . Globalization ;
7
7
using System . Linq ;
8
8
using System . Threading . Tasks ;
9
+ using Microsoft . Toolkit . Helpers ;
9
10
using Windows . ApplicationModel ;
10
11
using Windows . ApplicationModel . Activation ;
11
12
using Windows . Security . ExchangeActiveSyncProvisioning ;
@@ -25,7 +26,7 @@ public sealed class SystemInformation
25
26
/// <summary>
26
27
/// The <see cref="ApplicationDataStorageHelper"/> instance used to save and retrieve application settings.
27
28
/// </summary>
28
- private readonly ApplicationDataStorageHelper _localObjectStorageHelper = ApplicationDataStorageHelper . GetCurrent ( new Toolkit . Helpers . SystemSerializer ( ) ) ;
29
+ private readonly ISettingsStorageHelper _settingsStorage = ApplicationDataStorageHelper . GetCurrent ( new Toolkit . Helpers . SystemSerializer ( ) ) ;
29
30
30
31
/// <summary>
31
32
/// The starting time of the current application session (since app launch or last move to foreground).
@@ -216,7 +217,7 @@ public TimeSpan AppUptime
216
217
if ( LaunchCount > 0 )
217
218
{
218
219
var subSessionLength = DateTime . UtcNow . Subtract ( _sessionStart ) . Ticks ;
219
- var uptimeSoFar = _localObjectStorageHelper . Read < long > ( nameof ( AppUptime ) ) ;
220
+ var uptimeSoFar = _settingsStorage . Read < long > ( nameof ( AppUptime ) ) ;
220
221
221
222
return new ( uptimeSoFar + subSessionLength ) ;
222
223
}
@@ -232,9 +233,9 @@ public TimeSpan AppUptime
232
233
/// <param name="duration">The amount to time to add</param>
233
234
public void AddToAppUptime ( TimeSpan duration )
234
235
{
235
- var uptimeSoFar = _localObjectStorageHelper . Read < long > ( nameof ( AppUptime ) ) ;
236
+ var uptimeSoFar = _settingsStorage . Read < long > ( nameof ( AppUptime ) ) ;
236
237
237
- _localObjectStorageHelper . Save ( nameof ( AppUptime ) , uptimeSoFar + duration . Ticks ) ;
238
+ _settingsStorage . Save ( nameof ( AppUptime ) , uptimeSoFar + duration . Ticks ) ;
238
239
}
239
240
240
241
/// <summary>
@@ -245,8 +246,8 @@ public void ResetLaunchCount()
245
246
LastResetTime = DateTime . UtcNow ;
246
247
LaunchCount = 0 ;
247
248
248
- _localObjectStorageHelper . Save ( nameof ( LastResetTime ) , LastResetTime . ToFileTimeUtc ( ) ) ;
249
- _localObjectStorageHelper . Save ( nameof ( LaunchCount ) , LaunchCount ) ;
249
+ _settingsStorage . Save ( nameof ( LastResetTime ) , LastResetTime . ToFileTimeUtc ( ) ) ;
250
+ _settingsStorage . Save ( nameof ( LaunchCount ) , LaunchCount ) ;
250
251
}
251
252
252
253
/// <summary>
@@ -258,30 +259,30 @@ public void TrackAppUse(IActivatedEventArgs args, XamlRoot xamlRoot = null)
258
259
{
259
260
if ( args . PreviousExecutionState is ApplicationExecutionState . ClosedByUser or ApplicationExecutionState . NotRunning )
260
261
{
261
- LaunchCount = _localObjectStorageHelper . Read < long > ( nameof ( LaunchCount ) ) + 1 ;
262
- TotalLaunchCount = _localObjectStorageHelper . Read < long > ( nameof ( TotalLaunchCount ) ) + 1 ;
262
+ LaunchCount = _settingsStorage . Read < long > ( nameof ( LaunchCount ) ) + 1 ;
263
+ TotalLaunchCount = _settingsStorage . Read < long > ( nameof ( TotalLaunchCount ) ) + 1 ;
263
264
264
265
// In case we upgraded the properties, make TotalLaunchCount is correct
265
266
if ( TotalLaunchCount < LaunchCount )
266
267
{
267
268
TotalLaunchCount = LaunchCount ;
268
269
}
269
270
270
- _localObjectStorageHelper . Save ( nameof ( LaunchCount ) , LaunchCount ) ;
271
- _localObjectStorageHelper . Save ( nameof ( TotalLaunchCount ) , TotalLaunchCount ) ;
271
+ _settingsStorage . Save ( nameof ( LaunchCount ) , LaunchCount ) ;
272
+ _settingsStorage . Save ( nameof ( TotalLaunchCount ) , TotalLaunchCount ) ;
272
273
273
274
LaunchTime = DateTime . UtcNow ;
274
275
275
- var lastLaunch = _localObjectStorageHelper . Read < long > ( nameof ( LastLaunchTime ) ) ;
276
+ var lastLaunch = _settingsStorage . Read < long > ( nameof ( LastLaunchTime ) ) ;
276
277
277
278
LastLaunchTime = lastLaunch != 0
278
279
? DateTime . FromFileTimeUtc ( lastLaunch )
279
280
: LaunchTime ;
280
281
281
- _localObjectStorageHelper . Save ( nameof ( LastLaunchTime ) , LaunchTime . ToFileTimeUtc ( ) ) ;
282
- _localObjectStorageHelper . Save ( nameof ( AppUptime ) , 0L ) ;
282
+ _settingsStorage . Save ( nameof ( LastLaunchTime ) , LaunchTime . ToFileTimeUtc ( ) ) ;
283
+ _settingsStorage . Save ( nameof ( AppUptime ) , 0L ) ;
283
284
284
- var lastResetTime = _localObjectStorageHelper . Read < long > ( nameof ( LastResetTime ) ) ;
285
+ var lastResetTime = _settingsStorage . Read < long > ( nameof ( LastResetTime ) ) ;
285
286
286
287
LastResetTime = lastResetTime != 0
287
288
? DateTime . FromFileTimeUtc ( lastResetTime )
@@ -321,20 +322,20 @@ private void UpdateVisibility(bool visible)
321
322
else
322
323
{
323
324
var subSessionLength = DateTime . UtcNow . Subtract ( _sessionStart ) . Ticks ;
324
- var uptimeSoFar = _localObjectStorageHelper . Read < long > ( nameof ( AppUptime ) ) ;
325
+ var uptimeSoFar = _settingsStorage . Read < long > ( nameof ( AppUptime ) ) ;
325
326
326
- _localObjectStorageHelper . Save ( nameof ( AppUptime ) , uptimeSoFar + subSessionLength ) ;
327
+ _settingsStorage . Save ( nameof ( AppUptime ) , uptimeSoFar + subSessionLength ) ;
327
328
}
328
329
}
329
330
330
331
private bool DetectIfFirstUse ( )
331
332
{
332
- if ( _localObjectStorageHelper . KeyExists ( nameof ( IsFirstRun ) ) )
333
+ if ( _settingsStorage . KeyExists ( nameof ( IsFirstRun ) ) )
333
334
{
334
335
return false ;
335
336
}
336
337
337
- _localObjectStorageHelper . Save ( nameof ( IsFirstRun ) , true ) ;
338
+ _settingsStorage . Save ( nameof ( IsFirstRun ) , true ) ;
338
339
339
340
return true ;
340
341
}
@@ -347,13 +348,13 @@ private bool DetectIfFirstUse()
347
348
// is ever called. That is, this is either the first time the app has been launched, or the first
348
349
// time a previously existing app has run this method (or has run it after a new update of the app).
349
350
// In this case, save the current version and report the same version as previous version installed.
350
- if ( ! _localObjectStorageHelper . KeyExists ( nameof ( currentVersion ) ) )
351
+ if ( ! _settingsStorage . KeyExists ( nameof ( currentVersion ) ) )
351
352
{
352
- _localObjectStorageHelper . Save ( nameof ( currentVersion ) , currentVersion ) ;
353
+ _settingsStorage . Save ( nameof ( currentVersion ) , currentVersion ) ;
353
354
}
354
355
else
355
356
{
356
- var previousVersion = _localObjectStorageHelper . Read < string > ( nameof ( currentVersion ) ) ;
357
+ var previousVersion = _settingsStorage . Read < string > ( nameof ( currentVersion ) ) ;
357
358
358
359
// There are two possible cases if the "currentVersion" key exists:
359
360
// 1) The previous version is different than the current one. This means that the application
@@ -363,7 +364,7 @@ private bool DetectIfFirstUse()
363
364
// In this case we have nothing to do and just return the previous version installed to be the same.
364
365
if ( currentVersion != previousVersion )
365
366
{
366
- _localObjectStorageHelper . Save ( nameof ( currentVersion ) , currentVersion ) ;
367
+ _settingsStorage . Save ( nameof ( currentVersion ) , currentVersion ) ;
367
368
368
369
return ( true , previousVersion . ToPackageVersion ( ) ) ;
369
370
}
@@ -374,28 +375,28 @@ private bool DetectIfFirstUse()
374
375
375
376
private DateTime DetectFirstUseTime ( )
376
377
{
377
- if ( _localObjectStorageHelper . KeyExists ( nameof ( FirstUseTime ) ) )
378
+ if ( _settingsStorage . KeyExists ( nameof ( FirstUseTime ) ) )
378
379
{
379
- var firstUse = _localObjectStorageHelper . Read < long > ( nameof ( FirstUseTime ) ) ;
380
+ var firstUse = _settingsStorage . Read < long > ( nameof ( FirstUseTime ) ) ;
380
381
381
382
return DateTime . FromFileTimeUtc ( firstUse ) ;
382
383
}
383
384
384
385
DateTime utcNow = DateTime . UtcNow ;
385
386
386
- _localObjectStorageHelper . Save ( nameof ( FirstUseTime ) , utcNow . ToFileTimeUtc ( ) ) ;
387
+ _settingsStorage . Save ( nameof ( FirstUseTime ) , utcNow . ToFileTimeUtc ( ) ) ;
387
388
388
389
return utcNow ;
389
390
}
390
391
391
392
private PackageVersion DetectFirstVersionInstalled ( )
392
393
{
393
- if ( _localObjectStorageHelper . KeyExists ( nameof ( FirstVersionInstalled ) ) )
394
+ if ( _settingsStorage . KeyExists ( nameof ( FirstVersionInstalled ) ) )
394
395
{
395
- return _localObjectStorageHelper . Read < string > ( nameof ( FirstVersionInstalled ) ) . ToPackageVersion ( ) ;
396
+ return _settingsStorage . Read < string > ( nameof ( FirstVersionInstalled ) ) . ToPackageVersion ( ) ;
396
397
}
397
398
398
- _localObjectStorageHelper . Save ( nameof ( FirstVersionInstalled ) , ApplicationVersion . ToFormattedString ( ) ) ;
399
+ _settingsStorage . Save ( nameof ( FirstVersionInstalled ) , ApplicationVersion . ToFormattedString ( ) ) ;
399
400
400
401
return ApplicationVersion ;
401
402
}
0 commit comments