Skip to content

Commit c0408b8

Browse files
author
Yair Aichenbaum
committed
Improved SettingsViewModel
1 parent ef1394d commit c0408b8

File tree

8 files changed

+93
-168
lines changed

8 files changed

+93
-168
lines changed

Files/UserControls/ModernSidebar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ModernSidebar()
1717
this.InitializeComponent();
1818

1919
// Check if the acrylic sidebar setting is on
20-
if (App.AppSettings.SidebarThemeMode == Enums.SidebarOpacity.AcrylicEnabled)
20+
if (App.AppSettings.AcrylicSidebar == true)
2121
{
2222
this.Background = (Brush)Application.Current.Resources["BackgroundAcrylicBrush"];
2323
SidebarNavView.Resources["NavigationViewExpandedPaneBackground"] = Application.Current.Resources["BackgroundAcrylicBrush"];

Files/UserControls/Sidebar.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ public Sidebar()
2727
{
2828
this.InitializeComponent();
2929

30-
// Check if the acrylic sidebar setting is on
31-
if (App.AppSettings.SidebarThemeMode == Enums.SidebarOpacity.AcrylicEnabled)
32-
{
33-
this.Background = (Brush)Application.Current.Resources["BackgroundAcrylicBrush"];
34-
SidebarNavView.Resources["NavigationViewExpandedPaneBackground"] = Application.Current.Resources["BackgroundAcrylicBrush"];
35-
}
36-
else
37-
{
38-
this.Background = (Brush)Application.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"];
39-
SidebarNavView.Resources["NavigationViewExpandedPaneBackground"] = (Brush)Application.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"];
40-
}
4130
}
4231

4332
private INavigationControlItem _SelectedSidebarItem;

Files/View Models/SettingsViewModel.cs

Lines changed: 87 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using System.Diagnostics;
1111
using System.IO;
1212
using System.Linq;
13+
using System.Reflection;
14+
using System.Runtime.CompilerServices;
1315
using Windows.ApplicationModel;
1416
using Windows.Storage;
1517
using Windows.UI;
@@ -20,6 +22,8 @@ namespace Files.View_Models
2022
{
2123
public class SettingsViewModel : ViewModelBase
2224
{
25+
private readonly ApplicationDataContainer _roamingSettings;
26+
2327
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
2428

2529
public DrivesManager DrivesManager { get; }
@@ -29,52 +33,21 @@ public SettingsViewModel()
2933
DetectCustomLocations();
3034
DetectApplicationTheme();
3135
DetectDateTimeFormat();
32-
DetectStorageItemPreferences();
33-
DetectSidebarOpacity();
3436
PinSidebarLocationItems();
35-
DetectOneDrivePreference();
36-
DetectConfirmDeletePreference();
3737
DrivesManager = new DrivesManager();
3838

39+
_roamingSettings = ApplicationData.Current.RoamingSettings;
40+
3941
foundDrives = DrivesManager.Drives;
4042
//DetectWSLDistros();
4143
LoadTerminalApps();
4244
}
4345

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-
7246
private void PinSidebarLocationItems()
7347
{
7448
AddDefaultLocations();
7549
PopulatePinnedSidebarItems();
7650
}
77-
7851
private void AddDefaultLocations()
7952
{
8053
var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForViewIndependentUse();
@@ -249,45 +222,6 @@ private async void DetectWSLDistros()
249222
}
250223
}
251224

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-
291225
private void DetectDateTimeFormat()
292226
{
293227
if (localSettings.Values[LocalSettings.DateTimeFormat] != null)
@@ -393,8 +327,6 @@ private async void LoadTerminalApps()
393327
Terminals = terminals;
394328
}
395329

396-
private SidebarOpacity _SidebarThemeMode = SidebarOpacity.Opaque;
397-
398330
private IList<TerminalModel> _Terminals = null;
399331
public IList<TerminalModel> Terminals
400332
{
@@ -438,34 +370,6 @@ public bool AreLinuxFilesSupported
438370
set => Set(ref _AreLinuxFilesSupported, value);
439371
}
440372

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-
469373
private bool _ShowConfirmDeleteDialog = true;
470374
public bool ShowConfirmDeleteDialog
471375
{
@@ -636,19 +540,15 @@ public bool ShowRibbonContent
636540
}
637541
}
638542
}
639-
640-
private string _ToggleLayoutModeIcon = ""; // List View
641543
public string ToggleLayoutModeIcon
642544
{
643-
get => _ToggleLayoutModeIcon;
644-
set => Set(ref _ToggleLayoutModeIcon, value);
545+
get => Get(""); // List View;
546+
set => Set(value);
645547
}
646-
647-
private Int16 _LayoutMode = 0; // List View
648-
public Int16 LayoutMode
548+
public Int32 LayoutMode
649549
{
650-
get => _LayoutMode;
651-
set => Set(ref _LayoutMode, value);
550+
get => Get(0); // List View
551+
set => Set(value);
652552
}
653553

654554
private RelayCommand toggleLayoutMode;
@@ -678,21 +578,16 @@ public void UpdateToggleLayouModeIcon()
678578
}
679579
}
680580

681-
public SidebarOpacity SidebarThemeMode
581+
public bool AcrylicSidebar
682582
{
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);
696591
}
697592

698593
private TimeStyle _DisplayedTimeStyle = TimeStyle.Application;
@@ -720,5 +615,72 @@ public void Dispose()
720615
{
721616
DrivesManager.Dispose();
722617
}
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);
723685
}
724686
}

Files/Views/InstanceTabsView.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public InstanceTabsView()
6767
}
6868

6969
// Check if the acrylic sidebar setting is on
70-
if (App.AppSettings.SidebarThemeMode == Enums.SidebarOpacity.AcrylicEnabled)
70+
if (App.AppSettings.AcrylicSidebar == true)
7171
{
7272
this.Background = (Brush)Application.Current.Resources["BackgroundAcrylicBrush"];
7373
}

Files/Views/SettingsPages/Appearance.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="using:Files"
77
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
88
mc:Ignorable="d">
99

@@ -99,7 +99,7 @@
9999

100100
<ToggleSwitch
101101
x:Name="AcrylicSidebarSwitch"
102-
Grid.Column="2"
102+
Grid.Column="2" IsOn="{x:Bind local:App.AppSettings.AcrylicSidebar, Mode=TwoWay}"
103103
Width="40"
104104
HorizontalAlignment="Right"
105105
OffContent=""

Files/Views/SettingsPages/Appearance.xaml.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,6 @@ public Appearance()
6868
//await TimeFormatReminder.Fade(value: 0.0f, duration: 1500, delay: 0).StartAsync();
6969
};
7070
};
71-
72-
73-
AcrylicSidebarSwitch.IsOn = App.AppSettings.SidebarThemeMode.Equals(SidebarOpacity.Opaque) ? false : true;
74-
75-
AcrylicSidebarSwitch.Loaded += (sender, args) =>
76-
{
77-
AcrylicSidebarSwitch.Toggled += (o, eventArgs) =>
78-
{
79-
if (((ToggleSwitch)o).IsOn)
80-
{
81-
App.AppSettings.SidebarThemeMode = SidebarOpacity.AcrylicEnabled;
82-
}
83-
else
84-
{
85-
App.AppSettings.SidebarThemeMode = SidebarOpacity.Opaque;
86-
}
87-
};
88-
};
8971
}
9072

9173
}

Files/Views/SettingsPages/FilesAndFolders.xaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@
7777
Grid.Column="1"
7878
Width="40"
7979
HorizontalAlignment="Right"
80-
IsOn="{x:Bind local:App.AppSettings.ShowFileExtensions, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
80+
IsOn="{x:Bind local:App.AppSettings.ShowFileExtensions, Mode=TwoWay}"
8181
OffContent=""
82-
OnContent=""
83-
Toggled="FileExtensionsToggle_Toggled" />
82+
OnContent="" />
8483
</Grid>
8584

8685
<Grid HorizontalAlignment="Stretch">

Files/Views/SettingsPages/FilesAndFolders.xaml.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,5 @@ public FilesAndFolders()
1616
{
1717
InitializeComponent();
1818
}
19-
20-
private void FileExtensionsToggle_Toggled(object sender, RoutedEventArgs e)
21-
{
22-
FileExtensionsToggle.IsEnabled = false;
23-
App.AppSettings.ShowFileExtensions = FileExtensionsToggle.IsOn;
24-
FileExtensionsToggle.IsEnabled = true;
25-
}
2619
}
2720
}

0 commit comments

Comments
 (0)