fix: favorites sync logic
This commit is contained in:
parent
8381e71a45
commit
38fa6d4d21
@ -55,7 +55,7 @@
|
||||
<Reference Include="System.Net.Http" />
|
||||
</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="Newtonsoft.Json">
|
||||
<Version>12.0.3</Version>
|
||||
|
@ -145,7 +145,7 @@
|
||||
<Reference Include="System.Net.Http" />
|
||||
</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="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
@ -136,7 +136,7 @@ namespace Pixiview
|
||||
if (m.Success)
|
||||
{
|
||||
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));
|
||||
}
|
||||
else
|
||||
|
@ -43,7 +43,6 @@ namespace Pixiview.Illust
|
||||
var favorites = Stores.Favorites;
|
||||
if (favorites.Changed)
|
||||
{
|
||||
favorites.Reload();
|
||||
lastUpdated = default;
|
||||
startIndex = -1;
|
||||
nextIndex = 0;
|
||||
@ -69,6 +68,7 @@ namespace Pixiview.Illust
|
||||
return null;
|
||||
}
|
||||
favs = favorites.Illusts;
|
||||
favs.Reload();
|
||||
startIndex = 0;
|
||||
}
|
||||
else
|
||||
@ -200,7 +200,7 @@ namespace Pixiview.Illust
|
||||
var bookmark = list.FirstOrDefault(f => f.Id == b.Id);
|
||||
if (bookmark == null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(bookmarkId))
|
||||
if (bookmarkId != null)
|
||||
{
|
||||
// not exists in remote any more
|
||||
App.DebugPrint($"remove bookmark ({bookmarkId}) - {b.Id}: {b.Title}");
|
||||
@ -215,22 +215,17 @@ namespace Pixiview.Illust
|
||||
}
|
||||
}
|
||||
// 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}");
|
||||
item.Image = StyleDefinition.DownloadBackground;
|
||||
}
|
||||
nows.InsertRange(0, news);
|
||||
}
|
||||
else
|
||||
{
|
||||
CloseLoading();
|
||||
return;
|
||||
nows.InsertRange(0, list);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -115,12 +115,14 @@ namespace Pixiview.Illust
|
||||
{
|
||||
StartLoad();
|
||||
}
|
||||
else if (IsFavoriteVisible && IllustCollection != null)
|
||||
else if (IllustCollection != null)
|
||||
{
|
||||
var favorites = Stores.Favorites;
|
||||
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 virtual void OnIllustImageTapped(IllustItem illust)
|
||||
{
|
||||
var page = new ViewIllustPage(illust, true);
|
||||
var page = new ViewIllustPage(illust);
|
||||
Navigation.PushAsync(page);
|
||||
}
|
||||
protected virtual IllustCollection GetIllustsLoadedCollection(IllustCollection collection, bool bottom)
|
||||
@ -390,8 +392,8 @@ namespace Pixiview.Illust
|
||||
TextColor = StyleDefinition.ColorRedBackground,
|
||||
IsVisible = false
|
||||
}
|
||||
.Binding(Label.TextProperty, ".", converter: new FavoriteIconConverter(IsFavoriteVisible))
|
||||
.Binding(IsVisibleProperty, ".", converter: new FavoriteVisibleConverter())
|
||||
.Binding(Label.TextProperty, nameof(IllustItem.BookmarkId), converter: new FavoriteIconConverter(IsFavoriteVisible))
|
||||
.Binding(IsVisibleProperty, nameof(IllustItem.IsFavorite))
|
||||
.DynamicResource(Label.FontFamilyProperty, ThemeBase.IconSolidFontFamily);
|
||||
|
||||
#endregion
|
||||
@ -566,7 +568,9 @@ namespace Pixiview.Illust
|
||||
{
|
||||
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);
|
||||
|
||||
@ -751,8 +755,6 @@ namespace Pixiview.Illust
|
||||
string UserId { get; }
|
||||
string UserName { get; }
|
||||
ImageSource ProfileImage { get; }
|
||||
bool IsFavorite { get; }
|
||||
string BookmarkId { get; }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
|
@ -123,9 +123,9 @@ namespace Pixiview.Illust
|
||||
|
||||
public IllustItem IllustItem { get; private set; }
|
||||
|
||||
private readonly bool saveFavorites;
|
||||
private readonly ImageSource fontIconLove;
|
||||
private readonly ImageSource fontIconNotLove;
|
||||
private bool favoriteChanged;
|
||||
private IllustUgoiraData ugoiraData;
|
||||
private Ugoira ugoira;
|
||||
private ParallelTask task;
|
||||
@ -135,11 +135,10 @@ namespace Pixiview.Illust
|
||||
private int pageCount;
|
||||
private bool isPreloading;
|
||||
|
||||
public ViewIllustPage(IllustItem illust, bool save)
|
||||
public ViewIllustPage(IllustItem illust)
|
||||
{
|
||||
IllustItem = illust;
|
||||
Title = illust.Title;
|
||||
saveFavorites = save;
|
||||
BindingContext = this;
|
||||
|
||||
fontIconLove = (ImageSource)Application.Current.Resources[ThemeBase.FontIconLove];
|
||||
@ -184,7 +183,7 @@ namespace Pixiview.Illust
|
||||
ugoira = null;
|
||||
}
|
||||
|
||||
if (saveFavorites)
|
||||
if (favoriteChanged)
|
||||
{
|
||||
Stores.SaveFavoritesIllusts();
|
||||
}
|
||||
@ -523,6 +522,7 @@ namespace Pixiview.Illust
|
||||
favorites.RemoveAt(index);
|
||||
FavoriteIcon = fontIconNotLove;
|
||||
}
|
||||
favoriteChanged = true;
|
||||
|
||||
if (Configs.SyncFavType == SyncType.None)
|
||||
{
|
||||
@ -557,6 +557,10 @@ namespace Pixiview.Illust
|
||||
{
|
||||
_ = Task.Run(() => Stores.DeleteBookmark(illust.BookmarkId));
|
||||
}
|
||||
|
||||
// immediately save after changing remote
|
||||
Stores.SaveFavoritesIllusts();
|
||||
favoriteChanged = false;
|
||||
}
|
||||
|
||||
private void Image_Tapped(object sender, EventArgs e)
|
||||
|
@ -83,8 +83,8 @@ namespace Pixiview
|
||||
else
|
||||
{
|
||||
IsProxied = false;
|
||||
Host = string.Empty;
|
||||
Port = string.Empty;
|
||||
Host = Preferences.Get(Configs.HostKey, string.Empty);
|
||||
Port = Preferences.Get(Configs.PortKey, string.Empty);
|
||||
}
|
||||
|
||||
Cookie = Configs.Cookie;
|
||||
@ -134,7 +134,7 @@ namespace Pixiview
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (proxy != null)
|
||||
else
|
||||
{
|
||||
Preferences.Set(Configs.IsProxiedKey, false);
|
||||
Preferences.Set(Configs.HostKey, h);
|
||||
@ -142,8 +142,11 @@ namespace Pixiview
|
||||
{
|
||||
Preferences.Set(Configs.PortKey, pt);
|
||||
}
|
||||
Configs.Proxy = null;
|
||||
App.DebugPrint("clear proxy");
|
||||
if (proxy != null)
|
||||
{
|
||||
Configs.Proxy = null;
|
||||
App.DebugPrint("clear proxy");
|
||||
}
|
||||
}
|
||||
|
||||
var cookie = Cookie;
|
||||
|
@ -1,28 +1,10 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Pixiview.Illust;
|
||||
using Pixiview.UI;
|
||||
using Xamarin.Forms;
|
||||
|
||||
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
|
||||
{
|
||||
private readonly bool isFavorite;
|
||||
@ -34,14 +16,9 @@ namespace Pixiview.Utils
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is IIllustItem item)
|
||||
{
|
||||
var love = isFavorite ? StyleDefinition.IconLove : string.Empty;
|
||||
return !string.IsNullOrEmpty(item.BookmarkId) ?
|
||||
StyleDefinition.IconCircleLove :
|
||||
love;
|
||||
}
|
||||
return string.Empty;
|
||||
return value == null ?
|
||||
isFavorite ? StyleDefinition.IconLove : string.Empty :
|
||||
StyleDefinition.IconCircleLove;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
@ -205,7 +206,10 @@ namespace Pixiview.Utils
|
||||
|
||||
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 proxy = Configs.Proxy;
|
||||
var handler = new HttpClientHandler
|
||||
@ -234,7 +238,11 @@ namespace Pixiview.Utils
|
||||
headerAction(headers);
|
||||
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-Encoding", Configs.AcceptEncoding);
|
||||
|
@ -341,12 +341,16 @@ namespace Pixiview.Utils
|
||||
{
|
||||
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}");
|
||||
return result.body.last_bookmark_id;
|
||||
else
|
||||
{
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user