feature: lazy load favorites

This commit is contained in:
2020-05-18 22:30:15 +08:00
parent 508bab3395
commit c7135d4d76
3 changed files with 65 additions and 31 deletions

View File

@ -14,17 +14,20 @@
IconImageSource="{DynamicResource FontIconShare}"/> IconImageSource="{DynamicResource FontIconShare}"/>
</ContentPage.ToolbarItems> </ContentPage.ToolbarItems>
<Grid> <Grid>
<ScrollView x:Name="scrollView" HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never"> <ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
<StackLayout> <StackLayout>
<ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0" <ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
HeightRequest="40" HeightRequest="40"
IsRunning="{Binding IsLoading}" IsRunning="{Binding IsLoading}"
IsVisible="{Binding IsLoading}"/> IsVisible="{Binding IsLoading}"/>
<u:FlowLayout ItemsSource="{Binding Illusts}" <u:FlowLayout ItemsSource="{Binding Illusts}" MaxHeightChanged="FlowLayout_MaxHeightChanged"
HorizontalOptions="Fill" Column="{Binding Columns}" HorizontalOptions="Fill" Column="{Binding Columns}"
Margin="16" Margin="16" RowSpacing="16" ColumnSpacing="16"
RowSpacing="16" ColumnSpacing="16"
ItemTemplate="{StaticResource cardView}"/> ItemTemplate="{StaticResource cardView}"/>
<ActivityIndicator x:Name="activityBottomLoading" Margin="0, -10, 0, 16"
IsRunning="{Binding IsBottomLoading}"
IsVisible="{Binding IsBottomLoading}"/>
</StackLayout> </StackLayout>
</ScrollView> </ScrollView>
</Grid> </Grid>

View File

@ -12,22 +12,30 @@ namespace Pixiview.Illust
{ {
public partial class FavoritesPage : FavoriteIllustCollectionPage public partial class FavoritesPage : FavoriteIllustCollectionPage
{ {
private const int STEP = 24;
private int startIndex;
private int nextIndex;
private bool flag = false; private bool flag = false;
public FavoritesPage() public FavoritesPage()
{ {
Resources.Add("cardView", GetCardViewTemplate()); Resources.Add("cardView", GetCardViewTemplate());
InitializeComponent(); InitializeComponent();
startIndex = -1;
nextIndex = 0;
} }
protected override bool IsFavoriteVisible => false; protected override bool IsFavoriteVisible => false;
protected override ActivityIndicator LoadingIndicator => activityLoading; protected override ActivityIndicator LoadingIndicator => activityLoading;
protected override bool IsDelayLoading => true;
protected override void OnAppearing() protected override void OnAppearing()
{ {
if (lastUpdated != LastUpdated) if (lastUpdated != LastUpdated)
{ {
startIndex = -1;
nextIndex = 0;
StartLoad(); StartLoad();
} }
else else
@ -37,6 +45,8 @@ namespace Pixiview.Illust
{ {
favorites.Reload(); favorites.Reload();
lastUpdated = default; lastUpdated = default;
startIndex = -1;
nextIndex = 0;
StartLoad(); StartLoad();
} }
} }
@ -49,22 +59,51 @@ namespace Pixiview.Illust
protected override IllustItem[] DoLoadIllustData(bool force) protected override IllustItem[] DoLoadIllustData(bool force)
{ {
var favorites = Stores.GetFavoriteObject(flag); FavoriteList favs;
flag = false; if (startIndex < 0)
if (favorites == null)
{ {
return null; var favorites = Stores.GetFavoriteObject(flag);
} flag = false;
var illusts = favorites.Illusts; if (favorites == null)
for (var i = 0; i < illusts.Count; i++)
{
var item = illusts[i];
if (item.RankTitle == 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) public void Reload(bool force = false)
@ -74,6 +113,8 @@ namespace Pixiview.Illust
{ {
flag = true; flag = true;
} }
startIndex = -1;
nextIndex = 0;
StartLoad(force); StartLoad(force);
} }
@ -105,6 +146,8 @@ namespace Pixiview.Illust
{ {
flag = false; flag = false;
lastUpdated = default; lastUpdated = default;
startIndex = -1;
nextIndex = 0;
MainThread.BeginInvokeOnMainThread(() => StartLoad(true)); MainThread.BeginInvokeOnMainThread(() => StartLoad(true));
} }
}); });

View File

@ -12,11 +12,11 @@ using Xamarin.Forms;
namespace Pixiview.Illust namespace Pixiview.Illust
{ {
public abstract class FavoriteIllustCollectionPage : IllustCollectionPage<IllustItem[]> { }
public abstract class IllustDataCollectionPage : IllustCollectionPage<IllustData> { } public abstract class IllustDataCollectionPage : IllustCollectionPage<IllustData> { }
public abstract class IllustRankingDataCollectionPage : IllustScrollableCollectionPage<IllustRankingData> { } public abstract class IllustRankingDataCollectionPage : IllustScrollableCollectionPage<IllustRankingData> { }
public abstract class IllustRecommendsCollectionPage : IllustScrollableCollectionPage<IllustRecommendsData> { } public abstract class IllustRecommendsCollectionPage : IllustScrollableCollectionPage<IllustRecommendsData> { }
public abstract class IllustUserDataCollectionPage : IllustScrollableCollectionPage<IllustUserData> { } public abstract class IllustUserDataCollectionPage : IllustScrollableCollectionPage<IllustUserData> { }
public abstract class FavoriteIllustCollectionPage : IllustScrollableCollectionPage<IllustItem[]> { }
public interface IIllustCollectionPage public interface IIllustCollectionPage
{ {
@ -67,7 +67,6 @@ namespace Pixiview.Illust
protected virtual bool IsFavoriteVisible => true; protected virtual bool IsFavoriteVisible => true;
protected virtual bool IsAutoReload => true; protected virtual bool IsAutoReload => true;
protected virtual ActivityIndicator LoadingIndicator => null; protected virtual ActivityIndicator LoadingIndicator => null;
protected virtual bool IsDelayLoading => false;
protected virtual double IndicatorMarginTop => 16; protected virtual double IndicatorMarginTop => 16;
protected readonly Command<IllustItem> commandIllustImageTapped; protected readonly Command<IllustItem> commandIllustImageTapped;
@ -270,18 +269,7 @@ namespace Pixiview.Illust
IsLoading = false; IsLoading = false;
IsBottomLoading = false; IsBottomLoading = false;
#if __IOS__ #if __IOS__
if (IsDelayLoading) Illusts = collection;
{
Device.StartTimer(TimeSpan.FromMilliseconds(250), () =>
{
Illusts = collection;
return false;
});
}
else
{
Illusts = collection;
}
#else #else
Device.StartTimer(TimeSpan.FromMilliseconds(150), () => Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
{ {