Skip to content

Commit

Permalink
Merge pull request #997 from cbordeman/Friendly-name-filters-995
Browse files Browse the repository at this point in the history
Implemented Name on Quick Filters.
  • Loading branch information
rmcrackan authored Oct 15, 2024
2 parents 4f5df44 + 9da2a44 commit 8a87462
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 169 deletions.
2 changes: 1 addition & 1 deletion Source/AppScaffolding/AppScaffolding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>11.4.1.1</Version>
<Version>11.5</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Octokit" Version="11.0.1" />
Expand Down
72 changes: 62 additions & 10 deletions Source/AppScaffolding/LibationScaffolding.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ApplicationServices;
using ApplicationServices;
using AudibleUtilities;
using Dinah.Core;
using Dinah.Core.IO;
using Dinah.Core.Logging;
using LibationFileManager;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace AppScaffolding
{
public enum ReleaseIdentifier
public enum ReleaseIdentifier
{
None,
WindowsClassic = OS.Windows | Variety.Classic | Architecture.X64,
Expand Down Expand Up @@ -87,7 +88,8 @@ public static void RunPostConfigMigrations(Configuration config)
//

Migrations.migrate_to_v6_6_9(config);
}
Migrations.migrate_to_v11_5_0(config);
}

/// <summary>Initialize logging. Wire-up events. Run after migration</summary>
public static void RunPostMigrationScaffolding(Variety variety, Configuration config)
Expand Down Expand Up @@ -404,5 +406,55 @@ public static void migrate_to_v6_6_9(Configuration config)
UNSAFE_MigrationHelper.Settings_AddUniqueToArray("Serilog.Enrich", "WithExceptionDetails");
}
}
}

class FilterState_6_6_9
{
public bool UseDefault { get; set; }
public List<string> Filters { get; set; } = new();
}

public static void migrate_to_v11_5_0(Configuration config)
{
// Read file, but convert old format to new (with Name field) as necessary.
if (!File.Exists(QuickFilters.JsonFile))
{
QuickFilters.InMemoryState = new();
return;
}

try
{
if (JsonConvert.DeserializeObject<QuickFilters.FilterState>(File.ReadAllText(QuickFilters.JsonFile))
is QuickFilters.FilterState inMemState)
{
QuickFilters.InMemoryState = inMemState;
return;
}
}
catch
{
// Eat
}

try
{
if (JsonConvert.DeserializeObject<FilterState_6_6_9>(File.ReadAllText(QuickFilters.JsonFile))
is FilterState_6_6_9 inMemState)
{
// Copy old structure to new.
QuickFilters.InMemoryState = new();
QuickFilters.InMemoryState.UseDefault = inMemState.UseDefault;
foreach (var oldFilter in inMemState.Filters)
QuickFilters.InMemoryState.Filters.Add(new QuickFilters.NamedFilter(oldFilter, null));

return;
}
Debug.Assert(false, "Should not get here, QuickFilters.json deserialization issue");
}
catch
{
// Eat
}
}
}
}
13 changes: 11 additions & 2 deletions Source/LibationAvalonia/Dialogs/EditQuickFilters.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dialogs="clr-namespace:LibationAvalonia.Dialogs"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="350"
Width="800" Height="450"
x:Class="LibationAvalonia.Dialogs.EditQuickFilters"
Title="Audible Accounts"
Icon="/Assets/libation.ico">
Icon="/Assets/libation.ico"
x:DataType="dialogs:EditQuickFilters">
<Grid RowDefinitions="*,Auto">

<Grid.Styles>
Expand Down Expand Up @@ -43,7 +45,14 @@
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


<DataGridTextColumn
Width="*"
IsReadOnly="False"
Binding="{Binding Name, Mode=TwoWay}"
Header="Name"/>


<DataGridTextColumn
Width="*"
IsReadOnly="False"
Expand Down
21 changes: 17 additions & 4 deletions Source/LibationAvalonia/Dialogs/EditQuickFilters.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ public partial class EditQuickFilters : DialogWindow
public ObservableCollection<Filter> Filters { get; } = new();

public class Filter : ViewModels.ViewModelBase
{
private string _filterString;
{
private string _name;
public string Name
{
get => _name;
set
{
this.RaiseAndSetIfChanged(ref _name, value);
}
}

private string _filterString;
public string FilterString
{
get => _filterString;
Expand All @@ -25,6 +35,9 @@ public string FilterString
}
}
public bool IsDefault { get; private set; } = true;

public QuickFilters.NamedFilter AsNamedFilter() => new(FilterString, Name);

}
public EditQuickFilters()
{
Expand All @@ -40,7 +53,7 @@ public EditQuickFilters()

ControlToFocusOnShow = this.FindControl<Button>(nameof(saveBtn));

var allFilters = QuickFilters.Filters.Select(f => new Filter { FilterString = f }).ToList();
var allFilters = QuickFilters.Filters.Select(f => new Filter { FilterString = f.Filter, Name = f.Name }).ToList();
allFilters.Add(new Filter());

foreach (var f in allFilters)
Expand All @@ -61,7 +74,7 @@ private void Filter_PropertyChanged(object sender, System.ComponentModel.Propert

protected override void SaveAndClose()
{
QuickFilters.ReplaceAll(Filters.Where(f => !f.IsDefault).Select(f => f.FilterString));
QuickFilters.ReplaceAll(Filters.Where(f => !f.IsDefault).Select(x => x.AsNamedFilter()));
base.SaveAndClose();
}

Expand Down
22 changes: 11 additions & 11 deletions Source/LibationAvalonia/ViewModels/MainVM.Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace LibationAvalonia.ViewModels
{
partial class MainVM
{
private string lastGoodFilter = "";
private string _filterString;
private QuickFilters.NamedFilter lastGoodFilter = new(string.Empty, null);
private QuickFilters.NamedFilter _selectedNamedFilter;
private bool _firstFilterIsDefault = true;

/// <summary> Library filterting query </summary>
public string FilterString { get => _filterString; set => this.RaiseAndSetIfChanged(ref _filterString, value); }
public QuickFilters.NamedFilter SelectedNamedFilter { get => _selectedNamedFilter; set => this.RaiseAndSetIfChanged(ref _selectedNamedFilter, value); }
public AvaloniaList<Control> QuickFilterMenuItems { get; } = new();
/// <summary> Indicates if the first quick filter is the default filter </summary>
public bool FirstFilterIsDefault { get => _firstFilterIsDefault; set => QuickFilters.UseDefault = this.RaiseAndSetIfChanged(ref _firstFilterIsDefault, value); }
Expand Down Expand Up @@ -50,19 +50,19 @@ private void Configure_Filters()
QuickFilterMenuItems.Add(new Separator());
}

public void AddQuickFilterBtn() => QuickFilters.Add(FilterString);
public async Task FilterBtn() => await PerformFilter(FilterString);
public void AddQuickFilterBtn() => QuickFilters.Add(SelectedNamedFilter);
public async Task FilterBtn() => await PerformFilter(SelectedNamedFilter);
public async Task FilterHelpBtn() => await new LibationAvalonia.Dialogs.SearchSyntaxDialog().ShowDialog(MainWindow);
public void ToggleFirstFilterIsDefault() => FirstFilterIsDefault = !FirstFilterIsDefault;
public async Task EditQuickFiltersAsync() => await new LibationAvalonia.Dialogs.EditQuickFilters().ShowDialog(MainWindow);
public async Task PerformFilter(string filterString)
public async Task PerformFilter(QuickFilters.NamedFilter namedFilter)
{
FilterString = filterString;
SelectedNamedFilter = namedFilter;

try
{
await ProductsDisplay.Filter(filterString);
lastGoodFilter = filterString;
await ProductsDisplay.Filter(namedFilter.Filter);
lastGoodFilter = namedFilter;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -99,8 +99,8 @@ private void updateFiltersMenu(object _ = null, object __ = null)
{
var command = ReactiveCommand.Create(async () => await PerformFilter(filter));

var menuItem = new MenuItem { Header = $"{++index}: {filter}", Command = command };
var nativeMenuItem = new NativeMenuItem { Header = $"{index}: {filter}", Command = command };
var menuItem = new MenuItem { Header = $"{++index}: {(string.IsNullOrWhiteSpace(filter.Name) ? filter.Filter : filter.Name)}", Command = command };
var nativeMenuItem = new NativeMenuItem { Header = $"{index}: {(string.IsNullOrWhiteSpace(filter.Name) ? filter.Filter : filter.Name)}", Command = command };

if (Configuration.IsMacOs && index <= 10)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/LibationAvalonia/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
<Button IsVisible="{CompiledBinding RemoveButtonsVisible}" Command="{CompiledBinding DoneRemovingBtn}" Content="Done Removing Books"/>
</StackPanel>

<TextBox Grid.Column="1" Margin="10,0,0,0" Name="filterSearchTb" IsVisible="{CompiledBinding !RemoveButtonsVisible}" Text="{CompiledBinding FilterString, Mode=TwoWay}" KeyDown="filterSearchTb_KeyPress" />
<TextBox Grid.Column="1" Margin="10,0,0,0" Name="filterSearchTb" IsVisible="{CompiledBinding !RemoveButtonsVisible}" Text="{CompiledBinding SelectedNamedFilter, Mode=TwoWay}" KeyDown="filterSearchTb_KeyPress" />

<StackPanel Grid.Column="2" Height="30" Orientation="Horizontal">
<Button Name="filterBtn" Command="{CompiledBinding FilterBtn}" VerticalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Filter"/>
Expand Down
2 changes: 1 addition & 1 deletion Source/LibationAvalonia/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async void filterSearchTb_KeyPress(object _, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
await ViewModel.PerformFilter(ViewModel.FilterString);
await ViewModel.PerformFilter(ViewModel.SelectedNamedFilter);

// silence the 'ding'
e.Handled = true;
Expand Down
Loading

0 comments on commit 8a87462

Please sign in to comment.