optimize: remove unnecessary bindable objects

This commit is contained in:
2020-05-14 11:17:39 +08:00
parent f6dbec2fda
commit 08ad76d8de
16 changed files with 148 additions and 85 deletions

View File

@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Resources;
using Pixiview.Utils;
using Xamarin.Essentials;
@@ -33,13 +31,9 @@ namespace Pixiview.Illust
});
}
protected override IEnumerable<IllustItem> DoGetIllustList(IllustItem[] data, ICommand command)
protected override IEnumerable<IllustItem> DoGetIllustList(IllustItem[] data)
{
return data.Select(i =>
{
i.IllustTapped = command;
return i;
});
return data;
}
protected override IllustItem[] DoLoadIllustData(bool force)

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Newtonsoft.Json;
using Pixiview.Resources;
using Pixiview.UI;
@@ -56,12 +55,12 @@ namespace Pixiview.Illust
public IllustCollection IllustCollection { get; set; }
protected virtual bool IsFavoriteVisible => true;
protected virtual bool IsLazyload => false;
protected readonly Command<IllustItem> commandIllustImageTapped;
protected DateTime lastUpdated;
protected double topOffset;
private T illustData;
private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads };
private readonly Command<IllustItem> commandIllustImageTapped;
public IllustCollectionPage()
{
@@ -71,6 +70,10 @@ namespace Pixiview.Illust
private void IllustImageTapped(IllustItem illust)
{
if (illust == null)
{
return;
}
Start(() => OnIllustImageTapped(illust));
}
@@ -89,18 +92,11 @@ namespace Pixiview.Illust
if (lastUpdated != LastUpdated)
{
if (IsLazyload)
{
Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
{
StartLoad();
return false;
});
}
else
Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
{
StartLoad();
}
return false;
});
}
else if (IsFavoriteVisible && IllustCollection != null)
{
@@ -133,6 +129,7 @@ namespace Pixiview.Illust
{
base.OnSizeAllocated(width, height);
int columns;
#if __IOS__
var oldMargin = PanelTopMargin;
Thickness newMargin;
if (StyleDefinition.IsFullscreenDevice)
@@ -140,22 +137,26 @@ namespace Pixiview.Illust
newMargin = width > height ?
AppShell.HalfNavigationBarOffset :
AppShell.NavigationBarOffset;
topOffset = width > height ?
AppShell.NavigationBarOffset.Top :
AppShell.TotalBarOffset.Top;
}
else if (isPhone)
{
#if __IOS__
newMargin = width > height ?
StyleDefinition.TopOffset16 :
StyleDefinition.TopOffset32;
#elif __ANDROID__
newMargin = default;
#endif
topOffset = width > height ?
StyleDefinition.TopOffset32.Top :
AppShell.TotalBarOffset.Top;
}
else
{
// TODO ipad
newMargin = StyleDefinition.TopOffset32;
// ipad
newMargin = StyleDefinition.TopOffset37;
topOffset = AppShell.TotalBarOffset.Top;
}
#endif
if (width > height)
{
columns = isPhone ? 4 : 6;
@@ -164,11 +165,13 @@ namespace Pixiview.Illust
{
columns = isPhone ? 2 : 4;
}
#if __IOS__
if (oldMargin != newMargin)
{
PanelTopMargin = newMargin;
OnPanelTopMarginChanged(oldMargin, newMargin);
}
#endif
if (Columns != columns)
{
Columns = columns;
@@ -179,7 +182,7 @@ namespace Pixiview.Illust
#endregion
protected abstract T DoLoadIllustData(bool force);
protected abstract IEnumerable<IllustItem> DoGetIllustList(T data, ICommand command);
protected abstract IEnumerable<IllustItem> DoGetIllustList(T data);
protected virtual void OnIllustImageTapped(IllustItem illust)
{
var page = new ViewIllustPage(illust, IsFavoriteVisible);
@@ -217,9 +220,11 @@ namespace Pixiview.Illust
Aspect = Aspect.AspectFill,
GestureRecognizers =
{
new TapGestureRecognizer()
.Binding(TapGestureRecognizer.CommandProperty, nameof(IllustItem.IllustTapped))
.Binding(TapGestureRecognizer.CommandParameterProperty, ".")
new TapGestureRecognizer
{
Command = commandIllustImageTapped
}
.Binding(TapGestureRecognizer.CommandParameterProperty, ".")
}
}
.Binding(Image.SourceProperty, nameof(IllustItem.Image));
@@ -436,7 +441,7 @@ namespace Pixiview.Illust
lastUpdated = now;
}
var data = DoGetIllustList(illustData, commandIllustImageTapped).Where(i => i != null);
var data = DoGetIllustList(illustData).Where(i => i != null);
var collection = new IllustCollection(data);
if (IsFavoriteVisible)
@@ -493,6 +498,13 @@ namespace Pixiview.Illust
#endregion
}
public enum ScrollDirection
{
Stop,
Up,
Down
}
public class IllustCollection : List<IllustItem>
{
private static IllustCollection empty;
@@ -546,13 +558,29 @@ namespace Pixiview.Illust
Anime = 2
}
[JsonObject(MemberSerialization.OptIn)]
public class IllustItem : BindableObject
public interface IIllustUser
{
//public static readonly BindableProperty TitleProperty = BindableProperty.Create(
// nameof(Title), typeof(string), typeof(IllustItem));
//public static readonly BindableProperty RankTitleProperty = BindableProperty.Create(
// nameof(RankTitle), typeof(string), typeof(IllustItem));
string UserId { get; }
string UserName { get; }
ImageSource ProfileImage { get; }
}
[JsonObject(MemberSerialization.OptIn)]
public class IllustItem : BindableObject, IIllustUser
{
private static IllustItem empty;
public static IllustItem Empty
{
get
{
if (empty == null)
{
empty = new IllustItem();
}
return empty;
}
}
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
nameof(Image), typeof(ImageSource), typeof(IllustItem));
public static readonly BindableProperty ProfileImageProperty = BindableProperty.Create(
@@ -561,20 +589,10 @@ namespace Pixiview.Illust
nameof(ImageHeight), typeof(GridLength), typeof(IllustItem), GridLength.Auto);
public static readonly BindableProperty IsFavoriteProperty = BindableProperty.Create(
nameof(IsFavorite), typeof(bool), typeof(IllustItem));
//public static readonly BindableProperty IsPlayingProperty = BindableProperty.Create(
// nameof(IsPlaying), typeof(bool), typeof(IllustItem));
[JsonProperty]
public string Title { get; set; }
//{
// get => (string)GetValue(TitleProperty);
// set => SetValue(TitleProperty, value);
//}
public string RankTitle { get; set; }
//{
// get => (string)GetValue(RankTitleProperty);
// set => SetValue(RankTitleProperty, value);
//}
public ImageSource Image
{
get => (ImageSource)GetValue(ImageProperty);
@@ -596,11 +614,6 @@ namespace Pixiview.Illust
set => SetValue(IsFavoriteProperty, value);
}
public bool IsPlaying { get; set; }
//{
// get => (bool)GetValue(IsPlayingProperty);
// set => SetValue(IsPlayingProperty, value);
//}
public ICommand IllustTapped { get; set; }
[JsonProperty]
public string Id { get; set; }