Skip to content

Commit

Permalink
- fix not able to move window
Browse files Browse the repository at this point in the history
- use simkl for episode metadata
- fix simkl tracking not selectable in settings
  • Loading branch information
insomniachi committed Dec 6, 2023
1 parent b7583fc commit 92887e2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 49 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ISimklService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace Totoro.Core.Contracts;
public interface ISimklService
{
Task<AnimeIdExtended> GetId(ListServiceType type, long id);
Task<IEnumerable<EpisodeModel>> GetEpisodes(long id);
}
64 changes: 52 additions & 12 deletions Totoro.Core/Services/Simkl/SimklService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@ namespace Totoro.Core.Services.Simkl;
internal class SimklService : ISimklService
{
private readonly ISimklClient _simklClient;
private readonly ISettings _settings;

public SimklService(ISimklClient simklClient)
public SimklService(ISimklClient simklClient,
ISettings settings)
{
_simklClient = simklClient;
_settings = settings;
}

public async Task<AnimeIdExtended> GetId(ListServiceType type, long id)
{
var serviceType = type switch
{
ListServiceType.MyAnimeList => "mal",
ListServiceType.AniList => "anilist",
ListServiceType.Kitsu => "kitsu",
ListServiceType.AniDb => "anidb",
ListServiceType.Simkl => "simkl",
_ => throw new UnreachableException()
};

var response = await _simklClient.Search(serviceType, id);
var response = await _simklClient.Search(GetServiceType(type), id);
if (response.FirstOrDefault() is not { Id.Simkl: not null } metaData)
{
return null;
Expand Down Expand Up @@ -61,4 +54,51 @@ private static AnimeIdExtended ToExtendedId(SimklIds ids)

return extended;
}

public async Task<IEnumerable<EpisodeModel>> GetEpisodes(long id)
{
id = await GetSimklId(id);

if(id == 0)
{
return Enumerable.Empty<EpisodeModel>();
}

var episodes = await _simklClient.GetEpisodes(id);

return episodes.Select(x => new EpisodeModel
{
EpisodeNumber = x.EpisodeNumber,
EpisodeTitle = x.Title
});
}

private async ValueTask<long> GetSimklId(long id)
{
if(_settings.DefaultListService == ListServiceType.Simkl)
{
return id;
}

var response = await _simklClient.Search(GetServiceType(_settings.DefaultListService), id);
if (response.FirstOrDefault() is not { Id.Simkl: not null } metaData)
{
return 0;
}

return metaData.Id.Simkl ?? 0;
}

private static string GetServiceType(ListServiceType type)
{
return type switch
{
ListServiceType.MyAnimeList => "mal",
ListServiceType.AniList => "anilist",
ListServiceType.Kitsu => "kitsu",
ListServiceType.AniDb => "anidb",
ListServiceType.Simkl => "simkl",
_ => throw new UnreachableException()
};
}
}
2 changes: 1 addition & 1 deletion Totoro.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SettingsViewModel : NavigatableViewModel
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 };
public List<ListServiceType> ServiceTypes { get; } = new List<ListServiceType> { ListServiceType.MyAnimeList, ListServiceType.AniList, ListServiceType.Simkl };
public List<string> HomePages { get; } = new List<string> { "Discover", "My List" };
public List<string> AnimeActions { get; } = new List<string> { "Watch", "Info" };
public List<StreamQualitySelection> QualitySelections { get; } = Enum.GetValues<StreamQualitySelection>().Cast<StreamQualitySelection>().ToList();
Expand Down
10 changes: 5 additions & 5 deletions Totoro.Core/ViewModels/WatchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class WatchViewModel : NavigatableViewModel
private readonly IAnimeServiceContext _animeService;
private readonly IStreamPageMapper _streamPageMapper;
private readonly IVideoStreamResolverFactory _videoStreamResolverFactory;
private readonly IMyAnimeListService _myAnimeListService;
private readonly ISimklService _simklService;
private readonly IAnimeDetectionService _animeDetectionService;
private readonly INameService _nameService;
private readonly List<IMediaEventListener> _mediaEventListeners;
Expand All @@ -47,8 +47,8 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,
IMediaPlayerFactory mediaPlayerFactory,
IStreamPageMapper streamPageMapper,
IVideoStreamResolverFactory videoStreamResolverFactory,
ISimklService simklService,
IEnumerable<IMediaEventListener> mediaEventListeners,
IMyAnimeListService myAnimeListService,
IAnimeDetectionService animeDetectionService,
INameService nameService)
{
Expand All @@ -60,7 +60,7 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,
_animeService = animeService;
_streamPageMapper = streamPageMapper;
_videoStreamResolverFactory = videoStreamResolverFactory;
_myAnimeListService = myAnimeListService;
_simklService = simklService;
_animeDetectionService = animeDetectionService;
_nameService = nameService;
_mediaEventListeners = mediaEventListeners.ToList();
Expand Down Expand Up @@ -97,7 +97,7 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,

this.ObservableForProperty(x => x.Anime, x => x)
.WhereNotNull()
.Do(async model => _episodeMetadata ??= await myAnimeListService.GetEpisodes(model.Id))
.Do(async model => _episodeMetadata ??= await simklService.GetEpisodes(model.Id))
.SelectMany(model => Find(model.Id, model.Title))
.Where(x => x is not (null, null))
.Log(this, "Selected Anime", x => $"{x.Sub.Title}")
Expand Down Expand Up @@ -554,7 +554,7 @@ private void SetAnime(long id)
.Subscribe(async anime =>
{
_anime = anime;
_episodeMetadata = await _myAnimeListService.GetEpisodes(id);
_episodeMetadata = await _simklService.GetEpisodes(id);
SetAnime(_anime);
}, RxApp.DefaultExceptionHandler.OnError);
}
Expand Down
23 changes: 7 additions & 16 deletions Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
{
_timestampsService = timestampsService;

PlayRange = ReactiveCommand.Create(Play);
SetStartPosition = ReactiveCommand.Create(() => StartPosition = MediaPlayer.GetMediaPlayer().Position.TotalSeconds);
SetEndPosition = ReactiveCommand.Create(() => EndPosition = MediaPlayer.GetMediaPlayer().Position.TotalSeconds);
SkipNearEnd = ReactiveCommand.Create(() => MediaPlayer.SeekTo(TimeSpan.FromSeconds(EndPosition - 5)));
Expand Down Expand Up @@ -62,6 +61,9 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
this.WhenAnyValue(x => x.Stream)
.WhereNotNull()
.Subscribe(async x => await MediaPlayer.SetMedia(x));

this.WhenAnyValue(x => x.StartPosition)
.Subscribe(pos => EndPosition = pos + 90);
}

[Reactive] public double StartPosition { get; set; }
Expand All @@ -70,36 +72,23 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
[Reactive] public double CurrentPlayerPosition { get; set; }
[Reactive] public VideoStreamModel Stream { get; set; }
[Reactive] public AniSkipResult ExistingResult { get; set; }
[Reactive] public string Status { get; set; }
public long MalId { get; set; }
public int Episode { get; set; }
public double Duration { get; set; }
public string[] TimeStampTypes = new[] { "OP", "ED" };
public double SuggestedStartPosition { get; set; }
public double SuggestedEndPosition => Duration - 120;
public WinUIMediaPlayerWrapper MediaPlayer { get; } = App.GetService<IMediaPlayerFactory>().Create(MediaPlayerType.WindowsMediaPlayer) as WinUIMediaPlayerWrapper;
public bool HandleClose { get; set; }

public ICommand PlayRange { get; }
public ICommand SetStartPosition { get; }
public ICommand SetEndPosition { get; }
public ICommand SkipNearEnd { get; }
public ICommand Submit { get; }
public ICommand VoteUp { get; }
public ICommand VoteDown { get; }

private void Play()
{
_subscription?.Dispose();
MediaPlayer.Play(StartPosition);

_subscription = this.WhenAnyValue(x => x.CurrentPlayerPosition)
.Where(time => time >= EndPosition)
.Subscribe(_ =>
{
_subscription?.Dispose();
MediaPlayer.Pause();
});
}

private void Vote(bool vote)
{
if (SelectedTimeStampType == "OP" && ExistingResult.Opening is { } op)
Expand All @@ -114,7 +103,9 @@ private void Vote(bool vote)

public async Task SubmitTimeStamp()
{
HandleClose = true;
await _timestampsService.SubmitTimeStamp(MalId, Episode, SelectedTimeStampType.ToLower(), new Interval { StartTime = StartPosition, EndTime = EndPosition }, Duration);
Status = $"{SelectedTimeStampType} timestamp submitted";
}

}
10 changes: 1 addition & 9 deletions Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@
<RepeatButton x:Name="SetEndButton" Content="Set End" />
<Button Command="{x:Bind ViewModel.SkipNearEnd}" Content="Skip near End" />
</StackPanel>
<Button
Width="150"
Command="{x:Bind ViewModel.PlayRange}"
Content="Preview" />
<TextBlock Text="{x:Bind ViewModel.CurrentPlayerPosition, Mode=OneWay}" />
<Button
Width="150"
Command="{x:Bind ViewModel.Submit}"
Content="Submit" />
<TextBlock Text="{x:Bind ViewModel.Status, Mode=OneWay}" />
</StackPanel>

</local:SubmitTimeStampsViewBase>
14 changes: 12 additions & 2 deletions Totoro.WinUI/Services/ViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,18 @@ await _contentDialogService.ShowDialog(vm, d =>
{
d.Title = "Submit Timestamp";
d.IsPrimaryButtonEnabled = true;
d.IsSecondaryButtonEnabled = false;
d.PrimaryButtonText = "Close";
d.IsSecondaryButtonEnabled = true;
d.PrimaryButtonText = "Submit";
d.SecondaryButtonText = "Close";
d.PrimaryButtonCommand = vm.Submit;
d.SecondaryButtonClick += (_, _) => vm.HandleClose = false;
d.Closing += (_, args) =>
{
if (vm.HandleClose)
{
args.Cancel = true;
}
};
});

vm.MediaPlayer.Pause();
Expand Down
4 changes: 0 additions & 4 deletions Totoro.WinUI/Views/ShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ private void OnLoaded(object sender, RoutedEventArgs e)
};

App.MainWindow.ExtendsContentIntoTitleBar = true;
App.MainWindow.SetTitleBar(AppTitleBar);
AppTitleBarText.Text = "AppDisplayName".GetLocalized();

TitleBarHelper.UpdateTitleBar(RequestedTheme);

KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack));
Expand Down

0 comments on commit 92887e2

Please sign in to comment.