diff --git a/Sources/MyFirstBlazor/Components/Card.razor b/Sources/MyFirstBlazor/Components/Card.razor new file mode 100644 index 0000000..422077c --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Card.razor @@ -0,0 +1,6 @@ +@typeparam TItem +
+ @CardHeader(Item) + @CardBody(Item) + @CardFooter +
\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Components/Card.razor.cs b/Sources/MyFirstBlazor/Components/Card.razor.cs new file mode 100644 index 0000000..cc92a91 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Card.razor.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Components; + +namespace MyFirstBlazor.Components +{ + public partial class Card + { + [Parameter] + public RenderFragment CardBody { get; set; } + + [Parameter] + public RenderFragment CardFooter { get; set; } + + [Parameter] + public RenderFragment CardHeader { get; set; } + + [Parameter] + public TItem Item { get; set; } + } +} diff --git a/Sources/MyFirstBlazor/Components/Crafting.razor b/Sources/MyFirstBlazor/Components/Crafting.razor new file mode 100644 index 0000000..8650497 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Crafting.razor @@ -0,0 +1,51 @@ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Components/Crafting.razor.cs b/Sources/MyFirstBlazor/Components/Crafting.razor.cs new file mode 100644 index 0000000..59057e8 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Crafting.razor.cs @@ -0,0 +1,81 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using MyFirstBlazor.Models; +using System.Collections.ObjectModel; + +using System.Collections.Specialized; + +namespace MyFirstBlazor.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} diff --git a/Sources/MyFirstBlazor/Components/Crafting.razor.css b/Sources/MyFirstBlazor/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} diff --git a/Sources/MyFirstBlazor/Components/Crafting.razor.js b/Sources/MyFirstBlazor/Components/Crafting.razor.js new file mode 100644 index 0000000..8fdb58e --- /dev/null +++ b/Sources/MyFirstBlazor/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/Sources/MyFirstBlazor/Components/CraftingAction.cs b/Sources/MyFirstBlazor/Components/CraftingAction.cs new file mode 100644 index 0000000..dfb6249 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using MyFirstBlazor.Models; + +namespace MyFirstBlazor.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/Sources/MyFirstBlazor/Components/CraftingItem.razor b/Sources/MyFirstBlazor/Components/CraftingItem.razor new file mode 100644 index 0000000..e64e6cd --- /dev/null +++ b/Sources/MyFirstBlazor/Components/CraftingItem.razor @@ -0,0 +1,13 @@ +
+ + @if (Item != null) + { + @Item.DisplayName + } +
\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Components/CraftingItem.razor.cs b/Sources/MyFirstBlazor/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..1e86277 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/CraftingItem.razor.cs @@ -0,0 +1,66 @@ +using Blazorise; +using Microsoft.AspNetCore.Components; +using MyFirstBlazor.Models; +namespace MyFirstBlazor.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + + + } + +} diff --git a/Sources/MyFirstBlazor/Components/CraftingItem.razor.css b/Sources/MyFirstBlazor/Components/CraftingItem.razor.css new file mode 100644 index 0000000..b2d4521 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/Sources/MyFirstBlazor/Components/CraftingRecipe.cs b/Sources/MyFirstBlazor/Components/CraftingRecipe.cs new file mode 100644 index 0000000..942deaa --- /dev/null +++ b/Sources/MyFirstBlazor/Components/CraftingRecipe.cs @@ -0,0 +1,9 @@ +using MyFirstBlazor.Models; +namespace MyFirstBlazor.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/Sources/MyFirstBlazor/Components/ShowItems.razor b/Sources/MyFirstBlazor/Components/ShowItems.razor new file mode 100644 index 0000000..7739061 --- /dev/null +++ b/Sources/MyFirstBlazor/Components/ShowItems.razor @@ -0,0 +1,12 @@ +@typeparam TItem + +
+ @if ((Items?.Count ?? 0) != 0) + { + @foreach (var item in Items) + { + @ShowTemplate(item) + ; + } + } +
\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Components/ShowItems.razor.cs b/Sources/MyFirstBlazor/Components/ShowItems.razor.cs new file mode 100644 index 0000000..2a4344b --- /dev/null +++ b/Sources/MyFirstBlazor/Components/ShowItems.razor.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Components; + +namespace MyFirstBlazor.Components +{ + public partial class ShowItems + { + [Parameter] + public List Items { get; set; } + + [Parameter] + public RenderFragment ShowTemplate { get; set; } + } +} diff --git a/Sources/MyFirstBlazor/Models/Cake.cs b/Sources/MyFirstBlazor/Models/Cake.cs new file mode 100644 index 0000000..7029a9d --- /dev/null +++ b/Sources/MyFirstBlazor/Models/Cake.cs @@ -0,0 +1,9 @@ +namespace MyFirstBlazor.Models +{ + public class Cake + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Cost { get; set; } + } +} diff --git a/Sources/MyFirstBlazor/Models/Item.cs b/Sources/MyFirstBlazor/Models/Item.cs index 731bbc3..76a5001 100644 --- a/Sources/MyFirstBlazor/Models/Item.cs +++ b/Sources/MyFirstBlazor/Models/Item.cs @@ -11,5 +11,6 @@ public List RepairWith { get; set; } public DateTime CreatedDate { get; set; } public DateTime? UpdatedDate { get; set; } + public string ImageBase64 { get; set; } } } diff --git a/Sources/MyFirstBlazor/Models/ItemModel.cs b/Sources/MyFirstBlazor/Models/ItemModel.cs index 0db9927..a50d3ca 100644 --- a/Sources/MyFirstBlazor/Models/ItemModel.cs +++ b/Sources/MyFirstBlazor/Models/ItemModel.cs @@ -33,5 +33,8 @@ namespace MyFirstBlazor.Models [Required(ErrorMessage = "The image of the item is mandatory!")] public byte[] ImageContent { get; set; } + public string ImageBase64 { get; set; } + } + } diff --git a/Sources/MyFirstBlazor/Pages/Index.razor b/Sources/MyFirstBlazor/Pages/Index.razor index 0326fd3..f090ae3 100644 --- a/Sources/MyFirstBlazor/Pages/Index.razor +++ b/Sources/MyFirstBlazor/Pages/Index.razor @@ -1,5 +1,7 @@ @page "/" @using System.Globalization +@using MyFirstBlazor.Components; +@using MyFirstBlazor.Models; Index @@ -15,4 +17,8 @@ Welcome to your new app.
- + +
+ +
+ diff --git a/Sources/MyFirstBlazor/Pages/Index.razor.cs b/Sources/MyFirstBlazor/Pages/Index.razor.cs new file mode 100644 index 0000000..d6dc29e --- /dev/null +++ b/Sources/MyFirstBlazor/Pages/Index.razor.cs @@ -0,0 +1,53 @@ +using Microsoft.AspNetCore.Components; +using MyFirstBlazor.Components; +using MyFirstBlazor.Models; +using MyFirstBlazor.Services; + +namespace MyFirstBlazor.Pages +{ + public partial class Index + { + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + + } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + if(firstRender) StateHasChanged(); + await base.OnAfterRenderAsync(firstRender); + } + public List Cakes { get; set; } + + //protected override Task OnAfterRenderAsync(bool firstRender) + //{ + // LoadCakes(); + // StateHasChanged(); + // return base.OnAfterRenderAsync(firstRender); + //} + + public void LoadCakes() + { + Cakes = new List + { + // items hidden for display purpose + new Cake + { + Id = 1, + Name = "Red Velvet", + Cost = 60 + }, + }; + } + } +} diff --git a/Sources/MyFirstBlazor/Pages/_Layout.cshtml b/Sources/MyFirstBlazor/Pages/_Layout.cshtml index 31eca11..a7a5670 100644 --- a/Sources/MyFirstBlazor/Pages/_Layout.cshtml +++ b/Sources/MyFirstBlazor/Pages/_Layout.cshtml @@ -28,6 +28,7 @@ + diff --git a/Sources/MyFirstBlazor/Services/DataLocalService.cs b/Sources/MyFirstBlazor/Services/DataLocalService.cs index b378ae5..0f27546 100644 --- a/Sources/MyFirstBlazor/Services/DataLocalService.cs +++ b/Sources/MyFirstBlazor/Services/DataLocalService.cs @@ -1,5 +1,6 @@ using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; +using MyFirstBlazor.Components; using MyFirstBlazor.Factories; using MyFirstBlazor.Models; @@ -26,14 +27,6 @@ namespace MyFirstBlazor.Services public async Task Add(ItemModel model) { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); - - // Simulate the Id - model.Id = currentData.Max(s => s.Id) + 1; - - // Add the item to the current data - currentData.Add(ItemFactory.Create(model)); // Save the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); @@ -50,10 +43,6 @@ namespace MyFirstBlazor.Services // Write the file content await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); - - - // Save the data - await _localStorage.SetItemAsync("data", currentData); } public async Task Count() @@ -95,17 +84,9 @@ namespace MyFirstBlazor.Services public async Task Update(int id, ItemModel model) { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); + - // Get the item int the list - var item = currentData.FirstOrDefault(w => w.Id == id); - - // Check if item exist - if (item == null) - { - throw new Exception($"Unable to found the item with ID: {id}"); - } + // Save the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); @@ -115,7 +96,6 @@ namespace MyFirstBlazor.Services { imagePathInfo.Create(); } - // Delete the previous image if (item.Name != model.Name) { @@ -133,11 +113,8 @@ namespace MyFirstBlazor.Services // Write the file content await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); - // Modify the content of the item - ItemFactory.Update(item, model); + - // Save the data - await _localStorage.SetItemAsync("data", currentData); } public async Task Delete(int id) { @@ -162,5 +139,23 @@ namespace MyFirstBlazor.Services // Save the data await _localStorage.SetItemAsync("data", currentData); } + public Task> GetRecipes() + { + var items = new List + { + new CraftingRecipe + { + Give = new Item { DisplayName = "Diamond", Name = "diamond" }, + Have = new List> + { + new List { "dirt", "dirt", "dirt" }, + new List { "dirt", null, "dirt" }, + new List { "dirt", "dirt", "dirt" } + } + } + }; + + return Task.FromResult(items); + } } } diff --git a/Sources/MyFirstBlazor/Services/IDataService.cs b/Sources/MyFirstBlazor/Services/IDataService.cs index 8bcd2a7..320b00e 100644 --- a/Sources/MyFirstBlazor/Services/IDataService.cs +++ b/Sources/MyFirstBlazor/Services/IDataService.cs @@ -1,4 +1,5 @@ -using MyFirstBlazor.Models; +using MyFirstBlazor.Components; +using MyFirstBlazor.Models; namespace MyFirstBlazor.Services { @@ -13,5 +14,6 @@ namespace MyFirstBlazor.Services Task Update(int id, ItemModel model); Task Delete(int id); + Task> GetRecipes(); } }