fix: favorites sync logic

This commit is contained in:
2020-05-19 23:52:53 +08:00
parent 8381e71a45
commit 38fa6d4d21
10 changed files with 57 additions and 64 deletions

View File

@@ -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

View File

@@ -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)]

View File

@@ -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)