From 90816bc57e7284f98d568cda91e893999e918c00 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Fri, 15 May 2020 00:23:34 +0800 Subject: [PATCH] feature: user illusts page --- Pixiview/Illust/IllustCollectionPage.cs | 2 +- Pixiview/Illust/RelatedIllustsPage.xaml | 4 +- Pixiview/Illust/RelatedIllustsPage.xaml.cs | 1 - Pixiview/Illust/UserIllustPage.xaml | 10 +-- Pixiview/Illust/UserIllustPage.xaml.cs | 71 +++++++++++++++++++++- Pixiview/Utils/Stores.cs | 45 +++++++++----- 6 files changed, 109 insertions(+), 24 deletions(-) diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index 2336c97..8fd8bab 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -14,9 +14,9 @@ namespace Pixiview.Illust { public abstract class FavoriteIllustCollectionPage : IllustCollectionPage { } public abstract class IllustDataCollectionPage : IllustCollectionPage { } - public abstract class IllustUserDataCollectionPage : IllustCollectionPage { } public abstract class IllustRankingDataCollectionPage : IllustScrollableCollectionPage { } public abstract class IllustRecommendsCollectionPage : IllustScrollableCollectionPage { } + public abstract class IllustUserDataCollectionPage : IllustScrollableCollectionPage { } public interface IIllustCollectionPage { diff --git a/Pixiview/Illust/RelatedIllustsPage.xaml b/Pixiview/Illust/RelatedIllustsPage.xaml index 5b26c52..58dfbc1 100644 --- a/Pixiview/Illust/RelatedIllustsPage.xaml +++ b/Pixiview/Illust/RelatedIllustsPage.xaml @@ -12,8 +12,8 @@ IconImageSource="{DynamicResource FontIconRefresh}"/> - + = illustIds.Length) { // done - App.DebugPrint($"download completed: {startIndex}"); startIndex = nextIndex; } diff --git a/Pixiview/Illust/UserIllustPage.xaml b/Pixiview/Illust/UserIllustPage.xaml index 265c122..83615b9 100644 --- a/Pixiview/Illust/UserIllustPage.xaml +++ b/Pixiview/Illust/UserIllustPage.xaml @@ -6,9 +6,10 @@ x:Class="Pixiview.Illust.UserIllustPage" BackgroundColor="{DynamicResource WindowColor}"> - + + VerticalOptions="Center" + WidthRequest="34" HeightRequest="34" > @@ -23,8 +24,9 @@ IconImageSource="{DynamicResource FontIconRefresh}"/> - - + diff --git a/Pixiview/Illust/UserIllustPage.xaml.cs b/Pixiview/Illust/UserIllustPage.xaml.cs index 2051184..07539c7 100644 --- a/Pixiview/Illust/UserIllustPage.xaml.cs +++ b/Pixiview/Illust/UserIllustPage.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Pixiview.UI; using Pixiview.Utils; using Xamarin.Forms; @@ -8,6 +9,8 @@ namespace Pixiview.Illust { public partial class UserIllustPage : IllustUserDataCollectionPage { + private const int STEP = 48; + public static readonly BindableProperty UserIconProperty = BindableProperty.Create( nameof(UserIcon), typeof(ImageSource), typeof(UserIllustPage)); @@ -19,6 +22,10 @@ namespace Pixiview.Illust public IIllustUser UserItem { get; } + private int startIndex; + private int nextIndex; + private string[] illustIds; + public UserIllustPage(IIllustUser item) { UserItem = item; @@ -26,6 +33,9 @@ namespace Pixiview.Illust Resources.Add("cardView", GetCardViewTemplate(true)); InitializeComponent(); + + startIndex = -1; + nextIndex = 0; } protected override IEnumerable DoGetIllustList(IllustUserData data) @@ -39,7 +49,56 @@ namespace Pixiview.Illust protected override IllustUserData DoLoadIllustData(bool force) { - return Stores.LoadIllustUserData(UserItem.UserId, force); + var userId = UserItem.UserId; + if (startIndex < 0) + { + var init = Stores.LoadIllustUserInitData(userId); + if (init == null || init.body == null) + { + return null; + } + illustIds = init.body.illusts.Keys.OrderByDescending(i => i).ToArray(); + startIndex = 0; + } + + if (illustIds == null || startIndex >= illustIds.Length) + { + return null; + } + var ids = illustIds.Skip(startIndex).Take(STEP).ToArray(); + var data = Stores.LoadIllustUserData(userId, ids, startIndex == 0); + nextIndex = startIndex + STEP; + if (ids.Length == 0 || nextIndex >= illustIds.Length) + { + // done + App.DebugPrint($"download completed: {startIndex}"); + startIndex = nextIndex; + } + return data; + } + + private void FlowLayout_MaxHeightChanged(object sender, HeightEventArgs e) + { + if (e.ContentHeight > 0) + { + 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); } private void Refresh_Clicked(object sender, EventArgs e) @@ -48,6 +107,16 @@ namespace Pixiview.Illust { return; } + // release + var collection = IllustCollection; + if (collection != null) + { + collection.Running = false; + IllustCollection = null; + } + startIndex = -1; + nextIndex = 0; + illustIds = null; StartLoad(true); } } diff --git a/Pixiview/Utils/Stores.cs b/Pixiview/Utils/Stores.cs index ecfec5b..20159ac 100644 --- a/Pixiview/Utils/Stores.cs +++ b/Pixiview/Utils/Stores.cs @@ -341,29 +341,33 @@ namespace Pixiview.Utils return result; } - public static IllustUserData LoadIllustUserData(string userId, bool force = false) + public static IllustUserListData LoadIllustUserInitData(string userId) { var list = HttpUtility.LoadObject( null, string.Format(Configs.UrlIllustUserAll, userId), - string.Format(Configs.RefererIllustUser, userId), - force: force); + string.Format(Configs.RefererIllustUser, userId)); if (list == null || list.error) { - App.DebugPrint($"error when load user data: {list?.message}, force({force})"); + App.DebugPrint($"error when load user data: {list?.message}"); } + return list; + } - // TODO - var ids = string.Join("", list.body.illusts.Keys.Take(20).Select(id => $"ids%5B%5D={id}&")); - + public static IllustUserData LoadIllustUserData(string userId, string[] ids, bool firstPage) + { + if (ids == null || ids.Length == 0) + { + return null; + } + var ps = string.Concat(ids.Select(i => $"ids%5B%5D={i}&")); var result = HttpUtility.LoadObject( null, - string.Format(Configs.UrlIllustUserArtworks, userId, ids, 1), - string.Format(Configs.RefererIllustUser, userId), - force: force); + string.Format(Configs.UrlIllustUserArtworks, userId, ps, firstPage ? 1 : 0), + string.Format(Configs.RefererIllustUser, userId)); if (result == null || result.error) { - App.DebugPrint($"error when load user illust data: {result?.message}, force({force})"); + App.DebugPrint($"error when load user illust data: {result?.message}"); } return result; } @@ -449,7 +453,7 @@ namespace Pixiview.Utils public const string Referer = "https://www.pixiv.net/"; public const string RefererIllust = "https://www.pixiv.net/artworks/{0}"; public const string RefererIllustRanking = "https://www.pixiv.net/ranking.php?{0}"; - public const string RefererIllustUser = "https://www.pixiv.net/users/{0}/artworks"; + public const string RefererIllustUser = "https://www.pixiv.net/users/{0}/illustrations"; public static WebProxy Proxy; private static string Prefix => Proxy == null ? @@ -496,18 +500,29 @@ namespace Pixiview.Utils "first_visit_datetime_pc=2019-10-29+22%3A05%3A30; " + "yuid_b=NgcXQWQ"; #endif + private const string URL_PREVIEW = "https://i.pximg.net/c/360x360_70"; public static string GetThumbnailUrl(string url) { - url = url.Replace("/custom-thumb/", "/img-master/"); + if (url == null) + { + return null; + } + url = url.ToLower().Replace("/custom-thumb/", "/img-master/"); var index = url.LastIndexOf("_square1200.jpg"); if (index < 0) { index = url.LastIndexOf("_custom1200.jpg"); } - if (index >= 0) + if (index > 0) { - return url.Substring(0, index) + "_master1200.jpg"; + url = url.Substring(0, index) + "_master1200.jpg"; + + var start = url.IndexOf("/img-master/"); + if (start > 0) + { + url = URL_PREVIEW + url.Substring(start); + } } return url; }