10
10
using System . Diagnostics ;
11
11
using System . IO ;
12
12
using System . Linq ;
13
+ using System . Reflection ;
14
+ using System . Runtime . CompilerServices ;
13
15
using Windows . ApplicationModel ;
14
16
using Windows . Storage ;
15
17
using Windows . UI ;
@@ -20,6 +22,8 @@ namespace Files.View_Models
20
22
{
21
23
public class SettingsViewModel : ViewModelBase
22
24
{
25
+ private readonly ApplicationDataContainer _roamingSettings ;
26
+
23
27
ApplicationDataContainer localSettings = ApplicationData . Current . LocalSettings ;
24
28
25
29
public DrivesManager DrivesManager { get ; }
@@ -29,52 +33,21 @@ public SettingsViewModel()
29
33
DetectCustomLocations ( ) ;
30
34
DetectApplicationTheme ( ) ;
31
35
DetectDateTimeFormat ( ) ;
32
- DetectStorageItemPreferences ( ) ;
33
- DetectSidebarOpacity ( ) ;
34
36
PinSidebarLocationItems ( ) ;
35
- DetectOneDrivePreference ( ) ;
36
- DetectConfirmDeletePreference ( ) ;
37
37
DrivesManager = new DrivesManager ( ) ;
38
38
39
+ _roamingSettings = ApplicationData . Current . RoamingSettings ;
40
+
39
41
foundDrives = DrivesManager . Drives ;
40
42
//DetectWSLDistros();
41
43
LoadTerminalApps ( ) ;
42
44
}
43
45
44
- private void DetectConfirmDeletePreference ( )
45
- {
46
- if ( localSettings . Values [ "ShowConfirmDeleteDialog" ] == null ) { localSettings . Values [ "ShowConfirmDeleteDialog" ] = true ; }
47
-
48
- if ( ( bool ) localSettings . Values [ "ShowConfirmDeleteDialog" ] == true )
49
- {
50
- ShowConfirmDeleteDialog = true ;
51
- }
52
- else
53
- {
54
- ShowConfirmDeleteDialog = false ;
55
- }
56
- }
57
-
58
- private void DetectStorageItemPreferences ( )
59
- {
60
- if ( localSettings . Values [ "ShowFileExtensions" ] == null ) { ShowFileExtensions = true ; }
61
-
62
- if ( ( bool ) localSettings . Values [ "ShowFileExtensions" ] == true )
63
- {
64
- ShowFileExtensions = true ;
65
- }
66
- else
67
- {
68
- ShowFileExtensions = false ;
69
- }
70
- }
71
-
72
46
private void PinSidebarLocationItems ( )
73
47
{
74
48
AddDefaultLocations ( ) ;
75
49
PopulatePinnedSidebarItems ( ) ;
76
50
}
77
-
78
51
private void AddDefaultLocations ( )
79
52
{
80
53
var resourceLoader = Windows . ApplicationModel . Resources . ResourceLoader . GetForViewIndependentUse ( ) ;
@@ -249,45 +222,6 @@ private async void DetectWSLDistros()
249
222
}
250
223
}
251
224
252
- private void DetectOneDrivePreference ( )
253
- {
254
- if ( localSettings . Values [ "PinOneDrive" ] == null ) { localSettings . Values [ "PinOneDrive" ] = true ; }
255
-
256
- if ( ( bool ) localSettings . Values [ "PinOneDrive" ] == true )
257
- {
258
- PinOneDriveToSideBar = true ;
259
- }
260
- else
261
- {
262
- PinOneDriveToSideBar = false ;
263
- }
264
-
265
- try
266
- {
267
- StorageFolder . GetFolderFromPathAsync ( OneDrivePath ) ;
268
- }
269
- catch ( Exception )
270
- {
271
- PinOneDriveToSideBar = false ;
272
- }
273
- }
274
-
275
- private void DetectSidebarOpacity ( )
276
- {
277
- if ( localSettings . Values [ "acrylicSidebar" ] != null )
278
- {
279
- switch ( localSettings . Values [ "acrylicSidebar" ] )
280
- {
281
- case true :
282
- SidebarThemeMode = SidebarOpacity . AcrylicEnabled ;
283
- break ;
284
- case false :
285
- SidebarThemeMode = SidebarOpacity . Opaque ;
286
- break ;
287
- }
288
- }
289
- }
290
-
291
225
private void DetectDateTimeFormat ( )
292
226
{
293
227
if ( localSettings . Values [ LocalSettings . DateTimeFormat ] != null )
@@ -393,8 +327,6 @@ private async void LoadTerminalApps()
393
327
Terminals = terminals ;
394
328
}
395
329
396
- private SidebarOpacity _SidebarThemeMode = SidebarOpacity . Opaque ;
397
-
398
330
private IList < TerminalModel > _Terminals = null ;
399
331
public IList < TerminalModel > Terminals
400
332
{
@@ -438,34 +370,6 @@ public bool AreLinuxFilesSupported
438
370
set => Set ( ref _AreLinuxFilesSupported , value ) ;
439
371
}
440
372
441
- private bool _ShowFileExtensions = true ;
442
- public bool ShowFileExtensions
443
- {
444
- get => _ShowFileExtensions ;
445
- set
446
- {
447
- if ( localSettings . Values [ "ShowFileExtensions" ] == null )
448
- {
449
- localSettings . Values [ "ShowFileExtensions" ] = value ;
450
- }
451
- else
452
- {
453
- if ( value != _ShowFileExtensions )
454
- {
455
- Set ( ref _ShowFileExtensions , value ) ;
456
- if ( value == true )
457
- {
458
- localSettings . Values [ "ShowFileExtensions" ] = true ;
459
- }
460
- else
461
- {
462
- localSettings . Values [ "ShowFileExtensions" ] = false ;
463
- }
464
- }
465
- }
466
- }
467
- }
468
-
469
373
private bool _ShowConfirmDeleteDialog = true ;
470
374
public bool ShowConfirmDeleteDialog
471
375
{
@@ -636,19 +540,15 @@ public bool ShowRibbonContent
636
540
}
637
541
}
638
542
}
639
-
640
- private string _ToggleLayoutModeIcon = "" ; // List View
641
543
public string ToggleLayoutModeIcon
642
544
{
643
- get => _ToggleLayoutModeIcon ;
644
- set => Set ( ref _ToggleLayoutModeIcon , value ) ;
545
+ get => Get ( "" ) ; // List View ;
546
+ set => Set ( value ) ;
645
547
}
646
-
647
- private Int16 _LayoutMode = 0 ; // List View
648
- public Int16 LayoutMode
548
+ public Int32 LayoutMode
649
549
{
650
- get => _LayoutMode ;
651
- set => Set ( ref _LayoutMode , value ) ;
550
+ get => Get ( 0 ) ; // List View
551
+ set => Set ( value ) ;
652
552
}
653
553
654
554
private RelayCommand toggleLayoutMode ;
@@ -678,21 +578,16 @@ public void UpdateToggleLayouModeIcon()
678
578
}
679
579
}
680
580
681
- public SidebarOpacity SidebarThemeMode
581
+ public bool AcrylicSidebar
682
582
{
683
- get => _SidebarThemeMode ;
684
- set
685
- {
686
- Set ( ref _SidebarThemeMode , value ) ;
687
- if ( value . Equals ( SidebarOpacity . Opaque ) )
688
- {
689
- localSettings . Values [ "acrylicSidebar" ] = false ;
690
- }
691
- else
692
- {
693
- localSettings . Values [ "acrylicSidebar" ] = true ;
694
- }
695
- }
583
+ get => Get ( false ) ;
584
+ set => Set ( value ) ;
585
+ }
586
+
587
+ public bool ShowFileExtensions
588
+ {
589
+ get => Get ( true ) ;
590
+ set => Set ( value ) ;
696
591
}
697
592
698
593
private TimeStyle _DisplayedTimeStyle = TimeStyle . Application ;
@@ -720,5 +615,72 @@ public void Dispose()
720
615
{
721
616
DrivesManager . Dispose ( ) ;
722
617
}
618
+
619
+ public bool Set < TValue > ( TValue value , [ CallerMemberName ] string propertyName = null )
620
+ {
621
+ propertyName = propertyName != null && propertyName . StartsWith ( "set_" , StringComparison . InvariantCultureIgnoreCase )
622
+ ? propertyName . Substring ( 4 )
623
+ : propertyName ;
624
+
625
+ TValue originalValue = default ;
626
+
627
+ if ( _roamingSettings . Values . ContainsKey ( propertyName ) )
628
+ {
629
+ originalValue = Get ( originalValue , propertyName ) ;
630
+
631
+ if ( ! base . Set ( ref originalValue , value , propertyName ) ) return false ;
632
+ }
633
+
634
+ _roamingSettings . Values [ propertyName ] = value ;
635
+
636
+ return true ;
637
+ }
638
+
639
+ public TValue Get < TValue > ( TValue defaultValue , [ CallerMemberName ] string propertyName = null )
640
+ {
641
+ var name = propertyName ??
642
+ throw new ArgumentNullException ( nameof ( propertyName ) , "Cannot store property of unnamed." ) ;
643
+
644
+ name = name . StartsWith ( "get_" , StringComparison . InvariantCultureIgnoreCase )
645
+ ? propertyName . Substring ( 4 )
646
+ : propertyName ;
647
+
648
+ if ( _roamingSettings . Values . ContainsKey ( name ) )
649
+ {
650
+ var value = _roamingSettings . Values [ name ] ;
651
+
652
+ if ( ! ( value is TValue tValue ) )
653
+ {
654
+ if ( value is IConvertible )
655
+ {
656
+ tValue = ( TValue ) Convert . ChangeType ( value , typeof ( TValue ) ) ;
657
+ }
658
+ else
659
+ {
660
+ var valueType = value . GetType ( ) ;
661
+ var tryParse = typeof ( TValue ) . GetMethod ( "TryParse" , BindingFlags . Instance | BindingFlags . Public ) ;
662
+
663
+ if ( tryParse == null ) return default ;
664
+
665
+ var stringValue = value . ToString ( ) ;
666
+ tValue = default ;
667
+
668
+ var tryParseDelegate =
669
+ ( TryParseDelegate < TValue > ) Delegate . CreateDelegate ( valueType , tryParse , false ) ;
670
+
671
+ tValue = ( tryParseDelegate ? . Invoke ( stringValue , out tValue ) ?? false ) ? tValue : default ;
672
+ }
673
+
674
+ Set ( tValue , propertyName ) ; // Put the corrected value in settings.
675
+ return tValue ;
676
+ }
677
+
678
+ return tValue ;
679
+ }
680
+
681
+ return defaultValue ;
682
+ }
683
+
684
+ delegate bool TryParseDelegate < TValue > ( string inValue , out TValue parsedValue ) ;
723
685
}
724
686
}
0 commit comments