From 22536ea3957e36dedb67398dda59a689985e9611 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Tue, 19 May 2020 14:06:12 +0800 Subject: [PATCH] fix: favorites sync logic --- Pixiview/Illust/FavoritesPage.xaml.cs | 62 ++++++++++++++++--------- Pixiview/Illust/IllustCollectionPage.cs | 25 ++-------- Pixiview/UI/AdaptedPage.cs | 16 +++++-- Pixiview/Utils/Extensions.cs | 36 +++++++------- Pixiview/Utils/Ugoira.cs | 1 + 5 files changed, 70 insertions(+), 70 deletions(-) diff --git a/Pixiview/Illust/FavoritesPage.xaml.cs b/Pixiview/Illust/FavoritesPage.xaml.cs index c8338e5..e69c926 100644 --- a/Pixiview/Illust/FavoritesPage.xaml.cs +++ b/Pixiview/Illust/FavoritesPage.xaml.cs @@ -154,6 +154,21 @@ namespace Pixiview.Illust }); } + private void CloseLoading(Action next = null) + { + var offset = 16 - IndicatorMarginTop; + activityLoading.Animate("margin", top => + { + activityLoading.Margin = new Thickness(0, top, 0, offset); + }, + 16 - offset, loadingOffset - offset, easing: Easing.CubicIn, finished: (v, r) => + { + IsLoading = false; + + next?.Invoke(); + }); + } + private async void ConfirmNext(IllustItem[] list) { var cancel = ResourceHelper.Cancel; @@ -182,33 +197,41 @@ namespace Pixiview.Illust { var b = nows[i]; var bookmarkId = b.BookmarkId; - if (!string.IsNullOrEmpty(bookmarkId)) + var bookmark = list.FirstOrDefault(f => f.Id == b.Id); + if (bookmark == null) { - var bookmark = list.FirstOrDefault(f => f.Id == b.Id); - if (bookmark == null) + if (!string.IsNullOrEmpty(bookmarkId)) { // not exists in remote any more App.DebugPrint($"remove bookmark ({bookmarkId}) - {b.Id}: {b.Title}"); nows.RemoveAt(i); } - else if (bookmarkId != bookmark.BookmarkId) - { - // update bookmark id - App.DebugPrint($"change bookmark ({bookmarkId}) to ({bookmark.BookmarkId}) - {b.Id}: {b.Title}"); - b.BookmarkId = bookmark.BookmarkId; - } + } + else if (bookmarkId != bookmark.BookmarkId) + { + // update bookmark id + App.DebugPrint($"change bookmark ({bookmarkId}) to ({bookmark.BookmarkId}) - {b.Id}: {b.Title}"); + b.BookmarkId = bookmark.BookmarkId; } } // add bookmarks that exists in remote only - list = list.Where(f => !nows.Any(i => i.Id == f.Id)).ToArray(); + var news = list.Where(f => !nows.Any(i => i.Id == f.Id)).ToArray(); - for (var i = 0; i < list.Length; i++) + if (news.Length > 0) { - var item = list[i]; - App.DebugPrint($"add bookmark ({item.BookmarkId}) - {item.Id}: {item.Title}"); - item.Image = StyleDefinition.DownloadBackground; + for (var i = 0; i < news.Length; i++) + { + var item = news[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 { @@ -236,15 +259,8 @@ namespace Pixiview.Illust MainThread.BeginInvokeOnMainThread(() => { - var offset = 16 - IndicatorMarginTop; - activityLoading.Animate("margin", top => + CloseLoading(() => { - activityLoading.Margin = new Thickness(0, top, 0, offset); - }, - 16 - offset, loadingOffset - offset, easing: Easing.CubicIn, finished: (v, r) => - { - IsLoading = false; - flag = false; lastUpdated = default; StartLoad(); diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index 1ebac54..3d678c1 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -710,11 +710,11 @@ namespace Pixiview.Illust public IllustCollection() : base() { - running = true; + Running = true; } public IllustCollection(IEnumerable illusts) : base(illusts) { - running = true; + Running = true; } public void AddRange(List items) @@ -736,26 +736,7 @@ namespace Pixiview.Illust } } - private readonly object sync = new object(); - private bool running; - - public bool Running - { - get - { - lock (sync) - { - return running; - } - } - set - { - lock (sync) - { - running = value; - } - } - } + public bool Running { get; set; } } public enum IllustType diff --git a/Pixiview/UI/AdaptedPage.cs b/Pixiview/UI/AdaptedPage.cs index f4467a1..a65c891 100644 --- a/Pixiview/UI/AdaptedPage.cs +++ b/Pixiview/UI/AdaptedPage.cs @@ -143,7 +143,16 @@ namespace Pixiview.UI private class Tap : IDisposable { - public static bool IsBusy => _instance?.isBusy == true; + public static bool IsBusy + { + get + { + lock (sync) + { + return _instance?.isBusy == true; + } + } + } private static readonly object sync = new object(); private static readonly Tap _instance = new Tap(); @@ -163,10 +172,7 @@ namespace Pixiview.UI public void Dispose() { - lock (sync) - { - isBusy = false; - } + isBusy = false; } } } diff --git a/Pixiview/Utils/Extensions.cs b/Pixiview/Utils/Extensions.cs index 4092843..4782a1f 100644 --- a/Pixiview/Utils/Extensions.cs +++ b/Pixiview/Utils/Extensions.cs @@ -100,10 +100,15 @@ namespace Pixiview.Utils { if (toExclusive <= from) { + if (complete != null) + { + ThreadPool.QueueUserWorkItem(o => complete()); + } return null; } var task = new ParallelTask(from, toExclusive, maxCount, action, complete); - Task.Run(task.Start); + //Task.Run(task.Start); + ThreadPool.QueueUserWorkItem(task.Start); return task; } @@ -134,24 +139,21 @@ namespace Pixiview.Utils this.complete = complete; } - private void Start() + private void Start(object o) { for (int i = from; i < to; i++) { var index = i; while (true) { - lock (sync) + if (count < max) { - if (count < max) - { - break; - } - if (disposed) - { - App.DebugPrint($"parallel task determinate, disposed"); - return; - } + break; + } + if (disposed) + { + App.DebugPrint($"parallel task determinate, disposed"); + return; } Thread.Sleep(100); } @@ -165,10 +167,7 @@ namespace Pixiview.Utils { if (!action(index)) { - lock (sync) - { - disposed = true; - } + disposed = true; } } catch (Exception ex) @@ -190,10 +189,7 @@ namespace Pixiview.Utils public void Dispose() { - lock (sync) - { - disposed = true; - } + disposed = true; } } diff --git a/Pixiview/Utils/Ugoira.cs b/Pixiview/Utils/Ugoira.cs index 6b2c32b..38ebc55 100644 --- a/Pixiview/Utils/Ugoira.cs +++ b/Pixiview/Utils/Ugoira.cs @@ -321,6 +321,7 @@ namespace Pixiview.Utils } } } + App.DebugPrint("download over"); } }