fix: favorites sync logic
This commit is contained in:
		@@ -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();
 | 
			
		||||
 
 | 
			
		||||
@@ -710,11 +710,11 @@ namespace Pixiview.Illust
 | 
			
		||||
 | 
			
		||||
        public IllustCollection() : base()
 | 
			
		||||
        {
 | 
			
		||||
            running = true;
 | 
			
		||||
            Running = true;
 | 
			
		||||
        }
 | 
			
		||||
        public IllustCollection(IEnumerable<IllustItem> illusts) : base(illusts)
 | 
			
		||||
        {
 | 
			
		||||
            running = true;
 | 
			
		||||
            Running = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void AddRange(List<IllustItem> 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
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -321,6 +321,7 @@ namespace Pixiview.Utils
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                App.DebugPrint("download over");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user