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

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<i:FavoriteIllustCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.Illust.FavoritesPage"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Favorites}">
<Grid>
<ScrollView HorizontalOptions="Fill">
<u:FlowLayout ItemsSource="{Binding Illusts}"
HorizontalOptions="Fill" Column="{Binding Columns}"
Margin="16" RowSpacing="16" ColumnSpacing="16"
ItemTemplate="{StaticResource cardView}"/>
</ScrollView>
<Frame HasShadow="False" Margin="0" Padding="20" CornerRadius="8"
IsVisible="{Binding Loading}"
HorizontalOptions="Center" VerticalOptions="Center"
BackgroundColor="{DynamicResource MaskColor}">
<ActivityIndicator IsRunning="True" IsVisible="True"
Color="{DynamicResource WindowColor}"/>
</Frame>
</Grid>
</i:FavoriteIllustCollectionPage>

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Utils;
namespace Pixiview.Illust
{
public partial class FavoritesPage : FavoriteIllustCollectionPage
{
public FavoritesPage()
{
Resources.Add("cardView", GetCardViewTemplate());
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
StartLoad(true);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var illusts = Illusts;
if (illusts != null)
{
illusts.Clear();
}
}
protected override IEnumerable<IllustItem> DoGetIllustList(IllustItem[] data, ICommand command)
{
return data.Select(i =>
{
i.IllustTapped = command;
return i;
});
}
protected override IllustItem[] DoLoadIllustData(bool force)
{
var favorites = Stores.LoadFavoritesIllusts();
if (favorites == null)
{
return null;
}
return favorites.Illusts;
}
}
}

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

View File

@@ -1,14 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<i:IllustCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:util="clr-namespace:Pixiview.Utils"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.Illust.MainPage"
util:Screen.StatusBarStyle="{DynamicResource StatusBarStyle}"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Follow}">
<i:IllustDataCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.Illust.MainPage"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Follow}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Refresh_Clicked"
IconImageSource="{DynamicResource FontIconRefresh}"/>
@@ -28,4 +26,4 @@
Color="{DynamicResource WindowColor}"/>
</Frame>
</Grid>
</i:IllustCollectionPage>
</i:IllustDataCollectionPage>

View File

@@ -1,26 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Utils;
namespace Pixiview.Illust
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
//[DesignTimeVisible(false)]
public partial class MainPage : IllustCollectionPage
public partial class MainPage : IllustDataCollectionPage
{
public MainPage()
{
Resources.Add("cardView", GetCardViewTemplate());
InitializeComponent();
}
public override void OnLoad()
{
StartLoad();
}
protected override IEnumerable<string> DoGetIllustList(IllustData data)
public override void OnUnload()
{
return data.body.page.follow.Select(i => i.ToString());
Illusts = IllustCollection.Empty;
loaded = false;
}
protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, ICommand command)
{
return data.body.page.follow.Select(i =>
{
var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == i.ToString())?.ConvertToItem();
if (item != null)
{
item.IllustTapped = command;
}
return item;
});
}
protected override IllustData DoLoadIllustData(bool force)

View File

@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<i:IllustCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:util="clr-namespace:Pixiview.Utils"
x:Class="Pixiview.Illust.RankingPage"
util:Screen.StatusBarStyle="{DynamicResource StatusBarStyle}"
BackgroundColor="{DynamicResource WindowColor}">
<i:IllustDataCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
x:Class="Pixiview.Illust.RankingPage"
BackgroundColor="{DynamicResource WindowColor}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Refresh_Clicked"
IconImageSource="{DynamicResource FontIconRefresh}"/>
</ContentPage.ToolbarItems>
<Grid>
<ScrollView HorizontalOptions="Fill">
<u:FlowLayout ItemsSource="{Binding Illusts}"
@@ -22,4 +24,4 @@
Color="{DynamicResource WindowColor}"/>
</Frame>
</Grid>
</i:IllustCollectionPage>
</i:IllustDataCollectionPage>

View File

@@ -1,23 +1,42 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Utils;
using Xamarin.Forms;
namespace Pixiview.Illust
{
public partial class RankingPage : IllustCollectionPage
public partial class RankingPage : IllustDataCollectionPage
{
public RankingPage()
{
Resources.Add("cardView", GetCardViewTemplate());
InitializeComponent();
}
public override void OnLoad()
{
StartLoad();
}
protected override IEnumerable<string> DoGetIllustList(IllustData data)
public override void OnUnload()
{
return data.body.page.ranking.items.Select(i => i.id);
Illusts = IllustCollection.Empty;
loaded = false;
}
protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, ICommand command)
{
return data.body.page.ranking.items.Select(i =>
{
var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == i.id)?.ConvertToItem();
if (item != null)
{
item.IllustTapped = command;
}
return item;
});
}
protected override IllustData DoLoadIllustData(bool force)
@@ -34,5 +53,14 @@ namespace Pixiview.Illust
}
return data;
}
private void Refresh_Clicked(object sender, EventArgs e)
{
if (Loading)
{
return;
}
StartLoad(true);
}
}
}

View File

@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<i:IllustCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:util="clr-namespace:Pixiview.Utils"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.Illust.RecommendsPage"
util:Screen.StatusBarStyle="{DynamicResource StatusBarStyle}"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Recommends}">
<i:IllustDataCollectionPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.Illust.RecommendsPage"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Recommends}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Refresh_Clicked"
IconImageSource="{DynamicResource FontIconRefresh}"/>
</ContentPage.ToolbarItems>
<Grid>
<ScrollView HorizontalOptions="Fill">
<u:FlowLayout ItemsSource="{Binding Illusts}"
@@ -24,4 +26,4 @@
Color="{DynamicResource WindowColor}"/>
</Frame>
</Grid>
</i:IllustCollectionPage>
</i:IllustDataCollectionPage>

View File

@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Utils;
namespace Pixiview.Illust
{
public partial class RecommendsPage : IllustCollectionPage
public partial class RecommendsPage : IllustDataCollectionPage
{
public bool ByUser { get; set; }
@@ -12,19 +14,45 @@ namespace Pixiview.Illust
{
Resources.Add("cardView", GetCardViewTemplate());
InitializeComponent();
}
public override void OnLoad()
{
StartLoad();
}
protected override IEnumerable<string> DoGetIllustList(IllustData data)
public override void OnUnload()
{
Illusts = IllustCollection.Empty;
loaded = false;
}
protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, ICommand command)
{
if (ByUser)
{
return data.body.page.recommendUser.SelectMany(i => i.illustIds);
return data.body.page.recommendUser.SelectMany(i => i.illustIds)
.Select(id =>
{
var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == id)?.ConvertToItem();
if (item != null)
{
item.IllustTapped = command;
}
return item;
});
}
else
{
return data.body.page.recommend;
return data.body.page.recommend.Select(id =>
{
var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == id)?.ConvertToItem();
if (item != null)
{
item.IllustTapped = command;
}
return item;
});
}
}
@@ -32,5 +60,14 @@ namespace Pixiview.Illust
{
return Stores.LoadIllustData(force);
}
private void Refresh_Clicked(object sender, EventArgs e)
{
if (Loading)
{
return;
}
StartLoad(true);
}
}
}

View File

@@ -3,17 +3,15 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mdl="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:util="clr-namespace:Pixiview.Utils"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
x:Class="Pixiview.Illust.ViewIllustPage"
util:Screen.StatusBarStyle="{DynamicResource StatusBarStyle}"
ios:Page.UseSafeArea="False"
Shell.TabBarIsVisible="False"
BackgroundColor="{DynamicResource WindowColor}"
Title="{Binding IllustItem.Title}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Download_Clicked"
IconImageSource="{DynamicResource FontIconDownload}"/>
<ToolbarItem Order="Primary" Clicked="Favorite_Clicked"
IconImageSource="{Binding IsFavorite}"/>
</ContentPage.ToolbarItems>
<Grid Padding="{Binding PageTopMargin}">
<CarouselView ItemsSource="{Binding Illusts}" HorizontalScrollBarVisibility="Never"

View File

@@ -1,7 +1,9 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Pixiview.Resources;
using Pixiview.UI;
using Pixiview.UI.Theme;
using Pixiview.Utils;
using Xamarin.Essentials;
using Xamarin.Forms;
@@ -11,6 +13,8 @@ namespace Pixiview.Illust
[QueryProperty("IllustId", "id")]
public partial class ViewIllustPage : AdaptedPage
{
public static readonly BindableProperty IsFavoriteProperty = BindableProperty.Create(
nameof(IsFavorite), typeof(ImageSource), typeof(ViewIllustPage));
public static readonly BindableProperty IllustsProperty = BindableProperty.Create(
nameof(Illusts), typeof(IllustDetailItem[]), typeof(ViewIllustPage));
public static readonly BindableProperty PagePositionTextProperty = BindableProperty.Create(
@@ -22,6 +26,8 @@ namespace Pixiview.Illust
public static readonly BindableProperty PageTopMarginProperty = BindableProperty.Create(
nameof(PageTopMargin), typeof(Thickness), typeof(ViewIllustPage));
public ImageSource IsFavorite => (ImageSource)GetValue(IsFavoriteProperty);
public IllustDetailItem[] Illusts
{
get => (IllustDetailItem[])GetValue(IllustsProperty);
@@ -50,23 +56,38 @@ namespace Pixiview.Illust
public int CurrentPage { get; private set; }
public ViewIllustPage(IllustItem illust)
private readonly IIllustCollectionPage collectionPage;
private readonly object fontIconLove;
private readonly object fontIconNotLove;
public ViewIllustPage(IllustItem illust, IIllustCollectionPage page)
{
IllustItem = illust;
collectionPage = page;
BindingContext = this;
InitializeComponent();
}
fontIconLove = Application.Current.Resources[ThemeBase.FontIconLove];
fontIconNotLove = Application.Current.Resources[ThemeBase.FontIconNotLove];
if (page.Favorites != null)
{
SetValue(IsFavoriteProperty, page.Favorites.Any(i => i.Id == illust.Id)
? fontIconLove
: fontIconNotLove);
}
InitializeComponent();
public override void OnLoad()
{
var illust = IllustItem;
if (illust != null)
{
LoadIllust(illust);
}
}
public override void OnLoad()
{
OnOrientationChanged(CurrentOrientation);
}
private void LoadIllust(IllustItem illust)
{
if (illust == null)
@@ -95,7 +116,6 @@ namespace Pixiview.Illust
}
Illusts = items;
OnOrientationChanged(CurrentOrientation);
Task.Run(DoLoadImages);
}
@@ -134,6 +154,13 @@ namespace Pixiview.Illust
protected override void OnDisappearing()
{
base.OnDisappearing();
var favorite = new IllustFavorite
{
LastFavoriteUtc = DateTime.UtcNow,
Illusts = collectionPage.Favorites.ToArray()
};
Stores.SaveFavoritesIllusts(favorite);
Screen.SetHomeIndicatorAutoHidden(Shell.Current, false);
}
@@ -220,6 +247,25 @@ namespace Pixiview.Illust
item.Loading = false;
}
private void Favorite_Clicked(object sender, EventArgs e)
{
if (collectionPage.Favorites == null)
{
return;
}
var index = collectionPage.Favorites.FindIndex(i => i.Id == IllustItem.Id);
if (index < 0)
{
collectionPage.Favorites.Insert(0, IllustItem);
SetValue(IsFavoriteProperty, fontIconLove);
}
else
{
collectionPage.Favorites.RemoveAt(index);
SetValue(IsFavoriteProperty, fontIconNotLove);
}
}
private async void Download_Clicked(object sender, EventArgs e)
{
var status = await Permissions.CheckStatusAsync<Permissions.Photos>();