Move dialog logic to vm

This commit is contained in:
Vic 2022-11-06 14:52:21 +08:00
parent 977ec33283
commit 5d3e6e02af
2 changed files with 33 additions and 40 deletions

View file

@ -1,7 +1,6 @@
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
@ -9,24 +8,26 @@ namespace Flow.Launcher
{
public partial class CustomShortcutSetting : Window
{
private Settings _settings;
private bool update = false;
private SettingWindowViewModel viewModel;
public string Key { get; set; }
public string Value { get; set; }
public CustomShortcutModel ShortCut;
private string originalKey { get; init; } = null;
private string originalValue { get; init; } = null;
bool update = false;
public CustomShortcutSetting(Settings settings)
public CustomShortcutSetting(SettingWindowViewModel vm)
{
_settings = settings;
viewModel = vm;
InitializeComponent();
}
public CustomShortcutSetting(CustomShortcutModel shortcut, Settings settings)
public CustomShortcutSetting(string key, string value, SettingWindowViewModel vm)
{
Key = shortcut.Key;
Value = shortcut.Value;
ShortCut = shortcut;
_settings = settings;
viewModel = vm;
Key = key;
Value = value;
originalKey = key;
originalValue = value;
update = true;
InitializeComponent();
}
@ -39,32 +40,19 @@ namespace Flow.Launcher
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
{
bool modified = false;
if (String.IsNullOrEmpty(Key) || String.IsNullOrEmpty(Value))
{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut"));
return;
}
if (!update)
// Check if key is modified or adding a new one
if (((update && originalKey != Key) || !update)
&& viewModel.ShortcutExists(Key))
{
ShortCut = new CustomShortcutModel(Key, Value);
if (_settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key))
{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
return;
}
modified = true;
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
return;
}
else
{
if (ShortCut.Key != Key && _settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key))
{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
return;
}
modified = ShortCut.Key != Key || ShortCut.Value != Value;
}
DialogResult = modified;
DialogResult = !update || originalKey != Key || originalValue != Value;
Close();
}

View file

@ -685,12 +685,12 @@ namespace Flow.Launcher.ViewModel
public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; }
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
#endregion
#region shortcut
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
public void DeleteSelectedCustomShortcut()
{
var item = SelectedCustomShortcut;
@ -700,11 +700,10 @@ namespace Flow.Launcher.ViewModel
return;
}
string deleteWarning =
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
string deleteWarning = string.Format(
InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
item?.Key, item?.Value);
if (
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Settings.CustomShortcuts.Remove(item);
@ -720,7 +719,7 @@ namespace Flow.Launcher.ViewModel
return false;
}
var shortcutSettingWindow = new CustomShortcutSetting(item, Settings);
var shortcutSettingWindow = new CustomShortcutSetting(item.Key, item.Value, this);
if (shortcutSettingWindow.ShowDialog() == true)
{
item.Key = shortcutSettingWindow.Key;
@ -732,13 +731,19 @@ namespace Flow.Launcher.ViewModel
public void AddCustomShortcut()
{
var shortcutSettingWindow = new CustomShortcutSetting(Settings);
var shortcutSettingWindow = new CustomShortcutSetting(this);
if (shortcutSettingWindow.ShowDialog() == true)
{
Settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
var shortcut = new CustomShortcutModel(shortcutSettingWindow.Key, shortcutSettingWindow.Value);
Settings.CustomShortcuts.Add(shortcut);
}
}
public bool ShortcutExists(string key)
{
return Settings.CustomShortcuts.Any(x => x.Key == key) || Settings.BuiltinShortcuts.Any(x => x.Key == key);
}
#endregion
#region about