Skip to content

Commit

Permalink
manga plugins cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Sep 13, 2023
1 parent 8aa4f41 commit 1e9393e
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 44 deletions.
Binary file added Plugins Store/Totoro.Plugins.Manga.MangaDex.dll
Binary file not shown.
21 changes: 9 additions & 12 deletions Plugins/Manga/Totoro.Plugins.Manga.MangaDex/ChapterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ public async IAsyncEnumerable<ChapterModel> GetChapters(string url)
{
var response = await Config.Api
.WithDefaultUserAgent()
.AppendPathSegment("manga")
.AppendPathSegment(url)
.AppendPathSegment("feed")
.AppendPathSegment($"/manga/{url}/feed")
.SetQueryParam("translatedLanguage[]", "en")
.SetQueryParam("order[volume]", "asc")
.SetQueryParam("order[chapter]", "asc")
.GetStringAsync();

var jObject = JsonNode.Parse(response);
.GetJsonAsync();

foreach (var item in jObject?["data"]?.AsArray() ?? new JsonArray())
foreach (var item in response.data)
{
var volume = float.Parse(item?["attributes"]?["volume"]?.ToString() ?? "0");
var chapter = float.Parse(item?["attributes"]?["chapter"]?.ToString() ?? "0");
var id = item?["id"]?.ToString();
var volume = float.Parse(item.attributes.volume);
var chapter = float.Parse(item.attributes.chapter);
var title = item.attributes.title;
var id = item.id;

yield return new ChapterModel
{
Volume = volume,
Id = id,
Chapter = chapter
Chapter = chapter,
Title = title
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<Version>1.0</Version>
<OutputPath Condition="$(Configuration) == Release">..\..\..\Plugins Store\</OutputPath>
<AppendTargetFrameworkToOutputPath Condition="$(Configuration) == Release">false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath Condition="$(Configuration) == Release">false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -17,4 +21,4 @@
</AssemblyAttribute>
</ItemGroup>

</Project>
</Project>
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public interface ISettings : INotifyPropertyChanged
StartupOptions StartupOptions { get; set; }
DisplayMode ListDisplayMode { get; set; }
GridViewSettings UserListGridViewSettings { get; set; }
public string DefaultMangaProviderType { get; set; }
}

public class StartupOptions : ReactiveObject
Expand Down
7 changes: 6 additions & 1 deletion Totoro.Core/Services/PluginOptionsStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Manga;
using Totoro.Plugins.Torrents.Contracts;

namespace Totoro.Core.Services;
Expand All @@ -7,17 +8,21 @@ public class PluginOptionsStorage
{
private readonly IPluginOptionsStorage<AnimeProvider> _animePlugins;
private readonly IPluginOptionsStorage<ITorrentTracker> _torrentPlugins;
private readonly IPluginOptionsStorage<MangaProvider> _mangaPlugins;

public PluginOptionsStorage(IPluginOptionsStorage<AnimeProvider> animePlugins,
IPluginOptionsStorage<ITorrentTracker> torrentPlugins)
IPluginOptionsStorage<ITorrentTracker> torrentPlugins,
IPluginOptionsStorage<MangaProvider> mangaPlugins)
{
_animePlugins = animePlugins;
_torrentPlugins = torrentPlugins;
_mangaPlugins = mangaPlugins;
}

public void Initialize()
{
_animePlugins.Initialize();
_mangaPlugins.Initialize();
_torrentPlugins.Initialize();
}
}
1 change: 1 addition & 0 deletions Totoro.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static class Settings
public static Key<DisplayMode> ListDisplayMode { get; } = new("ListDisplayMode", DisplayMode.Grid);
public static Key<DataGridSettings> UserListDataGridSettings { get; } = new("UserListDataGridSettings", GetDefaultUserListDataGridSettings);
public static Key<GridViewSettings> UserListGridViewSettings { get; } = new("UserListGridViewSettings", new GridViewSettings());
public static Key<string> DefaultMangaProviderType { get; } = new("DefaultMangaProviderType", "manga-dex");

public static IEnumerable<string> GetObsoleteKeys()
{
Expand Down
17 changes: 10 additions & 7 deletions Totoro.Core/ViewModels/DiscoverMangaViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reactive.Concurrency;
using Totoro.Plugins;
using Totoro.Plugins.Contracts;
using Totoro.Plugins.Manga;
using Totoro.Plugins.Manga.Contracts;

Expand All @@ -11,7 +12,9 @@ public class DiscoverMangaViewModel : NavigatableViewModel
private readonly ReadOnlyObservableCollection<ICatalogItem> _mangaSearchResults;
private readonly INavigationService _navigationService;
private readonly MangaProvider _provider;
public DiscoverMangaViewModel(INavigationService navigationService)
public DiscoverMangaViewModel(INavigationService navigationService,
ISettings settings,
IPluginFactory<MangaProvider> pluginFactory)
{
_navigationService = navigationService;

Expand All @@ -24,7 +27,7 @@ public DiscoverMangaViewModel(INavigationService navigationService)
.Subscribe()
.DisposeWith(Garbage);

_provider = PluginFactory<MangaProvider>.Instance.CreatePlugin("manga-dex");
_provider = pluginFactory.CreatePlugin(settings.DefaultMangaProviderType);

this.WhenAnyValue(x => x.SearchText)
.Where(x => x is { Length: >= 2 })
Expand Down Expand Up @@ -57,7 +60,7 @@ private Task OnSearchResultSelected(ICatalogItem searchResult)
{
var navigationParameters = new Dictionary<string, object>
{
["SearchResult"] = searchResult
["SearchResult"] = searchResult,
};

_navigationService.NavigateTo<ReadViewModel>(parameter: navigationParameters);
Expand All @@ -72,10 +75,10 @@ private async Task Search(string query)
return;
}

await foreach(var item in _provider.Catalog.Search(query))
{
_mangaSearchResultCache.AddOrUpdate(item);
}
SetLoading(true);
var items = await _provider.Catalog.Search(query).ToListAsync();
_mangaSearchResultCache.EditDiff(items, (item1, item2) => item1.Url == item2.Url);
SetLoading(false);
}

}
17 changes: 12 additions & 5 deletions Totoro.Core/ViewModels/ReadViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using Totoro.Plugins;
using Totoro.Plugins.Contracts;
using Totoro.Plugins.Manga;
using Totoro.Plugins.Manga.Contracts;

namespace Totoro.Core.ViewModels;

public class ReadViewModel : NavigatableViewModel
{
private readonly MangaProvider _provider;
private readonly ISettings _settings;
private readonly IPluginFactory<MangaProvider> _providerFactory;
private MangaProvider _provider;

public ReadViewModel()
public ReadViewModel(ISettings settings,
IPluginFactory<MangaProvider> providerFactory)
{
_provider = PluginFactory<MangaProvider>.Instance.CreatePlugin("manga-dex");
_settings = settings;
_providerFactory = providerFactory;

this.WhenAnyValue(x => x.SelectedChapter)
.WhereNotNull()
Expand All @@ -23,6 +27,7 @@ public ReadViewModel()
SelectedPage = 0;
})
.Subscribe(images => Pages.AddRange(images));

}

[Reactive] public List<ChapterModel> Chapters { get; set; }
Expand All @@ -34,11 +39,13 @@ public ReadViewModel()

public override async Task OnNavigatedTo(IReadOnlyDictionary<string, object> parameters)
{
var providerType = parameters.GetValueOrDefault("ProviderType", _settings.DefaultMangaProviderType) as string;
_provider = _providerFactory.CreatePlugin(providerType);

if(parameters.ContainsKey("SearchResult"))
{
var result = (ICatalogItem)parameters["SearchResult"];
Chapters = await _provider.ChapterProvider.GetChapters(result.Url).ToListAsync();
}

}
}
2 changes: 2 additions & 0 deletions Totoro.Core/ViewModels/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public SettingsModel(ILocalSettingsService localSettingsService,
StartupOptions = localSettingsService.ReadSetting(Settings.StartupOptions);
ListDisplayMode = localSettingsService.ReadSetting(Settings.ListDisplayMode);
UserListGridViewSettings = localSettingsService.ReadSetting(Settings.UserListGridViewSettings);
DefaultMangaProviderType = localSettingsService.ReadSetting(Settings.DefaultMangaProviderType);

if (UseDiscordRichPresense && !_dRpc.IsInitialized)
{
Expand Down Expand Up @@ -139,6 +140,7 @@ private void ObserveObject<T>(T target, Key<T> key)
[Reactive] public StartupOptions StartupOptions { get; set; }
[Reactive] public DisplayMode ListDisplayMode { get; set; }
[Reactive] public GridViewSettings UserListGridViewSettings { get; set; }
[Reactive] public string DefaultMangaProviderType { get; set; }
}


Expand Down
5 changes: 5 additions & 0 deletions Totoro.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Splat;
using Totoro.Plugins;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Manga;
using Totoro.Plugins.Torrents.Contracts;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

Expand All @@ -16,6 +17,7 @@ public class SettingsViewModel : NavigatableViewModel
[Reactive] public bool IsMalConnected { get; set; }
[Reactive] public bool IsAniListConnected { get; set; }
[Reactive] public PluginInfo SelectedProvider { get; set; }
[Reactive] public PluginInfo SelectedMangaProvider { get; set; }
[Reactive] public PluginInfo SelectedTracker { get; set; }
[Reactive] public PluginInfo SelectedMediaPlayer { get; set; }
[Reactive] public ElementTheme Theme { get; set; }
Expand All @@ -28,6 +30,7 @@ public class SettingsViewModel : NavigatableViewModel
public List<ElementTheme> Themes { get; } = Enum.GetValues<ElementTheme>().Cast<ElementTheme>().ToList();
public IEnumerable<PluginInfo> ProviderTypes => PluginFactory<AnimeProvider>.Instance.Plugins;
public IEnumerable<PluginInfo> TrackerTypes => PluginFactory<ITorrentTracker>.Instance.Plugins;
public IEnumerable<PluginInfo> MangaProviderTypes => PluginFactory<MangaProvider>.Instance.Plugins;
public IEnumerable<DisplayMode> ListDisplayModes { get; } = Enum.GetValues<DisplayMode>().Cast<DisplayMode>().Take(2).ToList();
public List<LogLevel> LogLevels { get; } = new List<LogLevel> { LogLevel.Debug, LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical };
public List<ListServiceType> ServiceTypes { get; } = new List<ListServiceType> { ListServiceType.MyAnimeList, ListServiceType.AniList };
Expand Down Expand Up @@ -60,6 +63,8 @@ public SettingsViewModel(ISettings settings,
?? PluginFactory<AnimeProvider>.Instance.Plugins.FirstOrDefault(x => x.Name == "anime-pahe");
SelectedTracker = PluginFactory<ITorrentTracker>.Instance.Plugins.FirstOrDefault(x => x.Name == settings.DefaultTorrentTrackerType)
?? PluginFactory<ITorrentTracker>.Instance.Plugins.FirstOrDefault(x => x.Name == "nya");
SelectedMangaProvider = PluginFactory<MangaProvider>.Instance.Plugins.FirstOrDefault(x => x.Name == settings.DefaultMangaProviderType)
?? PluginFactory<MangaProvider>.Instance.Plugins.FirstOrDefault(x => x.Name == "manga-dex");
AuthenticateCommand = ReactiveCommand.CreateFromTask<ListServiceType>(viewService.Authenticate);
ShowAbout = ReactiveCommand.CreateFromTask(async () =>
{
Expand Down
1 change: 1 addition & 0 deletions Totoro.Plugins.Manga/Contracts/IMangaCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface IChapterProvider
public class ChapterModel
{
public string Id { get; set; }
public string Title { get; set; }
public float Chapter { get; set; }
public float Volume { get; set; }
}
5 changes: 4 additions & 1 deletion Totoro.Plugins/PluginInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Totoro.Plugins;
using System.Diagnostics;

namespace Totoro.Plugins;

#nullable disable

[DebuggerDisplay("{DisplayName}")]
public class PluginInfo
{
public PluginInfo()
Expand Down
8 changes: 8 additions & 0 deletions Totoro.WinUI/Helpers/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Totoro.Core.Services;
using Totoro.Plugins;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Manga;
using Totoro.Plugins.Manga.Contracts;
using Totoro.Plugins.MediaDetection.Contracts;
using Totoro.Plugins.Options;
using Totoro.Plugins.Torrents.Contracts;
Expand Down Expand Up @@ -233,9 +235,15 @@ public static ImageSource StreamToImage(Stream stream)
return bmp;
}

public static string ToOneBasedIndex(int number) => (number + 1).ToString();
public static string ToTitle(ChapterModel chapter) => string.IsNullOrEmpty(chapter.Title)
? chapter.Chapter.ToString()
: $"{chapter.Chapter} - {chapter.Title}";

public static PluginOptions GetAnimeOptions(string pluginName) => GetOptions<AnimeProvider>(pluginName);
public static PluginOptions GetTorrentsOptions(string pluginName) => GetOptions<ITorrentTracker>(pluginName);
public static PluginOptions GetMediaOptions(string pluginName) => GetOptions<INativeMediaPlayer>(pluginName);
public static PluginOptions GetMangaOptions(string pluginName) => GetOptions<MangaProvider>(pluginName);

private static PluginOptions GetOptions<T>(string pluginName)
{
Expand Down
4 changes: 4 additions & 0 deletions Totoro.WinUI/Totoro.WinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ItemGroup>
<None Remove="Views\DiscoverMangaPage.xaml" />
<None Remove="Views\ReadPage.xaml" />
<None Remove="Views\SettingsSections\MangaPluginsSection.xaml" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -97,6 +98,9 @@
<Page Update="Views\NowPlayingPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\SettingsSections\MangaPluginsSection.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Views\SettingsSections\MediaPlayerSection.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
Expand Down
5 changes: 0 additions & 5 deletions Totoro.WinUI/Views/DiscoverMangaPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@
<Grid.Background>
<SolidColorBrush Opacity="0.5" Color="Black" />
</Grid.Background>
<!--<TextBlock
FontSize="20"
Foreground="White"
Text="{x:Bind help:ModelHelpers.GetRating((pc:ICatalogItem))}" />-->
</Grid>

<Border
Expand All @@ -100,7 +96,6 @@
TextTrimming="WordEllipsis"
TextWrapping="NoWrap"
ToolTipService.ToolTip="{x:Bind Title}" />
<!--<TextBlock HorizontalAlignment="Center" Text="{x:Bind help:ModelHelpers.GetAdditionalInformation((pc:ICatalogItem))}" />-->
</StackPanel>
</Border>
</Grid>
Expand Down
Loading

0 comments on commit 1e9393e

Please sign in to comment.