Skip to content

Commit

Permalink
Reduce redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Nov 16, 2023
1 parent 7995f77 commit 094fb0d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 44 deletions.
5 changes: 3 additions & 2 deletions Pkmds.Web/Components/BoxGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

<BoxSlotComponent BoxNumber="@BoxNumber"
SlotNumber="slotNum"
Pokemon="@pkm" />

Pokemon="@pkm"
OnSlotClick="@(() => SetSelectedPokemon(pkm, BoxNumber, slotNum))"
GetStyleFunction="@(() => GetStyle(slotNum))" />
}
</div>
}
10 changes: 9 additions & 1 deletion Pkmds.Web/Components/BoxGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ public partial class BoxGrid : IDisposable
[Parameter, EditorRequired]
public int BoxNumber { get; set; }

private string BoxGridClass => AppState.SaveFile?.BoxSlotCount == 20 ? "box-grid-20" : "box-grid-30";
private string BoxGridClass =>
AppState.SaveFile?.BoxSlotCount == 20 ? "box-grid-20" : "box-grid-30";

private void SetSelectedPokemon(PKM? pokemon, int boxNumber, int slotNumber) =>
AppService.SetSelectedBoxPokemon(pokemon, boxNumber, slotNumber);

private string GetStyle(int slotNumber) => AppState.SelectedBoxSlotNumber == slotNumber
? "border: 4px solid orange; border-radius: 6px;"
: string.Empty;

protected override void OnInitialized() =>
RefreshService.OnAppStateChanged += StateHasChanged;
Expand Down
21 changes: 1 addition & 20 deletions Pkmds.Web/Components/BoxSlotComponent.razor.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
namespace Pkmds.Web.Components;

public partial class BoxSlotComponent : IDisposable
public class BoxSlotComponent : PokemonSlotComponent
{
[Parameter, EditorRequired]
public int BoxNumber { get; set; }

[Parameter, EditorRequired]
public int SlotNumber { get; set; }

[Parameter, EditorRequired]
public PKM? Pokemon { get; set; }

private string Style => AppState.SelectedBoxSlotNumber == SlotNumber
? "border: 4px solid orange; border-radius: 6px;"
: string.Empty;

protected override void OnInitialized() =>
RefreshService.OnAppStateChanged += StateHasChanged;

public void Dispose() =>
RefreshService.OnAppStateChanged -= StateHasChanged;

private void SetSelectedPokemon() =>
AppService.SetSelectedBoxPokemon(Pokemon, BoxNumber, SlotNumber);
}
6 changes: 4 additions & 2 deletions Pkmds.Web/Components/PartyGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
var slotNum = i;
PKM? pkm = i < AppState.SaveFile.PartyCount ? AppState.SaveFile.PartyData[slotNum] : null;
<MudItem xs="2">
<PartySlotComponent Pokemon="@pkm"
SlotNumber="@slotNum" />
<PokemonSlotComponent Pokemon="@pkm"
SlotNumber="@slotNum"
OnSlotClick="@(() => SetSelectedPokemon(pkm, slotNum))"
GetStyleFunction="@(() => GetStyle(slotNum))" />
</MudItem>
}
</MudGrid>
Expand Down
7 changes: 7 additions & 0 deletions Pkmds.Web/Components/PartyGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ namespace Pkmds.Web.Components;

public partial class PartyGrid
{
private void SetSelectedPokemon(PKM? pokemon, int slotNumber) =>
AppService.SetSelectedPartyPokemon(pokemon, slotNumber);

private string GetStyle(int slotNumber) => AppState.SelectedPartySlotNumber == slotNumber
? "border: 4px solid orange; border-radius: 6px;"
: string.Empty;

protected override void OnInitialized()
{
RefreshService.OnAppStateChanged += StateHasChanged;
Expand Down
10 changes: 0 additions & 10 deletions Pkmds.Web/Components/PartySlotComponent.razor

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@inherits BasePkmdsComponent
@implements IDisposable

<MudPaper Elevation="3"
@onclick="SetSelectedPokemon"
@onclick="HandleClick"
Width="68px"
Height="68px"
Class="@SpriteHelper.GetSpriteCssClass(Pokemon)"
Style="@Style">
Style="@GetStyle()">
<img class="pkm-sprite" src="@(Pokemon is { Species: > 0 } ? SpriteHelper.GetPokemonSpriteFilename(Pokemon) : null)" />
</MudPaper>
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
namespace Pkmds.Web.Components;

public partial class PartySlotComponent : IDisposable
public partial class PokemonSlotComponent
{
[Parameter, EditorRequired]
public int SlotNumber { get; set; }

[Parameter, EditorRequired]
public PKM? Pokemon { get; set; }

private string Style => AppState.SelectedPartySlotNumber == SlotNumber
? "border: 4px solid orange; border-radius: 6px;"
: string.Empty;
[Parameter, EditorRequired]
public EventCallback OnSlotClick { get; set; }

[Parameter, EditorRequired]
public Func<string>? GetStyleFunction { get; set; }

private async Task HandleClick() =>
await OnSlotClick.InvokeAsync();

private string GetStyle() =>
GetStyleFunction?.Invoke() ?? string.Empty;

protected override void OnInitialized() =>
RefreshService.OnAppStateChanged += StateHasChanged;

public void Dispose() =>
RefreshService.OnAppStateChanged -= StateHasChanged;

private void SetSelectedPokemon() =>
AppService.SetSelectedPartyPokemon(Pokemon, SlotNumber);
}

0 comments on commit 094fb0d

Please sign in to comment.