Skip to content

Commit

Permalink
Add a copy / paste feature, closes #49
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Nov 19, 2024
1 parent 0d11a2b commit 500ced1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Pkmds.Web/Components/EditForms/PokemonEditForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ AppState.SelectedSlotsAreValid)
Save
</MudButton>

<MudButton OnClick="@OnClickCopy"
ButtonType="@ButtonType.Button"
Variant="@Variant.Filled"
StartIcon="@Icons.Material.Filled.ContentCopy"
Class="my-2"
Color="@Color.Default"
Disabled="@(Pokemon.Species == (ushort)Species.None)">
Copy
</MudButton>

<MudButton OnClick="@OnClickPaste"
ButtonType="@ButtonType.Button"
Variant="@Variant.Filled"
StartIcon="@Icons.Material.Filled.ContentPaste"
Class="my-2"
Color="@Color.Default"
Disabled="@(CopiedPokemon is null)">
Paste
</MudButton>

<MudButton OnClick="@ExportAsShowdown"
ButtonType="@ButtonType.Button"
Variant="@Variant.Filled"
Expand Down
75 changes: 75 additions & 0 deletions Pkmds.Web/Components/EditForms/PokemonEditForm.razor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Microsoft.Extensions.Options;

namespace Pkmds.Web.Components.EditForms;

public partial class PokemonEditForm : IDisposable
{
[Parameter, EditorRequired]
public PKM? Pokemon { get; set; }

private PKM? CopiedPokemon { get; set; }

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

Expand Down Expand Up @@ -65,4 +69,75 @@ void OnDeleteConfirm(bool confirmed)
}
}
}

private void OnClickCopy()
{
if (Pokemon is null)
{
return;
}

CopiedPokemon = Pokemon.Clone();

Snackbar.Add("The selected Pokémon has been copied.");
}

private void OnClickPaste()
{
if (CopiedPokemon is null)
{
return;
}

if (Pokemon is { Species: > (int)Species.None })
{
ShowPasteConfirmation();
}
else
{
PastePokemon();
}

void ShowPasteConfirmation()
{
var parameters = new DialogParameters
{
{ nameof(ConfirmActionDialog.Title), "Paste Pokémon" },
{ nameof(ConfirmActionDialog.Message), "Are you sure you want to paste the copied Pokémon? The Pokémon in the selected slot will be replaced." },
{ nameof(ConfirmActionDialog.ConfirmText), "Paste" },
{ nameof(ConfirmActionDialog.ConfirmIcon), Icons.Material.Filled.Delete },
{ nameof(ConfirmActionDialog.ConfirmColor), Color.Default },
{ nameof(ConfirmActionDialog.CancelText), "Cancel" },
{ nameof(ConfirmActionDialog.CancelIcon), Icons.Material.Filled.Clear },
{ nameof(ConfirmActionDialog.CancelColor), Color.Primary},
{ nameof(ConfirmActionDialog.OnConfirm), EventCallback.Factory.Create<bool>(this, OnPasteConfirm) }
};

DialogService.Show<ConfirmActionDialog>(
"Confirm Action",
parameters,
new DialogOptions
{
CloseOnEscapeKey = true,
MaxWidth = MaxWidth.Small,
});
}

void OnPasteConfirm(bool confirmed)
{
if (!confirmed)
{
return;
}

PastePokemon();
}

void PastePokemon()
{
Pokemon = CopiedPokemon.Clone();
AppService.SavePokemon(Pokemon);
Snackbar.Add("The copied Pokémon has been pasted.");
}
}
}
8 changes: 7 additions & 1 deletion Pkmds.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

services
.AddMudServices(config =>
{
config.SnackbarConfiguration.PreventDuplicates = false;
config.SnackbarConfiguration.ClearAfterNavigation = true;
});

services
.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) })
.AddMudServices()
.AddFileSystemAccessService()
.AddScoped<IAppState, AppState>()
.AddScoped<IRefreshService, RefreshService>()
Expand Down
1 change: 1 addition & 0 deletions Pkmds.Web/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

@inject IJSRuntime JSRuntime
@inject IDialogService DialogService
@inject ISnackbar Snackbar
@inject IAppState AppState
@inject IRefreshService RefreshService
@inject IAppService AppService
Expand Down

0 comments on commit 500ced1

Please sign in to comment.