Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Mar 28, 2024
1 parent cd4dfd7 commit 2f8d56a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/aoWebWallet/Pages/WalletDetail.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override void OnInitialized()
protected override void OnParametersSet()
{
BindingContext.SelectedWallet = null;
BindingContext.SelectedAddress = null;
//BindingContext.SelectedAddress = null;

if(Address != null && Address.Length != 43)
{
Expand Down
13 changes: 10 additions & 3 deletions src/aoWebWallet/Services/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void AddSystemToken(List<Token> list, string tokenId)
list.Add(new Token { TokenId = tokenId, IsSystemToken = true });
}

public async ValueTask AddToken(string tokenId, TokenData data, bool isUserAdded)
public async ValueTask<Token> AddToken(string tokenId, TokenData data, bool isUserAdded)
{
var list = await GetTokenIds();

Expand All @@ -53,9 +53,14 @@ public async ValueTask AddToken(string tokenId, TokenData data, bool isUserAdded
existing.IsUserAdded = true;
}
else
list.Add(new Token { TokenId = tokenId, TokenData = data, IsUserAdded = isUserAdded });
{
existing = new Token { TokenId = tokenId, TokenData = data, IsUserAdded = isUserAdded };
list.Add(existing);
}

await SaveTokenList(list);

return existing;
}

public async ValueTask DeleteToken(string tokenId)
Expand Down Expand Up @@ -132,7 +137,9 @@ public async Task AddToLog(ActivityLogType type, string id)
existing.Count++;
existing.LastAddDateTime = DateTimeOffset.UtcNow;

await localStorage.SetItemAsync(type.ToString(), all);
var ordered = all.OrderByDescending(x => x.LastAddDateTime).Take(15).ToList();

await localStorage.SetItemAsync(type.ToString(), ordered);
}

public async ValueTask<List<LogData>> GetLog(ActivityLogType type)
Expand Down
67 changes: 44 additions & 23 deletions src/aoWebWallet/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@ public Task LoadTokenTransferListForToken(string? tokenId) => TokenTransferList.
}
});

public Task LoadTokenList() => TokenList.DataLoader.LoadAsync(async () =>
public Task LoadTokenList(bool force = false) => TokenList.DataLoader.LoadAsync(async () =>
{
TokenList.Data = new();
await foreach (var item in dataService.LoadTokenDataAsync())
if (TokenList.Data == null || force)
{
TokenList.Data.Add(item);
TokenList.ForcePropertyChanged();
TokenList.Data = new();
await foreach (var item in dataService.LoadTokenDataAsync())
{
TokenList.Data.Add(item);
TokenList.ForcePropertyChanged();
}
}
TokenList.ForcePropertyChanged();
return TokenList.Data;
});

Expand Down Expand Up @@ -182,7 +185,7 @@ public Task LoadBalanceDataList(string address) => BalanceDataList.DataLoader.Lo
{
//First clear
BalanceDataList.Data = null;
var tokens = await storageService.GetTokenIds();
var tokens = TokenList.Data ?? new();
var result = new List<DataLoaderViewModel<BalanceDataViewModel>>();
Expand All @@ -207,17 +210,32 @@ public Task LoadBalanceDataList(string address) => BalanceDataList.DataLoader.Lo



public async Task LoadWalletList()
public async Task LoadWalletList(bool force = false)
{
var list = await storageService.GetWallets();
WalletList.Data = list;
if (WalletList.Data == null || force)
{
var list = await storageService.GetWallets();
WalletList.Data = list;
}
}

public async Task AddToken(string tokenId, TokenData data, bool isUserAdded = false)
{
BalanceDataList.Data = null;
await storageService.AddToken(tokenId, data, isUserAdded);
await LoadTokenList();
var newToken = await storageService.AddToken(tokenId, data, isUserAdded);
var existing = TokenList.Data?.Where(x => x.TokenId == newToken.TokenId).FirstOrDefault();
if(existing == null)
{
if (TokenList.Data == null)
TokenList.Data = new();

TokenList.Data.Add(newToken);
TokenList.ForcePropertyChanged();
}
else
{
existing = newToken;
}

if(!string.IsNullOrEmpty(SelectedAddress))
{
Expand All @@ -235,32 +253,32 @@ public async Task DeleteToken(string tokenId)
{
BalanceDataList.Data = null;
await storageService.DeleteToken(tokenId);
await this.LoadTokenList();
await this.LoadTokenList(force: true);
}

public async Task TokenToggleVisibility(string tokenId)
{
var all = await storageService.GetTokenIds();
var all = TokenList.Data ?? new();
var token = all.Where(x => x.TokenId == tokenId).FirstOrDefault();
if(token != null)
{
token.IsVisible = !token.IsVisible;
await storageService.SaveTokenList(all);
await this.LoadTokenList();
await this.LoadTokenList(force: true);
}
}

public async Task SaveWallet(Wallet wallet)
{
await storageService.SaveWallet(wallet);
await LoadWalletList();
await LoadWalletList(force: true);
await SetClaims();
}

public async Task DeleteWallet(Wallet wallet)
{
await storageService.DeleteWallet(wallet);
await LoadWalletList();
await LoadWalletList(force: true);
}

public async Task DownloadWallet(Wallet wallet)
Expand Down Expand Up @@ -331,7 +349,12 @@ private async Task SelectWallet(string? address)
{
if (!string.IsNullOrEmpty(address))
{
var all = await storageService.GetWallets();
if (this.WalletList.Data == null)
{
await LoadWalletList();
}

var all = this.WalletList.Data ?? new();
var current = all.Where(x => x.Address == address).FirstOrDefault();
if (current != null)
{
Expand All @@ -356,8 +379,8 @@ private async Task SelectWallet(string? address)
SelectedWalletIndex = 5;
}

await this.LoadBalanceDataList(address);
await this.LoadTokenTransferList(address);
this.LoadBalanceDataList(address);
this.LoadTokenTransferList(address);
}
else
{
Expand All @@ -370,7 +393,6 @@ partial void OnSelectedTransactionIdChanged(string? value)
{
this.LoadSelectedTokenTransfer();


if (value != null)
this.AddToLog(ActivityLogType.ViewTransaction, value);
}
Expand All @@ -379,7 +401,6 @@ partial void OnSelectedTokenIdChanged(string? value)
{
this.LoadTokenTransferListForToken(value);


if (value != null)
this.AddToLog(ActivityLogType.ViewToken, value);
}
Expand Down Expand Up @@ -421,7 +442,7 @@ public async Task AddWalletAsReadonly()
{
SelectedWallet.Source = WalletTypes.Manual;
await storageService.SaveWallet(SelectedWallet);
await LoadWalletList();
await LoadWalletList(force: true);

snackbar.Add("Wallet added to list as read-only wallet.", Severity.Info);

Expand Down

0 comments on commit 2f8d56a

Please sign in to comment.