fix: favorites sync logic

This commit is contained in:
2020-05-19 23:52:53 +08:00
parent 8381e71a45
commit 38fa6d4d21
10 changed files with 57 additions and 64 deletions

View File

@ -55,7 +55,7 @@
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.6.0.772" /> <PackageReference Include="Xamarin.Forms" Version="4.6.0.800" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" /> <PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Newtonsoft.Json"> <PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version> <Version>12.0.3</Version>

View File

@ -145,7 +145,7 @@
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.6.0.772" /> <PackageReference Include="Xamarin.Forms" Version="4.6.0.800" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" /> <PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -136,7 +136,7 @@ namespace Pixiview
if (m.Success) if (m.Success)
{ {
var illust = new IllustItem { Id = m.Groups[1].Value }; var illust = new IllustItem { Id = m.Groups[1].Value };
var page = new ViewIllustPage(illust, true); var page = new ViewIllustPage(illust);
MainThread.BeginInvokeOnMainThread(() => current.Navigation.PushAsync(page)); MainThread.BeginInvokeOnMainThread(() => current.Navigation.PushAsync(page));
} }
else else

View File

@ -43,7 +43,6 @@ namespace Pixiview.Illust
var favorites = Stores.Favorites; var favorites = Stores.Favorites;
if (favorites.Changed) if (favorites.Changed)
{ {
favorites.Reload();
lastUpdated = default; lastUpdated = default;
startIndex = -1; startIndex = -1;
nextIndex = 0; nextIndex = 0;
@ -69,6 +68,7 @@ namespace Pixiview.Illust
return null; return null;
} }
favs = favorites.Illusts; favs = favorites.Illusts;
favs.Reload();
startIndex = 0; startIndex = 0;
} }
else else
@ -200,7 +200,7 @@ namespace Pixiview.Illust
var bookmark = list.FirstOrDefault(f => f.Id == b.Id); var bookmark = list.FirstOrDefault(f => f.Id == b.Id);
if (bookmark == null) if (bookmark == null)
{ {
if (!string.IsNullOrEmpty(bookmarkId)) if (bookmarkId != null)
{ {
// not exists in remote any more // not exists in remote any more
App.DebugPrint($"remove bookmark ({bookmarkId}) - {b.Id}: {b.Title}"); App.DebugPrint($"remove bookmark ({bookmarkId}) - {b.Id}: {b.Title}");
@ -215,22 +215,17 @@ namespace Pixiview.Illust
} }
} }
// add bookmarks that exists in remote only // add bookmarks that exists in remote only
var news = list.Where(f => !nows.Any(i => i.Id == f.Id)).ToArray(); list = list.Where(f => !nows.Any(i => i.Id == f.Id)).ToArray();
if (news.Length > 0) if (list.Length > 0)
{ {
for (var i = 0; i < news.Length; i++) for (var i = 0; i < list.Length; i++)
{ {
var item = news[i]; var item = list[i];
App.DebugPrint($"add bookmark ({item.BookmarkId}) - {item.Id}: {item.Title}"); App.DebugPrint($"add bookmark ({item.BookmarkId}) - {item.Id}: {item.Title}");
item.Image = StyleDefinition.DownloadBackground; item.Image = StyleDefinition.DownloadBackground;
} }
nows.InsertRange(0, news); nows.InsertRange(0, list);
}
else
{
CloseLoading();
return;
} }
} }
else else

View File

@ -115,12 +115,14 @@ namespace Pixiview.Illust
{ {
StartLoad(); StartLoad();
} }
else if (IsFavoriteVisible && IllustCollection != null) else if (IllustCollection != null)
{ {
var favorites = Stores.Favorites; var favorites = Stores.Favorites;
foreach (var item in IllustCollection) foreach (var item in IllustCollection)
{ {
item.IsFavorite = favorites.Any(i => i.Id == item.Id); item.IsFavorite =
item.BookmarkId != null ||
favorites.Any(i => i.Id == item.Id);
} }
} }
} }
@ -174,7 +176,7 @@ namespace Pixiview.Illust
protected abstract IEnumerable<IllustItem> DoGetIllustList(T data); protected abstract IEnumerable<IllustItem> DoGetIllustList(T data);
protected virtual void OnIllustImageTapped(IllustItem illust) protected virtual void OnIllustImageTapped(IllustItem illust)
{ {
var page = new ViewIllustPage(illust, true); var page = new ViewIllustPage(illust);
Navigation.PushAsync(page); Navigation.PushAsync(page);
} }
protected virtual IllustCollection GetIllustsLoadedCollection(IllustCollection collection, bool bottom) protected virtual IllustCollection GetIllustsLoadedCollection(IllustCollection collection, bool bottom)
@ -390,8 +392,8 @@ namespace Pixiview.Illust
TextColor = StyleDefinition.ColorRedBackground, TextColor = StyleDefinition.ColorRedBackground,
IsVisible = false IsVisible = false
} }
.Binding(Label.TextProperty, ".", converter: new FavoriteIconConverter(IsFavoriteVisible)) .Binding(Label.TextProperty, nameof(IllustItem.BookmarkId), converter: new FavoriteIconConverter(IsFavoriteVisible))
.Binding(IsVisibleProperty, ".", converter: new FavoriteVisibleConverter()) .Binding(IsVisibleProperty, nameof(IllustItem.IsFavorite))
.DynamicResource(Label.FontFamilyProperty, ThemeBase.IconSolidFontFamily); .DynamicResource(Label.FontFamilyProperty, ThemeBase.IconSolidFontFamily);
#endregion #endregion
@ -566,7 +568,9 @@ namespace Pixiview.Illust
{ {
item.Image = StyleDefinition.DownloadBackground; item.Image = StyleDefinition.DownloadBackground;
} }
item.IsFavorite = IsFavoriteVisible && favorites.Any(i => i.Id == item.Id); item.IsFavorite =
item.BookmarkId != null ||
favorites.Any(i => i.Id == item.Id);
} }
DoIllustsLoaded(collection, bottom); DoIllustsLoaded(collection, bottom);
@ -751,8 +755,6 @@ namespace Pixiview.Illust
string UserId { get; } string UserId { get; }
string UserName { get; } string UserName { get; }
ImageSource ProfileImage { get; } ImageSource ProfileImage { get; }
bool IsFavorite { get; }
string BookmarkId { get; }
} }
[JsonObject(MemberSerialization.OptIn)] [JsonObject(MemberSerialization.OptIn)]

View File

@ -123,9 +123,9 @@ namespace Pixiview.Illust
public IllustItem IllustItem { get; private set; } public IllustItem IllustItem { get; private set; }
private readonly bool saveFavorites;
private readonly ImageSource fontIconLove; private readonly ImageSource fontIconLove;
private readonly ImageSource fontIconNotLove; private readonly ImageSource fontIconNotLove;
private bool favoriteChanged;
private IllustUgoiraData ugoiraData; private IllustUgoiraData ugoiraData;
private Ugoira ugoira; private Ugoira ugoira;
private ParallelTask task; private ParallelTask task;
@ -135,11 +135,10 @@ namespace Pixiview.Illust
private int pageCount; private int pageCount;
private bool isPreloading; private bool isPreloading;
public ViewIllustPage(IllustItem illust, bool save) public ViewIllustPage(IllustItem illust)
{ {
IllustItem = illust; IllustItem = illust;
Title = illust.Title; Title = illust.Title;
saveFavorites = save;
BindingContext = this; BindingContext = this;
fontIconLove = (ImageSource)Application.Current.Resources[ThemeBase.FontIconLove]; fontIconLove = (ImageSource)Application.Current.Resources[ThemeBase.FontIconLove];
@ -184,7 +183,7 @@ namespace Pixiview.Illust
ugoira = null; ugoira = null;
} }
if (saveFavorites) if (favoriteChanged)
{ {
Stores.SaveFavoritesIllusts(); Stores.SaveFavoritesIllusts();
} }
@ -523,6 +522,7 @@ namespace Pixiview.Illust
favorites.RemoveAt(index); favorites.RemoveAt(index);
FavoriteIcon = fontIconNotLove; FavoriteIcon = fontIconNotLove;
} }
favoriteChanged = true;
if (Configs.SyncFavType == SyncType.None) if (Configs.SyncFavType == SyncType.None)
{ {
@ -557,6 +557,10 @@ namespace Pixiview.Illust
{ {
_ = Task.Run(() => Stores.DeleteBookmark(illust.BookmarkId)); _ = Task.Run(() => Stores.DeleteBookmark(illust.BookmarkId));
} }
// immediately save after changing remote
Stores.SaveFavoritesIllusts();
favoriteChanged = false;
} }
private void Image_Tapped(object sender, EventArgs e) private void Image_Tapped(object sender, EventArgs e)

View File

@ -83,8 +83,8 @@ namespace Pixiview
else else
{ {
IsProxied = false; IsProxied = false;
Host = string.Empty; Host = Preferences.Get(Configs.HostKey, string.Empty);
Port = string.Empty; Port = Preferences.Get(Configs.PortKey, string.Empty);
} }
Cookie = Configs.Cookie; Cookie = Configs.Cookie;
@ -134,7 +134,7 @@ namespace Pixiview
} }
} }
} }
else if (proxy != null) else
{ {
Preferences.Set(Configs.IsProxiedKey, false); Preferences.Set(Configs.IsProxiedKey, false);
Preferences.Set(Configs.HostKey, h); Preferences.Set(Configs.HostKey, h);
@ -142,8 +142,11 @@ namespace Pixiview
{ {
Preferences.Set(Configs.PortKey, pt); Preferences.Set(Configs.PortKey, pt);
} }
Configs.Proxy = null; if (proxy != null)
App.DebugPrint("clear proxy"); {
Configs.Proxy = null;
App.DebugPrint("clear proxy");
}
} }
var cookie = Cookie; var cookie = Cookie;

View File

@ -1,28 +1,10 @@
using System; using System;
using System.Globalization; using System.Globalization;
using Pixiview.Illust;
using Pixiview.UI; using Pixiview.UI;
using Xamarin.Forms; using Xamarin.Forms;
namespace Pixiview.Utils namespace Pixiview.Utils
{ {
public class FavoriteVisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IIllustItem item)
{
return item.IsFavorite || !string.IsNullOrEmpty(item.BookmarkId);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class FavoriteIconConverter : IValueConverter public class FavoriteIconConverter : IValueConverter
{ {
private readonly bool isFavorite; private readonly bool isFavorite;
@ -34,14 +16,9 @@ namespace Pixiview.Utils
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
if (value is IIllustItem item) return value == null ?
{ isFavorite ? StyleDefinition.IconLove : string.Empty :
var love = isFavorite ? StyleDefinition.IconLove : string.Empty; StyleDefinition.IconCircleLove;
return !string.IsNullOrEmpty(item.BookmarkId) ?
StyleDefinition.IconCircleLove :
love;
}
return string.Empty;
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
@ -205,7 +206,10 @@ namespace Pixiview.Utils
private static HttpResponseMessage Download(string url, Action<HttpRequestHeaders> headerAction, HttpContent post = null) private static HttpResponseMessage Download(string url, Action<HttpRequestHeaders> headerAction, HttpContent post = null)
{ {
App.DebugPrint($"GET: {url}"); #if DEBUG
var method = post == null ? "GET" : "POST";
App.DebugPrint($"{method}: {url}");
#endif
var uri = new Uri(url); var uri = new Uri(url);
var proxy = Configs.Proxy; var proxy = Configs.Proxy;
var handler = new HttpClientHandler var handler = new HttpClientHandler
@ -234,7 +238,11 @@ namespace Pixiview.Utils
headerAction(headers); headerAction(headers);
if (proxy == null) if (proxy == null)
{ {
headers.Add("x-reverse", "yes"); var time = BitConverter.GetBytes(DateTime.UtcNow.Ticks);
headers.Add("X-Reverse-Ticks", Convert.ToBase64String(time));
time = time.Concat(Encoding.UTF8.GetBytes("_reverse_for_pixiv_by_tsanie")).ToArray();
var reverse = System.Security.Cryptography.SHA256.Create().ComputeHash(time);
headers.Add("X-Reverse", Convert.ToBase64String(reverse));
} }
headers.Add("Accept-Language", Configs.AcceptLanguage); headers.Add("Accept-Language", Configs.AcceptLanguage);
//headers.Add("Accept-Encoding", Configs.AcceptEncoding); //headers.Add("Accept-Encoding", Configs.AcceptEncoding);

View File

@ -341,12 +341,16 @@ namespace Pixiview.Utils
{ {
App.DebugPrint($"failed to add bookmark, error: {error}"); App.DebugPrint($"failed to add bookmark, error: {error}");
} }
else if (result == null || result.error) else if (result == null || result.error || result.body == null)
{ {
App.DebugPrint($"failed to add bookmark, message: {result.message}"); App.DebugPrint($"failed to add bookmark, message: {result?.message}");
} }
App.DebugPrint($"successs, bookmark id: {result.body.last_bookmark_id}, status: {result.body.stacc_status_id}"); else
return result.body.last_bookmark_id; {
App.DebugPrint($"successs, bookmark id: {result.body.last_bookmark_id}, status: {result.body.stacc_status_id}");
return result.body.last_bookmark_id;
}
return null;
} }
public static bool DeleteBookmark(string id) public static bool DeleteBookmark(string id)