Browse Source

Added tag search

flabbet 3 years ago
parent
commit
1d59fb1f26

+ 18 - 0
PixiEditor/Models/Events/InputBoxEventArgs.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Models.Events
+{
+    public class InputBoxEventArgs : EventArgs
+    {
+        public string Input { get; set; }
+
+        public InputBoxEventArgs(string input)
+        {
+            Input = input;
+        }
+    }
+}

+ 16 - 5
PixiEditor/Models/ExternalServices/LospecPaletteFetcher.cs

@@ -1,6 +1,7 @@
 using Newtonsoft.Json;
 using PixiEditor.Models.DataHolders;
 using System;
+using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Threading.Tasks;
@@ -10,22 +11,32 @@ namespace PixiEditor.Models.ExternalServices
     public static class LospecPaletteFetcher
     {
         public const string LospecApiUrl = "https://lospec.com/palette-list";
-        public static async Task<PaletteList> FetchPage(int page, string sortingType = "default")
+        public static async Task<PaletteList> FetchPage(int page, string sortingType = "default", string[] tags = null)
         {
             try
             {
                 using (HttpClient client = new HttpClient())
                 {
-                    string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&tag=&sortingType={sortingType}";
+                    string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&sortingType={sortingType}&tag=";
+                    
+                    if(tags != null && tags.Length > 0)
+                    {
+                        url += $"{string.Join(',', tags)}";
+                    }
+
                     HttpResponseMessage response = await client.GetAsync(url);
                     if (response.StatusCode == HttpStatusCode.OK)
                     {
                         string content = await response.Content.ReadAsStringAsync();
                         var obj = JsonConvert.DeserializeObject<PaletteList>(content);
-                        obj.FetchedCorrectly = true;
-                        foreach (var palette in obj.Palettes)
+
+                        obj.FetchedCorrectly = obj.Palettes != null;
+                        if (obj.Palettes != null)
                         {
-                            ReadjustColors(palette.Colors);
+                            foreach (var palette in obj.Palettes)
+                            {
+                                ReadjustColors(palette.Colors);
+                            }
                         }
 
                         return obj;

+ 5 - 4
PixiEditor/Views/Dialogs/LospecPalettesBrowser.xaml

@@ -5,8 +5,8 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:PixiEditor.Views.UserControls.Lospec" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              mc:Ignorable="d" 
-             xmlns:gif="http://wpfanimatedgif.codeplex.com"
-             Title="Palettes Browser" WindowStartupLocation="CenterScreen" MinWidth="200" Height="600" Width="800" WindowStyle="None"
+             xmlns:gif="http://wpfanimatedgif.codeplex.com" xmlns:usercontrols="clr-namespace:PixiEditor.Views.UserControls"
+        Title="Palettes Browser" WindowStartupLocation="CenterScreen" MinWidth="200" Height="600" Width="800" WindowStyle="None"
              Name="lospecPalettesBrowser">
     <Window.Resources>
         <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
@@ -21,7 +21,7 @@
                         Executed="CommandBinding_Executed_Close" />
     </Window.CommandBindings>
 
-    <Grid Background="{StaticResource AccentColor}" FocusVisualStyle="{x:Null}">
+    <Grid Background="{StaticResource AccentColor}" FocusVisualStyle="{x:Null}" Focusable="True" MouseDown="Grid_MouseDown">
         <Grid.RowDefinitions>
             <RowDefinition Height="35" />
             <RowDefinition Height="55"/>
@@ -44,7 +44,8 @@
                     <ComboBoxItem>Newest</ComboBoxItem>
                 </ComboBox>
                 <Label Margin="10 0 0 0" Content="Tags:" Style="{StaticResource BaseLabel}" VerticalAlignment="Center" FontSize="16"/>
-                <TextBox FontSize="16" VerticalAlignment="Center" Style="{StaticResource DarkTextBoxStyle}" Width="150"/>
+                <usercontrols:InputBox OnSubmit="TagsInput_OnSubmit" FontSize="16" VerticalAlignment="Center" 
+                                       Style="{StaticResource DarkTextBoxStyle}" Width="250" />
             </StackPanel>
         </StackPanel>
         <Grid Grid.Row="2" Margin="10">

+ 18 - 2
PixiEditor/Views/Dialogs/LospecPalettesBrowser.xaml.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Events;
 using PixiEditor.Models.ExternalServices;
 using System;
 using System.Collections.Generic;
@@ -58,6 +59,9 @@ namespace PixiEditor.Views.Dialogs
             DependencyProperty.Register("IsFetching", typeof(bool), typeof(LospecPalettesBrowser), new PropertyMetadata(false));
 
         public string SortingType { get; set; } = "Default";
+        public string[] Tags { get; set; } = Array.Empty<string>();
+
+        private char[] _separators = new char[] { ' ', ',' };
 
 
         private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
@@ -81,7 +85,7 @@ namespace PixiEditor.Views.Dialogs
             IsFetching = true;
             if (PaletteList == null || refetch)
             {
-                PaletteList = await LospecPaletteFetcher.FetchPage(0, SortingType.ToLower());
+                PaletteList = await LospecPaletteFetcher.FetchPage(0, SortingType.ToLower(), Tags);
                 OnListFetched.Invoke(PaletteList);
             }
 
@@ -90,7 +94,7 @@ namespace PixiEditor.Views.Dialogs
 
         private async void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
         {
-            if (PaletteList == null) return;
+            if (PaletteList == null || PaletteList.Palettes == null) return;
             var scrollViewer = (ScrollViewer)sender;
             if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
             {
@@ -118,5 +122,17 @@ namespace PixiEditor.Views.Dialogs
                 scrollViewer.ScrollToHome();
             }
         }
+
+        private async void TagsInput_OnSubmit(object sender, InputBoxEventArgs e)
+        {
+            Tags = e.Input.Split(_separators, options: StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
+            await UpdatePaletteList(true);
+            scrollViewer.ScrollToHome();
+        }
+
+        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            ((Grid)sender).Focus();
+        }
     }
 }

+ 59 - 0
PixiEditor/Views/UserControls/InputBox.cs

@@ -0,0 +1,59 @@
+using PixiEditor.Models.Events;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+
+namespace PixiEditor.Views.UserControls
+{
+    public class InputBox : TextBox
+    {
+        public ICommand SubmitCommand
+        {
+            get { return (ICommand)GetValue(SubmitCommandProperty); }
+            set { SetValue(SubmitCommandProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for SubmitCommand.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty SubmitCommandProperty =
+            DependencyProperty.Register("SubmitCommand", typeof(ICommand), typeof(InputBox));
+
+        public object SubmitCommandParameter
+        {
+            get { return (object)GetValue(SubmitCommandParameterProperty); }
+            set { SetValue(SubmitCommandParameterProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for SubmitCommandParameter.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty SubmitCommandParameterProperty =
+            DependencyProperty.Register("SubmitCommandParameter", typeof(object), typeof(InputBox), new PropertyMetadata(null));
+
+        public event EventHandler<InputBoxEventArgs> OnSubmit;
+
+        protected override void OnLostFocus(RoutedEventArgs e)
+        {
+            OnSubmit?.Invoke(this, new InputBoxEventArgs(Text));
+            Keyboard.ClearFocus();
+
+            base.OnLostFocus(e);
+        }
+
+        protected override void OnKeyUp(KeyEventArgs e)
+        {
+            if (e.Key != Key.Enter) return;
+
+            if(SubmitCommand != null && SubmitCommand.CanExecute(SubmitCommandParameter))
+            {
+                SubmitCommand.Execute(SubmitCommandParameter);
+            }
+
+            OnSubmit?.Invoke(this, new InputBoxEventArgs(Text));
+
+            e.Handled = true;
+        }
+    }
+}