bugfix and refactor

1. Fix issues with uid.
2. Implement #1486.
This commit is contained in:
Vic 2022-10-25 17:39:01 +08:00
parent 2f9b19ddfc
commit 874383bd07
6 changed files with 74 additions and 47 deletions

View file

@ -55,6 +55,7 @@ namespace Flow.Launcher.Plugin.Program
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
{
string path = Directory.Text;
bool modified = false;
if (!System.IO.Directory.Exists(path))
{
System.Windows.MessageBox.Show(_context.API.GetTranslation("flowlauncher_plugin_program_invalid_path"));
@ -65,18 +66,22 @@ namespace Flow.Launcher.Plugin.Program
if (!ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier.Equals(path, System.StringComparison.InvariantCultureIgnoreCase)))
{
var source = new ProgramSource(path);
modified = true;
_settings.ProgramSources.Insert(0, source);
ProgramSetting.ProgramSettingDisplayList.Add(source);
}
}
else
{
_editing.Location = path;
_editing.Enabled = Chkbox.IsChecked ?? true; // Fixme, need to add to disabled source if not custom source
modified = _editing.Location != path || _editing.Enabled != Chkbox.IsChecked;
if (modified)
{
_editing.SetLocation(path);
_editing.Enabled = Chkbox.IsChecked ?? true;
}
}
DialogResult = true;
DialogResult = modified;
Close();
}
}

View file

@ -197,7 +197,7 @@ namespace Flow.Launcher.Plugin.Program
_settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete));
var t1 = Task.Run(() =>
{
IndexWin32Programs();
IndexUwpPrograms();
_settings.LastIndexTime = DateTime.Today;
});
}
@ -208,7 +208,7 @@ namespace Flow.Launcher.Plugin.Program
_settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete));
var t1 = Task.Run(() =>
{
IndexUwpPrograms();
IndexWin32Programs();
_settings.LastIndexTime = DateTime.Today;
});
}

View file

@ -25,7 +25,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
public class Win32 : IProgram, IEquatable<Win32>
{
public string Name { get; set; }
public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); }
public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); } // For path comparison
public string IcoPath { get; set; }
public string FullPath { get; set; }
public string LnkResolvedPath { get; set; }

View file

@ -52,30 +52,31 @@ namespace Flow.Launcher.Plugin.Program.Views.Commands
internal static void StoreDisabledInSettings()
{
Main._settings.ProgramSources
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled))
.ToList()
.ForEach(t1 => t1.Enabled = false);
// no need since using refernce now
//Main._settings.ProgramSources
// .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled))
// .ToList()
// .ForEach(t1 => t1.Enabled = false);
ProgramSetting.ProgramSettingDisplayList
// Disabled, not in DisabledProgramSources or ProgramSources
var tmp = ProgramSetting.ProgramSettingDisplayList
.Where(t1 => !t1.Enabled
&& !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
.ToList()
.ForEach(x => Main._settings.DisabledProgramSources
.Add(new DisabledProgramSource(x)));
&& !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)
&& !Main._settings.ProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
.Select(x => new DisabledProgramSource(x));
Main._settings.DisabledProgramSources.AddRange(tmp);
}
internal static void RemoveDisabledFromSettings()
{
Main._settings.ProgramSources
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
.ToList()
.ForEach(t1 => t1.Enabled = true);
//Main._settings.ProgramSources
// .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
// .ToList()
// .ForEach(t1 => t1.Enabled = true);
Main._settings.DisabledProgramSources
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
.ToList()
.ForEach(x => Main._settings.DisabledProgramSources.Remove(x));
.RemoveAll(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled));
}
internal static bool IsReindexRequired(this List<ProgramSource> selectedItems)

View file

@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Text.Json.Serialization;
using System.Windows.Media.Imaging;
using Flow.Launcher.Plugin.Program.Programs;
namespace Flow.Launcher.Plugin.Program.Views.Models
@ -16,17 +18,20 @@ namespace Flow.Launcher.Plugin.Program.Views.Models
{
private string name;
public string Location { get; set; }
public string Location { get; private set; }
public string Name { get => name ?? new DirectoryInfo(Location).Name; set => name = value; }
public bool Enabled { get; set; } = true;
/// <summary>
/// Guaranteed lowercase.
/// </summary>
public string UniqueIdentifier { get => uid; set => uid = value.ToLowerInvariant(); }
private string uid { get; set; }
public string UniqueIdentifier { get; private set; }
public ProgramSource() { } // only for json deserialization
[JsonConstructor]
public ProgramSource(string name, string location, bool enabled, string uniqueIdentifier)
{
Location = location;
this.name = name;
Enabled = enabled;
UniqueIdentifier = uniqueIdentifier;
}
/// <summary>
/// Add source by location
@ -37,7 +42,7 @@ namespace Flow.Launcher.Plugin.Program.Views.Models
{
Location = location;
Enabled = enabled;
UniqueIdentifier = location;
UniqueIdentifier = location.ToLowerInvariant(); // For path comparison
}
public ProgramSource(ProgramSource source)
@ -68,13 +73,21 @@ namespace Flow.Launcher.Plugin.Program.Views.Models
public override int GetHashCode()
{
return HashCode.Combine(uid);
return HashCode.Combine(UniqueIdentifier);
}
public void SetLocation(string value)
{
if (Location == value) return;
Location = value;
UniqueIdentifier = value.ToLowerInvariant(); // Update
}
}
public class DisabledProgramSource : ProgramSource
{
public DisabledProgramSource() { } // only for json deserialization
[JsonConstructor]
public DisabledProgramSource(string name, string location, bool enabled, string uniqueIdentifier) : base(name, location, enabled, uniqueIdentifier) { }
public DisabledProgramSource(string location) : base(location, false) { }

View file

@ -149,19 +149,34 @@ namespace Flow.Launcher.Plugin.Program.Views
private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e)
{
var selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null)
EditProgramSource(selectedProgramSource);
}
private void EditProgramSource(ProgramSource selectedProgramSource)
{
if (selectedProgramSource == null)
{
string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source");
MessageBox.Show(msg);
}
else
{
var add = new AddProgramSource(selectedProgramSource, _settings);
if (add.ShowDialog() ?? false)
{
if (selectedProgramSource.Enabled)
{
ProgramSettingDisplay.SetProgramSourcesStatus(new List<ProgramSource> { selectedProgramSource }, true); // sync status in win32, uwp and disabled
ProgramSettingDisplay.RemoveDisabledFromSettings();
}
else
{
ProgramSettingDisplay.SetProgramSourcesStatus(new List<ProgramSource> { selectedProgramSource }, false);
ProgramSettingDisplay.StoreDisabledInSettings();
}
ReIndexing();
}
}
else
{
string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source");
MessageBox.Show(msg);
}
}
private void btnReindex_Click(object sender, RoutedEventArgs e)
@ -352,14 +367,7 @@ namespace Flow.Launcher.Plugin.Program.Views
private void programSourceView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null)
{
var add = new AddProgramSource(selectedProgramSource, _settings);
if (add.ShowDialog() ?? false)
{
ReIndexing();
}
}
EditProgramSource(selectedProgramSource);
}
private bool IsAllItemsUserAdded(List<ProgramSource> items)