1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 13:28:33 +00:00

Profiles - Added workshop installing

This commit is contained in:
Robert 2023-08-26 20:47:48 +02:00
parent 4143cc2de8
commit 671c587df6
21 changed files with 401 additions and 73 deletions

View File

@ -76,8 +76,9 @@ public interface IProfileService : IArtemisService
/// Creates a new profile category and saves it to persistent storage.
/// </summary>
/// <param name="name">The name of the new profile category, must be unique.</param>
/// <param name="addToTop">A boolean indicating whether or not to add the category to the top.</param>
/// <returns>The newly created profile category.</returns>
ProfileCategory CreateProfileCategory(string name);
ProfileCategory CreateProfileCategory(string name, bool addToTop = false);
/// <summary>
/// Permanently deletes the provided profile category.

View File

@ -286,12 +286,26 @@ internal class ProfileService : IProfileService
}
/// <inheritdoc />
public ProfileCategory CreateProfileCategory(string name)
public ProfileCategory CreateProfileCategory(string name, bool addToTop = false)
{
ProfileCategory profileCategory;
lock (_profileRepository)
{
profileCategory = new ProfileCategory(name, _profileCategories.Count + 1);
if (addToTop)
{
profileCategory = new ProfileCategory(name, 1);
foreach (ProfileCategory category in _profileCategories)
{
category.Order++;
category.Save();
_profileCategoryRepository.Save(category.Entity);
}
}
else
{
profileCategory = new ProfileCategory(name, _profileCategories.Count + 1);
}
_profileCategories.Add(profileCategory);
SaveProfileCategory(profileCategory);
}
@ -370,7 +384,7 @@ internal class ProfileService : IProfileService
profile.ProfileEntity.IsFreshImport = false;
_profileRepository.Save(profile.ProfileEntity);
// If the provided profile is external (cloned or from the workshop?) but it is loaded locally too, reload the local instance
// A bit dodge but it ensures local instances always represent the latest stored version
ProfileConfiguration? localInstance = ProfileConfigurations.FirstOrDefault(p => p.Profile != null && p.Profile != profile && p.ProfileId == profile.ProfileEntity.Id);
@ -450,7 +464,7 @@ internal class ProfileService : IProfileService
List<Module> modules = _pluginManagementService.GetFeaturesOfType<Module>();
profileConfiguration.LoadModules(modules);
SaveProfileCategory(category);
return profileConfiguration;
}

View File

@ -3,12 +3,13 @@ using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Artemis.UI.Shared.Utilities;
namespace Artemis.UI.Extensions
namespace Artemis.UI.Shared.Extensions
{
public static class HttpClientProgressExtensions
{
public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress<float>? progress, CancellationToken cancellationToken)
public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress<StreamProgress>? progress, CancellationToken cancellationToken)
{
using HttpResponseMessage response = await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
@ -23,13 +24,10 @@ namespace Artemis.UI.Extensions
}
// Such progress and contentLength much reporting Wow!
Progress<long> progressWrapper = new(totalBytes => progress.Report(GetProgressPercentage(totalBytes, contentLength.Value)));
await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken);
float GetProgressPercentage(float totalBytes, float currentBytes) => (totalBytes / currentBytes) * 100f;
await download.CopyToAsync(destination, 81920, progress, contentLength, cancellationToken);
}
static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress, CancellationToken cancellationToken)
static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<StreamProgress> progress, long? contentLength, CancellationToken cancellationToken)
{
if (bufferSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
@ -49,7 +47,7 @@ namespace Artemis.UI.Extensions
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
progress?.Report(new StreamProgress(totalBytesRead, contentLength ?? totalBytesRead));
}
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using Artemis.UI.Shared.Utilities;
namespace Artemis.UI.Extensions;
@ -16,7 +17,7 @@ public static class ZipArchiveExtensions
/// <param name="overwriteFiles">A boolean indicating whether to override existing files</param>
/// <param name="progress">The progress to report to.</param>
/// <param name="cancellationToken">A cancellation token</param>
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, bool overwriteFiles, IProgress<float> progress, CancellationToken cancellationToken)
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, bool overwriteFiles, IProgress<StreamProgress> progress, CancellationToken cancellationToken)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
@ -28,7 +29,7 @@ public static class ZipArchiveExtensions
{
ZipArchiveEntry entry = source.Entries[index];
entry.ExtractRelativeToDirectory(destinationDirectoryName, overwriteFiles);
progress.Report((index + 1f) / source.Entries.Count * 100f);
progress.Report(new StreamProgress(index + 1, source.Entries.Count));
cancellationToken.ThrowIfCancellationRequested();
}
}

View File

@ -5,6 +5,7 @@ using Artemis.UI.Screens.Settings;
using Artemis.UI.Screens.Settings.Updating;
using Artemis.UI.Screens.SurfaceEditor;
using Artemis.UI.Screens.Workshop;
using Artemis.UI.Screens.Workshop.Home;
using Artemis.UI.Screens.Workshop.Layout;
using Artemis.UI.Screens.Workshop.Profile;
using Artemis.UI.Shared.Routing;
@ -21,6 +22,7 @@ public static class Routes
{
Children = new List<IRouterRegistration>()
{
new RouteRegistration<WorkshopOfflineViewModel>("offline/{message:string}"),
new RouteRegistration<ProfileListViewModel>("profiles/{page:int}"),
new RouteRegistration<ProfileDetailsViewModel>("profiles/{entryId:guid}"),
new RouteRegistration<LayoutListViewModel>("layouts/{page:int}"),

View File

@ -77,16 +77,16 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
Observable.FromEventPattern<ProfileConfigurationEventArgs>(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x)
.Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration)))
.DisposeWith(d);
profileConfigurations.Edit(updater =>
{
updater.Clear();
updater.AddRange(profileCategory.ProfileConfigurations);
});
_isCollapsed = ProfileCategory.WhenAnyValue(vm => vm.IsCollapsed).ToProperty(this, vm => vm.IsCollapsed).DisposeWith(d);
_isSuspended = ProfileCategory.WhenAnyValue(vm => vm.IsSuspended).ToProperty(this, vm => vm.IsSuspended).DisposeWith(d);
});
profileConfigurations.Edit(updater =>
{
foreach (ProfileConfiguration profileConfiguration in profileCategory.ProfileConfigurations)
updater.Add(profileConfiguration);
});
}
public ReactiveCommand<Unit, Unit> ImportProfile { get; }

View File

@ -9,6 +9,8 @@
x:DataType="home:WorkshopHomeViewModel">
<Border Classes="router-container">
<Grid RowDefinitions="200,*,*">
<ProgressBar ZIndex="999" IsIndeterminate="True" IsVisible="{CompiledBinding !WorkshopReachable}" Grid.Row="0" VerticalAlignment="Top"></ProgressBar>
<Image Grid.Row="0"
Grid.RowSpan="2"
VerticalAlignment="Top"
@ -36,9 +38,9 @@
<DropShadowEffect Color="Black" OffsetX="2" OffsetY="2" BlurRadius="5"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<StackPanel Margin="30 -75 30 0" Grid.Row="1">
<StackPanel Spacing="10" Orientation="Horizontal" VerticalAlignment="Top">
<StackPanel Spacing="10" Orientation="Horizontal" VerticalAlignment="Top">
<Button Width="150" Height="180" Command="{CompiledBinding AddSubmission}" VerticalContentAlignment="Top">
<StackPanel>
<avalonia:MaterialIcon Kind="CloudUpload" HorizontalAlignment="Left" Width="60" Height="60" Margin="0 5" />
@ -46,7 +48,7 @@
<TextBlock TextWrapping="Wrap" FontSize="12" Opacity="0.8">Upload your own creations to the workshop!</TextBlock>
</StackPanel>
</Button>
<Button Width="150" Height="180" Command="{CompiledBinding Navigate}" CommandParameter="workshop/profiles/1" VerticalContentAlignment="Top">
<StackPanel>
<avalonia:MaterialIcon Kind="FolderVideo" HorizontalAlignment="Left" Width="60" Height="60" Margin="0 5" />
@ -62,17 +64,16 @@
<TextBlock TextWrapping="Wrap" FontSize="12" Opacity="0.8">Layouts make your devices look great in the editor.</TextBlock>
</StackPanel>
</Button>
</StackPanel>
<TextBlock Classes="h4" Margin="0 15 0 5">Featured submissions</TextBlock>
<TextBlock>Not yet implemented, here we'll show submissions we think are worth some extra attention.</TextBlock>
<TextBlock Classes="h4" Margin="0 15 0 5">Recently updated</TextBlock>
<TextBlock>Not yet implemented, here we'll a few of the most recent uploads/updates to the workshop.</TextBlock>
</StackPanel>
</Grid>
</Border>
</UserControl>

View File

@ -1,4 +1,5 @@
using System.Reactive;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Artemis.UI.Screens.Workshop.SubmissionWizard;
@ -6,6 +7,8 @@ using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services;
using Artemis.WebClient.Workshop;
using Artemis.WebClient.Workshop.Services;
using Avalonia.Threading;
using ReactiveUI;
namespace Artemis.UI.Screens.Workshop.Home;
@ -13,21 +16,38 @@ namespace Artemis.UI.Screens.Workshop.Home;
public class WorkshopHomeViewModel : ActivatableViewModelBase, IWorkshopViewModel
{
private readonly IWindowService _windowService;
private readonly IWorkshopService _workshopService;
private bool _workshopReachable;
public WorkshopHomeViewModel(IRouter router, IWindowService windowService)
public WorkshopHomeViewModel(IRouter router, IWindowService windowService, IWorkshopService workshopService)
{
_windowService = windowService;
AddSubmission = ReactiveCommand.CreateFromTask(ExecuteAddSubmission);
Navigate = ReactiveCommand.CreateFromTask<string>(async r => await router.Navigate(r));
_workshopService = workshopService;
AddSubmission = ReactiveCommand.CreateFromTask(ExecuteAddSubmission, this.WhenAnyValue(vm => vm.WorkshopReachable));
Navigate = ReactiveCommand.CreateFromTask<string>(async r => await router.Navigate(r), this.WhenAnyValue(vm => vm.WorkshopReachable));
this.WhenActivated((CompositeDisposable _) => Dispatcher.UIThread.InvokeAsync(ValidateWorkshopStatus));
}
public ReactiveCommand<Unit, Unit> AddSubmission { get; }
public ReactiveCommand<string, Unit> Navigate { get; }
public bool WorkshopReachable
{
get => _workshopReachable;
private set => RaiseAndSetIfChanged(ref _workshopReachable, value);
}
private async Task ExecuteAddSubmission(CancellationToken arg)
{
await _windowService.ShowDialogAsync<SubmissionWizardViewModel, bool>();
}
private async Task ValidateWorkshopStatus()
{
WorkshopReachable = await _workshopService.ValidateWorkshopStatus();
}
public EntryType? EntryType => null;
}

View File

@ -0,0 +1,36 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:home="clr-namespace:Artemis.UI.Screens.Workshop.Home"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="550"
x:Class="Artemis.UI.Screens.Workshop.Home.WorkshopOfflineView"
x:DataType="home:WorkshopOfflineViewModel">
<Border Classes="router-container">
<Panel>
<ProgressBar ZIndex="999" IsIndeterminate="True" IsVisible="{CompiledBinding Retry.IsExecuting^}" VerticalAlignment="Top"></ProgressBar>
<StackPanel Margin="0 75 0 0">
<StackPanel.Styles>
<Styles>
<Style Selector="TextBlock">
<Setter Property="TextAlignment" Value="Center"></Setter>
<Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>
</Styles>
</StackPanel.Styles>
<TextBlock Theme="{StaticResource TitleTextBlockStyle}">Could not reach the workshop</TextBlock>
<TextBlock Text="{CompiledBinding Message}" MaxWidth="600" Classes="subtitle"/>
<avalonia:MaterialIcon Kind="LanDisconnect" Width="120" Height="120" Margin="0 60"></avalonia:MaterialIcon>
<TextBlock Theme="{StaticResource SubtitleTextBlockStyle}">Please ensure you are connected to the internet.</TextBlock>
<TextBlock Margin="0 10" Classes="subtitle">If this keeps occuring, hit us up on Discord</TextBlock>
<Button HorizontalAlignment="Center" Margin="0 20" Command="{CompiledBinding Retry}">Retry</Button>
</StackPanel>
</Panel>
</Border>
</UserControl>

View File

@ -0,0 +1,14 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
namespace Artemis.UI.Screens.Workshop.Home;
public partial class WorkshopOfflineView : ReactiveUserControl<WorkshopOfflineViewModel>
{
public WorkshopOfflineView()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,57 @@
using System.Net;
using System.Reactive;
using System.Threading;
using System.Threading.Tasks;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing;
using Artemis.WebClient.Workshop;
using Artemis.WebClient.Workshop.Services;
using ReactiveUI;
namespace Artemis.UI.Screens.Workshop.Home;
public class WorkshopOfflineViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopOfflineParameters>, IWorkshopViewModel
{
private readonly IRouter _router;
private readonly IWorkshopService _workshopService;
private string _message;
/// <inheritdoc />
public WorkshopOfflineViewModel(IWorkshopService workshopService, IRouter router)
{
_workshopService = workshopService;
_router = router;
Retry = ReactiveCommand.CreateFromTask(ExecuteRetry);
}
public ReactiveCommand<Unit, Unit> Retry { get; }
public string Message
{
get => _message;
set => RaiseAndSetIfChanged(ref _message, value);
}
public override Task OnNavigating(WorkshopOfflineParameters parameters, NavigationArguments args, CancellationToken cancellationToken)
{
Message = parameters.Message;
return base.OnNavigating(parameters, args, cancellationToken);
}
private async Task ExecuteRetry(CancellationToken cancellationToken)
{
IWorkshopService.WorkshopStatus status = await _workshopService.GetWorkshopStatus();
if (status.IsReachable)
await _router.Navigate("workshop");
Message = status.Message;
}
public EntryType? EntryType => null;
}
public class WorkshopOfflineParameters
{
public string Message { get; set; }
}

View File

@ -86,29 +86,36 @@
<StackPanel>
<TextBlock Theme="{StaticResource SubtitleTextBlockStyle}">Latest release</TextBlock>
<Border Classes="card-separator" />
<Button CornerRadius="8"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<Button CornerRadius="8"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Command="{CompiledBinding DownloadLatestRelease}">
<Grid ColumnDefinitions="Auto,*">
<!-- Icon -->
<Border Grid.Column="0"
CornerRadius="8"
CornerRadius="4"
Background="{StaticResource SystemAccentColor}"
VerticalAlignment="Center"
Margin="0 6"
Width="45"
Height="45"
Width="50"
Height="50"
ClipToBounds="True">
<avalonia:MaterialIcon Kind="Download"></avalonia:MaterialIcon>
</Border>
<!-- Body -->
<StackPanel Grid.Column="1" Margin="10 6">
<StackPanel Grid.Column="1" Margin="10 0" VerticalAlignment="Center">
<TextBlock Text="{CompiledBinding Entry.LatestRelease.Version, FallbackValue=Version}"></TextBlock>
<TextBlock Classes="subtitle">
<avalonia:MaterialIcon Kind="BoxOutline" />
<Run Text="{CompiledBinding Entry.LatestRelease.DownloadSize, Converter={StaticResource BytesToStringConverter}, Mode=OneWay}"></Run>
</TextBlock>
<TextBlock Classes="subtitle"
ToolTip.Tip="{CompiledBinding Entry.LatestRelease.CreatedAt, Converter={StaticResource DateTimeConverter}}">
<avalonia:MaterialIcon Kind="Calendar" />
<Run>Created</Run>
<Run Text="{CompiledBinding Entry.LatestRelease.CreatedAt, Converter={StaticResource DateTimeConverter}, ConverterParameter='humanize'}"></Run>
</TextBlock>
</StackPanel>
</Grid>
</Button>

View File

@ -1,11 +1,18 @@
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.UI.Screens.Workshop.Parameters;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.Builders;
using Artemis.UI.Shared.Utilities;
using Artemis.WebClient.Workshop;
using Artemis.WebClient.Workshop.DownloadHandlers;
using Artemis.WebClient.Workshop.DownloadHandlers.Implementations;
using ReactiveUI;
using StrawberryShake;
@ -14,18 +21,24 @@ namespace Artemis.UI.Screens.Workshop.Profile;
public class ProfileDetailsViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopDetailParameters>, IWorkshopViewModel
{
private readonly IWorkshopClient _client;
private readonly ProfileEntryDownloadHandler _downloadHandler;
private readonly INotificationService _notificationService;
private readonly ObservableAsPropertyHelper<DateTimeOffset?> _updatedAt;
private IGetEntryById_Entry? _entry;
public ProfileDetailsViewModel(IWorkshopClient client)
public ProfileDetailsViewModel(IWorkshopClient client, ProfileEntryDownloadHandler downloadHandler, INotificationService notificationService)
{
_client = client;
_downloadHandler = downloadHandler;
_notificationService = notificationService;
_updatedAt = this.WhenAnyValue(vm => vm.Entry).Select(e => e?.LatestRelease?.CreatedAt ?? e?.CreatedAt).ToProperty(this, vm => vm.UpdatedAt);
DownloadLatestRelease = ReactiveCommand.CreateFromTask(ExecuteDownloadLatestRelease);
}
public DateTimeOffset? UpdatedAt => _updatedAt.Value;
public ReactiveCommand<Unit, Unit> DownloadLatestRelease { get; }
public EntryType? EntryType => null;
public DateTimeOffset? UpdatedAt => _updatedAt.Value;
public IGetEntryById_Entry? Entry
{
@ -43,7 +56,21 @@ public class ProfileDetailsViewModel : RoutableScreen<ActivatableViewModelBase,
IOperationResult<IGetEntryByIdResult> result = await _client.GetEntryById.ExecuteAsync(entryId, cancellationToken);
if (result.IsErrorResult())
return;
Entry = result.Data?.Entry;
}
private async Task ExecuteDownloadLatestRelease(CancellationToken cancellationToken)
{
if (Entry?.LatestRelease == null)
return;
EntryInstallResult<ProfileConfiguration> result = await _downloadHandler.InstallProfileAsync(Entry.LatestRelease.Id, new Progress<StreamProgress>(), cancellationToken);
if (result.IsSuccess)
_notificationService.CreateNotification().WithTitle("Profile installed").WithSeverity(NotificationSeverity.Success).Show();
else
_notificationService.CreateNotification().WithTitle("Failed to install profile").WithMessage(result.Message).WithSeverity(NotificationSeverity.Error).Show();
}
public EntryType? EntryType => null;
}

View File

@ -3,10 +3,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:steps="clr-namespace:Artemis.UI.Screens.Workshop.SubmissionWizard.Steps"
mc:Ignorable="d" d:DesignWidth="970" d:DesignHeight="900"
mc:Ignorable="d" d:DesignWidth="970" d:DesignHeight="900"
x:Class="Artemis.UI.Screens.Workshop.SubmissionWizard.Steps.UploadStepView"
x:DataType="steps:UploadStepViewModel">
<StackPanel Margin="0 50 0 0" >
<StackPanel Margin="0 50 0 0">
<StackPanel.Styles>
<Styles>
<Style Selector="TextBlock">
@ -15,23 +15,31 @@
</Style>
</Styles>
</StackPanel.Styles>
<TextBlock Theme="{StaticResource TitleTextBlockStyle}" TextAlignment="Center" TextWrapping="Wrap">
<TextBlock IsVisible="{CompiledBinding !Finished}" Theme="{StaticResource TitleTextBlockStyle}" TextAlignment="Center" TextWrapping="Wrap">
Uploading your submission...
</TextBlock>
<TextBlock TextAlignment="Center" TextWrapping="Wrap">
Wooo, the final step, that was pretty easy, right!?
</TextBlock>
<ProgressBar Margin="0 15 0 0"
Width="380"
IsVisible="{CompiledBinding !Finished}"
IsIndeterminate="{CompiledBinding ProgressIndeterminate}"
Value="{CompiledBinding ProgressPercentage}"></ProgressBar>
<StackPanel IsVisible="{CompiledBinding Finished}" Margin="0 100 0 0">
<Lottie Path="/Assets/Animations/success.json" RepeatCount="1" Width="300" Height="300"></Lottie>
<ProgressBar IsVisible="{CompiledBinding !Finished}"
Margin="0 15 0 0"
Width="380"
IsIndeterminate="{CompiledBinding ProgressIndeterminate}"
Value="{CompiledBinding ProgressPercentage}">
</ProgressBar>
<StackPanel IsVisible="{CompiledBinding Succeeded}">
<Lottie Path="/Assets/Animations/success.json" RepeatCount="1" Width="250" Height="250" Margin="0 100"></Lottie>
<TextBlock Theme="{StaticResource TitleTextBlockStyle}">All done! Hit finish to view your submission.</TextBlock>
</StackPanel>
<StackPanel IsVisible="{CompiledBinding Failed}">
<TextBlock FontSize="140" Margin="0 100">😢</TextBlock>
<TextBlock Theme="{StaticResource SubtitleTextBlockStyle}">
Unfortunately something went wrong while uploading your submission.
</TextBlock>
<TextBlock>Hit finish to view your submission, from there you can try to upload a new release.</TextBlock>
<TextBlock Margin="0 10" Classes="subtitle">If this keeps occuring, hit us up on Discord</TextBlock>
</StackPanel>
</StackPanel>
</UserControl>
</UserControl>

View File

@ -28,8 +28,10 @@ public class UploadStepViewModel : SubmissionViewModel
private readonly ObservableAsPropertyHelper<int> _progressPercentage;
private readonly ObservableAsPropertyHelper<bool> _progressIndeterminate;
private bool _finished;
private Guid? _entryId;
private bool _finished;
private bool _succeeded;
private bool _failed;
/// <inheritdoc />
public UploadStepViewModel(IWorkshopClient workshopClient, IWorkshopService workshopService, EntryUploadHandlerFactory entryUploadHandlerFactory, IWindowService windowService, IRouter router)
@ -43,14 +45,14 @@ public class UploadStepViewModel : SubmissionViewModel
ShowGoBack = false;
ContinueText = "Finish";
Continue = ReactiveCommand.CreateFromTask(ExecuteContinue, this.WhenAnyValue(vm => vm.Finished));
_progressPercentage = Observable.FromEventPattern<StreamProgress>(x => _progress.ProgressChanged += x, x => _progress.ProgressChanged -= x)
.Select(e => e.EventArgs.ProgressPercentage)
.ToProperty(this, vm => vm.ProgressPercentage);
_progressIndeterminate = Observable.FromEventPattern<StreamProgress>(x => _progress.ProgressChanged += x, x => _progress.ProgressChanged -= x)
.Select(e => e.EventArgs.ProgressPercentage == 0)
.ToProperty(this, vm => vm.ProgressIndeterminate);
this.WhenActivated(d => Observable.FromAsync(ExecuteUpload).Subscribe().DisposeWith(d));
}
@ -69,6 +71,18 @@ public class UploadStepViewModel : SubmissionViewModel
set => RaiseAndSetIfChanged(ref _finished, value);
}
public bool Succeeded
{
get => _succeeded;
set => RaiseAndSetIfChanged(ref _succeeded, value);
}
public bool Failed
{
get => _failed;
set => RaiseAndSetIfChanged(ref _failed, value);
}
public async Task ExecuteUpload(CancellationToken cancellationToken)
{
IOperationResult<IAddEntryResult> result = await _workshopClient.AddEntry.ExecuteAsync(new CreateEntryInput
@ -85,12 +99,13 @@ public class UploadStepViewModel : SubmissionViewModel
if (result.IsErrorResult() || entryId == null)
{
await _windowService.ShowConfirmContentDialog("Failed to create workshop entry", result.Errors.ToString() ?? "Not even an error message", "Close", null);
State.ChangeScreen<SubmitStepViewModel>();
return;
}
if (cancellationToken.IsCancellationRequested)
return;
// Upload image
if (State.Icon != null)
await _workshopService.SetEntryIcon(entryId.Value, _progress, State.Icon, cancellationToken);
@ -113,21 +128,27 @@ public class UploadStepViewModel : SubmissionViewModel
}
_entryId = entryId;
Finished = true;
Succeeded = true;
}
catch (Exception e)
{
// Something went wrong when creating a release :c
// We'll keep the workshop entry so that the user can make changes and try again
Failed = true;
}
finally
{
Finished = true;
}
}
private async Task ExecuteContinue()
{
State.Finish();
if (_entryId == null)
return;
State.Finish();
switch (State.EntryType)
{
case EntryType.Layout:

View File

@ -9,6 +9,8 @@ using System.Threading.Tasks;
using Artemis.Core;
using Artemis.UI.Exceptions;
using Artemis.UI.Extensions;
using Artemis.UI.Shared.Extensions;
using Artemis.UI.Shared.Utilities;
using Artemis.WebClient.Updating;
using Octodiff.Core;
using Octodiff.Diagnostics;
@ -32,7 +34,7 @@ public class ReleaseInstaller : CorePropertyChanged
private IGetReleaseById_PublishedRelease _release = null!;
private IGetReleaseById_PublishedRelease_Artifacts _artifact = null!;
private Progress<float> _stepProgress = new();
private Progress<StreamProgress> _stepProgress = new();
private string _status = string.Empty;
private float _floatProgress;
@ -69,9 +71,7 @@ public class ReleaseInstaller : CorePropertyChanged
public async Task InstallAsync(CancellationToken cancellationToken)
{
_stepProgress = new Progress<float>();
((IProgress<float>) _progress).Report(0);
_stepProgress = new Progress<StreamProgress>();
Status = "Retrieving details";
_logger.Information("Retrieving details for release {ReleaseId}", _releaseId);
@ -99,7 +99,7 @@ public class ReleaseInstaller : CorePropertyChanged
{
// 10 - 50%
_stepProgress.ProgressChanged += StepProgressOnProgressChanged;
void StepProgressOnProgressChanged(object? sender, float e) => ((IProgress<float>) _progress).Report(10f + e * 0.4f);
void StepProgressOnProgressChanged(object? sender, StreamProgress e) => ((IProgress<float>) _progress).Report(10f + e.ProgressPercentage * 0.4f);
Status = "Downloading...";
await using MemoryStream stream = new();
@ -113,7 +113,7 @@ public class ReleaseInstaller : CorePropertyChanged
{
// 50 - 60%
_stepProgress.ProgressChanged += StepProgressOnProgressChanged;
void StepProgressOnProgressChanged(object? sender, float e) => ((IProgress<float>) _progress).Report(50f + e * 0.1f);
void StepProgressOnProgressChanged(object? sender, StreamProgress e) => ((IProgress<float>) _progress).Report(50f + e.ProgressPercentage * 0.1f);
Status = "Patching...";
await using FileStream newFileStream = new(Path.Combine(Constants.UpdatingFolder, $"{_release.Version}.zip"), FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
@ -139,7 +139,7 @@ public class ReleaseInstaller : CorePropertyChanged
{
// 10 - 60%
_stepProgress.ProgressChanged += StepProgressOnProgressChanged;
void StepProgressOnProgressChanged(object? sender, float e) => ((IProgress<float>) _progress).Report(10f + e * 0.5f);
void StepProgressOnProgressChanged(object? sender, StreamProgress e) => ((IProgress<float>) _progress).Report(10f + e.ProgressPercentage * 0.5f);
Status = "Downloading...";
await using FileStream stream = new(Path.Combine(Constants.UpdatingFolder, $"{_release.Version}.zip"), FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
@ -155,7 +155,7 @@ public class ReleaseInstaller : CorePropertyChanged
{
// 60 - 100%
_stepProgress.ProgressChanged += StepProgressOnProgressChanged;
void StepProgressOnProgressChanged(object? sender, float e) => ((IProgress<float>) _progress).Report(60f + e * 0.4f);
void StepProgressOnProgressChanged(object? sender, StreamProgress e) => ((IProgress<float>) _progress).Report(60f + e.ProgressPercentage * 0.4f);
Status = "Extracting...";
// Ensure the directory is empty

View File

@ -0,0 +1,28 @@
using Artemis.Web.Workshop.Entities;
namespace Artemis.WebClient.Workshop.DownloadHandlers;
public class EntryInstallResult<T>
{
public bool IsSuccess { get; set; }
public string? Message { get; set; }
public T? Result { get; set; }
public static EntryInstallResult<T> FromFailure(string? message)
{
return new EntryInstallResult<T>
{
IsSuccess = false,
Message = message
};
}
public static EntryInstallResult<T> FromSuccess(T installationResult)
{
return new EntryInstallResult<T>
{
IsSuccess = true,
Result = installationResult
};
}
}

View File

@ -0,0 +1,7 @@
using Artemis.UI.Shared.Utilities;
namespace Artemis.WebClient.Workshop.DownloadHandlers;
public interface IEntryDownloadHandler
{
}

View File

@ -0,0 +1,51 @@
using System.IO.Compression;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared.Extensions;
using Artemis.UI.Shared.Utilities;
using Newtonsoft.Json;
namespace Artemis.WebClient.Workshop.DownloadHandlers.Implementations;
public class ProfileEntryDownloadHandler : IEntryDownloadHandler
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IProfileService _profileService;
public ProfileEntryDownloadHandler(IHttpClientFactory httpClientFactory, IProfileService profileService)
{
_httpClientFactory = httpClientFactory;
_profileService = profileService;
}
public async Task<EntryInstallResult<ProfileConfiguration>> InstallProfileAsync(Guid releaseId, Progress<StreamProgress> progress, CancellationToken cancellationToken)
{
try
{
HttpClient client = _httpClientFactory.CreateClient(WorkshopConstants.WORKSHOP_CLIENT_NAME);
using MemoryStream stream = new();
await client.DownloadDataAsync($"releases/download/{releaseId}", stream, progress, cancellationToken);
using ZipArchive zipArchive = new(stream, ZipArchiveMode.Read);
List<ZipArchiveEntry> profiles = zipArchive.Entries.Where(e => e.Name.EndsWith("json", StringComparison.InvariantCultureIgnoreCase)).ToList();
ZipArchiveEntry userProfileEntry = profiles.First();
ProfileConfigurationExportModel profile = await GetProfile(userProfileEntry);
ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == "Workshop") ?? _profileService.CreateProfileCategory("Workshop", true);
ProfileConfiguration profileConfiguration = _profileService.ImportProfile(category, profile, true, true, null);
return EntryInstallResult<ProfileConfiguration>.FromSuccess(profileConfiguration);
}
catch (Exception e)
{
return EntryInstallResult<ProfileConfiguration>.FromFailure(e.Message);
}
}
private async Task<ProfileConfigurationExportModel> GetProfile(ZipArchiveEntry userProfileEntry)
{
await using Stream stream = userProfileEntry.Open();
using StreamReader reader = new(stream);
return JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(await reader.ReadToEndAsync(), IProfileService.ExportSettings)!;
}
}

View File

@ -1,4 +1,5 @@
using System.Reflection;
using Artemis.WebClient.Workshop.DownloadHandlers;
using Artemis.WebClient.Workshop.Extensions;
using Artemis.WebClient.Workshop.Repositories;
using Artemis.WebClient.Workshop.Services;
@ -48,5 +49,6 @@ public static class ContainerExtensions
container.Register<EntryUploadHandlerFactory>(Reuse.Transient);
container.RegisterMany(workshopAssembly, type => type.IsAssignableTo<IEntryUploadHandler>(), Reuse.Transient);
container.RegisterMany(workshopAssembly, type => type.IsAssignableTo<IEntryDownloadHandler>(), Reuse.Transient);
}
}

View File

@ -1,4 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services.MainWindow;
using Artemis.UI.Shared.Utilities;
using Artemis.WebClient.Workshop.UploadHandlers;
@ -11,11 +13,13 @@ public class WorkshopService : IWorkshopService
{
private readonly Dictionary<Guid, Stream> _entryIconCache = new();
private readonly IHttpClientFactory _httpClientFactory;
private readonly IRouter _router;
private readonly SemaphoreSlim _iconCacheLock = new(1);
public WorkshopService(IHttpClientFactory httpClientFactory, IMainWindowService mainWindowService)
public WorkshopService(IHttpClientFactory httpClientFactory, IMainWindowService mainWindowService, IRouter router)
{
_httpClientFactory = httpClientFactory;
_router = router;
mainWindowService.MainWindowClosed += (_, _) => Dispatcher.UIThread.InvokeAsync(async () =>
{
await Task.Delay(1000);
@ -43,6 +47,31 @@ public class WorkshopService : IWorkshopService
return ImageUploadResult.FromSuccess();
}
/// <inheritdoc />
public async Task<IWorkshopService.WorkshopStatus> GetWorkshopStatus()
{
try
{
// Don't use the workshop client which adds auth headers
HttpClient client = _httpClientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, WorkshopConstants.WORKSHOP_URL + "/status"));
return new IWorkshopService.WorkshopStatus(response.IsSuccessStatusCode, response.StatusCode.ToString());
}
catch (HttpRequestException e)
{
return new IWorkshopService.WorkshopStatus(false, e.Message);
}
}
/// <inheritdoc />
public async Task<bool> ValidateWorkshopStatus()
{
IWorkshopService.WorkshopStatus status = await GetWorkshopStatus();
if (!status.IsReachable)
await _router.Navigate($"workshop/offline/{status.Message}");
return status.IsReachable;
}
private void ClearCache()
{
try
@ -63,4 +92,8 @@ public class WorkshopService : IWorkshopService
public interface IWorkshopService
{
Task<ImageUploadResult> SetEntryIcon(Guid entryId, Progress<StreamProgress> progress, Stream icon, CancellationToken cancellationToken);
Task<WorkshopStatus> GetWorkshopStatus();
Task<bool> ValidateWorkshopStatus();
public record WorkshopStatus(bool IsReachable, string Message);
}