Skip to content

Commit

Permalink
Improvement/txs load by request (#87)
Browse files Browse the repository at this point in the history
* Currency txs load by request

* Token transfers load by request

* Nfts unsubscribe from quotes updated
  • Loading branch information
mismirnov authored Feb 24, 2023
1 parent 9e8c266 commit a80cfef
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
3 changes: 2 additions & 1 deletion atomex/ViewModels/CurrencyViewModels/CollectibleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public CollectibleViewModel(
.SubscribeInMainThread(async token =>
{
_navigationService?.ShowPage(new NftPage(token), TabNavigation.Portfolio);

token.IsOpenToken = true;

await Task.Run(async () =>
{
await SelectedToken!.LoadTransfersAsync();
Expand Down
12 changes: 8 additions & 4 deletions atomex/ViewModels/CurrencyViewModels/CurrencyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class CurrencyViewModel : BaseViewModel, IDisposable
public bool HasDapps => CurrencyCode == TezosConfig.Xtz;
public bool CanBuy => BuyViewModel.Currencies.Contains(Currency?.Name);

public bool IsOpenCurrency { get; set; }

protected CancellationTokenSource CancellationTokenSource;

[Reactive] public decimal TotalAmount { get; set; }
Expand Down Expand Up @@ -277,10 +279,10 @@ public virtual async Task LoadTransactionsAsync()

try
{
IsTxsLoading = true;

if (App.Account == null)
if (!IsOpenCurrency || App.Account == null)
return;

IsTxsLoading = true;

var txs = await Task.Run(async () =>
{
Expand Down Expand Up @@ -395,7 +397,7 @@ await scanner.ScanAsync(
currency: Currency.Name,
skipUsed: true,
cancellationToken: CancellationTokenSource.Token);

await Task.Run(async () => await LoadTransactionsAsync());

if (CancellationTokenSource is {IsCancellationRequested: false})
Expand Down Expand Up @@ -610,6 +612,8 @@ public virtual void Reset()
{
try
{
IsOpenCurrency = false;

if (Transactions == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public override async Task LoadTransactionsAsync()

try
{
if (App.Account == null)
if (!IsOpenCurrency || App.Account == null)
return;

IsTxsLoading = true;

var fa12Currency = Currency as Fa12Config;

var tezosConfig = App.Account
Expand Down Expand Up @@ -90,6 +92,10 @@ await Device.InvokeOnMainThreadAsync(() =>
{
Log.Error(e, "LoadTransactionsAsync error for {@Currency}", Currency?.Name);
}
finally
{
IsTxsLoading = false;
}
}

protected override void OnReceiveClick()
Expand Down
8 changes: 7 additions & 1 deletion atomex/ViewModels/CurrencyViewModels/Fa2CurrencyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public override async Task LoadTransactionsAsync()

try
{
if (App.Account == null)
if (!IsOpenCurrency || App.Account == null)
return;

IsTxsLoading = true;

var fa2Currency = Currency as Fa2Config;

var tezosConfig = App.Account
Expand Down Expand Up @@ -90,6 +92,10 @@ await Device.InvokeOnMainThreadAsync(() =>
{
Log.Error(e, "LoadTransactionsAsync error for {@Currency}", Currency?.Name);
}
finally
{
IsTxsLoading = false;
}
}

protected override void OnReceiveClick()
Expand Down
19 changes: 15 additions & 4 deletions atomex/ViewModels/CurrencyViewModels/TezosTokenViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class TezosTokenViewModel : BaseViewModel
public static string BaseCurrencyCode => "USD";
public string CurrencyCode => TokenBalance?.Symbol ?? "TOKENS";
public string Description => TokenBalance?.Name ?? TokenBalance?.Symbol;

public bool IsOpenToken { get; set; }

public bool IsConvertable => _app?.Account?.Currencies
.Any(c => c is Fa12Config fa12 && fa12?.TokenContractAddress == Contract.Address) ?? false;
Expand Down Expand Up @@ -241,10 +243,10 @@ public async Task LoadTransfersAsync()
{
try
{
IsTransfersLoading = true;

if (_app.Account == null)
if (!IsOpenToken || _app.Account == null)
return;

IsTransfersLoading = true;

var tezosAccount = _app.Account
.GetCurrencyAccount<TezosAccount>(TezosConfig.Xtz);
Expand Down Expand Up @@ -308,6 +310,7 @@ await Device.InvokeOnMainThreadAsync(() =>
private async Task LoadMoreTxs()
{
if (IsTransfersLoading ||
Transactions == null ||
QtyDisplayedTxs >= Transactions.Count) return;

IsTransfersLoading = true;
Expand Down Expand Up @@ -412,6 +415,8 @@ public virtual void Reset()
{
try
{
IsOpenToken = false;

if (Transactions == null)
return;

Expand Down Expand Up @@ -448,7 +453,11 @@ private async void OnBalanceUpdatedEventHandler(object sender, CurrencyEventArgs
args.TokenContract != null && (args.TokenContract != TokenBalance?.Contract ||
args.TokenId != TokenBalance?.TokenId)) return;

await Task.Run(async () => await UpdateBalanceAsync());
await Task.Run(async () =>
{
await UpdateBalanceAsync();
await LoadTransfersAsync();
});
}
catch (Exception e)
{
Expand All @@ -459,6 +468,8 @@ private async void OnBalanceUpdatedEventHandler(object sender, CurrencyEventArgs
public void SubscribeToUpdates()
{
_app.Account.BalanceUpdated += OnBalanceUpdatedEventHandler;

if (TokenBalance.IsNft) return;
_app.QuotesProvider.QuotesUpdated += OnQuotesUpdatedEventHandler;
}

Expand Down
12 changes: 2 additions & 10 deletions atomex/ViewModels/CurrencyViewModels/TezosTokensViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public class TezosTokensViewModel : BaseViewModel
[Reactive] public bool IsTokensLoading { get; set; }
public int LoadingStepTokens => 20;

private TezosTokenViewModel _openToken;

[Reactive] public string SearchPattern { get; set; }

public TezosTokensViewModel(
Expand All @@ -90,7 +88,7 @@ public TezosTokensViewModel(
.SubscribeInMainThread(async token =>
{
_navigationService?.ShowPage(new TokenPage(token), TabNavigation.Portfolio);
_openToken = token;
token.IsOpenToken = true;

await Task.Run(async () =>
{
Expand Down Expand Up @@ -276,12 +274,7 @@ private async void OnBalanceUpdatedEventHandler(object sender, CurrencyEventArgs
{
try
{
if (!args.IsTokenUpdate ||
args.TokenContract != null && (args.TokenContract != _openToken?.Contract?.Address ||
args.TokenId != _openToken?.TokenBalance?.TokenId)) return;

if (_openToken != null)
await Task.Run(async () => await _openToken.LoadTransfersAsync());
if (!args.IsTokenUpdate || (args.TokenContract != null && Contracts == null)) return;

await Task.Run(async () => await ReloadTokenContractsAsync());
}
Expand Down Expand Up @@ -402,7 +395,6 @@ public void Reset()
try
{
SearchPattern = null;
_openToken = null;

if (UserTokens == null)
return;
Expand Down
2 changes: 2 additions & 0 deletions atomex/ViewModels/PortfolioViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public PortfolioViewModel(IAtomexApp app)
case CurrencyActionType.Show:
NavigationService?.ShowPage(new CurrencyPage(c), TabNavigation.Portfolio);
NavigationService?.SetInitiatedPage(TabNavigation.Portfolio);

c.IsOpenCurrency = true;
await Task.Run(async () =>
{
await c.LoadTransactionsAsync();
Expand Down

0 comments on commit a80cfef

Please sign in to comment.