Browse Source

Added items per load

flabbet 3 years ago
parent
commit
e529aef699

+ 2 - 1
PixiEditor/Models/DataHolders/Palettes/PaletteList.cs

@@ -1,8 +1,9 @@
 using PixiEditor.Helpers;
+using System.Collections.ObjectModel;
 
 namespace PixiEditor.Models.DataHolders.Palettes
 {
-    public class PaletteList
+    public class PaletteList : NotifyableObject
     {
         public bool FetchedCorrectly { get; set; } = false;
         public WpfObservableRangeCollection<Palette> Palettes { get; set; } = new WpfObservableRangeCollection<Palette>();

+ 6 - 3
PixiEditor/Models/DataProviders/LocalPalettesFetcher.cs

@@ -17,7 +17,7 @@ namespace PixiEditor.Models.DataProviders
             Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
             "PixiEditor", "Palettes");
 
-        public override async Task<PaletteList> FetchPaletteList()
+        public override async Task<PaletteList> FetchPaletteList(int startIndex, int count)
         {
             string[] files = DirectoryExtensions.GetFiles(PathToPalettesFolder, string.Join("|", AvailableParsers.SelectMany(x => x.SupportedFileExtensions)), SearchOption.TopDirectoryOnly);
 
@@ -26,9 +26,12 @@ namespace PixiEditor.Models.DataProviders
                 Palettes = new WpfObservableRangeCollection<Palette>()
             };
 
-            for (int i = 0; i < files.Length; i++)
+            if (startIndex >= files.Length) return result;
+
+            for (int i = 0; i < count; i++)
             {
-                string filePath = files[i];
+                if (startIndex + i >= files.Length) break;
+                string filePath = files[i + startIndex];
                 string extension = Path.GetExtension(filePath);
                 var foundParser = AvailableParsers.First(x => x.SupportedFileExtensions.Contains(extension));
                 if (foundParser != null)

+ 1 - 1
PixiEditor/Models/DataProviders/PaletteListDataSource.cs

@@ -8,7 +8,7 @@ namespace PixiEditor.Models.DataProviders
     public abstract class PaletteListDataSource
     {
         public virtual void Initialize() { }
-        public abstract Task<PaletteList> FetchPaletteList();
+        public abstract Task<PaletteList> FetchPaletteList(int startIndex, int items);
         public List<PaletteFileParser> AvailableParsers { get; set; }
 
     }

+ 33 - 22
PixiEditor/Views/Dialogs/PalettesBrowser.xaml.cs

@@ -20,7 +20,7 @@ namespace PixiEditor.Views.Dialogs
     public partial class PalettesBrowser : Window
     {
         public event ListFetched OnListFetched;
-        private int _currentPage = 0;
+        public const int ItemsPerLoad = 10;
 
         public PaletteList PaletteList
         {
@@ -107,13 +107,18 @@ namespace PixiEditor.Views.Dialogs
             IsFetching = true;
             if (PaletteList == null || refetch)
             {
-                //PaletteList.Palettes.Clear();
                 for (int i = 0; i < PaletteListDataSources.Count; i++)
                 {
-                    var src = await PaletteListDataSources[i].FetchPaletteList();
+                    PaletteList src = await FetchPaletteList(i);
                     if (!src.FetchedCorrectly) continue;
-                    //PaletteList.Palettes.AddRange(src.Palettes); //WHY TF IT DOESN'T UPDATE IN VIEW
-                    PaletteList = src; // So yeah, since we don't have more than 1 palette data source right now, then temp solution
+                    if (PaletteList == null)
+                    {
+                        PaletteList = src;
+                    }
+                    else
+                    {
+                        PaletteList.Palettes.AddRange(src.Palettes);
+                    }
                 }
 
                 OnListFetched.Invoke(PaletteList);
@@ -122,25 +127,31 @@ namespace PixiEditor.Views.Dialogs
             IsFetching = false;
         }
 
+        private async Task<PaletteList> FetchPaletteList(int index)
+        {
+            int startIndex = PaletteList != null ? PaletteList.Palettes.Count : 0;
+            var src = await PaletteListDataSources[index].FetchPaletteList(startIndex, ItemsPerLoad);
+            return src;
+        }
+
         private async void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
         {
-            //if (PaletteList == null || PaletteList.Palettes == null) return;
-            //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);
-            //    OnListFetched.Invoke(PaletteList);
-            //    IsFetching = false;
-            //}
+            if (PaletteList == null || PaletteList.Palettes == null) return;
+            var scrollViewer = (ScrollViewer)sender;
+            if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
+            {
+                IsFetching = true;
+                var newPalettes = await FetchPaletteList(0);
+                if (newPalettes == null || !newPalettes.FetchedCorrectly || newPalettes.Palettes == null)
+                {
+                    IsFetching = false;
+                    return;
+                }
+
+                PaletteList.Palettes.AddRange(newPalettes.Palettes);
+                OnListFetched.Invoke(PaletteList);
+                IsFetching = false;
+            }
         }
 
         private async void SortingComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)