feature: lazy load favorites

This commit is contained in:
Tsanie Lily 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}"/>
</ContentPage.ToolbarItems>
<Grid>
<ScrollView x:Name="scrollView" HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
<ScrollView x:Name="scrollView" Scrolled="ScrollView_Scrolled"
HorizontalOptions="Fill" HorizontalScrollBarVisibility="Never">
<StackLayout>
<ActivityIndicator x:Name="activityLoading" Margin="0, 16, 0, 0"
HeightRequest="40"
IsRunning="{Binding IsLoading}"
IsVisible="{Binding IsLoading}"/>
<u:FlowLayout ItemsSource="{Binding Illusts}"
<u:FlowLayout ItemsSource="{Binding Illusts}" MaxHeightChanged="FlowLayout_MaxHeightChanged"
HorizontalOptions="Fill" Column="{Binding Columns}"
Margin="16"
RowSpacing="16" ColumnSpacing="16"
Margin="16" RowSpacing="16" ColumnSpacing="16"
ItemTemplate="{StaticResource cardView}"/>
<ActivityIndicator x:Name="activityBottomLoading" Margin="0, -10, 0, 16"
IsRunning="{Binding IsBottomLoading}"
IsVisible="{Binding IsBottomLoading}"/>
</StackLayout>
</ScrollView>
</Grid>

View File

@ -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));
}
});

View File

@ -12,11 +12,11 @@ using Xamarin.Forms;
namespace Pixiview.Illust
{
public abstract class FavoriteIllustCollectionPage : IllustCollectionPage<IllustItem[]> { }
public abstract class IllustDataCollectionPage : IllustCollectionPage<IllustData> { }
public abstract class IllustRankingDataCollectionPage : IllustScrollableCollectionPage<IllustRankingData> { }
public abstract class IllustRecommendsCollectionPage : IllustScrollableCollectionPage<IllustRecommendsData> { }
public abstract class IllustUserDataCollectionPage : IllustScrollableCollectionPage<IllustUserData> { }
public abstract class FavoriteIllustCollectionPage : IllustScrollableCollectionPage<IllustItem[]> { }
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<IllustItem> 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), () =>
{