Skip to content

Commit

Permalink
Merge pull request #1 from AnimaSA/master
Browse files Browse the repository at this point in the history
Added context menu +1 progress
  • Loading branch information
DiekoMA authored May 15, 2024
2 parents 2f6732d + b6188bc commit 37558dc
Showing 1 changed file with 135 additions and 38 deletions.
173 changes: 135 additions & 38 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,18 +49,27 @@ public List<Result> 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:
Expand All @@ -77,18 +87,28 @@ public List<Result> 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;
}
Expand All @@ -114,11 +134,84 @@ public Control CreateSettingPanel()

public List<Result> LoadContextMenus(Result selectedResult)

Check warning on line 135 in Main.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'Anilist.LoadContextMenus(Result)'
{
var results = new List<Result>
var results = new List<Result>();
var media = selectedResult.ContextData as Media;

Func<ActionContext, ValueTask<bool>> PlusOneActionCreator(bool updateVolume)
{
new ()
return async ValueTask<bool> (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)

Check warning on line 174 in Main.cs

View workflow job for this annotation

GitHub Actions / publish

The variable 'e' is declared but never used
{
// 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 =>
{
Expand All @@ -130,23 +223,23 @@ public List<Result> 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 =>
{
Expand All @@ -158,17 +251,21 @@ public List<Result> 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;
}
Expand Down

0 comments on commit 37558dc

Please sign in to comment.