Skip to content

Commit

Permalink
about release dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Jan 1, 2023
1 parent e6c2b3c commit b488633
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Totoro.Core/Contracts/IUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public interface IUpdateService
IObservable<VersionInfo> OnUpdateAvailable { get; }
Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo);
void InstallUpdate(VersionInfo versionInfo);
ValueTask<VersionInfo> GetCurrentVersionInfo();
}

public class VersionInfo
Expand All @@ -13,5 +14,6 @@ public class VersionInfo
public string Details { get; set; }
public string Url { get; set; }
public string FilePath { get; set; }
public string Body { 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 @@ -9,4 +9,5 @@ public interface IViewService
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);
Task<Unit> Information(string title, string message);
}
34 changes: 28 additions & 6 deletions Totoro.Core/Services/WindowsUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

namespace Totoro.Core.Services;

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

public IObservable<VersionInfo> OnUpdateAvailable => _onUpdate;

public WindowsUpdateService(HttpClient httpClient)
Expand All @@ -21,15 +24,34 @@ public WindowsUpdateService(HttpClient httpClient)
.Select(jsonNode => new VersionInfo()
{
Version = new Version(jsonNode["tag_name"].ToString()),
Url = (string)jsonNode["assets"][0]["browser_download_url"].AsValue()
Url = (string)jsonNode["assets"][0]["browser_download_url"].AsValue(),
Body = jsonNode?["body"]?.ToString()
})
#if !DEBUG
.Where(vi => vi.Version > Assembly.GetEntryAssembly().GetName().Version)
#else
.Where(vi => false)
#endif
.Log(this, "New Version", vi => vi.Version.ToString())
.Throttle(TimeSpan.FromSeconds(3));
_httpClient = httpClient;
}

public async ValueTask<VersionInfo> GetCurrentVersionInfo()
{
if(_current is null)
{
var url = $"https://api.github.com/repositories/522584084/releases/tags/{Assembly.GetEntryAssembly().GetName().Version}";
var response = await _httpClient.GetAsync(url);

if(response.IsSuccessStatusCode)
{
var jsonNode = JsonNode.Parse(await response.Content.ReadAsStreamAsync());
_current = new VersionInfo()
{
Version = new Version(jsonNode["tag_name"].ToString()),
Url = (string)jsonNode["assets"][0]["browser_download_url"].AsValue(),
Body = jsonNode?["body"]?.ToString()
};
}
}
return _current;
}

public async Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo)
Expand Down
13 changes: 12 additions & 1 deletion Totoro.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class SettingsViewModel : NavigatableViewModel, ISettings
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 ICommand AuthenticateCommand { get; }
public ICommand ShowAbout { get; }

public SettingsViewModel(IThemeSelectorService themeSelectorService,
ILocalSettingsService localSettingsService,
IViewService viewService,
IDiscordRichPresense dRpc)
IDiscordRichPresense dRpc,
IUpdateService updateService)
{
Version = Assembly.GetEntryAssembly().GetName().Version;
ScrapperVersion = typeof(AnimDL.Core.DefaultUrl).Assembly.GetName().Version;
Expand All @@ -55,6 +57,15 @@ public SettingsViewModel(IThemeSelectorService themeSelectorService,
}

AuthenticateCommand = ReactiveCommand.CreateFromTask<ListServiceType>(viewService.Authenticate);
ShowAbout = ReactiveCommand.CreateFromTask(async () =>
{
var currentInfo = await updateService.GetCurrentVersionInfo();
if(currentInfo is null)
{
return;
}
await viewService.Information($"{currentInfo.Version}", currentInfo.Body);
});

if (UseDiscordRichPresense && !dRpc.IsInitialized)
{
Expand Down
17 changes: 17 additions & 0 deletions Totoro.WinUI/Services/ViewService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AnimDL.Core.Api;
using CommunityToolkit.WinUI.UI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Totoro.WinUI.Contracts;
Expand Down Expand Up @@ -177,4 +178,20 @@ public async Task<bool> Question(string title, string message)
var result = await dialog.ShowAsync();
return result == ContentDialogResult.Primary;
}

public async Task<Unit> Information(string title, string message)
{
var dialog = new ContentDialog()
{
Title = title,
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
XamlRoot = App.MainWindow.Content.XamlRoot,
DefaultButton = ContentDialogButton.Primary,
Content = new MarkdownTextBlock() { Text = message, TextWrapping = TextWrapping.WrapWholeWords, Padding = new Thickness(10)},
PrimaryButtonText = "Yes",
};

var result = await dialog.ShowAsync();
return Unit.Default;
}
}
3 changes: 2 additions & 1 deletion Totoro.WinUI/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@
OnContent="On" />
</labs:SettingsCard>
<labs:SettingsCard
Command="{x:Bind ViewModel.ShowAbout}"
Description="{x:Bind ViewModel.GetDescripton()}"
Header="Totoro"
Header="About"
IsClickEnabled="True">
<labs:SettingsCard.HeaderIcon>
<FontIcon Glyph="&#xe756;" />
Expand Down

0 comments on commit b488633

Please sign in to comment.