Skip to content

Commit

Permalink
ViewModel refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 18, 2024
1 parent bd116f0 commit acf2ae3
Show file tree
Hide file tree
Showing 30 changed files with 819 additions and 666 deletions.
6 changes: 3 additions & 3 deletions src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@inherits MvvmComponentBase<MainViewModel>
@inject IDialogService DialogService
@inject ISnackbar Snackbar
@inject NavigationManager NavigationManager;
@inject NavigationManager NavigationManager
@inject TokenDataService dataService

<PageTitle>@Program.PageTitlePostFix</PageTitle>

Expand All @@ -13,7 +14,6 @@

<MudStack>
TODO: Select a wallet, or create a new wallet
<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />

@if (BindingContext.WalletList.Data != null)
{
Expand Down Expand Up @@ -78,7 +78,7 @@
else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance)
{
var tokenId = param.Args.FirstOrDefault();
var token = BindingContext.TokenList.Data?.Where(x => x.TokenId == tokenId && x.TokenData != null).FirstOrDefault();
var token = dataService.TokenList.Where(x => x.TokenId == tokenId && x.TokenData != null).FirstOrDefault();

if(token != null)
{
Expand Down
10 changes: 5 additions & 5 deletions src/aoWebWallet/Pages/ActionPage.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using aoWebWallet.Models;
using aoWebWallet.Services;
using aoWebWallet.ViewModels;
using Microsoft.AspNetCore.Components.Routing;
using System.Net;
Expand All @@ -12,9 +13,8 @@ public partial class ActionPage : MvvmComponentBase<MainViewModel>
protected override void OnInitialized()
{
GetQueryStringValues();
WatchDataLoaderVM(BindingContext.TokenList);
//WatchDataLoaderVM(BindingContext.TokenList);
WatchDataLoaderVM(BindingContext.WalletList);
WatchDataLoaderVM(BindingContext.BalanceDataList);

NavigationManager.LocationChanged += NavigationManager_LocationChanged;

Expand All @@ -28,7 +28,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
await BindingContext.CheckHasArConnectExtension();

await BindingContext.LoadWalletList();
await BindingContext.LoadTokenList();
await dataService.LoadTokenList();
}

await base.OnAfterRenderAsync(firstRender);
Expand Down Expand Up @@ -104,9 +104,9 @@ private async void GetQueryStringValues()
.Where(x => x.ParamType == ActionParamType.Balance || x.ParamType == ActionParamType.Quantity)
.Select(x => x.Args.FirstOrDefault())
.Distinct()
.ToList();
.ToList();

await BindingContext.TryAddTokenIds(tokens);
await dataService.TryAddTokenIds(tokens);

StateHasChanged();
}
Expand Down
28 changes: 26 additions & 2 deletions src/aoWebWallet/Pages/MvvmComponentBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using aoWebWallet.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.AspNetCore.Components;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using webvNext.DataLoader;
Expand All @@ -13,6 +14,7 @@ public abstract class MvvmComponentBase<T> : ComponentBase, IDisposable where T
public T BindingContext { get; set; } = default!;

public List<INotifyPropertyChanged> ObjWatch { get; set; } = new();
public List<INotifyCollectionChanged> CollectionWatch { get; set; } = new();

protected override void OnInitialized()
{
Expand All @@ -23,9 +25,14 @@ protected override void OnInitialized()
obj.PropertyChanged += ObjWatch_PropertyChanged;
}

foreach (var obj in CollectionWatch)
{
obj.CollectionChanged += Obj_CollectionChanged;
}

base.OnInitialized();
}

protected override async Task OnInitializedAsync()
{
await LoadDataAsync();
Expand All @@ -51,17 +58,27 @@ internal void ObjWatch_PropertyChanged(object? sender, System.ComponentModel.Pro
{
this.StateHasChanged();
}
private void Obj_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("Collection change " + this.GetType());
this.StateHasChanged();
}

protected virtual Task LoadDataAsync()
{
return Task.CompletedTask;
}

protected void WatchObject<D>(D obj) where D : ObservableObject
protected void WatchObject<D>(D obj) where D : INotifyPropertyChanged
{
ObjWatch.Add(obj);
}

protected void WatchCollection<D>(D obj) where D : INotifyCollectionChanged
{
CollectionWatch.Add(obj);
}

protected void WatchDataLoaderVM<D>(DataLoaderViewModel<D> vm) where D : class
{
ObjWatch.Add(vm);
Expand All @@ -76,6 +93,13 @@ public virtual void Dispose()
{
obj.PropertyChanged -= ObjWatch_PropertyChanged;
}

foreach (var obj in CollectionWatch)
{
obj.CollectionChanged -= Obj_CollectionChanged;
}

Console.WriteLine("Dispose " + this.GetType().FullName);
}
}
}
9 changes: 5 additions & 4 deletions src/aoWebWallet/Pages/TokenDetail.razor
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
@page "/token/{tokenId}"
@using aoWebWallet.Models
@inherits MvvmComponentBase<MainViewModel>
@inherits MvvmComponentBase<TokenDetailViewModel>
@inject IDialogService DialogService
@inject ISnackbar Snackbar
@inject NavigationManager NavigationManager;
@inject TokenDataService dataService;

<PageTitle>@Program.PageTitlePostFix</PageTitle>


<MudContainer Class="mt-16 px-8" MaxWidth="MaxWidth.False">
<MudText Typo="Typo.h5">Token Explorer</MudText>

<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />

<MudStack>
@if (BindingContext.TokenList.Data != null)
@if (dataService.TokenList != null)
{
var token = BindingContext.TokenList.Data.Where(x => x.TokenId == TokenId).FirstOrDefault();
var token = dataService.TokenList.Where(x => x.TokenId == TokenId).FirstOrDefault();
if (token != null)
{
<MudPaper Class="pa-4 mb-14 mt-4">
Expand Down
17 changes: 9 additions & 8 deletions src/aoWebWallet/Pages/TokenDetail.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@

namespace aoWebWallet.Pages
{
public partial class TokenDetail : MvvmComponentBase<MainViewModel>
public partial class TokenDetail : MvvmComponentBase<TokenDetailViewModel>
{
protected override void OnInitialized()
{
WatchDataLoaderVM(BindingContext.TokenList);
//WatchDataLoaderVM(dataService.TokenList);
WatchDataLoaderVM(BindingContext.TokenTransferList);

base.OnInitialized();
}

protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
BindingContext.SelectedTokenId = null;
if (TokenId != null && TokenId.Length != 43)
if (TokenId == null || TokenId.Length != 43)
{
NavigationManager.NavigateTo("");
}
BindingContext.SelectedTokenId = TokenId;

base.OnParametersSet();
if(TokenId != null)
await BindingContext.Initialize(TokenId);

base.OnParametersSetAsync();

Check warning on line 26 in src/aoWebWallet/Pages/TokenDetail.razor.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

protected override async Task LoadDataAsync()
{
await BindingContext.LoadTokenList();
await dataService.LoadTokenList();

await base.LoadDataAsync();

Expand Down
25 changes: 7 additions & 18 deletions src/aoWebWallet/Pages/Tokens.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@using aoWebWallet.Models
@inherits MvvmComponentBase<MainViewModel>
@inject IDialogService DialogService
@inject TokenDataService dataService
@inject ISnackbar Snackbar

<PageTitle>@Program.PageTitlePostFix</PageTitle>
Expand All @@ -10,20 +11,20 @@
<MudContainer Class="mt-16 px-8" MaxWidth="MaxWidth.False">
<MudText Typo="Typo.h5">Token Explorer</MudText>

<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />

<MudContainer style="max-width: 100%;" Width="100%" Class="d-flex justify-end mb-4 pr-4">
<MudIconButton Icon="@Icons.Material.Filled.AddCircle" aria-label="add token" OnClick="OpenAddTokenDialog"></MudIconButton>
</MudContainer>

<MudStack>
@if (BindingContext.TokenList.Data != null)
@if (dataService.TokenList != null)
{
var autoAddedTokens = BindingContext.TokenList.Data.Where(x => !x.IsVisible);
var autoAddedTokens = dataService.TokenList.Where(x => !x.IsVisible);

<MudTabs Elevation="2" Rounded="true" ApplyEffectsToContainer="true" PanelClass="pa-6">
<MudTabPanel Text="My Tokens">
@foreach (var token in BindingContext.TokenList.Data.Where(x => x.IsVisible))
@foreach (var token in dataService.TokenList.Where(x => x.IsVisible))
{
<TokenListComponent token="token" DeleteToken="DeleteToken" ToggleVisibility="ToggleVisibility"></TokenListComponent>
}
Expand All @@ -35,17 +36,6 @@
}
</MudTabPanel>
</MudTabs>
@* else
{
foreach (var token in BindingContext.TokenList.Data)
{
<TokenListComponent token="token" DeleteToken="DeleteToken"></TokenListComponent>
}
} *@




}
</MudStack>
</MudContainer>
Expand All @@ -61,8 +51,7 @@

private async void ToggleVisibility(Token token)
{

await BindingContext.TokenToggleVisibility(token.TokenId);
await dataService.TokenToggleVisibility(token.TokenId);

StateHasChanged();
}
Expand All @@ -76,7 +65,7 @@

if (result != null)
{
await BindingContext.DeleteToken(token.TokenId);
await dataService.DeleteToken(token.TokenId);

Snackbar.Add($"Token {token.TokenData?.Name} deleted ({token.TokenId})", Severity.Info);
}
Expand Down
5 changes: 1 addition & 4 deletions src/aoWebWallet/Pages/Tokens.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ public partial class Tokens : MvvmComponentBase<MainViewModel>
{
protected override void OnInitialized()
{
WatchDataLoaderVM(BindingContext.TokenList);

base.OnInitialized();
}

protected override async Task LoadDataAsync()
{
await BindingContext.LoadTokenList();
await dataService.LoadTokenList();

await base.LoadDataAsync();

}

}
Expand Down
7 changes: 4 additions & 3 deletions src/aoWebWallet/Pages/TransactionDetail.razor
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
@page "/transaction/{txid}"
@using aoWebWallet.Models
@inherits MvvmComponentBase<MainViewModel>
@inherits MvvmComponentBase<TransactionDetailViewModel>
@inject NavigationManager NavigationManager;
@inject TokenDataService dataService

<PageTitle>@TxId - @Program.PageTitlePostFix</PageTitle>


<MudContainer Class="mt-8 px-8" MaxWidth="MaxWidth.False">

<MudStack>
<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />
<DataLoaderProgress DataLoader="BindingContext.SelectedTransaction.DataLoader" Title="transaction" />

@if (BindingContext.SelectedTransaction.Data != null)
Expand All @@ -21,7 +22,7 @@
@transfer.Id
</MudText>

var tokenData = BindingContext.TokenList.Data?.Where(x => x.TokenId == transfer.TokenId).Select(x => x.TokenData).FirstOrDefault();
var tokenData = dataService.TokenList.Where(x => x.TokenId == transfer.TokenId).Select(x => x.TokenData).FirstOrDefault();

<MudPaper Class="pa-4">
<MudStack Row=true>
Expand Down
16 changes: 8 additions & 8 deletions src/aoWebWallet/Pages/TransactionDetail.razor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using aoWebWallet.ViewModels;
using aoWebWallet.Models;
using aoWebWallet.ViewModels;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using System.Net;

namespace aoWebWallet.Pages
{
public partial class TransactionDetail : MvvmComponentBase<MainViewModel>
public partial class TransactionDetail : MvvmComponentBase<TransactionDetailViewModel>
{
[Parameter]
public string? TxId { get; set; }
Expand All @@ -18,23 +19,22 @@ protected override void OnInitialized()
base.OnInitialized();
}

protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
BindingContext.SelectedTransactionId = null;

if (TxId != null && TxId.Length != 43)
{
NavigationManager.NavigateTo("");
}

BindingContext.SelectedTransactionId = this.TxId;
if (TxId != null)
await BindingContext.Initialize(TxId);

base.OnParametersSet();
base.OnParametersSetAsync();

Check warning on line 32 in src/aoWebWallet/Pages/TransactionDetail.razor.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

protected override async Task LoadDataAsync()
{
await BindingContext.LoadTokenList();
await dataService.LoadTokenList();

//if (!string.IsNullOrEmpty(Address))
//{
Expand Down
Loading

0 comments on commit acf2ae3

Please sign in to comment.