update folder structure

This commit is contained in:
2020-05-06 20:47:06 +08:00
parent 010c93f8d5
commit 00abfed1b3
23 changed files with 197 additions and 70 deletions

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<u:AdaptedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mdl="clr-namespace:Pixiview.Illust"
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:util="clr-namespace:Pixiview.Utils"
xmlns:r="clr-namespace:Pixiview.Resources"
mc:Ignorable="d"
x:Class="Pixiview.Illust.MainPage"
util:Screen.StatusBarStyle="{DynamicResource StatusBarStyle}"
Shell.NavBarHasShadow="True"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Follow}"
OrientationChanged="Page_OrientationChanged">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Refresh_Clicked"
IconImageSource="{DynamicResource FontIconRefresh}"/>
</ContentPage.ToolbarItems>
<Grid>
<ScrollView HorizontalOptions="Fill">
<u:FlowLayout ItemsSource="{Binding Illusts}"
HorizontalOptions="Fill" Column="{Binding Columns}"
Margin="16" RowSpacing="16" ColumnSpacing="16">
<u:FlowLayout.ItemTemplate>
<DataTemplate x:DataType="mdl:IllustItem">
<u:CardView Padding="0" Margin="0" CornerRadius="10"
ShadowColor="#20000000"
ShadowOffset="2, 2"
BackgroundColor="{DynamicResource SubColor}">
<Grid HorizontalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding ImageHeight}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<u:RoundImage BackgroundColor="LightGray"
CornerRadius="10"
CornerMasks="Top"
Source="{Binding Image}"
HorizontalOptions="Fill"
Aspect="AspectFill">
<u:RoundImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding IllustTapped}" CommandParameter="{Binding .}"/>
</u:RoundImage.GestureRecognizers>
</u:RoundImage>
<u:RoundLabel Text="R-18" BackgroundColor="#fd4363" Margin="6, 6, 0, 0"
Padding="6, 2" CornerRadius="4"
HorizontalOptions="Start" VerticalOptions="Start"
FontSize="Micro" TextColor="White"
IsVisible="{Binding IsRestrict}"/>
<u:RoundLabel Text="{Binding PageCountText}"
FontFamily="{DynamicResource IconSolidFontFamily}"
BackgroundColor="#50000000" Margin="0, 6, 6, 0"
Padding="6, 4" CornerRadius="6"
HorizontalOptions="End" VerticalOptions="Start"
FontSize="Micro" TextColor="White"
IsVisible="{Binding IsPageVisible}"/>
<Label Grid.Row="1" Text="{Binding Title}"
Padding="8, 2"
TextColor="{DynamicResource TextColor}"
HorizontalOptions="Start"
LineBreakMode="TailTruncation"
FontSize="Small"/>
<StackLayout Grid.Row="2" Orientation="Horizontal" Padding="8, 0, 8, 8">
<u:CircleImage WidthRequest="30" HeightRequest="30" Aspect="AspectFill"
Source="{Binding ProfileImage}"/>
<Label Text="{Binding UserName}"
VerticalOptions="Center"
TextColor="{DynamicResource SubTextColor}"
LineBreakMode="TailTruncation"
FontSize="Micro"/>
</StackLayout>
</Grid>
</u:CardView>
</DataTemplate>
</u:FlowLayout.ItemTemplate>
</u:FlowLayout>
</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>
</u:AdaptedPage>

View File

@@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Pixiview.UI;
using Pixiview.Utils;
using Xamarin.Essentials;
using Xamarin.Forms;
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 : AdaptedPage
{
#region - Properties -
public static readonly BindableProperty IllustsProperty = BindableProperty.Create(
nameof(Illusts), typeof(IllustCollection), typeof(MainPage));
public static readonly BindableProperty ColumnsProperty = BindableProperty.Create(
nameof(Columns), typeof(int), typeof(MainPage), 2);
public static readonly BindableProperty LoadingProperty = BindableProperty.Create(
nameof(Loading), typeof(bool), typeof(MainPage), propertyChanged: OnLoadingPropertyChanged);
private static void OnLoadingPropertyChanged(BindableObject obj, object oldValue, object newValue)
{
var page = (MainPage)obj;
var now = (bool)newValue;
if (!page.loaded && now && Stores.NetworkAvailable)
{
page.loaded = true;
Task.Run(() => page.DoLoadIllusts());
}
}
public IllustCollection Illusts
{
get => (IllustCollection)GetValue(IllustsProperty);
set => SetValue(IllustsProperty, value);
}
public int Columns
{
get => (int)GetValue(ColumnsProperty);
set => SetValue(ColumnsProperty, value);
}
public bool Loading
{
get => (bool)GetValue(LoadingProperty);
set => SetValue(LoadingProperty, value);
}
#endregion
private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads };
private readonly Command<IllustItem> commandIllustImageTapped;
private IllustData illustData;
private bool loaded;
public MainPage()
{
BindingContext = this;
InitializeComponent();
commandIllustImageTapped = new Command<IllustItem>(OnIllustImageTapped);
}
#region - Overrides -
public override void OnLoad()
{
App.DebugPrint($"folder: {Stores.PersonalFolder}");
Loading = true;
}
protected override void OnAppearing()
{
base.OnAppearing();
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Connectivity.ConnectivityChanged -= Connectivity_ConnectivityChanged;
}
#endregion
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
if (e.NetworkAccess == NetworkAccess.Internet || e.NetworkAccess == NetworkAccess.ConstrainedInternet)
{
if (!loaded)
{
Loading = true;
}
}
}
#region - Illust Tasks -
void DoLoadIllusts(bool force = false)
{
illustData = Stores.LoadIllustData(force);
if (illustData == null)
{
App.DebugError("illusts.load", "failed to load illusts data.");
return;
}
var data = illustData.body.page.follow.Select(i =>
{
var illust = illustData.body.thumbnails.illust.FirstOrDefault(l => l.illustId == i.ToString());
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 collection = new IllustCollection(data);
Illusts = collection;
Loading = false;
DoLoadImages(collection);
}
void DoLoadImages(IllustCollection collection)
{
Parallel.ForEach(collection, parallelOptions, illust =>
{
if (!collection.Running)
{
return;
}
if (illust.ImageUrl != null)
{
var url = Configs.GetThumbnailUrl(illust.ImageUrl);
var image = Stores.LoadThumbnailImage(url);
if (image != null)
{
illust.Image = image;
}
}
if (illust.ProfileUrl != null)
{
var userImage = Stores.LoadUserProfileImage(illust.ProfileUrl);
if (userImage != null)
{
illust.ProfileImage = userImage;
}
}
});
}
#endregion
private void OnIllustImageTapped(IllustItem illust)
{
Start(() =>
{
var page = new ViewIllustPage(illust);
Navigation.PushAsync(page);
});
}
private void Page_OrientationChanged(object sender, OrientationEventArgs e)
{
switch (e.CurrentOrientation)
{
case Orientation.Portrait:
Columns = 2;
break;
case Orientation.PortraitUpsideDown:
if (DeviceInfo.Idiom == DeviceIdiom.Phone)
{
Columns = 4;
}
else
{
Columns = 2;
}
break;
case Orientation.Unknown:
case Orientation.LandscapeLeft:
case Orientation.LandscapeRight:
default:
Columns = 4;
break;
}
}
private void Refresh_Clicked(object sender, EventArgs e)
{
if (Loading)
{
return;
}
Loading = true;
Task.Run(() => DoLoadIllusts(true));
}
}
public class IllustCollection : ObservableCollection<IllustItem>
{
private static readonly object sync = new object();
public IllustCollection(IEnumerable<IllustItem> illusts) : base(illusts)
{
running = true;
}
private bool running;
public bool Running
{
get => running;
set
{
lock (sync)
{
running = value;
}
}
}
}
public class IllustItem : BindableObject
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
nameof(Image), typeof(ImageSource), typeof(IllustItem));
public static readonly BindableProperty ProfileImageProperty = BindableProperty.Create(
nameof(ProfileImage), typeof(ImageSource), typeof(IllustItem));
public static readonly BindableProperty ImageHeightProperty = BindableProperty.Create(
nameof(ImageHeight), typeof(GridLength), typeof(IllustItem), GridLength.Auto);
public ImageSource Image
{
get => (ImageSource)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
public ImageSource ProfileImage
{
get => (ImageSource)GetValue(ProfileImageProperty);
set => SetValue(ProfileImageProperty, value);
}
public GridLength ImageHeight
{
get => (GridLength)GetValue(ImageHeightProperty);
set => SetValue(ImageHeightProperty, value);
}
public ICommand IllustTapped { get; set; }
public string Id { get; set; }
public string ImageUrl { get; set; }
public string Title { get; set; }
public bool IsRestrict { get; set; }
public string ProfileUrl { get; set; }
public string UserId { get; set; }
public string UserName { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int PageCount { get; set; }
public string PageCountText => $"{StyleDefinition.IconLayer} {PageCount}";
public bool IsPageVisible => PageCount > 1;
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Pixiview.Illust.NewsPage">
<ContentPage.Content>
<Label Text="News" HorizontalOptions="Center" VerticalOptions="Center"/>
</ContentPage.Content>
</ContentPage>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Pixiview.Illust
{
public partial class NewsPage : ContentPage
{
public NewsPage()
{
InitializeComponent();
}
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Pixiview.Illust.RankingPage">
<ContentPage.Content>
<Label Text="Ranking #100" HorizontalOptions="Center" VerticalOptions="Center"/>
</ContentPage.Content>
</ContentPage>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Pixiview.Illust
{
public partial class RankingPage : ContentPage
{
public RankingPage()
{
InitializeComponent();
}
}
}

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<u:AdaptedPage xmlns="http://xamarin.com/schemas/2014/forms"
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"
Shell.NavBarHasShadow="True"
BackgroundColor="{DynamicResource WindowColor}"
Title="{Binding IllustItem.Title}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Clicked="Download_Clicked"
IconImageSource="{DynamicResource FontIconDownload}"/>
</ContentPage.ToolbarItems>
<Grid Padding="{Binding PageTopMargin}">
<CarouselView ItemsSource="{Binding Illusts}" HorizontalScrollBarVisibility="Never"
PositionChanged="CarouselView_PositionChanged">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="20"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="mdl:IllustDetailItem">
<Grid>
<Image Source="{Binding Image}"
HorizontalOptions="Fill" VerticalOptions="Fill"
Aspect="AspectFit"/>
<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>
<ActivityIndicator IsRunning="True" IsVisible="{Binding Downloading}"
Margin="10"
HorizontalOptions="Start" VerticalOptions="Start"
Color="{DynamicResource TextColor}"/>
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<u:RoundLabel Text="{Binding PagePositionText}"
BackgroundColor="{DynamicResource MaskColor}" Margin="0, 6, 6, 0"
Padding="6, 4" CornerRadius="6"
HorizontalOptions="End" VerticalOptions="Start"
FontSize="Micro" TextColor="White"
IsVisible="{Binding IsPageVisible}"/>
</Grid>
</u:AdaptedPage>

View File

@@ -0,0 +1,281 @@
using System;
using System.Threading.Tasks;
using Pixiview.Resources;
using Pixiview.UI;
using Pixiview.Utils;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Pixiview.Illust
{
public partial class ViewIllustPage : AdaptedPage
{
public static readonly BindableProperty IllustsProperty = BindableProperty.Create(
nameof(Illusts), typeof(IllustDetailItem[]), typeof(ViewIllustPage));
public static readonly BindableProperty PagePositionTextProperty = BindableProperty.Create(
nameof(PagePositionText), typeof(string), typeof(ViewIllustPage));
public static readonly BindableProperty IsPageVisibleProperty = BindableProperty.Create(
nameof(IsPageVisible), typeof(bool), typeof(ViewIllustPage));
public static readonly BindableProperty IllustItemProperty = BindableProperty.Create(
nameof(IllustItem), typeof(IllustItem), typeof(ViewIllustPage));
public static readonly BindableProperty PageTopMarginProperty = BindableProperty.Create(
nameof(PageTopMargin), typeof(Thickness), typeof(ViewIllustPage));
public IllustDetailItem[] Illusts
{
get => (IllustDetailItem[])GetValue(IllustsProperty);
set => SetValue(IllustsProperty, value);
}
public string PagePositionText
{
get => (string)GetValue(PagePositionTextProperty);
set => SetValue(PagePositionTextProperty, value);
}
public bool IsPageVisible
{
get => (bool)GetValue(IsPageVisibleProperty);
set => SetValue(IsPageVisibleProperty, value);
}
public IllustItem IllustItem
{
get => (IllustItem)GetValue(IllustItemProperty);
private set => SetValue(IllustItemProperty, value);
}
public Thickness PageTopMargin
{
get => (Thickness)GetValue(PageTopMarginProperty);
private set => SetValue(PageTopMarginProperty, value);
}
public int CurrentPage { get; private set; }
public ViewIllustPage(IllustItem illust)
{
IllustItem = illust;
BindingContext = this;
InitializeComponent();
}
public override void OnLoad()
{
var illust = IllustItem;
if (illust == null)
{
return;
}
var items = new IllustDetailItem[illust.PageCount];
if (items.Length > 1)
{
IsPageVisible = true;
PagePositionText = $"1/{items.Length}";
}
else
{
IsPageVisible = false;
}
for (var i = 0; i < items.Length; i++)
{
items[i] = new IllustDetailItem();
if (i == 0)
{
items[i].Image = illust.Image;
}
}
Illusts = items;
OnOrientationChanged(CurrentOrientation);
Task.Run(DoLoadImages);
}
public override void OnOrientationChanged(Orientation orientation)
{
switch (orientation)
{
case Orientation.Portrait:
PageTopMargin = AppShell.TotalBarOffset;
break;
case Orientation.PortraitUpsideDown:
if (DeviceInfo.Idiom == DeviceIdiom.Phone)
{
PageTopMargin = AppShell.NavigationBarOffset;
}
else
{
PageTopMargin = AppShell.TotalBarOffset;
}
break;
case Orientation.Unknown:
case Orientation.LandscapeLeft:
case Orientation.LandscapeRight:
default:
PageTopMargin = AppShell.NavigationBarOffset;
break;
}
}
protected override void OnAppearing()
{
base.OnAppearing();
Screen.SetHomeIndicatorAutoHidden(Shell.Current, true);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Screen.SetHomeIndicatorAutoHidden(Shell.Current, false);
}
private void CarouselView_PositionChanged(object sender, PositionChangedEventArgs e)
{
var index = e.CurrentPosition;
CurrentPage = index;
var items = Illusts;
var length = items.Length;
PagePositionText = $"{index + 1}/{length}";
var item = items[index];
if (!item.Loading && item.Image == null)
{
Task.Run(() => DoLoadImage(index));
}
if (index < length - 1)
{
item = items[index + 1];
if (!item.Loading && item.Image == null)
{
Task.Run(() => DoLoadImage(index + 1));
}
}
}
private void DoLoadImages()
{
var pages = Stores.LoadIllustPageData(IllustItem.Id);
if (pages == null)
{
App.DebugError("illustPage.load", $"failed to load illust page data, id: {IllustItem.Id}");
return;
}
var items = Illusts;
if (pages.body.Length > items.Length)
{
App.DebugPrint($"local page count ({items.Length}) is not equals the remote one ({pages.body.Length})");
var tmp = new IllustDetailItem[pages.body.Length];
items.CopyTo(items, 0);
for (var i = items.Length; i < tmp.Length; i++)
{
tmp[i] = new IllustDetailItem();
}
items = tmp;
}
for (var i = 0; i < items.Length; i++)
{
var item = items[i];
var p = pages.body[i];
item.PreviewUrl = p.urls.regular;
item.OriginalUrl = p.urls.original;
}
DoLoadImage(0);
if (items.Length > 1)
{
DoLoadImage(1);
}
}
private void DoLoadImage(int index)
{
var items = Illusts;
if (index < 0 || index >= items.Length)
{
App.DebugPrint($"invalid index: {index}");
return;
}
var item = items[index];
if (item.Loading || (index > 0 && item.Image != null))
{
App.DebugPrint($"skipped, loading or already loaded, index: {index}, loading: {item.Loading}");
return;
}
item.Loading = true;
var image = Stores.LoadPreviewImage(item.PreviewUrl);
if (image != null)
{
item.Image = image;
}
item.Loading = false;
}
private async void Download_Clicked(object sender, EventArgs e)
{
var status = await Permissions.CheckStatusAsync<Permissions.Photos>();
if (status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.Photos>();
if (status != PermissionStatus.Granted)
{
App.DebugPrint("access denied to gallery.");
return;
}
}
var item = Illusts[CurrentPage];
if (item.Downloading)
{
return;
}
item.Downloading = true;
_ = Task.Run(() => DoLoadOriginalImage(item));
}
private void DoLoadOriginalImage(IllustDetailItem item)
{
var image = Stores.LoadIllustImage(item.OriginalUrl);
if (image != null)
{
Device.BeginInvokeOnMainThread(async () =>
{
var service = DependencyService.Get<IFileStore>();
var result = await service.SaveImageToGalleryAsync(image);
string message = result ?? ResourceHelper.SaveSuccess;
await DisplayAlert(ResourceHelper.Title, message, ResourceHelper.Ok);
});
}
item.Downloading = false;
}
}
public class IllustDetailItem : BindableObject
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
nameof(Image), typeof(ImageSource), typeof(IllustDetailItem));
public static readonly BindableProperty LoadingProperty = BindableProperty.Create(
nameof(Loading), typeof(bool), typeof(IllustDetailItem));
public static readonly BindableProperty DownloadingProperty = BindableProperty.Create(
nameof(Downloading), typeof(bool), typeof(IllustDetailItem));
public ImageSource Image
{
get => (ImageSource)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
public bool Loading
{
get => (bool)GetValue(LoadingProperty);
set => SetValue(LoadingProperty, value);
}
public bool Downloading
{
get => (bool)GetValue(DownloadingProperty);
set => SetValue(DownloadingProperty, value);
}
public string PreviewUrl { get; set; }
public string OriginalUrl { get; set; }
}
}