Skip to content

Commit

Permalink
Code cleanup, renaming, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Feb 26, 2025
1 parent bb3a915 commit ec9ecfe
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Pkmds.Web/Components/EditForms/PokemonEditForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Class="my-2"
StartIcon="@Icons.Material.Filled.Delete"
title="Delete Pokémon"
Disabled="@(Pokemon.Species == (ushort)Species.None)">
Disabled="@(Pokemon.Species.IsInvalidSpecies())">
Delete
</MudButton>

Expand All @@ -34,7 +34,7 @@
Color="@Color.Primary"
Size="@Size.Small"
title="Save Pokémon"
Disabled="@(Pokemon.Species == (ushort)Species.None)">
Disabled="@(Pokemon.Species.IsInvalidSpecies())">
Save
</MudButton>

Expand All @@ -46,7 +46,7 @@
Color="@Color.Default"
Size="@Size.Small"
title="Copy Pokémon"
Disabled="@(Pokemon.Species == (ushort)Species.None)">
Disabled="@(Pokemon.Species.IsInvalidSpecies())">
Copy
</MudButton>

Expand All @@ -70,7 +70,7 @@
Color="@Color.Default"
Size="@Size.Small"
title="Export Pokémon as Showdown"
Disabled="@(Pokemon.Species == (ushort)Species.None)">
Disabled="@(Pokemon.Species.IsInvalidSpecies())">
Showdown
</MudButton>

Expand Down
10 changes: 5 additions & 5 deletions Pkmds.Web/Components/EditForms/PokemonEditForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void OnDeleteConfirm(bool confirmed)
var selectedPokemonType = AppService.GetSelectedPokemonSlot(out var partySlot, out var boxNumber, out var boxSlot);
switch (selectedPokemonType)
{
case Services.AppService.SelectedPokemonType.Party:
case SelectedPokemonType.Party:
AppService.DeletePokemon(partySlot);
break;
case Services.AppService.SelectedPokemonType.Box:
case SelectedPokemonType.Box:
AppService.DeletePokemon(boxNumber, boxSlot);
break;
}
Expand All @@ -76,7 +76,7 @@ private void OnClickPaste()
return;
}

if (Pokemon is { Species: > (int)Species.None })
if ((Pokemon?.Species).IsValidSpecies())
{
ShowPasteConfirmation();
}
Expand Down Expand Up @@ -124,10 +124,10 @@ void PastePokemon()
var selectedPokemonType = AppService.GetSelectedPokemonSlot(out var partySlot, out var boxNumber, out var boxSlot);
switch (selectedPokemonType)
{
case Services.AppService.SelectedPokemonType.Party:
case SelectedPokemonType.Party:
AppService.SetSelectedPartyPokemon(Pokemon, partySlot);
break;
case Services.AppService.SelectedPokemonType.Box:
case SelectedPokemonType.Box:
AppService.SetSelectedBoxPokemon(Pokemon, boxNumber, boxSlot);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Pkmds.Web/Components/EditForms/Tabs/MainTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
SearchFunc="@SearchPokemonNames"
ToStringFunc="@(species => species?.Text)"/>

if (Pokemon != saveFile.BlankPKM && Pokemon.Species != (ushort)Species.None)
if (Pokemon != saveFile.BlankPKM && Pokemon.Species.IsValidSpecies())
{
@TypeSummary(Pokemon.GetGenerationTypes())
}
Expand Down
4 changes: 2 additions & 2 deletions Pkmds.Web/Components/Layout/MainLayout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ private async Task ExportSelectedPokemon()
AppState.ShowProgressIndicator = false;
}

private byte[] GetPokemonFileData(PKM? pokemon)
private byte[] GetPokemonFileData(PKM? pokemon)
{
if (pokemon is null)
if (pokemon is null)
{
return [];
}
Expand Down
2 changes: 0 additions & 2 deletions Pkmds.Web/Components/MainTabPages/BadgesComponent.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics.CodeAnalysis;

namespace Pkmds.Web.Components.MainTabPages;

public partial class BadgesComponent : IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ShowNextButton/>

<MudSelect T="int"
@bind-Value="pageSize"
@bind-Value="itemsPerPage"
@bind-Value:after="OnPageSizeChange"
Label="Items per page">
@foreach (var pageSize in pagesSizes)
Expand Down Expand Up @@ -70,7 +70,7 @@
Color="@Color.Default"
Size="@Size.Small"
title="Copy Pokémon"
Disabled="@(mysteryGift.Species == (ushort)Species.None)">
Disabled="@(mysteryGift.Species.IsInvalidSpecies())">
Copy
</MudButton>
<MudButton OnClick="@(() => OnClickImport(mysteryGift))"
Expand All @@ -80,7 +80,7 @@
Color="@Color.Default"
Size="@Size.Small"
title="Import Card"
Disabled="@(mysteryGift.Species == (ushort)Species.None)">
Disabled="@(mysteryGift.Species.IsInvalidSpecies())">
Import
</MudButton>
</MudCardActions>
Expand Down
25 changes: 15 additions & 10 deletions Pkmds.Web/Components/MainTabPages/MysteryGiftDatabaseTab.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public partial class MysteryGiftDatabaseTab

private List<MysteryGift> paginatedItems = [];
private int currentPage = 1;
private int pageSize = 20; // Number of items per page
private int itemsPerPage = 20; // Number of items per page
private readonly int[] pagesSizes = [10, 20, 50, 100];

private int TotalPages => (int)Math.Ceiling((double)mysteryGiftsList.Count / pageSize);
private int TotalPages => (int)Math.Ceiling((double)mysteryGiftsList.Count / itemsPerPage);

protected override void OnInitialized()
{
Expand All @@ -33,10 +33,10 @@ private void LoadData()
{
encounterDatabase = saveFile switch
{
SAV9SV sav9sv => encounterDatabase.Where(IsPresent(sav9sv.Personal)),
SAV8SWSH sav8swsh => encounterDatabase.Where(IsPresent(sav8swsh.Personal)),
SAV8BS sav8bs => encounterDatabase.Where(IsPresent(sav8bs.Personal)),
SAV8LA sav8la => encounterDatabase.Where(IsPresent(sav8la.Personal)),
SAV9SV sav9Sv => encounterDatabase.Where(IsPresent(sav9Sv.Personal)),
SAV8SWSH sav8Swsh => encounterDatabase.Where(IsPresent(sav8Swsh.Personal)),
SAV8BS sav8Bs => encounterDatabase.Where(IsPresent(sav8Bs.Personal)),
SAV8LA sav8La => encounterDatabase.Where(IsPresent(sav8La.Personal)),
SAV7b => encounterDatabase.Where(mysteryGift => mysteryGift is WB7),
SAV7 => encounterDatabase.Where(mysteryGift => mysteryGift.Generation < 7 || mysteryGift is WC7),
_ => encounterDatabase.Where(mysteryGift => mysteryGift.Generation <= saveFile.Generation),
Expand All @@ -56,9 +56,12 @@ static Func<MysteryGift, bool> IsPresent<TTable>(TTable personalTable) where TTa
mysteryGift => personalTable.IsPresentInGame(mysteryGift.Species, mysteryGift.Form);
}

private void UpdatePaginatedItems() => paginatedItems = [.. mysteryGiftsList
.Skip((currentPage - 1) * pageSize)
.Take(pageSize)];
private void UpdatePaginatedItems() => paginatedItems =
[
.. mysteryGiftsList
.Skip((currentPage - 1) * itemsPerPage)
.Take(itemsPerPage)
];

private void GoToPage() => UpdatePaginatedItems();

Expand Down Expand Up @@ -103,7 +106,9 @@ private async Task OnClickImport(MysteryGift mysteryGift)
}

await AppService.ImportMysteryGift(dataMysteryGift, out var isSuccessful, out var resultsMessage);
Snackbar.Add(resultsMessage, isSuccessful ? MudBlazor.Severity.Success : MudBlazor.Severity.Error);
Snackbar.Add(resultsMessage, isSuccessful
? MudBlazor.Severity.Success
: MudBlazor.Severity.Error);
}

private static string RenderListAsHtml(IReadOnlyList<string> items, string tag = "p")
Expand Down
4 changes: 1 addition & 3 deletions Pkmds.Web/Components/PokemonSlotComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
Height="68px"
Class="@SpriteHelper.GetSpriteCssClass(Pokemon)"
Style="@GetStyle()">
@{ var title = Pokemon is { Species: > 0 }
? AppService.GetPokemonSpeciesName(Pokemon.Species)
: "Unknown"; }
@{ var title = GetPokemonTitle(); }
<object class="pkm-sprite"
type="image/png"
style="object-fit: contain;"
Expand Down
4 changes: 4 additions & 0 deletions Pkmds.Web/Components/PokemonSlotComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ protected override void OnInitialized() =>

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

private string GetPokemonTitle() => Pokemon is { Species: > 0 }
? AppService.GetPokemonSpeciesName(Pokemon.Species)
: "Unknown";
}
29 changes: 16 additions & 13 deletions Pkmds.Web/Services/AppService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using PKHeX.Core;

namespace Pkmds.Web.Services;
namespace Pkmds.Web.Services;

public class AppService(IAppState appState, IRefreshService refreshService) : IAppService
{
Expand Down Expand Up @@ -159,6 +157,7 @@ public void SavePokemon(PKM? pokemon)
{
RefreshService.RefreshPartyState();
}

break;
case SelectedPokemonType.Box:
AppState.SaveFile.SetBoxSlotAtIndex(pokemon, boxNumber, boxSlot);
Expand Down Expand Up @@ -349,7 +348,7 @@ public Task ImportMysteryGift(DataMysteryGift gift, out bool isSuccessful, out s
var index = 0;

var lastUnfilled = GetLastUnfilledByType(gift, album);
if (lastUnfilled > -1 && lastUnfilled < index)
if (lastUnfilled > -1)
{
index = lastUnfilled;
}
Expand Down Expand Up @@ -407,13 +406,16 @@ static int GetLastUnfilledByType(DataMysteryGift gift, DataMysteryGift[] album)

return i;
}

return -1;
}

static DataMysteryGift[] LoadMysteryGifts(SaveFile saveFile, IMysteryGiftStorage cards)
{
var count = cards.GiftCountMax;
var size = saveFile is SAV4HGSS ? count + 1 : count;
var size = saveFile is SAV4HGSS
? count + 1
: count;
var result = new DataMysteryGift[size];
for (var i = 0; i < count; i++)
{
Expand All @@ -429,8 +431,8 @@ static DataMysteryGift[] LoadMysteryGifts(SaveFile saveFile, IMysteryGiftStorage
}

static IMysteryGiftStorage GetMysteryGiftProvider(SaveFile saveFile) => saveFile is IMysteryGiftStorageProvider provider
? provider.MysteryGiftStorage
: throw new ArgumentException("Save file does not support Mystery Gifts.", nameof(saveFile));
? provider.MysteryGiftStorage
: throw new ArgumentException("Save file does not support Mystery Gifts.", nameof(saveFile));

static void SetCardId(int cardId, IMysteryGiftFlags? flags, List<string> receivedFlags)
{
Expand Down Expand Up @@ -466,7 +468,7 @@ static void SaveReceivedFlags(IMysteryGiftFlags? flags, List<string> receivedFla
flags.ClearReceivedFlags();
foreach (var o in receivedFlags)
{
if (o?.ToString() is not { } x || !int.TryParse(x, out var index))
if (!int.TryParse(o, out var index))
{
continue;
}
Expand All @@ -487,6 +489,7 @@ static void SaveReceivedCards(SaveFile saveFile, IMysteryGiftStorage cards, Data
hgss.LockCapsuleSlot = (PCD)album[^1];
}
}

var count = cards.GiftCountMax;
for (var i = 0; i < count; i++)
{
Expand All @@ -503,13 +506,13 @@ static void SaveReceivedCards(SaveFile saveFile, IMysteryGiftStorage cards, Data
public Task ImportMysteryGift(byte[] data, string fileExtension, out bool isSuccessful, out string resultsMessage)
{
var gift = MysteryGift.GetMysteryGift(data, fileExtension);
if (gift is null)
if (gift is not null)
{
isSuccessful = false;
resultsMessage = "The Mystery Gift could not be imported.";
return Task.CompletedTask;
return ImportMysteryGift(gift, out isSuccessful, out resultsMessage);
}

return ImportMysteryGift(gift, out isSuccessful, out resultsMessage);
isSuccessful = false;
resultsMessage = "The Mystery Gift could not be imported.";
return Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion Pkmds.Web/SpriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static string GetPokemonSpriteFilename(ushort species, EntityContext con
species: (ushort)Species.Frillish or (ushort)Species.Jellicent, gender: (byte)Gender.Female
} => $"{species}f",
{ species: (ushort)Species.Alcremie } => $"{species}-{form}-{formArg1}",
(_, _, _, > 0, _, _) when FormInfo.HasTotemForm(species) && FormInfo.IsTotemForm(species, form) =>
{ form: > 0 } when FormInfo.HasTotemForm(species) && FormInfo.IsTotemForm(species, form) =>
$"{species}-{FormInfo.GetTotemBaseForm(species, form)}",
{ form: > 0 } => species switch
{
Expand Down

0 comments on commit ec9ecfe

Please sign in to comment.