feature: single illust bookmark sync

This commit is contained in:
2020-05-18 18:45:24 +08:00
parent 1589d6b089
commit d6c235d6ef
17 changed files with 368 additions and 47 deletions

View File

@@ -837,6 +837,8 @@ namespace Pixiview.Illust
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string BookmarkId { get; set; }
[JsonProperty]
public DateTime FavoriteDateUtc { get; set; }
[JsonProperty]
public string ImageUrl { get; set; }

View File

@@ -133,6 +133,7 @@ namespace Pixiview.Illust
private readonly object sync = new object();
private int downloaded = 0;
private int pageCount;
private bool isPreloading;
public ViewIllustPage(IllustItem illust, bool save)
{
@@ -383,28 +384,7 @@ namespace Pixiview.Illust
if (i == 0 && illustItem.ImageUrl == null)
{
// maybe open from a link
var preload = Stores.LoadIllustPreloadData(illustItem.Id);
if (preload != null && preload.illust.TryGetValue(illustItem.Id, out var illust))
{
illust.CopyToItem(illustItem);
MainThread.BeginInvokeOnMainThread(() =>
{
var count = illustItem.PageCount;
pageCount = count;
Title = illustItem.Title;
IsAnimateSliderVisible = illustItem.IsAnimeVisible;
if (count > 1)
{
IsPageVisible = true;
ProgressVisible = true;
PagePositionText = $"1/{count}";
}
});
if (preload.user.TryGetValue(illust.userId, out var user))
{
illustItem.ProfileUrl = user.image;
}
}
DoForcePreload(false);
}
}
@@ -434,13 +414,7 @@ namespace Pixiview.Illust
private void DoLoadImage(int index, bool force = false)
{
var items = Illusts;
if (index == 0 && force)
{
// error, refresh all
Task.Run(() => DoLoadImages(true));
return;
}
if (index < 0 || index >= items.Length)
if (index < 0 || (!force && index >= items.Length))
{
App.DebugPrint($"invalid index: {index}");
return;
@@ -525,12 +499,13 @@ namespace Pixiview.Illust
}
}
private void Favorite_Clicked(object sender, EventArgs e)
private async void Favorite_Clicked(object sender, EventArgs e)
{
var favorites = Stores.Favorites;
var illust = IllustItem;
var index = favorites.FindIndex(i => i.Id == illust.Id);
if (index < 0)
bool add = index < 0;
if (add)
{
illust.IsFavorite = true;
illust.FavoriteDateUtc = DateTime.UtcNow;
@@ -543,6 +518,40 @@ namespace Pixiview.Illust
favorites.RemoveAt(index);
FavoriteIcon = fontIconNotLove;
}
if (Configs.SyncFavType == SyncType.None)
{
return;
}
if (Configs.SyncFavType == SyncType.Prompt)
{
var ok = await DisplayAlert(
ResourceHelper.Title,
ResourceHelper.ConfirmSyncFavorite,
ResourceHelper.Yes,
ResourceHelper.No);
if (!ok)
{
return;
}
}
if (add)
{
var id = await Task.Run(() => Stores.AddBookmark(illust.Id));
if (id != null)
{
illust.BookmarkId = id;
}
}
else
{
var bookmarkId = illust.BookmarkId;
if (!string.IsNullOrEmpty(bookmarkId))
{
_ = Task.Run(() => Stores.DeleteBookmark(bookmarkId));
}
}
}
private void Image_Tapped(object sender, EventArgs e)
@@ -582,9 +591,47 @@ namespace Pixiview.Illust
CurrentAnimeFrame = e.FrameIndex;
}
private void DoForcePreload(bool force)
{
isPreloading = true;
var illustItem = IllustItem;
// force to reload
var preload = Stores.LoadIllustPreloadData(illustItem.Id, force);
if (preload != null && preload.illust.TryGetValue(illustItem.Id, out var illust))
{
illust.CopyToItem(illustItem);
MainThread.BeginInvokeOnMainThread(() =>
{
var count = illustItem.PageCount;
pageCount = count;
Title = illustItem.Title;
IsAnimateSliderVisible = illustItem.IsAnimeVisible;
if (count > 1)
{
IsPageVisible = true;
ProgressVisible = true;
PagePositionText = $"1/{count}";
}
});
if (preload.user.TryGetValue(illust.userId, out var user))
{
illustItem.ProfileUrl = user.image;
}
}
isPreloading = false;
}
private void Refresh_Clicked(object sender, EventArgs e)
{
Task.Run(() => DoLoadImage(CurrentPage, true));
if (isPreloading)
{
return;
}
Task.Run(() =>
{
DoForcePreload(true);
DoLoadImage(CurrentPage, true);
});
}
private async void More_Clicked(object sender, EventArgs e)