From 38fa6d4d21f3ecba0fdd807715f94db38f56d158 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Tue, 19 May 2020 23:52:53 +0800 Subject: [PATCH] fix: favorites sync logic --- Pixiview.Android/Pixiview.Android.csproj | 2 +- Pixiview.iOS/Pixiview.iOS.csproj | 2 +- Pixiview/App.cs | 2 +- Pixiview/Illust/FavoritesPage.xaml.cs | 19 ++++++---------- Pixiview/Illust/IllustCollectionPage.cs | 18 ++++++++------- Pixiview/Illust/ViewIllustPage.xaml.cs | 12 ++++++---- Pixiview/OptionPage.xaml.cs | 13 +++++++---- Pixiview/Utils/Converters.cs | 29 +++--------------------- Pixiview/Utils/HttpUtility.cs | 12 ++++++++-- Pixiview/Utils/Stores.cs | 12 ++++++---- 10 files changed, 57 insertions(+), 64 deletions(-) diff --git a/Pixiview.Android/Pixiview.Android.csproj b/Pixiview.Android/Pixiview.Android.csproj index f4b971c..bfc83d9 100644 --- a/Pixiview.Android/Pixiview.Android.csproj +++ b/Pixiview.Android/Pixiview.Android.csproj @@ -55,7 +55,7 @@ - + 12.0.3 diff --git a/Pixiview.iOS/Pixiview.iOS.csproj b/Pixiview.iOS/Pixiview.iOS.csproj index 0de050d..dc3adb2 100644 --- a/Pixiview.iOS/Pixiview.iOS.csproj +++ b/Pixiview.iOS/Pixiview.iOS.csproj @@ -145,7 +145,7 @@ - + diff --git a/Pixiview/App.cs b/Pixiview/App.cs index 70a006d..926dfeb 100644 --- a/Pixiview/App.cs +++ b/Pixiview/App.cs @@ -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 diff --git a/Pixiview/Illust/FavoritesPage.xaml.cs b/Pixiview/Illust/FavoritesPage.xaml.cs index e69c926..875dfa2 100644 --- a/Pixiview/Illust/FavoritesPage.xaml.cs +++ b/Pixiview/Illust/FavoritesPage.xaml.cs @@ -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 diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index 3d678c1..f6e70ec 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -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 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)] diff --git a/Pixiview/Illust/ViewIllustPage.xaml.cs b/Pixiview/Illust/ViewIllustPage.xaml.cs index 6ecbb0a..93c048f 100644 --- a/Pixiview/Illust/ViewIllustPage.xaml.cs +++ b/Pixiview/Illust/ViewIllustPage.xaml.cs @@ -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) diff --git a/Pixiview/OptionPage.xaml.cs b/Pixiview/OptionPage.xaml.cs index f00a9a0..d7df4e8 100644 --- a/Pixiview/OptionPage.xaml.cs +++ b/Pixiview/OptionPage.xaml.cs @@ -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; diff --git a/Pixiview/Utils/Converters.cs b/Pixiview/Utils/Converters.cs index 58ea905..37cbcf1 100644 --- a/Pixiview/Utils/Converters.cs +++ b/Pixiview/Utils/Converters.cs @@ -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) diff --git a/Pixiview/Utils/HttpUtility.cs b/Pixiview/Utils/HttpUtility.cs index 066fa9b..093f613 100644 --- a/Pixiview/Utils/HttpUtility.cs +++ b/Pixiview/Utils/HttpUtility.cs @@ -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 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); diff --git a/Pixiview/Utils/Stores.cs b/Pixiview/Utils/Stores.cs index 93db6e4..c9a1420 100644 --- a/Pixiview/Utils/Stores.cs +++ b/Pixiview/Utils/Stores.cs @@ -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)