Skip to content

Commit

Permalink
add settings to autoskip fillers under media player section #90
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Mar 4, 2024
1 parent 0eb4a17 commit 82cc1e3
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 73 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface ISettings : INotifyPropertyChanged
DisplayMode ListDisplayMode { get; set; }
GridViewSettings UserListGridViewSettings { get; set; }
public string DefaultMangaProviderType { get; set; }
public bool SkipFillers { get; set; }
}

public class StartupOptions : ReactiveObject
Expand Down
46 changes: 30 additions & 16 deletions Totoro.Core/Models/EpisodeModelCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

namespace Totoro.Core.Models
{
public class EpisodeModelCollection : Collection<EpisodeModel>, INotifyPropertyChanged
public class EpisodeModelCollection(bool skipFillers) : Collection<EpisodeModel>, INotifyPropertyChanged
{
public EpisodeModelCollection() { }
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string property = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));

Expand All @@ -34,18 +33,33 @@ public void SelectEpisode(int episode)

public void SelectNext()
{
Current = this.FirstOrDefault(x => x.EpisodeNumber == Current.EpisodeNumber + 1);
if(skipFillers)
{
Current = this.Where(x => !x.IsFillter).FirstOrDefault(x => x.EpisodeNumber > Current.EpisodeNumber);
}
else
{
Current = this.FirstOrDefault(x => x.EpisodeNumber == Current.EpisodeNumber + 1);
}
}

public void SelectPrevious()
{
Current = this.FirstOrDefault(x => x.EpisodeNumber == Current.EpisodeNumber - 1);
if(skipFillers)
{
Current = this.Where(x => !x.IsFillter).FirstOrDefault(x => x.EpisodeNumber < Current.EpisodeNumber);
}
else
{
Current = this.FirstOrDefault(x => x.EpisodeNumber == Current.EpisodeNumber - 1);
}

}


public static EpisodeModelCollection FromEpisodeCount(int count)
public static EpisodeModelCollection FromEpisodeCount(int count, bool skipFillers)
{
var collection = new EpisodeModelCollection();
var collection = new EpisodeModelCollection(skipFillers);
collection.AddRange(Enumerable.Range(1, count).Select(ep => new EpisodeModel
{
EpisodeNumber = ep,
Expand All @@ -59,21 +73,21 @@ public static EpisodeModelCollection FromEpisodeCount(int count)

public static EpisodeModelCollection FromEpisode(int episode)
{
return
[
return new(false)
{
new EpisodeModel
{
EpisodeNumber = episode,
IsSpecial = false,
SpecialEpisodeNumber = string.Empty,
EpisodeTitle = string.Empty,
}
];
};
}

public static EpisodeModelCollection FromEpisodes(IEnumerable<int> episodes)
public static EpisodeModelCollection FromEpisodes(IEnumerable<int> episodes, bool skipFillers)
{
var collecton = new EpisodeModelCollection();
var collecton = new EpisodeModelCollection(skipFillers);
collecton.AddRange(episodes.Select(ep => new EpisodeModel
{
EpisodeNumber = ep,
Expand All @@ -85,9 +99,9 @@ public static EpisodeModelCollection FromEpisodes(IEnumerable<int> episodes)
return collecton;
}

public static EpisodeModelCollection FromEpisode(int start, int end)
public static EpisodeModelCollection FromEpisode(int start, int end, bool skipFillers)
{
var collecton = new EpisodeModelCollection();
var collecton = new EpisodeModelCollection(skipFillers);
collecton.AddRange(Enumerable.Range(start, end - start + 1).Select(ep => new EpisodeModel
{
EpisodeNumber = ep,
Expand All @@ -99,9 +113,9 @@ public static EpisodeModelCollection FromEpisode(int start, int end)
return collecton;
}

public static EpisodeModelCollection FromDirectDownloadLinks(IEnumerable<DirectDownloadLink> links)
public static EpisodeModelCollection FromDirectDownloadLinks(IEnumerable<DirectDownloadLink> links, bool skipFillers)
{
var collecton = new EpisodeModelCollection();
var collecton = new EpisodeModelCollection(skipFillers);
var options = new Options(title: true, extension: false, group: false);
collecton.AddRange(links.Select(ddl =>
{
Expand Down Expand Up @@ -137,6 +151,6 @@ public static EpisodeModelCollection FromDirectDownloadLinks(IEnumerable<DirectD
return collecton;
}

public static EpisodeModelCollection Empty { get; } = [];
public static EpisodeModelCollection Empty { get; } = new(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<VideoStreamsForEpisodeModel> ResolveEpisode(int episode, Strea

var results = await GetStreams(episode, streamType);

if (!results.Any())
if (results.Count == 0)
{
this.Log().Debug("No streams found");
return null;
Expand Down Expand Up @@ -71,7 +71,7 @@ public async Task<int> GetNumberOfEpisodes(StreamType streamType)

public async Task<EpisodeModelCollection> ResolveAllEpisodes(StreamType streamType)
{
return EpisodeModelCollection.FromEpisodeCount(await GetNumberOfEpisodes(streamType));
return EpisodeModelCollection.FromEpisodeCount(await GetNumberOfEpisodes(streamType), _settings.SkipFillers);
}

private async Task<List<VideoStreamsForEpisode>> GetStreams(int episode, StreamType streamType)
Expand Down
18 changes: 7 additions & 11 deletions Totoro.Core/Services/StreamResolvers/DebridStreamModelResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@

namespace Totoro.Core.Services.StreamResolvers;

public class DebridStreamModelResolver : IVideoStreamModelResolver, IEnableLogger
public class DebridStreamModelResolver(IDebridServiceContext debridService,
ISettings settings,
string magnet) : IVideoStreamModelResolver, IEnableLogger
{
private readonly IDebridServiceContext _debridService;
private readonly string _magnet;
private readonly IDebridServiceContext _debridService = debridService;
private readonly ISettings _settings = settings;
private readonly string _magnet = magnet;
private List<DirectDownloadLink> _links;

public DebridStreamModelResolver(IDebridServiceContext debridService,
string magnet)
{
_debridService = debridService;
_magnet = magnet;
}

public async Task Populate()
{
_links = (await _debridService.GetDirectDownloadLinks(_magnet)).ToList();
Expand All @@ -35,7 +31,7 @@ public async Task Populate()

public Task<EpisodeModelCollection> ResolveAllEpisodes(StreamType streamType)
{
return Task.FromResult(EpisodeModelCollection.FromDirectDownloadLinks(_links));
return Task.FromResult(EpisodeModelCollection.FromDirectDownloadLinks(_links, _settings.SkipFillers));
}

public Task<VideoStreamsForEpisodeModel> ResolveEpisode(int episode, StreamType streamType)
Expand Down
18 changes: 7 additions & 11 deletions Totoro.Core/Services/StreamResolvers/FileSystemStreamResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

namespace Totoro.Core.Services.StreamResolvers;

internal class FileSystemStreamResolver : IVideoStreamModelResolver, IDisposable
internal class FileSystemStreamResolver(string directory, ISettings settings) : IVideoStreamModelResolver, IDisposable
{
private readonly DirectoryInfo _directory;
private readonly string[] _videoFileExtensions = new[] { ".mkv", ".mp4" };
private readonly DirectoryInfo _directory = new(directory);
private readonly string[] _videoFileExtensions = [".mkv", ".mp4"];
private readonly Dictionary<int, string> _episodes = [];
private readonly ISettings _settings = settings;
private static Stream _prevStream;

public FileSystemStreamResolver(string directory)
{
_directory = new(directory);
}

public Task<EpisodeModelCollection> ResolveAllEpisodes(StreamType streamType)
{
foreach (var fileInfo in _directory.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
Expand All @@ -34,20 +30,20 @@ public Task<EpisodeModelCollection> ResolveAllEpisodes(StreamType streamType)
_episodes.Add(episode, fileInfo.FullName);
}

return Task.FromResult(EpisodeModelCollection.FromEpisodes(_episodes.Keys.Order()));
return Task.FromResult(EpisodeModelCollection.FromEpisodes(_episodes.Keys.Order(), _settings.SkipFillers));
}

public Task<VideoStreamsForEpisodeModel> ResolveEpisode(int episode, StreamType streamType)
{
_prevStream?.Dispose();
_prevStream = null;

if (!_episodes.ContainsKey(episode))
if (!_episodes.TryGetValue(episode, out string value))
{
return Task.FromResult<VideoStreamsForEpisodeModel>(default);
}

_prevStream = File.OpenRead(_episodes[episode]);
_prevStream = File.OpenRead(value);
return Task.FromResult(new VideoStreamsForEpisodeModel(_prevStream));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class MonoTorrentStreamModelResolver : IVideoStreamModelResolver,
ISpecialVideoStreamModelResolver
{
private readonly ITorrentEngine _torrentEngine;
private readonly ISettings _settings;
private readonly Torrent _torrent;
private readonly string _torrentUrl;
private readonly string _saveDirectory;
Expand All @@ -24,24 +25,26 @@ public sealed class MonoTorrentStreamModelResolver : IVideoStreamModelResolver,

public MonoTorrentStreamModelResolver(ITorrentEngine torrentEngine,
IEnumerable<Element> parsedResults,
string torrentUrl,
string saveDirectory)
ISettings settings,
string torrentUrl)
{
_torrentEngine = torrentEngine;
_settings = settings;
_torrentUrl = torrentUrl;
var folder = parsedResults.First(x => x.Category == Element.ElementCategory.ElementAnimeTitle).Value;
_saveDirectory = Path.Combine(saveDirectory, folder);
_saveDirectory = Path.Combine(settings.UserTorrentsDownloadDirectory, folder);
}

public MonoTorrentStreamModelResolver(ITorrentEngine torrentEngine,
Torrent torrent,
string saveDirectory)
ISettings settings,
Torrent torrent)
{
_torrentEngine = torrentEngine;
_torrent = torrent;
_settings = settings;
var parsedResults = AnitomySharp.AnitomySharp.Parse(torrent.Name);
var folder = parsedResults.First(x => x.Category == Element.ElementCategory.ElementAnimeTitle).Value;
_saveDirectory = Path.Combine(saveDirectory, folder);
_saveDirectory = Path.Combine(settings.UserTorrentsDownloadDirectory, folder);
}

public async ValueTask DisposeAsync()
Expand Down Expand Up @@ -73,7 +76,7 @@ public async Task<EpisodeModelCollection> ResolveAllEpisodes(StreamType streamTy
.DisposeWith(_disposable);

var index = 0;
var eps = new EpisodeModelCollection();
var eps = new EpisodeModelCollection(_settings.SkipFillers);
foreach (var file in _torrentManager.Torrent.Files.Select(x => x.Path))
{
var result = AnitomySharp.AnitomySharp.Parse(file);
Expand Down
35 changes: 12 additions & 23 deletions Totoro.Core/Services/StreamResolvers/VideoStreamResolverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,15 @@

namespace Totoro.Core.Services.StreamResolvers;

public class VideoStreamResolverFactory : IVideoStreamResolverFactory
public class VideoStreamResolverFactory(IPluginFactory<AnimeProvider> providerFactory,
ISettings settings,
IDebridServiceContext debridService,
ITorrentEngine torrentEngine) : IVideoStreamResolverFactory
{
private readonly IPluginFactory<AnimeProvider> _providerFactory;
private readonly ISettings _settings;
private readonly IDebridServiceContext _debridService;
private readonly IKnownFolders _knownFolders;
private readonly ITorrentEngine _torrentEngine;

public VideoStreamResolverFactory(IPluginFactory<AnimeProvider> providerFactory,
ISettings settings,
IDebridServiceContext debridService,
IKnownFolders knownFolders,
ITorrentEngine torrentEngine)
{
_providerFactory = providerFactory;
_settings = settings;
_debridService = debridService;
_knownFolders = knownFolders;
_torrentEngine = torrentEngine;
}
private readonly IPluginFactory<AnimeProvider> _providerFactory = providerFactory;
private readonly ISettings _settings = settings;
private readonly IDebridServiceContext _debridService = debridService;
private readonly ITorrentEngine _torrentEngine = torrentEngine;

public IVideoStreamModelResolver CreateAnimDLResolver(string providerType, string baseUrl)
{
Expand All @@ -38,24 +27,24 @@ public IVideoStreamModelResolver CreateSubDubResolver(string providerType, strin

public async Task<IVideoStreamModelResolver> CreateDebridStreamResolver(string magnet)
{
var resolver = new DebridStreamModelResolver(_debridService, magnet);
var resolver = new DebridStreamModelResolver(_debridService, _settings, magnet);
await resolver.Populate();
return resolver;
}

public IVideoStreamModelResolver CreateMonoTorrentStreamResolver(IEnumerable<Element> parsedResults, string magnet)
{
return new MonoTorrentStreamModelResolver(_torrentEngine, parsedResults, magnet, _settings.UserTorrentsDownloadDirectory);
return new MonoTorrentStreamModelResolver(_torrentEngine, parsedResults, _settings, magnet);
}

public IVideoStreamModelResolver CreateMonoTorrentStreamResolver(Torrent torrent)
{
return new MonoTorrentStreamModelResolver(_torrentEngine, torrent, _settings.UserTorrentsDownloadDirectory);
return new MonoTorrentStreamModelResolver(_torrentEngine, _settings, torrent);
}

public IVideoStreamModelResolver CreateLocalStreamResolver(string directory)
{
return new FileSystemStreamResolver(directory);
return new FileSystemStreamResolver(directory, _settings);
}
}

1 change: 1 addition & 0 deletions Totoro.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static class Settings
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 Key<bool> SkipFillers { get; } = new("SkipFillers", false);

public static IEnumerable<string> GetObsoleteKeys()
{
Expand Down
2 changes: 2 additions & 0 deletions Totoro.Core/ViewModels/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public SettingsModel(ILocalSettingsService localSettingsService,
ListDisplayMode = localSettingsService.ReadSetting(Settings.ListDisplayMode);
UserListGridViewSettings = localSettingsService.ReadSetting(Settings.UserListGridViewSettings);
DefaultMangaProviderType = localSettingsService.ReadSetting(Settings.DefaultMangaProviderType);
SkipFillers = localSettingsService.ReadSetting(Settings.SkipFillers);

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


Expand Down
13 changes: 10 additions & 3 deletions Totoro.WinUI/Views/SettingsSections/MediaPlayerSection.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
<!-- Licensed under the MIT License. -->

<Page
x:Class="Totoro.WinUI.Views.SettingsSections.MediaPlayerSection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Expand Down Expand Up @@ -53,6 +50,16 @@
Value="{x:Bind ViewModel.Settings.OpeningSkipDurationInSeconds, Mode=TwoWay}" />
</labs:SettingsCard>

<labs:SettingsCard
Description="Auto skip filler episodes"
Header="Skip Fillers"
HeaderIcon="{ui:FontIcon Glyph=&#xEB9D;}">
<ToggleSwitch
IsOn="{x:Bind ViewModel.Settings.SkipFillers, Mode=TwoWay}"
OffContent="Off"
OnContent="On" />
</labs:SettingsCard>

<labs:SettingsCard
Description="Small skip amount in seconds"
Header="Small Skip"
Expand Down

0 comments on commit 82cc1e3

Please sign in to comment.