Skip to content

Commit acf2ae3

Browse files
committed
ViewModel refactoring
1 parent bd116f0 commit acf2ae3

30 files changed

+819
-666
lines changed

src/aoWebWallet/Pages/ActionPage.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
@inherits MvvmComponentBase<MainViewModel>
44
@inject IDialogService DialogService
55
@inject ISnackbar Snackbar
6-
@inject NavigationManager NavigationManager;
6+
@inject NavigationManager NavigationManager
7+
@inject TokenDataService dataService
78

89
<PageTitle>@Program.PageTitlePostFix</PageTitle>
910

@@ -13,7 +14,6 @@
1314

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

1818
@if (BindingContext.WalletList.Data != null)
1919
{
@@ -78,7 +78,7 @@
7878
else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance)
7979
{
8080
var tokenId = param.Args.FirstOrDefault();
81-
var token = BindingContext.TokenList.Data?.Where(x => x.TokenId == tokenId && x.TokenData != null).FirstOrDefault();
81+
var token = dataService.TokenList.Where(x => x.TokenId == tokenId && x.TokenData != null).FirstOrDefault();
8282

8383
if(token != null)
8484
{

src/aoWebWallet/Pages/ActionPage.razor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using aoWebWallet.Models;
2+
using aoWebWallet.Services;
23
using aoWebWallet.ViewModels;
34
using Microsoft.AspNetCore.Components.Routing;
45
using System.Net;
@@ -12,9 +13,8 @@ public partial class ActionPage : MvvmComponentBase<MainViewModel>
1213
protected override void OnInitialized()
1314
{
1415
GetQueryStringValues();
15-
WatchDataLoaderVM(BindingContext.TokenList);
16+
//WatchDataLoaderVM(BindingContext.TokenList);
1617
WatchDataLoaderVM(BindingContext.WalletList);
17-
WatchDataLoaderVM(BindingContext.BalanceDataList);
1818

1919
NavigationManager.LocationChanged += NavigationManager_LocationChanged;
2020

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

3030
await BindingContext.LoadWalletList();
31-
await BindingContext.LoadTokenList();
31+
await dataService.LoadTokenList();
3232
}
3333

3434
await base.OnAfterRenderAsync(firstRender);
@@ -104,9 +104,9 @@ private async void GetQueryStringValues()
104104
.Where(x => x.ParamType == ActionParamType.Balance || x.ParamType == ActionParamType.Quantity)
105105
.Select(x => x.Args.FirstOrDefault())
106106
.Distinct()
107-
.ToList();
107+
.ToList();
108108

109-
await BindingContext.TryAddTokenIds(tokens);
109+
await dataService.TryAddTokenIds(tokens);
110110

111111
StateHasChanged();
112112
}

src/aoWebWallet/Pages/MvvmComponentBase.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using aoWebWallet.ViewModels;
22
using CommunityToolkit.Mvvm.ComponentModel;
33
using Microsoft.AspNetCore.Components;
4+
using System.Collections.Specialized;
45
using System.ComponentModel;
56
using System.Diagnostics;
67
using webvNext.DataLoader;
@@ -13,6 +14,7 @@ public abstract class MvvmComponentBase<T> : ComponentBase, IDisposable where T
1314
public T BindingContext { get; set; } = default!;
1415

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

1719
protected override void OnInitialized()
1820
{
@@ -23,9 +25,14 @@ protected override void OnInitialized()
2325
obj.PropertyChanged += ObjWatch_PropertyChanged;
2426
}
2527

28+
foreach (var obj in CollectionWatch)
29+
{
30+
obj.CollectionChanged += Obj_CollectionChanged;
31+
}
32+
2633
base.OnInitialized();
2734
}
28-
35+
2936
protected override async Task OnInitializedAsync()
3037
{
3138
await LoadDataAsync();
@@ -51,17 +58,27 @@ internal void ObjWatch_PropertyChanged(object? sender, System.ComponentModel.Pro
5158
{
5259
this.StateHasChanged();
5360
}
61+
private void Obj_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
62+
{
63+
Console.WriteLine("Collection change " + this.GetType());
64+
this.StateHasChanged();
65+
}
5466

5567
protected virtual Task LoadDataAsync()
5668
{
5769
return Task.CompletedTask;
5870
}
5971

60-
protected void WatchObject<D>(D obj) where D : ObservableObject
72+
protected void WatchObject<D>(D obj) where D : INotifyPropertyChanged
6173
{
6274
ObjWatch.Add(obj);
6375
}
6476

77+
protected void WatchCollection<D>(D obj) where D : INotifyCollectionChanged
78+
{
79+
CollectionWatch.Add(obj);
80+
}
81+
6582
protected void WatchDataLoaderVM<D>(DataLoaderViewModel<D> vm) where D : class
6683
{
6784
ObjWatch.Add(vm);
@@ -76,6 +93,13 @@ public virtual void Dispose()
7693
{
7794
obj.PropertyChanged -= ObjWatch_PropertyChanged;
7895
}
96+
97+
foreach (var obj in CollectionWatch)
98+
{
99+
obj.CollectionChanged -= Obj_CollectionChanged;
100+
}
101+
102+
Console.WriteLine("Dispose " + this.GetType().FullName);
79103
}
80104
}
81105
}

src/aoWebWallet/Pages/TokenDetail.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
@page "/token/{tokenId}"
22
@using aoWebWallet.Models
3-
@inherits MvvmComponentBase<MainViewModel>
3+
@inherits MvvmComponentBase<TokenDetailViewModel>
44
@inject IDialogService DialogService
55
@inject ISnackbar Snackbar
66
@inject NavigationManager NavigationManager;
7+
@inject TokenDataService dataService;
78

89
<PageTitle>@Program.PageTitlePostFix</PageTitle>
910

1011

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

14-
<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
15+
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />
1516

1617
<MudStack>
17-
@if (BindingContext.TokenList.Data != null)
18+
@if (dataService.TokenList != null)
1819
{
19-
var token = BindingContext.TokenList.Data.Where(x => x.TokenId == TokenId).FirstOrDefault();
20+
var token = dataService.TokenList.Where(x => x.TokenId == TokenId).FirstOrDefault();
2021
if (token != null)
2122
{
2223
<MudPaper Class="pa-4 mb-14 mt-4">

src/aoWebWallet/Pages/TokenDetail.razor.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@
33

44
namespace aoWebWallet.Pages
55
{
6-
public partial class TokenDetail : MvvmComponentBase<MainViewModel>
6+
public partial class TokenDetail : MvvmComponentBase<TokenDetailViewModel>
77
{
88
protected override void OnInitialized()
99
{
10-
WatchDataLoaderVM(BindingContext.TokenList);
10+
//WatchDataLoaderVM(dataService.TokenList);
1111
WatchDataLoaderVM(BindingContext.TokenTransferList);
1212

1313
base.OnInitialized();
1414
}
1515

16-
protected override void OnParametersSet()
16+
protected override async Task OnParametersSetAsync()
1717
{
18-
BindingContext.SelectedTokenId = null;
19-
if (TokenId != null && TokenId.Length != 43)
18+
if (TokenId == null || TokenId.Length != 43)
2019
{
2120
NavigationManager.NavigateTo("");
2221
}
23-
BindingContext.SelectedTokenId = TokenId;
2422

25-
base.OnParametersSet();
23+
if(TokenId != null)
24+
await BindingContext.Initialize(TokenId);
25+
26+
base.OnParametersSetAsync();
2627
}
2728

2829
protected override async Task LoadDataAsync()
2930
{
30-
await BindingContext.LoadTokenList();
31+
await dataService.LoadTokenList();
3132

3233
await base.LoadDataAsync();
3334

src/aoWebWallet/Pages/Tokens.razor

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@using aoWebWallet.Models
33
@inherits MvvmComponentBase<MainViewModel>
44
@inject IDialogService DialogService
5+
@inject TokenDataService dataService
56
@inject ISnackbar Snackbar
67

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

13-
<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
14+
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />
1415

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

1920
<MudStack>
20-
@if (BindingContext.TokenList.Data != null)
21+
@if (dataService.TokenList != null)
2122
{
22-
var autoAddedTokens = BindingContext.TokenList.Data.Where(x => !x.IsVisible);
23+
var autoAddedTokens = dataService.TokenList.Where(x => !x.IsVisible);
2324

2425
<MudTabs Elevation="2" Rounded="true" ApplyEffectsToContainer="true" PanelClass="pa-6">
2526
<MudTabPanel Text="My Tokens">
26-
@foreach (var token in BindingContext.TokenList.Data.Where(x => x.IsVisible))
27+
@foreach (var token in dataService.TokenList.Where(x => x.IsVisible))
2728
{
2829
<TokenListComponent token="token" DeleteToken="DeleteToken" ToggleVisibility="ToggleVisibility"></TokenListComponent>
2930
}
@@ -35,17 +36,6 @@
3536
}
3637
</MudTabPanel>
3738
</MudTabs>
38-
@* else
39-
{
40-
foreach (var token in BindingContext.TokenList.Data)
41-
{
42-
<TokenListComponent token="token" DeleteToken="DeleteToken"></TokenListComponent>
43-
}
44-
} *@
45-
46-
47-
48-
4939
}
5040
</MudStack>
5141
</MudContainer>
@@ -61,8 +51,7 @@
6151

6252
private async void ToggleVisibility(Token token)
6353
{
64-
65-
await BindingContext.TokenToggleVisibility(token.TokenId);
54+
await dataService.TokenToggleVisibility(token.TokenId);
6655

6756
StateHasChanged();
6857
}
@@ -76,7 +65,7 @@
7665

7766
if (result != null)
7867
{
79-
await BindingContext.DeleteToken(token.TokenId);
68+
await dataService.DeleteToken(token.TokenId);
8069

8170
Snackbar.Add($"Token {token.TokenData?.Name} deleted ({token.TokenId})", Severity.Info);
8271
}

src/aoWebWallet/Pages/Tokens.razor.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ public partial class Tokens : MvvmComponentBase<MainViewModel>
66
{
77
protected override void OnInitialized()
88
{
9-
WatchDataLoaderVM(BindingContext.TokenList);
10-
119
base.OnInitialized();
1210
}
1311

1412
protected override async Task LoadDataAsync()
1513
{
16-
await BindingContext.LoadTokenList();
14+
await dataService.LoadTokenList();
1715

1816
await base.LoadDataAsync();
19-
2017
}
2118

2219
}

src/aoWebWallet/Pages/TransactionDetail.razor

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
@page "/transaction/{txid}"
22
@using aoWebWallet.Models
3-
@inherits MvvmComponentBase<MainViewModel>
3+
@inherits MvvmComponentBase<TransactionDetailViewModel>
44
@inject NavigationManager NavigationManager;
5+
@inject TokenDataService dataService
56

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

89

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

1112
<MudStack>
12-
<DataLoaderProgress DataLoader="BindingContext.TokenList.DataLoader" Title="tokens" />
13+
<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />
1314
<DataLoaderProgress DataLoader="BindingContext.SelectedTransaction.DataLoader" Title="transaction" />
1415

1516
@if (BindingContext.SelectedTransaction.Data != null)
@@ -21,7 +22,7 @@
2122
@transfer.Id
2223
</MudText>
2324

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

2627
<MudPaper Class="pa-4">
2728
<MudStack Row=true>

src/aoWebWallet/Pages/TransactionDetail.razor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using aoWebWallet.ViewModels;
1+
using aoWebWallet.Models;
2+
using aoWebWallet.ViewModels;
23
using Microsoft.AspNetCore.Components;
34
using MudBlazor;
45
using System.Net;
56

67
namespace aoWebWallet.Pages
78
{
8-
public partial class TransactionDetail : MvvmComponentBase<MainViewModel>
9+
public partial class TransactionDetail : MvvmComponentBase<TransactionDetailViewModel>
910
{
1011
[Parameter]
1112
public string? TxId { get; set; }
@@ -18,23 +19,22 @@ protected override void OnInitialized()
1819
base.OnInitialized();
1920
}
2021

21-
protected override void OnParametersSet()
22+
protected override async Task OnParametersSetAsync()
2223
{
23-
BindingContext.SelectedTransactionId = null;
24-
2524
if (TxId != null && TxId.Length != 43)
2625
{
2726
NavigationManager.NavigateTo("");
2827
}
2928

30-
BindingContext.SelectedTransactionId = this.TxId;
29+
if (TxId != null)
30+
await BindingContext.Initialize(TxId);
3131

32-
base.OnParametersSet();
32+
base.OnParametersSetAsync();
3333
}
3434

3535
protected override async Task LoadDataAsync()
3636
{
37-
await BindingContext.LoadTokenList();
37+
await dataService.LoadTokenList();
3838

3939
//if (!string.IsNullOrEmpty(Address))
4040
//{

0 commit comments

Comments
 (0)