From 01d782660baf96577f0cca9eb2ac6101e38de042 Mon Sep 17 00:00:00 2001
From: Tsanie Lily <tsorgy@gmail.com>
Date: Tue, 19 May 2020 00:16:42 +0800
Subject: [PATCH] optimize animation

---
 Pixiview/Illust/FavoritesPage.xaml      |  2 +-
 Pixiview/Illust/FavoritesPage.xaml.cs   |  2 +-
 Pixiview/Illust/IllustCollectionPage.cs | 36 +++++++++++++------------
 Pixiview/Illust/MainPage.xaml           |  2 +-
 Pixiview/Illust/RankingPage.xaml        |  2 +-
 Pixiview/Illust/RecommendsPage.xaml     |  2 +-
 Pixiview/Illust/RecommendsPage.xaml.cs  |  5 ++--
 Pixiview/Illust/RelatedIllustsPage.xaml |  2 +-
 Pixiview/Illust/UserIllustPage.xaml     |  2 +-
 9 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/Pixiview/Illust/FavoritesPage.xaml b/Pixiview/Illust/FavoritesPage.xaml
index 1ee6747..8947ddc 100644
--- a/Pixiview/Illust/FavoritesPage.xaml
+++ b/Pixiview/Illust/FavoritesPage.xaml
@@ -17,7 +17,7 @@
         <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                     HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, -40, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>
diff --git a/Pixiview/Illust/FavoritesPage.xaml.cs b/Pixiview/Illust/FavoritesPage.xaml.cs
index b66b282..698facb 100644
--- a/Pixiview/Illust/FavoritesPage.xaml.cs
+++ b/Pixiview/Illust/FavoritesPage.xaml.cs
@@ -12,7 +12,7 @@ namespace Pixiview.Illust
 {
     public partial class FavoritesPage : FavoriteIllustCollectionPage
     {
-        private const int STEP = 24;
+        private const int STEP = 20;
 
         private int startIndex;
         private int nextIndex;
diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs
index 0af2502..f4e17a7 100644
--- a/Pixiview/Illust/IllustCollectionPage.cs
+++ b/Pixiview/Illust/IllustCollectionPage.cs
@@ -200,7 +200,7 @@ namespace Pixiview.Illust
                 lastUpdated = LastUpdated;
 
                 var indicator = LoadingIndicator;
-                if (indicator == null)
+                if (indicator == null || isBottom)
                 {
                     if (isBottom)
                     {
@@ -212,35 +212,28 @@ namespace Pixiview.Illust
                         IsLoading = true;
                     }
 #if __IOS__
-                    Task.Run(() => DoLoadIllusts(force, isBottom));
+                    Device.StartTimer(TimeSpan.FromMilliseconds(48), () =>
 #else
                     Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
+#endif
                     {
                         Task.Run(() => DoLoadIllusts(force, isBottom));
                         return false;
                     });
-#endif
                 }
                 else
                 {
+                    InvalidateCollection();
+                    IsLoading = true;
+
                     var offset = 16 - IndicatorMarginTop;
-                    indicator.Margin = new Thickness(0, loadingOffset - offset, 0, offset);
-                    if (isBottom)
-                    {
-                        IsBottomLoading = true;
-                    }
-                    else
-                    {
-                        InvalidateCollection();
-                        IsLoading = true;
-                    }
                     indicator.Animate("margin", top =>
                     {
                         indicator.Margin = new Thickness(0, top, 0, offset);
                     },
                     loadingOffset - offset, 16 - offset, easing: Easing.CubicOut, finished: (v, r) =>
                     {
-                        Task.Run(() => DoLoadIllusts(force, isBottom));
+                        _ = Task.Run(() => DoLoadIllusts(force, isBottom));
                     });
                 }
             }
@@ -253,9 +246,17 @@ namespace Pixiview.Illust
             var indicator = LoadingIndicator;
             if (indicator == null || bottom)
             {
-                Illusts = collection;
                 IsLoading = false;
                 IsBottomLoading = false;
+#if __IOS__
+                Device.StartTimer(TimeSpan.FromMilliseconds(48), () =>
+#else
+                Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
+#endif
+                {
+                    Illusts = collection;
+                    return false;
+                });
             }
             else
             {
@@ -266,17 +267,18 @@ namespace Pixiview.Illust
                 },
                 16 - offset, loadingOffset - offset, easing: Easing.CubicIn, finished: (v, r) =>
                 {
+                    indicator.Margin = new Thickness(0, v, 0, offset);
                     IsLoading = false;
                     IsBottomLoading = false;
 #if __IOS__
-                    Illusts = collection;
+                    Device.StartTimer(TimeSpan.FromMilliseconds(48), () =>
 #else
                     Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
+#endif
                     {
                         Illusts = collection;
                         return false;
                     });
-#endif
                 });
             }
         }
diff --git a/Pixiview/Illust/MainPage.xaml b/Pixiview/Illust/MainPage.xaml
index 4127ff4..26924d0 100644
--- a/Pixiview/Illust/MainPage.xaml
+++ b/Pixiview/Illust/MainPage.xaml
@@ -15,7 +15,7 @@
         <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                     HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 66, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, 10, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>
diff --git a/Pixiview/Illust/RankingPage.xaml b/Pixiview/Illust/RankingPage.xaml
index e1429e8..83fd07d 100644
--- a/Pixiview/Illust/RankingPage.xaml
+++ b/Pixiview/Illust/RankingPage.xaml
@@ -49,7 +49,7 @@
         <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                     HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, -40, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>
diff --git a/Pixiview/Illust/RecommendsPage.xaml b/Pixiview/Illust/RecommendsPage.xaml
index 983244d..5a16527 100644
--- a/Pixiview/Illust/RecommendsPage.xaml
+++ b/Pixiview/Illust/RecommendsPage.xaml
@@ -14,7 +14,7 @@
     <Grid>
         <ScrollView x:Name="scrollView" HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, -40, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>
diff --git a/Pixiview/Illust/RecommendsPage.xaml.cs b/Pixiview/Illust/RecommendsPage.xaml.cs
index 6a37423..aeba33c 100644
--- a/Pixiview/Illust/RecommendsPage.xaml.cs
+++ b/Pixiview/Illust/RecommendsPage.xaml.cs
@@ -286,16 +286,15 @@ namespace Pixiview.Illust
                 IsLoading = false;
                 UserRecommendsVisible = list.Count > 0;
 #if __IOS__
-                Users = list;
-                Illusts = IllustCollection;
+                Device.StartTimer(TimeSpan.FromMilliseconds(48), () =>
 #else
                 Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
+#endif
                 {
                     Users = list;
                     Illusts = IllustCollection;
                     return false;
                 });
-#endif
             });
 
             DoLoadUserRecommendsImages(list);
diff --git a/Pixiview/Illust/RelatedIllustsPage.xaml b/Pixiview/Illust/RelatedIllustsPage.xaml
index 0e06198..cb205c1 100644
--- a/Pixiview/Illust/RelatedIllustsPage.xaml
+++ b/Pixiview/Illust/RelatedIllustsPage.xaml
@@ -15,7 +15,7 @@
         <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                     HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, -40, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>
diff --git a/Pixiview/Illust/UserIllustPage.xaml b/Pixiview/Illust/UserIllustPage.xaml
index da4bd54..a53c2e5 100644
--- a/Pixiview/Illust/UserIllustPage.xaml
+++ b/Pixiview/Illust/UserIllustPage.xaml
@@ -27,7 +27,7 @@
         <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
                     HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
             <StackLayout>
-                <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
+                <ActivityIndicator x:Name="activityLoading" Margin="0, -40, 0, 0"
                                    HeightRequest="40"
                                    IsRunning="{Binding IsLoading}"
                                    IsVisible="{Binding IsLoading}"/>