Skip to content

Commit

Permalink
Design tweaks, "please load save file" opens drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Dec 19, 2023
1 parent 2abdfcc commit 12e7597
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 206 deletions.
20 changes: 10 additions & 10 deletions Pkmds.Web/Components/EditForms/Tabs/MainTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EntityContext.MaxInvalid &&
}

<MudCheckBox Label="Is Nicknamed"
@bind-Checked="@Pokemon.IsNicknamed"
@bind-Value="@Pokemon.IsNicknamed"
For="@(() => Pokemon.IsNicknamed)" />

<MudTextField Label="Nickname"
Expand Down Expand Up @@ -189,28 +189,28 @@ EntityContext.MaxInvalid &&
<MudStack Row="@true">

<MudCheckBox Label="Is Egg"
@bind-Checked="@Pokemon.IsEgg"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon.IsEgg"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.IsEgg)" />

<MudCheckBox Label="Infected"
@bind-Checked="@Pokemon.PKRS_Infected"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon.PKRS_Infected"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.PKRS_Infected)" />

<MudCheckBox Label="Cured"
@bind-Checked="@Pokemon.PKRS_Cured"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon.PKRS_Cured"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.PKRS_Cured)" />
@*
<MudCheckBox Label="N's Sparkle"
@bind-Checked="@Pokemon"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.PKRS_Cured)" />
*@

<MudCheckBox Label="Shiny"
Checked="@Pokemon.IsShiny"
Value="@Pokemon.IsShiny"
ReadOnly="@true"
For="@(() => Pokemon.IsShiny)" />

Expand Down
8 changes: 4 additions & 4 deletions Pkmds.Web/Components/EditForms/Tabs/MetTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ EntityContext.MaxInvalid &&
((AppState.SelectedBoxNumber is not null && AppState.SelectedBoxSlotNumber is not null) || (AppState.SelectedPartySlotNumber is not null)))
{
<MudCheckBox Label="Nicknamed"
@bind-Checked="@Pokemon.IsNicknamed"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon.IsNicknamed"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.IsNicknamed)" />

<MudSelect Label="Origin Game"
Expand Down Expand Up @@ -69,8 +69,8 @@ EntityContext.MaxInvalid &&

@*
<MudCheckBox Label="Met as Egg"
@bind-Checked="@Pokemon.WasEgg"
@bind-Checked:after="@RefreshService.Refresh"
@bind-Value="@Pokemon.WasEgg"
@bind-Value:after="@RefreshService.Refresh"
For="@(() => Pokemon.WasEgg)" />
*@
<MudAutocomplete T="ComboItem"
Expand Down
217 changes: 28 additions & 189 deletions Pkmds.Web/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,35 @@
Color="@Color.Inherit"
Edge="@Edge.Start"
OnClick="@DrawerToggle" />
PKMDS Save Editor
<MudText Typo="@Typo.h6">
PKMDS Save Editor
</MudText>
<MudSpacer />
<MudSwitch @bind-Value="@isDarkMode"
Color="@Color.Primary"
Label="@(isDarkMode ? "Dark" : "Light")" />
<MudStack Row="@true">
<MudSwitch @bind-Value="@isDarkMode"
Color="Color.Primary"
Label="@(isDarkMode ? "Dark" : "Light")" />
<MudButton Class="d-none d-md-flex"
EndIcon="@Icons.Custom.Brands.GitHub"
Href="https://github.com/codemonkey85/PKMDS-Blazor"
Target="_blank"
title="Source code on GitHub"
Color="@Color.Inherit"
Variant="@Variant.Outlined">
Source code on GitHub
</MudButton>
<MudIconButton Class="d-md-none"
Icon="@Icons.Custom.Brands.GitHub"
Href="https://github.com/codemonkey85/PKMDS-Blazor"
Target="_blank"
Title="Source code on GitHub"
Color="@Color.Inherit"
Variant="@Variant.Outlined" />
</MudStack>
</MudAppBar>
<MudDrawer @bind-Open="@drawerOpen">
<MudDrawerHeader>
<MudText Typo="@Typo.h6"
Color="@Color.Primary">
PKMDS Save Editor
</MudText>
</MudDrawerHeader>
<MudStack Spacing="3">
<MudDrawer @bind-Open="@AppService.IsDrawerOpen">
<MudStack Spacing="3"
Class="my-3">
<MudButton OnClick="@ShowLoadSaveFileDialogAsync"
Variant="@Variant.Filled"
FullWidth="@false"
Expand All @@ -48,7 +63,7 @@
</MudStack>
<MudSpacer />
<MudStack Spacing="1"
Class="mx-1 mb-1">
Class="mx-1 my-3 mb-1">
<MudText Typo="@Typo.subtitle2">
Version @IAppState.AppVersion
</MudText>
Expand All @@ -71,15 +86,6 @@
Special thanks to Kaphotics, as well as <MudLink Href="https://github.com/kwsch/PKHeX/graphs/contributors"
Target="_blank">all the contributors to PKHeX and PKHeX.Core</MudLink>.
</MudText>
<MudButton Class="object-center"
EndIcon="@Icons.Custom.Brands.GitHub"
Href="https://github.com/codemonkey85/PKMDS-Blazor"
Target="_blank"
title="Source on GitHub"
Color="@Color.Inherit"
Variant="@Variant.Outlined">
Source on GitHub
</MudButton>
</MudStack>
</MudDrawer>
<MudMainContent>
Expand All @@ -88,170 +94,3 @@
</MudContainer>
</MudMainContent>
</MudLayout>

@code {
private bool drawerOpen = true;
private bool isDarkMode;
private MudThemeProvider? mudThemeProvider;

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

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

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && mudThemeProvider is not null)
{
isDarkMode = await mudThemeProvider.GetSystemPreference();
await mudThemeProvider.WatchSystemPreference(OnSystemPreferenceChanged);
StateHasChanged();
}
}

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
private async Task OnSystemPreferenceChanged(bool newValue)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
isDarkMode = newValue;
StateHasChanged();
}

private void DrawerToggle() => drawerOpen = !drawerOpen;

private const long MaxFileSize = 4_000_000L; // bytes
private IBrowserFile? browserLoadSaveFile;

private async Task ShowLoadSaveFileDialogAsync()
{
var dialog = await DialogService.ShowAsync<FileUploadDialog>();
var result = await dialog.Result;
if (result is { Data: IBrowserFile selectedFile })
{
browserLoadSaveFile = selectedFile;
await LoadSaveFileAsync();
}
}

private async Task LoadSaveFileAsync()
{
if (browserLoadSaveFile is null)
{
return;
}

AppState.SaveFile = null;
AppState.SelectedBoxNumber = null;
AppState.SelectedBoxSlotNumber = null;
AppState.ShowProgressIndicator = true;

await using var fileStream = browserLoadSaveFile.OpenReadStream(MaxFileSize);
using var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream);
var data = memoryStream.ToArray();
AppState.SaveFile = SaveUtil.GetVariantSAV(data);
AppState.ShowProgressIndicator = false;
if (AppState.SaveFile is null)
{
return;
}

RefreshService.Refresh();
}

private async Task ExportSaveFileAsync()
{
if (AppState.SaveFile is null)
{
return;
}

AppState.ShowProgressIndicator = true;

await WriteFile(AppState.SaveFile.Write(), browserLoadSaveFile?.Name ?? "save.sav");

AppState.ShowProgressIndicator = false;
}

private async Task ExportSelectedPokemonAsync()
{
if (AppService.EditFormPokemon is null)
{
return;
}

var pkm = AppService.EditFormPokemon;

AppState.ShowProgressIndicator = true;

pkm.RefreshChecksum();
var cleanFileName = AppService.GetCleanFileName(pkm);
await WriteFile(pkm.Data, cleanFileName);

AppState.ShowProgressIndicator = false;
}

private async Task WriteFile(byte[] data, string fileName)
{
if (await FileSystemAccessService.IsSupportedAsync() == false)
{
await WriteFileOldWay(data, fileName);
return;
}

try
{
await using var fileHandle = await FileSystemAccessService.ShowSaveFilePickerAsync(
new KristofferStrube.Blazor.FileSystemAccess.SaveFilePickerOptionsStartInFileSystemHandle
{
SuggestedName = fileName,
});

if (fileHandle is not null)
{
await using var writable = await fileHandle.CreateWritableAsync();
await writable.WriteAsync(data);
await writable.CloseAsync();
}
}
catch (JSException ex)
{
Console.WriteLine(ex);
}
}

private async Task WriteFileOldWay(byte[] data, string fileName)
{
// Convert the byte array to a base64 string
var base64String = Convert.ToBase64String(data);

// Create a download link element
var element = await JSRuntime.InvokeAsync<IJSObjectReference>("eval", "document.createElement('a')");

// Set the download link properties
await element.InvokeVoidAsync("setAttribute", "href", "data:application/octet-stream;base64," + base64String);
await element.InvokeVoidAsync("setAttribute", "target", "_blank");
await element.InvokeVoidAsync("setAttribute", "rel", "noopener noreferrer");
await element.InvokeVoidAsync("setAttribute", "download", fileName);

// Programmatically click the download link
await element.InvokeVoidAsync("click");
}

//private MudTheme myTheme = new()
//{
// Palette = new Palette
// {
// //Primary = "#0074D9",
// //Secondary = "#3D9970",
// //Info = "#001f3f",
// //Success = "#2ECC40",
// //Warning = "#FF851B",
// //Error = "#F012BE",
// //AppbarBackground = "#85144b",
// // more color properties
// //TextPrimary = Colors.Shades.White,
// }
//};
}
Loading

0 comments on commit 12e7597

Please sign in to comment.