Skip to content

Commit

Permalink
Create a component
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony committed Nov 22, 2023
1 parent 6ddc6d5 commit e318c84
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 58 deletions.
58 changes: 58 additions & 0 deletions src/Angor/Client/Components/ShowQrCode.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@using Angor.Shared.Services
@using Angor.Client.Models
@using Nostr.Client.Messages
@using Nostr.Client.Messages.Metadata
@using QRCoder


<div class="col-lg-6">
<h4 class="card-title">QR Code</h4>
@if (!string.IsNullOrEmpty(base64qrcode))
{
<img src="data:image /png;base64,@base64qrcode" class="card-img-top qrcode" alt="QR Code"/>
}
</div>

@code {

[Parameter]
public string Data { get; set; }

private static string lastqrcode;
private static string lastaddress;
private string base64qrcode;

protected override async Task OnInitializedAsync()
{
GenerateQRCode(Data);
}

public Task GenerateQRCode(string newData)
{
Data = newData;

if (lastaddress == Data)
{
base64qrcode = lastqrcode;
return Task.CompletedTask;
}

return Task.Run(() =>
{
base64qrcode = GenerateQRCodeInternal(Data);
lastqrcode = base64qrcode;
lastaddress = Data;

StateHasChanged();

});
}

public static string GenerateQRCodeInternal(string content)
{
using QRCodeGenerator qrGenerator = new QRCodeGenerator();
using QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
using PngByteQRCode pngByteQRCode = new PngByteQRCode(qrCodeData);
return Convert.ToBase64String(pngByteQRCode.GetGraphic(10));
}
}
56 changes: 17 additions & 39 deletions src/Angor/Client/Pages/Wallet.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@using Angor.Client.Services
@using Angor.Client.Storage
@using Angor.Shared.Models
@using Angor.Client.Components

@inject HttpClient Http
@inject IClientStorage storage;
Expand All @@ -25,8 +26,7 @@

<NotificationComponent @ref="notificationComponent" />


<!-- This part of the page is visible only if the wallet is found -->
<!-- This part of the page is visible only if the wallet is found -->
@if (!hasWallet)
{
<!-- No Wallet found -->
Expand Down Expand Up @@ -163,14 +163,12 @@
<div class="row">
<div class="col-lg-6">
<h4 class="card-title">Receive Address</h4>
<p id="receive-address" class="card-text">@localAccountInfo.GetNextReceiveAddress()</p>
<p id="receive-address" class="card-text">@nextReceiveAddress</p>
<button class="btn btn-primary" onclick="@CopyNextReceiveAddress">Copy Address</button>
</div>
<div class="col-lg-6">
<h4 class="card-title">QR Code</h4>
<!-- Replace the src with your actual QR code -->
<img src="data:image /png;base64,@base64qrcode" class="card-img-top qrcode" alt="QR Code" />
</div>

<ShowQrCode @ref="showQrCode" Data="@nextReceiveAddress" />

</div>
</div>
</div>
Expand Down Expand Up @@ -389,7 +387,9 @@
private int _feeMax = 3;
DateTime _lastFeeRefresh = DateTime.MinValue;

private string base64qrcode;
string? nextReceiveAddress = string.Empty;

ShowQrCode showQrCode;

protected override Task OnInitializedAsync()
{
Expand All @@ -398,10 +398,9 @@
if (hasWallet)
{
localAccountInfo = GetAccountInfoFromStorage();
nextReceiveAddress = localAccountInfo.GetNextReceiveAddress();
}

GenerateQRCode();

return Task.CompletedTask;
}

Expand All @@ -419,7 +418,11 @@
storage.SetAccountInfo(network.Name, accountInfo);

localAccountInfo = accountInfo;


nextReceiveAddress = localAccountInfo.GetNextReceiveAddress();

showQrCode.GenerateQRCode(nextReceiveAddress);

return new OperationResult { Success = true };
});

Expand Down Expand Up @@ -513,38 +516,13 @@

public async Task CopyNextReceiveAddress()
{
var address = localAccountInfo.GetNextReceiveAddress();

if (string.IsNullOrEmpty(address))
if (string.IsNullOrEmpty(nextReceiveAddress))
{
notificationComponent.ShowErrorMessage("New address was not created");
return;
}

await _clipboardService.WriteTextAsync(address);
}

private static string lastqrcode;
private static string lastaddress;

private void GenerateQRCode()
{
if (lastaddress == localAccountInfo.GetNextReceiveAddress())
{
base64qrcode = lastqrcode;
return;
}

Task.Run(() =>
{
var address = localAccountInfo.GetNextReceiveAddress();

base64qrcode = Util.GenerateQRCode(address);
lastqrcode = base64qrcode;
lastaddress = address;

StateHasChanged();
});
await _clipboardService.WriteTextAsync(nextReceiveAddress);
}

private async Task RefreshFee()
Expand Down
19 changes: 0 additions & 19 deletions src/Angor/Client/Shared/Util.cs

This file was deleted.

0 comments on commit e318c84

Please sign in to comment.