Skip to content

Commit

Permalink
Merge pull request #2391 from erri120/remove-imagecache
Browse files Browse the repository at this point in the history
Remove `IImageCache`
  • Loading branch information
erri120 authored Dec 17, 2024
2 parents 23b3589 + bb84602 commit 16e75a2
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 293 deletions.
2 changes: 1 addition & 1 deletion src/Games/NexusMods.Games.FOMOD.UI/GuidedInstallerUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static void SetupStep(
Percent progress)
{
var viewModel = window.ViewModel!;
viewModel.ActiveStepViewModel ??= currentScope.ServiceProvider.GetRequiredService<IGuidedInstallerStepViewModel>();
viewModel.ActiveStepViewModel ??= new GuidedInstallerStepViewModel(currentScope.ServiceProvider);

var activeStepViewModel = viewModel.ActiveStepViewModel;
activeStepViewModel.ModName = viewModel.WindowName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class GuidedInstallerStepViewModel : AViewModel<IGuidedInstallerStepViewM

[Reactive] public IGuidedInstallerOptionViewModel? HighlightedOptionViewModel { get; set; }

private IDisposable? _imageDisposable;
private readonly ObservableAsPropertyHelper<IImage?> _highlightedOptionImage;
public IImage? HighlightedOptionImage => _highlightedOptionImage.Value;

Expand All @@ -41,8 +42,11 @@ public class GuidedInstallerStepViewModel : AViewModel<IGuidedInstallerStepViewM

private Percent _previousProgress = Percent.Zero;

public GuidedInstallerStepViewModel(IImageCache imageCache)
public GuidedInstallerStepViewModel(IServiceProvider serviceProvider)
{
var remoteImagePipeline = ImagePipelines.GetGuidedInstallerRemoteImagePipeline(serviceProvider);
var fileImagePipeline = ImagePipelines.GetGuidedInstallerFileImagePipeline(serviceProvider);

_groupsSource
.Connect()
.Bind(out _groups)
Expand All @@ -54,21 +58,37 @@ public GuidedInstallerStepViewModel(IImageCache imageCache)
.WhereNotNull()
.Select(optionVM => optionVM.Option.Image)
.WhereNotNull()
.Select(optionImage =>
.OffUi()
.SelectMany(async optionImage =>
{
return optionImage.Match(
f0: uri => new ImageIdentifier(uri),
f1: imageStoredFile => new ImageIdentifier(imageStoredFile.FileHash)
);
try
{
return await optionImage.Match(
f0: uri => remoteImagePipeline.LoadResourceAsync(uri, CancellationToken.None),
f1: imageHash => fileImagePipeline.LoadResourceAsync(imageHash.FileHash, CancellationToken.None)
);
}
catch (Exception)
{
return null;
}
})
.OffUi()
.SelectMany(imageCache.GetImage)
.Select(static resource => resource?.Data)
.Do(lifetime =>
{
_imageDisposable?.Dispose();
_imageDisposable = lifetime;
})
.Select(static lifetime => lifetime?.Value)
.WhereNotNull()
.OnUI()
.ToProperty(this, vm => vm.HighlightedOptionImage);

var goToNextCommand = ReactiveCommand.Create(() =>
{
_imageDisposable?.Dispose();
_imageDisposable = null;

// NOTE(erri120): On the last step, we don't set the result but instead show a "installation complete"-screen.
if (InstallationStep!.HasNextStep || ShowInstallationCompleteScreen)
{
Expand All @@ -85,6 +105,9 @@ public GuidedInstallerStepViewModel(IImageCache imageCache)

var goToPrevCommand = ReactiveCommand.Create(() =>
{
_imageDisposable?.Dispose();
_imageDisposable = null;

if (ShowInstallationCompleteScreen)
{
ShowInstallationCompleteScreen = false;
Expand Down Expand Up @@ -180,7 +203,11 @@ public GuidedInstallerStepViewModel(IImageCache imageCache)
.BindToVM(this, vm => vm.FooterStepperViewModel.Progress)
.DisposeWith(disposables);

Disposable.Create(() => _highlightedOptionImage.Dispose()).DisposeWith(disposables);
Disposable.Create(() =>
{
_imageDisposable?.Dispose();
_highlightedOptionImage.Dispose();
}).DisposeWith(disposables);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using JetBrains.Annotations;
using Markdown.Avalonia.Plugins;
using Markdown.Avalonia.Utils;
using Microsoft.Extensions.Logging;
using NexusMods.Abstractions.Resources;
using NexusMods.Abstractions.UI;
using NexusMods.App.UI.Extensions;
using NexusMods.CrossPlatform.Process;
using NexusMods.Hashing.xxHash3;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

Expand All @@ -19,8 +21,8 @@ namespace NexusMods.App.UI.Controls.MarkdownRenderer;
public class MarkdownRendererViewModel : AViewModel<IMarkdownRendererViewModel>, IMarkdownRendererViewModel
{
private readonly ILogger _logger;
private readonly IImageCache _imageCache;
private readonly HttpClient _httpClient;
private readonly IResourceLoader<Uri, Bitmap> _remoteImagePipeline;

[Reactive] public string Contents { get; set; } = string.Empty;
[Reactive] public Uri? MarkdownUri { get; set; }
Expand All @@ -31,14 +33,14 @@ public class MarkdownRendererViewModel : AViewModel<IMarkdownRendererViewModel>,
public ReactiveCommand<string, Unit> OpenLinkCommand { get; }

public MarkdownRendererViewModel(
IServiceProvider serviceProvider,
ILogger<MarkdownRendererViewModel> logger,
IOSInterop osInterop,
IImageCache imageCache,
HttpClient httpClient)
{
_logger = logger;
_imageCache = imageCache;
_httpClient = httpClient;
_remoteImagePipeline = ImagePipelines.GetMarkdownRendererRemoteImagePipeline(serviceProvider);

PathResolver = new PathResolverImpl(this);
ImageResolverPlugin = new ImageResolvePluginImpl(new ImageResolverImpl(this));
Expand Down Expand Up @@ -120,14 +122,11 @@ private async Task<string> FetchMarkdown(Uri uri, CancellationToken cancellation
return await FetchRemoteImage(uri, cancellationToken);
}

private async Task<Stream?> FetchRemoteImage(Uri uri, CancellationToken cancellationToken = default)
private Task<Stream?> FetchRemoteImage(Uri uri, CancellationToken cancellationToken = default)
{
var hash = await _imageCache.Prefetch(new ImageIdentifier(uri), cancellationToken);
var hashValue = hash.Value;

var bytes = BitConverter.GetBytes(hashValue);
var bytes = Encoding.UTF8.GetBytes(uri.ToString());
var ms = new MemoryStream(bytes, writable: false);
return ms;
return Task.FromResult<Stream?>(ms);
}

private class ImageResolvePluginImpl : IMdAvPlugin
Expand Down Expand Up @@ -191,14 +190,12 @@ public ImageResolverImpl(MarkdownRendererViewModel parent)

public async Task<IImage?> Load(Stream stream)
{
var bytes = GC.AllocateUninitializedArray<byte>(sizeof(ulong));
stream.ReadExactly(bytes);

var hashValue = BitConverter.ToUInt64(bytes);
var hash = Hash.FromULong(hashValue);
using var sr = new StreamReader(stream, Encoding.UTF8);
var url = await sr.ReadToEndAsync();
var uri = new Uri(url, UriKind.Absolute);

var image = await _parent._imageCache.GetImage(new ImageIdentifier(hash), CancellationToken.None);
return image;
var resource = await _parent._remoteImagePipeline.LoadResourceAsync(uri, CancellationToken.None);
return resource.Data;
}
}
}
34 changes: 0 additions & 34 deletions src/NexusMods.App.UI/IImageCache.cs

This file was deleted.

143 changes: 0 additions & 143 deletions src/NexusMods.App.UI/ImageCache.cs

This file was deleted.

Loading

0 comments on commit 16e75a2

Please sign in to comment.