From 507b4c59f2a8e3813582396183a4efb41b80fba0 Mon Sep 17 00:00:00 2001
From: Tsanie Lily <tsorgy@gmail.com>
Date: Thu, 14 May 2020 22:58:11 +0800
Subject: [PATCH] feature: parallels downloading image, refresh btn

---
 Pixiview/Illust/RelatedIllustsPage.xaml    |  4 +++
 Pixiview/Illust/RelatedIllustsPage.xaml.cs | 15 ++++++++-
 Pixiview/Illust/ViewIllustPage.xaml.cs     | 37 ++++++++--------------
 Pixiview/UI/StyleDefinition.cs             |  2 +-
 Pixiview/Utils/HttpUtility.cs              |  1 -
 5 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/Pixiview/Illust/RelatedIllustsPage.xaml b/Pixiview/Illust/RelatedIllustsPage.xaml
index 4653544..5b26c52 100644
--- a/Pixiview/Illust/RelatedIllustsPage.xaml
+++ b/Pixiview/Illust/RelatedIllustsPage.xaml
@@ -7,6 +7,10 @@
                                   x:Class="Pixiview.Illust.RelatedIllustsPage"
                                   Title="{r:Text RelatedIllusts}"
                                   BackgroundColor="{DynamicResource WindowColor}">
+    <ContentPage.ToolbarItems>
+        <ToolbarItem Order="Primary" Clicked="Refresh_Clicked"
+                     IconImageSource="{DynamicResource FontIconRefresh}"/>
+    </ContentPage.ToolbarItems>
     <Grid>
         <ScrollView  x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                      HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
diff --git a/Pixiview/Illust/RelatedIllustsPage.xaml.cs b/Pixiview/Illust/RelatedIllustsPage.xaml.cs
index 151c299..84c4da0 100644
--- a/Pixiview/Illust/RelatedIllustsPage.xaml.cs
+++ b/Pixiview/Illust/RelatedIllustsPage.xaml.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using Pixiview.UI;
 using Pixiview.Utils;
@@ -88,5 +89,17 @@ namespace Pixiview.Illust
             var y = e.ScrollY;
             OnScrolled(y);
         }
+
+        private void Refresh_Clicked(object sender, EventArgs e)
+        {
+            if (IsLoading)
+            {
+                return;
+            }
+            startIndex = -1;
+            nextIndex = 0;
+            illustIds = null;
+            StartLoad(true);
+        }
     }
 }
diff --git a/Pixiview/Illust/ViewIllustPage.xaml.cs b/Pixiview/Illust/ViewIllustPage.xaml.cs
index 0d31c46..2c26f64 100644
--- a/Pixiview/Illust/ViewIllustPage.xaml.cs
+++ b/Pixiview/Illust/ViewIllustPage.xaml.cs
@@ -41,20 +41,6 @@ namespace Pixiview.Illust
             var items = page.Illusts;
             var length = items.Length;
             page.PagePositionText = $"{index + 1}/{length}";
-
-            var item = items[index];
-            if (!item.Loading && item.Image == null)
-            {
-                Task.Run(() => page.DoLoadImage(index));
-            }
-            if (index < length - 1)
-            {
-                item = items[index + 1];
-                if (!item.Loading && item.Image == null)
-                {
-                    Task.Run(() => page.DoLoadImage(index + 1));
-                }
-            }
         }
 
         private static void OnCurrentAnimeFramePropertyChanged(BindableObject obj, object old, object @new)
@@ -117,6 +103,7 @@ namespace Pixiview.Illust
         public IllustItem IllustItem { get; private set; }
         public bool IsPageVisible { get; private set; }
 
+        private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads };
         private readonly bool saveFavorites;
         private readonly ImageSource fontIconLove;
         private readonly ImageSource fontIconNotLove;
@@ -363,17 +350,18 @@ namespace Pixiview.Illust
                 }
             }
 
-            DoLoadImage(0, true);
-            var image = items[0].Image;
-            if (image != null)
-            {
-                illustItem.Image = image;
-            }
+            var isAnime = illustItem.IllustType == IllustType.Anime;
+            IsAnimateSliderVisible = isAnime;
+
+            Task.Run(() => DoLoadImage(0, true));
             if (items.Length > 1)
             {
-                DoLoadImage(1);
+                Parallel.For(1, items.Length, parallelOptions, i =>
+                {
+                    DoLoadImage(i);
+                });
             }
-            else if (illustItem.IllustType == IllustType.Anime)
+            else if (isAnime)
             {
                 // anime
                 ugoiraData = Stores.LoadIllustUgoiraData(illustItem.Id);
@@ -381,7 +369,6 @@ namespace Pixiview.Illust
                 {
                     var length = ugoiraData.body.frames.Length;
                     MaximumFrame = length > 0 ? length : 1;
-                    IsAnimateSliderVisible = true;
                 }
             }
         }
@@ -409,6 +396,10 @@ namespace Pixiview.Illust
             if (image != null)
             {
                 item.Image = image;
+                if(index == 0)
+                {
+                    IllustItem.Image = image;
+                }
             }
             item.Loading = false;
         }
diff --git a/Pixiview/UI/StyleDefinition.cs b/Pixiview/UI/StyleDefinition.cs
index 1c50a4d..07c1cc9 100644
--- a/Pixiview/UI/StyleDefinition.cs
+++ b/Pixiview/UI/StyleDefinition.cs
@@ -37,7 +37,7 @@ namespace Pixiview.UI
         public const string IconSparkles = "\uf890";
         public const string IconOrder = "\uf88f";
         public const string IconLayer = "\uf302";
-        public const string IconRefresh = "\uf2f1";
+        public const string IconRefresh = "\uf2f9";
         public const string IconLove = "\uf004";
         public const string IconOption = "\uf013";
         public const string IconFavorite = "\uf02e";
diff --git a/Pixiview/Utils/HttpUtility.cs b/Pixiview/Utils/HttpUtility.cs
index f22e1ec..3040a5b 100644
--- a/Pixiview/Utils/HttpUtility.cs
+++ b/Pixiview/Utils/HttpUtility.cs
@@ -132,7 +132,6 @@ namespace Pixiview.Utils
                     Directory.CreateDirectory(directory);
                 }
                 var file = Path.Combine(directory, Path.GetFileName(url));
-                App.DebugPrint($"download, url: {url}");
                 var response = Download(url, headers =>
                 {
                     headers.Referrer = new Uri(Configs.Referer);