Skip to content

Commit

Permalink
Improved INavigatedTo interface to be asynchronus #25 #29
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftTwixWand committed Dec 21, 2022
1 parent e5286e4 commit 0b8b9d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/Inventory.Application/Services/Navigation/INavigatedTo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Inventory.Application.Services.Navigation;
using System.Threading.Tasks;

namespace Inventory.Application.Services.Navigation;

public interface INavigatedTo<TParameter>
where TParameter : notnull
{
void OnNavigatedTo(TParameter parameter);
Task OnNavigatedTo(TParameter parameter);
}

public interface INavigatedTo : INavigatedTo<object>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BuildingBlocks.Application.ViewModels;
using System.Threading.Tasks;
using BuildingBlocks.Application.ViewModels;
using CommunityToolkit.Mvvm.Messaging;
using Inventory.Application.DomainOperations.Product.GetProductById;
using Inventory.Application.Models;
Expand All @@ -17,9 +18,9 @@ public ProductViewModel(IMediator mediator, IMessenger messenger)
_mediator = mediator;
}

public async void OnNavigatedTo(int productId)
public async Task OnNavigatedTo(int productId)
{
var product = await _mediator.Send(new GetProductByIdQuery(productId));
ProductModel? product = await _mediator.Send(new GetProductByIdQuery(productId));
Item = product ?? new ProductModel() { Name = "Product doesn't exist: Default product name" };
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Inventory.Domain.Products;
using System.Runtime.CompilerServices;
using Inventory.Domain.Products;
using Inventory.Persistence.Database.Properties;
using System.Runtime.CompilerServices;

namespace Inventory.Persistence.Database.Domain.Products;

Expand All @@ -9,31 +9,31 @@ internal sealed class ProductsRepository : IProductsRepository
private readonly List<Product> _products = new(
new[]
{
Product.Create("Gray Mini Lamp", "Desktop Lamp", "", Resources.Lamp1),
Product.Create("Modern LED Lamp", "Desktop Lamp", "", Resources.Lamp2),
Product.Create("15\" Table Lamp", "Desktop Lamp", "", Resources.Lamp3),
Product.Create("Matte Balck Lamp", "Floor Lamp", "", Resources.Lamp6),
Product.Create("Geometric Lamp", "Desktop Lamp", "", Resources.Lamp7),
Product.Create("Gray 20\" Lamp", "Desktop Lamp", "", Resources.Lamp8),
Product.Create("70\" Shared Lamp", "Floor Lamp", "", Resources.Lamp4),
Product.Create("Rechargeable Lamp", "Outdoor Lamp", "", Resources.Lamp5),
Product.Create("Gray Mini Lamp", "Desktop Lamp", string.Empty, Resources.Lamp1),
Product.Create("Modern LED Lamp", "Desktop Lamp", string.Empty, Resources.Lamp2),
Product.Create("15\" Table Lamp", "Desktop Lamp", string.Empty, Resources.Lamp3),
Product.Create("Matte Balck Lamp", "Floor Lamp", string.Empty, Resources.Lamp6),
Product.Create("Geometric Lamp", "Desktop Lamp", string.Empty, Resources.Lamp7),
Product.Create("Gray 20\" Lamp", "Desktop Lamp", string.Empty, Resources.Lamp8),
Product.Create("70\" Shared Lamp", "Floor Lamp", string.Empty, Resources.Lamp4),
Product.Create("Rechargeable Lamp", "Outdoor Lamp", string.Empty, Resources.Lamp5),
});

public async IAsyncEnumerable<Product> GetAllAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
await Task.CompletedTask;
for (int i = 0; i < 10; i++)
{
foreach (var product in _products)
foreach (Product product in _products)
{
await Task.Delay(100, cancellationToken);
yield return product;
}
}
}

public Task<Product?> GetByIdAsync(ProductId id)
{
var product = _products.FirstOrDefault(product => product.Id == id);
Product? product = _products.FirstOrDefault(product => product.Id == id);
return Task.FromResult(product);
}
}

0 comments on commit 0b8b9d3

Please sign in to comment.