瀏覽代碼

Added sorting

flabbet 3 年之前
父節點
當前提交
a20f0b69e3

+ 2 - 4
PixiEditor/Models/DataHolders/LospecPalette/PaletteList.cs

@@ -1,10 +1,8 @@
-using System.Collections.Generic;
-
-namespace PixiEditor.Models.DataHolders
+namespace PixiEditor.Models.DataHolders
 {
     public class PaletteList
     {
         public bool FetchedCorrectly { get; set; }
-        public List<Palette> Palettes { get; set; }
+        public ObservableCollection<Palette> Palettes { get; set; }
     }
 }

+ 7 - 3
PixiEditor/Models/ExternalServices/LospecPaletteFetcher.cs

@@ -10,20 +10,24 @@ 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)
+        public static async Task<PaletteList> FetchPage(int page, string sortingType = "default")
         {
             try
             {
                 using (HttpClient client = new HttpClient())
                 {
-                    string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&tag=&sortingType=default";
+                    string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&tag=&sortingType={sortingType}";
                     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;
-                        obj.Palettes.ForEach(x => ReadjustColors(x.Colors));
+                        foreach (var palette in obj.Palettes)
+                        {
+                            ReadjustColors(palette.Colors);
+                        }
+
                         return obj;
                     }
                 }

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

@@ -24,7 +24,9 @@
     <Grid Background="{StaticResource AccentColor}" FocusVisualStyle="{x:Null}">
         <Grid.RowDefinitions>
             <RowDefinition Height="35" />
+            <RowDefinition Height="50"/>
             <RowDefinition Height="1*"/>
+            <RowDefinition Height="20"/>
         </Grid.RowDefinitions>
 
         <DockPanel Grid.Row="0" Background="{StaticResource MainColor}">
@@ -32,13 +34,21 @@
                     WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close"
                     Command="{x:Static SystemCommands.CloseWindowCommand}" />
         </DockPanel>
-        <Grid Grid.Row="1" Margin="10">
+        <StackPanel Background="{StaticResource MainColor}" Orientation="Horizontal" Grid.Row="1">
+            <StackPanel Margin="10" Orientation="Horizontal" VerticalAlignment="Center">
+                <Label Content="Sort by:" Style="{StaticResource BaseLabel}" VerticalAlignment="Center" FontSize="16"/>
+                <ComboBox Name="sortingComboBox" FontSize="16" SelectionChanged="SortingComboBox_SelectionChanged">
+                    <ComboBoxItem IsSelected="True">Default</ComboBoxItem>
+                    <ComboBoxItem>Alphabetical</ComboBoxItem>
+                    <ComboBoxItem>Downloads</ComboBoxItem>
+                    <ComboBoxItem>Newest</ComboBoxItem>
+                </ComboBox>
+            </StackPanel>
+        </StackPanel>
+        <Grid Grid.Row="2" Margin="10">
             <TextBlock Text="Couldn't fetch palettes" Foreground="White" FontSize="20" HorizontalAlignment="Center" 
                        VerticalAlignment="Center" Visibility="{Binding ElementName=itemsControl, 
                 Path=Visibility, Converter={converters:OppositeVisibilityConverter}}"/>
-            <Image gif:ImageBehavior.AnimatedSource="/Images/Processing.gif" HorizontalAlignment="Center" VerticalAlignment="Center"
-                   Visibility="{Binding ElementName=lospecPalettesBrowser, Path=IsFetching, Converter={StaticResource BoolToVisibilityConverter}}"
-                   Height="50" gif:ImageBehavior.AnimationSpeedRatio="1.5"/>
             <ScrollViewer Margin="5" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" ScrollChanged="ScrollViewer_ScrollChanged">
                 <ItemsControl Name="itemsControl" ItemsSource="{Binding ElementName=lospecPalettesBrowser, Path=PaletteList.Palettes}" 
                           Visibility="{Binding ElementName=lospecPalettesBrowser, Path=PaletteList.FetchedCorrectly,
@@ -51,6 +61,10 @@
                     </ItemsControl.ItemTemplate>
                 </ItemsControl>
             </ScrollViewer>
+            <Image gif:ImageBehavior.AnimatedSource="/Images/Processing.gif" HorizontalAlignment="Center" VerticalAlignment="Center"
+                   Visibility="{Binding ElementName=lospecPalettesBrowser, Path=IsFetching, Converter={StaticResource BoolToVisibilityConverter}}"
+                   Height="50" gif:ImageBehavior.AnimationSpeedRatio="1.5"/>
         </Grid>
+        <TextBlock Grid.Row="3" HorizontalAlignment="Right" Margin="0 0 5 0" Text="Palettes fetched from lospec.com/palette-list" Foreground="White"/>
     </Grid>
 </Window>

+ 22 - 4
PixiEditor/Views/Dialogs/LospecPalettesBrowser.xaml.cs

@@ -57,6 +57,8 @@ namespace PixiEditor.Views.Dialogs
         public static readonly DependencyProperty IsFetchingProperty =
             DependencyProperty.Register("IsFetching", typeof(bool), typeof(LospecPalettesBrowser), new PropertyMetadata(false));
 
+        public string SortingType { get; set; } = "Default";
+
 
         private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
         {
@@ -74,12 +76,12 @@ namespace PixiEditor.Views.Dialogs
             InitializeComponent();
         }
 
-        public async Task FetchPalettes()
+        public async Task UpdatePaletteList(bool refetch = false)
         {
             IsFetching = true;
-            if (PaletteList == null)
+            if (PaletteList == null || refetch)
             {
-                PaletteList = await LospecPaletteFetcher.FetchPage(0);
+                PaletteList = await LospecPaletteFetcher.FetchPage(0, SortingType.ToLower());
                 OnListFetched.Invoke(PaletteList);
             }
 
@@ -92,11 +94,27 @@ namespace PixiEditor.Views.Dialogs
             var scrollViewer = (ScrollViewer)sender;
             if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
             {
+                IsFetching = true;
                 _currentPage++;
                 var newPalettes = await LospecPaletteFetcher.FetchPage(_currentPage);
+                if(newPalettes == null || !newPalettes.FetchedCorrectly || newPalettes.Palettes == null)
+                {
+                    IsFetching = false;
+                    return;
+                }
+
                 PaletteList.Palettes.AddRange(newPalettes.Palettes);
-                PaletteList = PaletteList;
                 OnListFetched.Invoke(PaletteList);
+                IsFetching = false;
+            }
+        }
+
+        private async void SortingComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            if (e.AddedItems != null && e.AddedItems.Count > 0 && e.AddedItems[0] is ComboBoxItem item && item.Content is string value)
+            {
+                SortingType = value;
+                await UpdatePaletteList(true);
             }
         }
     }

+ 2 - 1
PixiEditor/Views/UserControls/Lospec/LospecPaletteItem.xaml

@@ -10,7 +10,7 @@
     <Grid>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="100*"/>
-            <ColumnDefinition Width="60"/>
+            <ColumnDefinition Width="75"/>
         </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
             <RowDefinition Height="60"/>
@@ -44,6 +44,7 @@
             </ItemsControl.ItemTemplate>
         </ItemsControl>
         <Button Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" VerticalAlignment="Center"
+                ToolTip="Import"
                 Style="{StaticResource ToolButtonStyle}" Cursor="Hand" 
                     Command="{Binding ImportPaletteCommand, ElementName=paletteItem}"
                     CommandParameter="{Binding ElementName=paletteItem, Path=Palette.Colors}">

+ 1 - 1
PixiEditor/Views/UserControls/PaletteViewer.xaml.cs

@@ -181,7 +181,7 @@ namespace PixiEditor.Views.UserControls
             };
 
             browser.Show();
-            await browser.FetchPalettes();
+            await browser.UpdatePaletteList();
         }
     }
 }