Skip to content

Commit

Permalink
QoL changes,
Browse files Browse the repository at this point in the history
Auto updates
  • Loading branch information
insomniachi committed Dec 28, 2022
1 parent 45b61d5 commit 417f54d
Show file tree
Hide file tree
Showing 20 changed files with 953 additions and 940 deletions.
1 change: 0 additions & 1 deletion Totoro.Core.Tests/Builders/DiscoverViewModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal DiscoverViewModel Builder()
{
return new DiscoverViewModel(Mock.Of<IProviderFactory>(),
Mock.Of<ISettings>(),
_featuredAnimeProviderMock.Object,
_navigationServiceMock.Object,
_trackingServiceMock.Object,
_schedulerProvider?.Object ?? new SchedulerProvider());
Expand Down
1 change: 0 additions & 1 deletion Totoro.Core.Tests/Totoro.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Platforms>x64</Platforms>
<Platforms>x64</Platforms>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
Expand Down
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface ISettings
bool ContributeTimeStamps { get; set; }
public DefaultUrls DefaultUrls { get; set; }
LogLevel MinimumLogLevel { get; set; }
bool AutoUpdate { get; set; }
}

public class DefaultUrls : ReactiveObject
Expand Down
17 changes: 17 additions & 0 deletions Totoro.Core/Contracts/IUpdateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Totoro.Core.Contracts
{
public interface IUpdateService
{
IObservable<VersionInfo> OnUpdateAvailable { get; }
Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo);
void InstallUpdate(VersionInfo versionInfo);
}

public class VersionInfo
{
public Version Version { get; set; }
public string Details { get; set; }
public string Url { get; set; }
public string FilePath { get; set; }
}
}
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/IViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IViewService
Task PlayVideo(string title, string url);
Task<T> SelectModel<T>(IEnumerable<object> models) where T : class;
Task SubmitTimeStamp(long malId, int ep, VideoStream stream, double duration, double introStart);
Task<bool> Question(string title, string message);
}
1 change: 1 addition & 0 deletions Totoro.Core/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static IServiceCollection AddTotoro(this IServiceCollection services)
services.AddSingleton<ITorrentsService, TorrentsService>();
services.AddSingleton<ILocalMediaService, LocalMediaService>();
services.AddSingleton<IAiredEpisodeNotifier, AiredEpisodeNotifier>();
services.AddSingleton<IUpdateService, WindowsUpdateService>();

services.AddTransient<IFileService, FileService>();
services.AddTransient<MalToModelConverter>();
Expand Down
60 changes: 60 additions & 0 deletions Totoro.Core/Services/WindowsUpdateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json.Nodes;
using Splat;

namespace Totoro.Core.Services;

public class WindowsUpdateService : IUpdateService, IEnableLogger
{
private readonly IObservable<VersionInfo> _onUpdate;
private readonly string _updateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Totoro", "ApplicationData", "Updates");
public IObservable<VersionInfo> OnUpdateAvailable => _onUpdate;

public WindowsUpdateService(HttpClient httpClient)
{
_onUpdate = Observable
.Timer(TimeSpan.Zero, TimeSpan.FromHours(1))
.ObserveOn(RxApp.TaskpoolScheduler)
.SelectMany(_ => httpClient.GetStreamAsync("https://api.github.com/repos/athulrajts/AnimDL.GUI/releases/latest"))
.Select(x => JsonNode.Parse(x))
.Select(jsonNode => new VersionInfo()
{
Version = new Version(jsonNode["tag_name"].ToString()),
Url = (string)jsonNode["assets"][0]["browser_download_url"].AsValue()
})
.Log(this, "Latest Version", x => x.Version.ToString())
.Where(vi => vi.Version > Assembly.GetExecutingAssembly().GetName().Version)
.Throttle(TimeSpan.FromSeconds(3));
}

public async Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo)
{
Directory.CreateDirectory(_updateFolder);
var ext = Path.GetExtension(versionInfo.Url);
var fileName = $"Totoro_{versionInfo.Version}{ext}";
var fullPath = Path.Combine(_updateFolder, fileName);
versionInfo.FilePath = fullPath;

if(File.Exists(fullPath)) // already download before
{
return versionInfo;
}

using var client = new HttpClient();
using var s = await client.GetStreamAsync(versionInfo.Url);
using var fs = new FileStream(fullPath, FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
return versionInfo;
}

public void InstallUpdate(VersionInfo versionInfo)
{
Process.Start(new ProcessStartInfo
{
FileName = "msiexec",
Arguments = $"/i \"{versionInfo.FilePath}\""
});
Process.GetCurrentProcess().CloseMainWindow();
}
}
2 changes: 2 additions & 0 deletions Totoro.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SettingsViewModel : NavigatableViewModel, ISettings
[Reactive] public bool ContributeTimeStamps { get; set; }
[Reactive] public DefaultUrls DefaultUrls { get; set; }
[Reactive] public LogLevel MinimumLogLevel { get; set; }
[Reactive] public bool AutoUpdate { get; set; }
public List<ElementTheme> Themes { get; } = Enum.GetValues<ElementTheme>().Cast<ElementTheme>().ToList();
public List<ProviderType> ProviderTypes { get; } = new List<ProviderType> { ProviderType.AllAnime, ProviderType.AnimePahe, ProviderType.GogoAnime, ProviderType.Yugen };
public List<LogLevel> LogLevels { get; } = new List<LogLevel> { LogLevel.Debug, LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical };
Expand All @@ -35,6 +36,7 @@ public SettingsViewModel(IThemeSelectorService themeSelectorService,
ContributeTimeStamps = localSettingsService.ReadSetting(nameof(ContributeTimeStamps), false);
DefaultUrls = localSettingsService.ReadSetting(nameof(DefaultUrls), new DefaultUrls());
MinimumLogLevel = localSettingsService.ReadSetting(nameof(MinimumLogLevel), LogLevel.Debug);
AutoUpdate = localSettingsService.ReadSetting(nameof(AutoUpdate), true);

var id = localSettingsService.ReadSetting(nameof(AniSkipId), Guid.Empty);
if (id == Guid.Empty)
Expand Down
2 changes: 1 addition & 1 deletion Totoro.Core/ViewModels/WatchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ private void OnSubmitTimeStamps()
RxApp.MainThreadScheduler.Schedule(async () =>
{
MediaPlayer.Pause();
await _viewService.SubmitTimeStamp(Anime.Id, CurrentEpisode.Value, SelectedStream, CurrentMediaDuration, _userSkipOpeningTime - 5);
await _viewService.SubmitTimeStamp(Anime.Id, CurrentEpisode.Value, SelectedStream, CurrentMediaDuration, _userSkipOpeningTime == 0 ? 0 : _userSkipOpeningTime - 5);
MediaPlayer.Play();
});
}
Expand Down
Loading

0 comments on commit 417f54d

Please sign in to comment.