feature: favorite illusts

This commit is contained in:
2020-05-07 15:19:24 +08:00
parent 84aecdf39b
commit 8cf9ae288b
19 changed files with 446 additions and 94 deletions

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Newtonsoft.Json;
using Pixiview.Resources;
using Pixiview.UI;
using Pixiview.UI.Theme;
@@ -12,20 +14,28 @@ using Xamarin.Forms;
namespace Pixiview.Illust
{
public abstract class IllustCollectionPage : AdaptedPage
public interface IIllustCollectionPage
{
List<IllustItem> Favorites { get; }
}
public abstract class IllustDataCollectionPage : IllustCollectionPage<IllustData> { }
public abstract class FavoriteIllustCollectionPage : IllustCollectionPage<IllustItem[]> { }
public abstract class IllustCollectionPage<T> : AdaptedPage, IIllustCollectionPage
{
#region - Properties -
public static readonly BindableProperty IllustsProperty = BindableProperty.Create(
nameof(Illusts), typeof(IllustCollection), typeof(IllustCollectionPage));
nameof(Illusts), typeof(IllustCollection), typeof(IllustCollectionPage<T>));
public static readonly BindableProperty ColumnsProperty = BindableProperty.Create(
nameof(Columns), typeof(int), typeof(IllustCollectionPage), 2);
nameof(Columns), typeof(int), typeof(IllustCollectionPage<T>), 2);
public static readonly BindableProperty LoadingProperty = BindableProperty.Create(
nameof(Loading), typeof(bool), typeof(IllustCollectionPage), propertyChanged: OnLoadingPropertyChanged);
nameof(Loading), typeof(bool), typeof(IllustCollectionPage<T>), propertyChanged: OnLoadingPropertyChanged);
private static void OnLoadingPropertyChanged(BindableObject obj, object oldValue, object newValue)
{
var page = (IllustCollectionPage)obj;
var page = (IllustCollectionPage<T>)obj;
var now = (bool)newValue;
if (!page.loaded && now && Stores.NetworkAvailable)
{
@@ -50,11 +60,13 @@ namespace Pixiview.Illust
set => SetValue(LoadingProperty, value);
}
public List<IllustItem> Favorites { get; } = new List<IllustItem>();
#endregion
protected bool loaded;
private IllustData illustData;
private T illustData;
private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads };
private readonly Command<IllustItem> commandIllustImageTapped;
@@ -62,6 +74,12 @@ namespace Pixiview.Illust
{
BindingContext = this;
commandIllustImageTapped = new Command<IllustItem>(IllustImageTapped);
var favorites = Stores.LoadFavoritesIllusts();
if (favorites != null)
{
Favorites.AddRange(favorites.Illusts);
}
}
private void IllustImageTapped(IllustItem illust)
@@ -128,11 +146,11 @@ namespace Pixiview.Illust
#endregion
protected abstract IllustData DoLoadIllustData(bool force);
protected abstract IEnumerable<string> DoGetIllustList(IllustData data);
protected abstract T DoLoadIllustData(bool force);
protected abstract IEnumerable<IllustItem> DoGetIllustList(T data, ICommand command);
protected virtual void OnIllustImageTapped(IllustItem illust)
{
var page = new ViewIllustPage(illust);
var page = new ViewIllustPage(illust, this);
Navigation.PushAsync(page);
}
@@ -270,33 +288,12 @@ namespace Pixiview.Illust
illustData = DoLoadIllustData(force);
if (illustData == null)
{
App.DebugError("illusts.load", "failed to load illusts data.");
//App.DebugError("illusts.load", "failed to load illusts data.");
Loading = false;
return;
}
var data = DoGetIllustList(illustData).Select(id =>
{
var illust = illustData.body.thumbnails.illust.FirstOrDefault(l => l.illustId == id);
if (illust == null)
{
return null;
}
return new IllustItem
{
Id = illust.illustId,
ImageUrl = illust.urls.x360 ?? illust.url,
Title = illust.illustTitle,
IsRestrict = illust.xRestrict == 1,
ProfileUrl = illust.profileImageUrl,
UserId = illust.userId,
UserName = illust.userName,
Width = illust.width,
Height = illust.height,
PageCount = illust.pageCount,
IllustTapped = commandIllustImageTapped
};
}).Where(i => i != null);
var data = DoGetIllustList(illustData, commandIllustImageTapped).Where(i => i != null);
var collection = new IllustCollection(data);
Illusts = collection;
@@ -340,7 +337,24 @@ namespace Pixiview.Illust
public class IllustCollection : ObservableCollection<IllustItem>
{
private static readonly object sync = new object();
private static IllustCollection empty;
public static IllustCollection Empty
{
get
{
if (empty == null)
{
empty = new IllustCollection();
}
return empty;
}
}
public IllustCollection() : base()
{
running = true;
}
public IllustCollection(IEnumerable<IllustItem> illusts) : base(illusts)
{
running = true;
@@ -360,6 +374,13 @@ namespace Pixiview.Illust
}
}
public class IllustFavorite
{
public DateTime LastFavoriteUtc { get; set; }
public IllustItem[] Illusts { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class IllustItem : BindableObject
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
@@ -379,6 +400,7 @@ namespace Pixiview.Illust
get => (ImageSource)GetValue(ProfileImageProperty);
set => SetValue(ProfileImageProperty, value);
}
[JsonProperty]
public GridLength ImageHeight
{
get => (GridLength)GetValue(ImageHeightProperty);
@@ -386,16 +408,27 @@ namespace Pixiview.Illust
}
public ICommand IllustTapped { get; set; }
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string ImageUrl { get; set; }
[JsonProperty]
public string Title { get; set; }
[JsonProperty]
public bool IsRestrict { get; set; }
[JsonProperty]
public string ProfileUrl { get; set; }
[JsonProperty]
public string UserId { get; set; }
[JsonProperty]
public string UserName { get; set; }
[JsonProperty]
public int Width { get; set; }
[JsonProperty]
public int Height { get; set; }
[JsonProperty]
public int PageCount { get; set; }
public string PageCountText => $"{StyleDefinition.IconLayer} {PageCount}";
public bool IsPageVisible => PageCount > 1;
}