From c7135d4d760fb933c0ab6857f0892194a851210e Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Mon, 18 May 2020 22:30:15 +0800 Subject: [PATCH] feature: lazy load favorites --- Pixiview/Illust/FavoritesPage.xaml | 11 ++-- Pixiview/Illust/FavoritesPage.xaml.cs | 69 ++++++++++++++++++++----- Pixiview/Illust/IllustCollectionPage.cs | 16 +----- 3 files changed, 65 insertions(+), 31 deletions(-) diff --git a/Pixiview/Illust/FavoritesPage.xaml b/Pixiview/Illust/FavoritesPage.xaml index eefc9ca..1ee6747 100644 --- a/Pixiview/Illust/FavoritesPage.xaml +++ b/Pixiview/Illust/FavoritesPage.xaml @@ -14,17 +14,20 @@ IconImageSource="{DynamicResource FontIconShare}"/> - + - + diff --git a/Pixiview/Illust/FavoritesPage.xaml.cs b/Pixiview/Illust/FavoritesPage.xaml.cs index ed8d998..b66b282 100644 --- a/Pixiview/Illust/FavoritesPage.xaml.cs +++ b/Pixiview/Illust/FavoritesPage.xaml.cs @@ -12,22 +12,30 @@ namespace Pixiview.Illust { public partial class FavoritesPage : FavoriteIllustCollectionPage { + private const int STEP = 24; + + private int startIndex; + private int nextIndex; private bool flag = false; public FavoritesPage() { Resources.Add("cardView", GetCardViewTemplate()); InitializeComponent(); + + startIndex = -1; + nextIndex = 0; } protected override bool IsFavoriteVisible => false; protected override ActivityIndicator LoadingIndicator => activityLoading; - protected override bool IsDelayLoading => true; protected override void OnAppearing() { if (lastUpdated != LastUpdated) { + startIndex = -1; + nextIndex = 0; StartLoad(); } else @@ -37,6 +45,8 @@ namespace Pixiview.Illust { favorites.Reload(); lastUpdated = default; + startIndex = -1; + nextIndex = 0; StartLoad(); } } @@ -49,22 +59,51 @@ namespace Pixiview.Illust protected override IllustItem[] DoLoadIllustData(bool force) { - var favorites = Stores.GetFavoriteObject(flag); - flag = false; - if (favorites == null) + FavoriteList favs; + if (startIndex < 0) { - return null; - } - var illusts = favorites.Illusts; - for (var i = 0; i < illusts.Count; i++) - { - var item = illusts[i]; - if (item.RankTitle == null) + var favorites = Stores.GetFavoriteObject(flag); + flag = false; + if (favorites == null) { - item.RankTitle = item.Title; + return null; } + favs = favorites.Illusts; + startIndex = 0; } - return illusts.ToArray(); + else + { + favs = Stores.Favorites; + } + var illusts = favs.Skip(startIndex).Take(STEP).ToArray(); + nextIndex = startIndex + STEP; + if (illusts.Length == 0 || nextIndex >= Stores.Favorites.Count) + { + // reach the bottom + startIndex = nextIndex; + } + return illusts; + } + + private void FlowLayout_MaxHeightChanged(object sender, HeightEventArgs e) + { + SetOffset(e.ContentHeight - scrollView.Bounds.Height - SCROLL_OFFSET); + } + + protected override bool CheckRefresh() + { + if (nextIndex > startIndex) + { + startIndex = nextIndex; + return true; + } + return false; + } + + private void ScrollView_Scrolled(object sender, ScrolledEventArgs e) + { + var y = e.ScrollY; + OnScrolled(y); } public void Reload(bool force = false) @@ -74,6 +113,8 @@ namespace Pixiview.Illust { flag = true; } + startIndex = -1; + nextIndex = 0; StartLoad(force); } @@ -105,6 +146,8 @@ namespace Pixiview.Illust { flag = false; lastUpdated = default; + startIndex = -1; + nextIndex = 0; MainThread.BeginInvokeOnMainThread(() => StartLoad(true)); } }); diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index ce6887b..0af2502 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -12,11 +12,11 @@ using Xamarin.Forms; namespace Pixiview.Illust { - public abstract class FavoriteIllustCollectionPage : IllustCollectionPage { } public abstract class IllustDataCollectionPage : IllustCollectionPage { } public abstract class IllustRankingDataCollectionPage : IllustScrollableCollectionPage { } public abstract class IllustRecommendsCollectionPage : IllustScrollableCollectionPage { } public abstract class IllustUserDataCollectionPage : IllustScrollableCollectionPage { } + public abstract class FavoriteIllustCollectionPage : IllustScrollableCollectionPage { } public interface IIllustCollectionPage { @@ -67,7 +67,6 @@ namespace Pixiview.Illust protected virtual bool IsFavoriteVisible => true; protected virtual bool IsAutoReload => true; protected virtual ActivityIndicator LoadingIndicator => null; - protected virtual bool IsDelayLoading => false; protected virtual double IndicatorMarginTop => 16; protected readonly Command commandIllustImageTapped; @@ -270,18 +269,7 @@ namespace Pixiview.Illust IsLoading = false; IsBottomLoading = false; #if __IOS__ - if (IsDelayLoading) - { - Device.StartTimer(TimeSpan.FromMilliseconds(250), () => - { - Illusts = collection; - return false; - }); - } - else - { - Illusts = collection; - } + Illusts = collection; #else Device.StartTimer(TimeSpan.FromMilliseconds(150), () => {