Skip to content

Commit 6ce2cf9

Browse files
committed
Refactor with MVVM
1 parent d0f0edb commit 6ce2cf9

File tree

8 files changed

+260
-199
lines changed

8 files changed

+260
-199
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public App()
9898
.AddTransient<SettingsPanePluginStoreViewModel>()
9999
.AddTransient<SettingsPaneProxyViewModel>()
100100
.AddTransient<SettingsPaneThemeViewModel>()
101+
// Use transient instance for dialog view models because
102+
// settings will change and we need to recreate them
103+
.AddTransient<SelectBrowserViewModel>()
104+
.AddTransient<SelectFileManagerViewModel>()
101105
).Build();
102106
Ioc.Default.ConfigureServices(host.Services);
103107
}

Flow.Launcher/SelectBrowserWindow.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
xmlns:local="clr-namespace:Flow.Launcher"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:ui="http://schemas.modernwpf.com/2019"
9+
xmlns:vm="clr-namespace:Flow.Launcher.ViewModel"
910
Title="{DynamicResource defaultBrowserTitle}"
1011
Width="550"
12+
d:DataContext="{d:DesignInstance vm:SelectBrowserViewModel}"
1113
Background="{DynamicResource PopuBGColor}"
12-
DataContext="{Binding RelativeSource={RelativeSource Self}}"
1314
Foreground="{DynamicResource PopupTextColor}"
1415
ResizeMode="NoResize"
1516
SizeToContent="Height"
@@ -97,11 +98,11 @@
9798
</ComboBox>
9899
<Button
99100
Margin="10 0 0 0"
100-
Click="btnAdd_Click"
101+
Command="{Binding AddCommand}"
101102
Content="{DynamicResource add}" />
102103
<Button
103104
Margin="10 0 0 0"
104-
Click="btnDelete_Click"
105+
Command="{Binding DeleteCommand}"
105106
Content="{DynamicResource delete}"
106107
IsEnabled="{Binding CustomBrowser.Editable}" />
107108

Flow.Launcher/SelectBrowserWindow.xaml.cs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
1-
using System.Collections.ObjectModel;
2-
using System.Linq;
3-
using System.Windows;
1+
using System.Windows;
42
using System.Windows.Controls;
5-
using CommunityToolkit.Mvvm.ComponentModel;
6-
using Flow.Launcher.Infrastructure.UserSettings;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
4+
using Flow.Launcher.ViewModel;
75

86
namespace Flow.Launcher
97
{
10-
[INotifyPropertyChanged]
118
public partial class SelectBrowserWindow : Window
129
{
13-
private readonly Settings _settings;
10+
private readonly SelectBrowserViewModel _viewModel;
1411

15-
private int selectedCustomBrowserIndex;
16-
17-
public int SelectedCustomBrowserIndex
18-
{
19-
get => selectedCustomBrowserIndex;
20-
set
21-
{
22-
selectedCustomBrowserIndex = value;
23-
OnPropertyChanged(nameof(CustomBrowser));
24-
}
25-
}
26-
27-
public ObservableCollection<CustomBrowserViewModel> CustomBrowsers { get; }
28-
29-
public CustomBrowserViewModel CustomBrowser => CustomBrowsers[SelectedCustomBrowserIndex];
30-
31-
public SelectBrowserWindow(Settings settings)
12+
public SelectBrowserWindow()
3213
{
33-
_settings = settings;
34-
CustomBrowsers = new ObservableCollection<CustomBrowserViewModel>(_settings.CustomBrowserList.Select(x => x.Copy()));
35-
SelectedCustomBrowserIndex = _settings.CustomBrowserIndex;
14+
_viewModel = Ioc.Default.GetRequiredService<SelectBrowserViewModel>();
15+
DataContext = _viewModel;
3616
InitializeComponent();
3717
}
3818

@@ -43,32 +23,19 @@ private void btnCancel_Click(object sender, RoutedEventArgs e)
4323

4424
private void btnDone_Click(object sender, RoutedEventArgs e)
4525
{
46-
_settings.CustomBrowserList = CustomBrowsers.ToList();
47-
_settings.CustomBrowserIndex = SelectedCustomBrowserIndex;
48-
Close();
49-
}
50-
51-
private void btnAdd_Click(object sender, RoutedEventArgs e)
52-
{
53-
CustomBrowsers.Add(new()
26+
if (_viewModel.SaveSettings())
5427
{
55-
Name = "New Profile"
56-
});
57-
SelectedCustomBrowserIndex = CustomBrowsers.Count - 1;
58-
}
59-
60-
private void btnDelete_Click(object sender, RoutedEventArgs e)
61-
{
62-
CustomBrowsers.RemoveAt(SelectedCustomBrowserIndex--);
28+
Close();
29+
}
6330
}
6431

6532
private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
6633
{
67-
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
34+
var dlg = new Microsoft.Win32.OpenFileDialog();
6835
var result = dlg.ShowDialog();
6936
if (result == true)
7037
{
71-
TextBox path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox");
38+
var path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox");
7239
path.Text = dlg.FileName;
7340
path.Focus();
7441
((Button)sender).Focus();

Flow.Launcher/SelectFileManagerWindow.xaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
xmlns:local="clr-namespace:Flow.Launcher"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:ui="http://schemas.modernwpf.com/2019"
9+
xmlns:vm="clr-namespace:Flow.Launcher.ViewModel"
910
Title="{DynamicResource fileManagerWindow}"
1011
Width="600"
12+
d:DataContext="{d:DesignInstance vm:SelectFileManagerViewModel}"
1113
Background="{DynamicResource PopuBGColor}"
12-
DataContext="{Binding RelativeSource={RelativeSource Self}}"
1314
Foreground="{DynamicResource PopupTextColor}"
1415
ResizeMode="NoResize"
1516
SizeToContent="Height"
@@ -78,7 +79,8 @@
7879
x:Name="btnTips"
7980
Margin="0 7 0 7"
8081
HorizontalAlignment="Left"
81-
Click="btnTips_Click"
82+
Command="{Binding OpenFilesTipsCommand}"
83+
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
8284
Content="{DynamicResource fileManager_files_btn}" />
8385
<TextBlock Margin="10 4 0 4" VerticalAlignment="Center">
8486
<Hyperlink NavigateUri="https://www.flowlauncher.com/docs/#/filemanager" RequestNavigate="Hyperlink_RequestNavigate">
@@ -115,11 +117,11 @@
115117
</ComboBox>
116118
<Button
117119
Margin="10 0 0 0"
118-
Click="btnAdd_Click"
120+
Command="{Binding AddCommand}"
119121
Content="{DynamicResource add}" />
120122
<Button
121123
Margin="10 0 0 0"
122-
Click="btnDelete_Click"
124+
Command="{Binding DeleteCommand}"
123125
Content="{DynamicResource delete}"
124126
IsEnabled="{Binding CustomExplorer.Editable}" />
125127

Flow.Launcher/SelectFileManagerWindow.xaml.cs

Lines changed: 13 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,48 @@
1-
using System;
2-
using System.Collections.ObjectModel;
3-
using System.ComponentModel;
4-
using System.Diagnostics;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Windows;
1+
using System.Windows;
82
using System.Windows.Controls;
9-
using System.Windows.Documents;
103
using System.Windows.Navigation;
11-
using CommunityToolkit.Mvvm.ComponentModel;
12-
using Flow.Launcher.Infrastructure.UserSettings;
4+
using CommunityToolkit.Mvvm.DependencyInjection;
135
using Flow.Launcher.ViewModel;
14-
using ModernWpf.Controls;
156

167
namespace Flow.Launcher
178
{
18-
[INotifyPropertyChanged]
199
public partial class SelectFileManagerWindow : Window
2010
{
21-
private readonly Settings _settings;
11+
private readonly SelectFileManagerViewModel _viewModel;
2212

23-
private int selectedCustomExplorerIndex;
24-
25-
public int SelectedCustomExplorerIndex
13+
public SelectFileManagerWindow()
2614
{
27-
get => selectedCustomExplorerIndex;
28-
set
29-
{
30-
selectedCustomExplorerIndex = value;
31-
OnPropertyChanged(nameof(CustomExplorer));
32-
}
33-
}
34-
35-
public ObservableCollection<CustomExplorerViewModel> CustomExplorers { get; }
36-
37-
public CustomExplorerViewModel CustomExplorer => CustomExplorers[SelectedCustomExplorerIndex];
38-
39-
public SelectFileManagerWindow(Settings settings)
40-
{
41-
_settings = settings;
42-
CustomExplorers = new ObservableCollection<CustomExplorerViewModel>(_settings.CustomExplorerList.Select(x => x.Copy()));
43-
SelectedCustomExplorerIndex = _settings.CustomExplorerIndex;
15+
_viewModel = Ioc.Default.GetRequiredService<SelectFileManagerViewModel>();
16+
DataContext = _viewModel;
4417
InitializeComponent();
4518
}
4619

4720
private void btnCancel_Click(object sender, RoutedEventArgs e)
4821
{
4922
Close();
5023
}
51-
52-
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
53-
{
54-
App.API.OpenUrl(e.Uri.AbsoluteUri);
55-
e.Handled = true;
56-
}
5724

5825
private void btnDone_Click(object sender, RoutedEventArgs e)
5926
{
60-
// Check if the selected file manager path is valid
61-
if (!IsFileManagerValid(CustomExplorer.Path))
27+
if (_viewModel.SaveSettings())
6228
{
63-
var result = App.API.ShowMsgBox(
64-
string.Format(App.API.GetTranslation("fileManagerPathNotFound"),
65-
CustomExplorer.Name, CustomExplorer.Path),
66-
App.API.GetTranslation("fileManagerPathError"),
67-
MessageBoxButton.YesNo,
68-
MessageBoxImage.Warning);
69-
70-
if (result == MessageBoxResult.No)
71-
{
72-
return;
73-
}
29+
Close();
7430
}
75-
76-
_settings.CustomExplorerList = CustomExplorers.ToList();
77-
_settings.CustomExplorerIndex = SelectedCustomExplorerIndex;
78-
Close();
7931
}
8032

81-
private bool IsFileManagerValid(string path)
82-
{
83-
if (string.Equals(path, "explorer", StringComparison.OrdinalIgnoreCase))
84-
return true;
85-
86-
if (Path.IsPathRooted(path))
87-
{
88-
return File.Exists(path);
89-
}
90-
91-
try
92-
{
93-
var process = new Process
94-
{
95-
StartInfo = new ProcessStartInfo
96-
{
97-
FileName = "where",
98-
Arguments = path,
99-
RedirectStandardOutput = true,
100-
UseShellExecute = false,
101-
CreateNoWindow = true
102-
}
103-
};
104-
process.Start();
105-
string output = process.StandardOutput.ReadToEnd();
106-
process.WaitForExit();
107-
108-
return !string.IsNullOrEmpty(output);
109-
}
110-
catch
111-
{
112-
return false;
113-
}
114-
}
115-
116-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
117-
private async void btnTips_Click(object sender, RoutedEventArgs e)
118-
{
119-
var tipText = (string)Application.Current.Resources["fileManager_files_tips"];
120-
var url = "https://files.community/docs/contributing/updates";
121-
122-
var textBlock = new TextBlock
123-
{
124-
FontSize = 14,
125-
TextWrapping = TextWrapping.Wrap,
126-
Margin = new Thickness(0, 0, 0, 0)
127-
};
128-
129-
textBlock.Inlines.Add(tipText);
130-
131-
var hyperlink = new Hyperlink
132-
{
133-
NavigateUri = new Uri(url)
134-
};
135-
hyperlink.Inlines.Add(url);
136-
hyperlink.RequestNavigate += (s, args) =>
137-
{
138-
App.API.OpenUrl(args.Uri.AbsoluteUri);
139-
args.Handled = true;
140-
};
141-
142-
textBlock.Inlines.Add(hyperlink);
143-
144-
var tipsDialog = new ContentDialog()
145-
{
146-
Owner = Window.GetWindow((DependencyObject)sender),
147-
Title = (string)Application.Current.Resources["fileManager_files_btn"],
148-
Content = textBlock,
149-
PrimaryButtonText = (string)Application.Current.Resources["commonOK"],
150-
CornerRadius = new CornerRadius(8),
151-
Style = (Style)Application.Current.Resources["ContentDialog"]
152-
};
153-
154-
await tipsDialog.ShowAsync();
155-
}
156-
157-
private void btnAdd_Click(object sender, RoutedEventArgs e)
158-
{
159-
CustomExplorers.Add(new()
160-
{
161-
Name = "New Profile"
162-
});
163-
SelectedCustomExplorerIndex = CustomExplorers.Count - 1;
164-
}
165-
166-
private void btnDelete_Click(object sender, RoutedEventArgs e)
33+
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
16734
{
168-
CustomExplorers.RemoveAt(SelectedCustomExplorerIndex--);
35+
App.API.OpenUrl(e.Uri.AbsoluteUri);
36+
e.Handled = true;
16937
}
17038

17139
private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
17240
{
173-
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
41+
var dlg = new Microsoft.Win32.OpenFileDialog();
17442
var result = dlg.ShowDialog();
17543
if (result == true)
17644
{
177-
TextBox path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox");
45+
var path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox");
17846
path.Text = dlg.FileName;
17947
path.Focus();
18048
((Button)sender).Focus();

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ private void SelectNode()
335335
[RelayCommand]
336336
private void SelectFileManager()
337337
{
338-
var fileManagerChangeWindow = new SelectFileManagerWindow(Settings);
338+
var fileManagerChangeWindow = new SelectFileManagerWindow();
339339
fileManagerChangeWindow.ShowDialog();
340340
}
341341

342342
[RelayCommand]
343343
private void SelectBrowser()
344344
{
345-
var browserWindow = new SelectBrowserWindow(Settings);
345+
var browserWindow = new SelectBrowserWindow();
346346
browserWindow.ShowDialog();
347347
}
348348
}

0 commit comments

Comments
 (0)