Skip to content

Commit

Permalink
Move image converting logic to converter in presentation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftTwixWand committed Nov 27, 2023
1 parent c25231c commit 8e62e8e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using BuildingBlocks.Application.CQRS.Queries;
using Inventory.Application.Helpers;
using Inventory.Application.Models;
using Inventory.Domain.Products;

Expand Down Expand Up @@ -36,7 +35,7 @@ public GetProductByIdQueryHandler(IProductsRepository productsRepository)
Category = product.Category,
Description = product.Description,
Price = productPrice,
ImageSource = await ImageConverter.GetBitmapAsync(product.Image),
ImageSource = product.Image,
};

return productModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Inventory.Application.Helpers;
using Inventory.Application.Models;
using Inventory.Domain.Products;
using MediatR;
Expand All @@ -26,7 +25,7 @@ public async IAsyncEnumerable<ProductModel> Handle(GetProductsStreamQuery query,
Id = product.Id.Value,
Name = product.Name,
Category = product.Category,
ImageSource = await ImageConverter.GetBitmapAsync(product.Image),
ImageSource = product.Image,
};
}
}
Expand Down
54 changes: 0 additions & 54 deletions src/Inventory.Application/Helpers/ImageConverter.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/Inventory.Application/Models/ProductModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Media;

namespace Inventory.Application.Models;

Expand All @@ -19,7 +18,7 @@ public sealed partial class ProductModel : ObservableObject
private string? description;

[ObservableProperty]
private ImageSource? imageSource;
private byte[]? imageSource;

[ObservableProperty]
private decimal price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<local:EnumToBooleanConverter x:Key="EnumToBooleanConverter"/>
<local:SelectionModeToBooleanConverter x:Key="SelectionModeToBooleanConverter"/>
<local:ImageConverter x:Key="ImageConverter"/>

<local:ValueConverterGroup x:Key="InvertBoolToVisibilityConverter">
<communityToolkit:BoolNegationConverter />
Expand Down
28 changes: 28 additions & 0 deletions src/Inventory.Presentation/Converters/ImageConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.IO;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media.Imaging;

namespace Inventory.Presentation.Converters;

internal sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is byte[] byteArray && byteArray.Length > 0)
{
using var stream = new MemoryStream(byteArray);
var bitmapImage = new BitmapImage();
stream.Position = 0;
bitmapImage.SetSource(stream.AsRandomAccessStream());
return bitmapImage;
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<Image
Width="160"
Height="160"
Source="{x:Bind ImageSource, Mode=OneWay}"
Source="{x:Bind ImageSource, Mode=OneWay, Converter={StaticResource ImageConverter}}"
Stretch="Fill" />
</Border>

Expand Down

0 comments on commit 8e62e8e

Please sign in to comment.