From e318c8464b24b92bce28f802645dd1cbdb0b9005 Mon Sep 17 00:00:00 2001 From: dangershony Date: Wed, 22 Nov 2023 16:19:47 +0000 Subject: [PATCH] Create a component --- src/Angor/Client/Components/ShowQrCode.razor | 58 ++++++++++++++++++++ src/Angor/Client/Pages/Wallet.razor | 56 ++++++------------- src/Angor/Client/Shared/Util.cs | 19 ------- 3 files changed, 75 insertions(+), 58 deletions(-) create mode 100644 src/Angor/Client/Components/ShowQrCode.razor delete mode 100644 src/Angor/Client/Shared/Util.cs diff --git a/src/Angor/Client/Components/ShowQrCode.razor b/src/Angor/Client/Components/ShowQrCode.razor new file mode 100644 index 00000000..dd54bf46 --- /dev/null +++ b/src/Angor/Client/Components/ShowQrCode.razor @@ -0,0 +1,58 @@ +@using Angor.Shared.Services +@using Angor.Client.Models +@using Nostr.Client.Messages +@using Nostr.Client.Messages.Metadata +@using QRCoder + + +
+

QR Code

+ @if (!string.IsNullOrEmpty(base64qrcode)) + { + QR Code + } +
+ +@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)); + } +} \ No newline at end of file diff --git a/src/Angor/Client/Pages/Wallet.razor b/src/Angor/Client/Pages/Wallet.razor index c0ece45d..91787a94 100644 --- a/src/Angor/Client/Pages/Wallet.razor +++ b/src/Angor/Client/Pages/Wallet.razor @@ -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; @@ -25,8 +26,7 @@ - - + @if (!hasWallet) { @@ -163,14 +163,12 @@

Receive Address

-

@localAccountInfo.GetNextReceiveAddress()

+

@nextReceiveAddress

-
-

QR Code

- - QR Code -
+ + +
@@ -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() { @@ -398,10 +398,9 @@ if (hasWallet) { localAccountInfo = GetAccountInfoFromStorage(); + nextReceiveAddress = localAccountInfo.GetNextReceiveAddress(); } - GenerateQRCode(); - return Task.CompletedTask; } @@ -419,7 +418,11 @@ storage.SetAccountInfo(network.Name, accountInfo); localAccountInfo = accountInfo; - + + nextReceiveAddress = localAccountInfo.GetNextReceiveAddress(); + + showQrCode.GenerateQRCode(nextReceiveAddress); + return new OperationResult { Success = true }; }); @@ -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() diff --git a/src/Angor/Client/Shared/Util.cs b/src/Angor/Client/Shared/Util.cs deleted file mode 100644 index c28cbeb7..00000000 --- a/src/Angor/Client/Shared/Util.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Angor.Client.Storage; -using Angor.Shared.Models; -using Microsoft.AspNetCore.Components; -using QRCoder; - -namespace Angor.Client.Shared -{ - public class Util - { - public static string GenerateQRCode(string content) - { - using QRCodeGenerator qrGenerator = new QRCodeGenerator(); - using QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q); - using PngByteQRCode pngByteQRCode = new PngByteQRCode(qrCodeData); - return Convert.ToBase64String(pngByteQRCode.GetGraphic(10)); - } - - } -}