From b6188bc41e42176b508fc50df34379f575bf5829 Mon Sep 17 00:00:00 2001 From: Anima Azura Date: Wed, 15 May 2024 00:19:18 -0700 Subject: [PATCH] Added context menu +1 progress --- Main.cs | 173 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 135 insertions(+), 38 deletions(-) diff --git a/Main.cs b/Main.cs index 7931255..bb0435b 100644 --- a/Main.cs +++ b/Main.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using System.Windows.Controls; namespace Flow.Launcher.Plugin.Anilist @@ -48,18 +49,27 @@ public List Query(Query query) foreach (var anime in animeSearchResults.Result.Data) { - results.Add(new Result + var result = new Result(); + result.Title = anime.Title.EnglishTitle ?? anime.Title.RomajiTitle; + if (anime.Entry != null) { - Title = anime.Title.EnglishTitle ?? anime.Title.RomajiTitle, - SubTitle = $"Format: {anime.Format} \nStatus: {anime.Status}", - IcoPath = anime.Cover.ExtraLargeImageUrl.ToString(), - Action = e => - { - string url = $"https://anilist.co/anime/{anime.Id}"; - _context.API.OpenUrl(url); - return true; - } - }); + result.SubTitle = $"Format: {anime.Format} | Status: {anime.Status} \n" + + $"Watched: {anime.Entry?.Progress ?? 0} / {anime.Episodes}"; + } + else + { + result.SubTitle = $"Format: {anime.Format} | Status: {anime.Status} \n" + + $"Episodes: {anime.Episodes?.ToString() ?? "?"}"; + } + result.IcoPath = anime.Cover.ExtraLargeImageUrl.ToString(); + result.Action = e => + { + string url = $"https://anilist.co/anime/{anime.Id}"; + _context.API.OpenUrl(url); + return true; + }; + result.ContextData = anime; + results.Add(result); } break; case MediaType.Manga: @@ -77,18 +87,28 @@ public List Query(Query query) foreach (var manga in mangaSearchResults.Result.Data) { - results.Add(new Result + var result = new Result(); + result.Title = manga.Title.EnglishTitle ?? manga.Title.RomajiTitle; + if (manga.Entry != null) { - Title = manga.Title.EnglishTitle ?? manga.Title.RomajiTitle, - SubTitle = $"Format: {manga.Format} \nStatus: {manga.Status}", - IcoPath = manga.Cover.ExtraLargeImageUrl.ToString(), - Action = e => - { - string url = $"https://anilist.co/anime/{manga.Id}"; - _context.API.OpenUrl(url); - return true; - } - }); + result.SubTitle = $"Format: {manga.Format} | Status: {manga.Status} \n" + + $"Chapters Read: {manga.Entry?.Progress ?? 0} / {manga.Chapters} | Volumes Read: {manga.Entry?.VolumeProgress ?? 0} / {manga.Volumes}"; + } + else + { + result.SubTitle = $"Format: {manga.Format} | Status: {manga.Status} \n" + + $"Chapters: {manga.Chapters} | Volumes: {manga.Volumes}"; + } + + result.IcoPath = manga.Cover.ExtraLargeImageUrl.ToString(); + result.Action = e => + { + string url = $"https://anilist.co/anime/{manga.Id}"; + _context.API.OpenUrl(url); + return true; + }; + result.ContextData = manga; + results.Add(result); } break; } @@ -114,11 +134,84 @@ public Control CreateSettingPanel() public List LoadContextMenus(Result selectedResult) { - var results = new List + var results = new List(); + var media = selectedResult.ContextData as Media; + + Func> PlusOneActionCreator(bool updateVolume) { - new () + return async ValueTask (ActionContext c) => { - Title = "Add to list", + try + { + var mediaEntryMutation = new MediaEntryMutation(); + switch (media.Type) + { + case MediaType.Anime: + mediaEntryMutation.Progress = media.Entry.Progress + 1; + if (mediaEntryMutation.Progress >= media.Episodes) + { + mediaEntryMutation.Status = MediaEntryStatus.Completed; + mediaEntryMutation.CompleteDate = DateTime.Now; + } + break; + case MediaType.Manga: + // TODO: Support volume progress + if (updateVolume) + { + mediaEntryMutation.VolumeProgress = media.Entry.VolumeProgress + 1; + } + else + { + mediaEntryMutation.Progress = media.Entry.Progress + 1; + } + break; + default: + throw new ArgumentOutOfRangeException(); + } + + await _client.SaveMediaEntryAsync(media.Id, mediaEntryMutation); + } + catch (Exception e) + { + // ignored + } + + return true; + }; + } + + // If already on list + if (media.Entry != null) + { + if (media.Type == MediaType.Anime) + { + // Add a +1 progress option + results.Add(new () + { + Title = "+1 Watched", + IcoPath = "Assets\\AniListLogo.png", + AsyncAction = PlusOneActionCreator(false), + }); + } + else + { + results.Add(new () + { + Title = "+1 Chapter Read", + IcoPath = "Assets\\AniListLogo.png", + AsyncAction = PlusOneActionCreator(false) + }); + results.Add(new () + { + Title = "+1 Volume Read", + IcoPath = "Assets\\AniListLogo.png", + AsyncAction = PlusOneActionCreator(true) + }); + } + + results.Add(new Result + { + Title = "Remove from list", IcoPath = "Assets\\AniListlogo.png", AsyncAction = async c => { @@ -130,23 +223,23 @@ public List LoadContextMenus(Result selectedResult) Type = _settings.DefaultMediaType, Sort = MediaSort.Popularity }); - await _client.SaveMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id, new MediaEntryMutation() - { - StartDate = DateTime.Today, - Progress = 0, - Status = MediaEntryStatus.Planning - }); + await _client.DeleteMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id); } catch (Exception) { _context.API.ShowMsgError("Not Authenticated", "Please add your token in the plugin settings"); } + return true; }, - }, - new () + }); + } + // If not on list, add an option to add to list + else + { + results.Add(new Result { - Title = "Remove from list", + Title = "Add to list", IcoPath = "Assets\\AniListlogo.png", AsyncAction = async c => { @@ -158,17 +251,21 @@ public List LoadContextMenus(Result selectedResult) Type = _settings.DefaultMediaType, Sort = MediaSort.Popularity }); - await _client.DeleteMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id); + await _client.SaveMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id, new MediaEntryMutation() + { + StartDate = DateTime.Today, + Progress = 0, + Status = MediaEntryStatus.Planning + }); } catch (Exception) { _context.API.ShowMsgError("Not Authenticated", "Please add your token in the plugin settings"); } - return true; }, - } - }; + }); + } return results; }