Skip to content

Commit

Permalink
refactor(TokenRepository): ♻️ Rework dependency injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Jan 14, 2025
1 parent 396227a commit d37c6a4
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 310 deletions.
2 changes: 1 addition & 1 deletion lib/application/aeswap/dex_token.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/application/session/session.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 23 additions & 25 deletions lib/application/tokens/tokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:developer';
import 'package:aewallet/application/account/accounts_notifier.dart';
import 'package:aewallet/application/aeswap/dex_token.dart';
import 'package:aewallet/application/api_service.dart';
import 'package:aewallet/domain/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/infrastructure/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/modules/aeswap/application/pool/dex_pool.dart';
import 'package:aewallet/modules/aeswap/application/session/provider.dart';
Expand All @@ -16,27 +17,33 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'tokens.g.dart';

@riverpod
TokensRepositoryImpl tokensRepositoryImpl(
TokensRepository tokensRepository(
Ref ref,
) =>
TokensRepositoryImpl();
) {
final apiService = ref.watch(apiServiceProvider);
final environment = ref.watch(environmentProvider);
return TokensRepositoryImpl(
apiService: apiService,
defTokensRepository: aedappfm.DefTokensRepositoryImpl(),
verifiedTokensRepository: aedappfm.VerifiedTokensRepositoryImpl(
apiService: apiService,
environment: environment,
),
);
}

@riverpod
Future<Map<String, archethic.Token>> tokensFromAddresses(
Ref ref,
List<String> addresses,
) async {
final apiService = ref.watch(apiServiceProvider);

return ref
.watch(
tokensRepositoryImplProvider,
)
.getTokensFromAddresses(
addresses,
apiService,
);
}
) =>
ref
.watch(
tokensRepositoryProvider,
)
.getTokensFromAddresses(
addresses,
);

@riverpod
Future<List<aedappfm.AEToken>> tokensFromUserBalance(
Expand All @@ -47,36 +54,27 @@ Future<List<aedappfm.AEToken>> tokensFromUserBalance(
bool withNotVerified = true,
bool withCustomToken = true,
}) async {
final apiService = ref.watch(apiServiceProvider);

final environment = ref.watch(environmentProvider);
final poolListRaw = await ref.watch(DexPoolProviders.getPoolListRaw.future);
final selectedAccount =
await ref.watch(accountsNotifierProvider.future).selectedAccount;

if (selectedAccount == null) return [];

final tokensRepository = ref.watch(tokensRepositoryImplProvider);
final defTokensRepository =
ref.watch(aedappfm.defTokensRepositoryImplProvider);

return ref
.watch(
tokensRepositoryImplProvider,
tokensRepositoryProvider,
)
.getTokensFromUserBalance(
selectedAccount.genesisAddress,
selectedAccount.customTokenAddressList ?? [],
apiService,
poolListRaw,
environment,
withUCO: withUCO,
withVerified: withVerified,
withLPToken: withLPToken,
withNotVerified: withNotVerified,
withCustomToken: withCustomToken,
defTokensRepository,
tokensRepository,
);
}

Expand Down
24 changes: 11 additions & 13 deletions lib/application/tokens/tokens.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions lib/domain/models/app_wallet.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final _privateConstructorUsedError = UnsupportedError(

/// @nodoc
mixin _$AppWallet {
String get seed =>
throw _privateConstructorUsedError; // TODO(redddwarf03): Mutualize keychain infos
String get seed => throw _privateConstructorUsedError;
AppKeychain get appKeychain => throw _privateConstructorUsedError;
KeychainSecuredInfos get keychainSecuredInfos =>
throw _privateConstructorUsedError;
Expand Down Expand Up @@ -151,7 +150,6 @@ class _$AppWalletImpl extends _AppWallet {

@override
final String seed;
// TODO(redddwarf03): Mutualize keychain infos
@override
final AppKeychain appKeychain;
@override
Expand Down Expand Up @@ -196,7 +194,7 @@ abstract class _AppWallet extends AppWallet {
const _AppWallet._() : super._();

@override
String get seed; // TODO(redddwarf03): Mutualize keychain infos
String get seed;
@override
AppKeychain get appKeychain;
@override
Expand Down
7 changes: 3 additions & 4 deletions lib/domain/models/token_parser.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:aewallet/infrastructure/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/domain/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/modules/aeswap/domain/models/dex_token.dart';
import 'package:aewallet/modules/aeswap/domain/models/util/get_pool_list_response.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
Expand All @@ -13,8 +13,8 @@ mixin TokenParser {
List<GetPoolListResponse> poolsListRaw,
aedappfm.Environment environment,
archethic.ApiService apiService,
aedappfm.DefTokensRepositoryImpl defTokensRepository,
TokensRepositoryImpl tokensRepository,
aedappfm.DefTokensRepositoryInterface defTokensRepository,
TokensRepository tokensRepository,
) async {
String? pairSymbolToken1;
String? pairSymbolToken2;
Expand Down Expand Up @@ -46,7 +46,6 @@ mixin TokenParser {

final tokensSymbolMap = await tokensRepository.getTokensFromAddresses(
tokenSymbolSearch,
apiService,
);
pairSymbolToken1 = token1Address != kUCOAddress
? tokensSymbolMap[token1Address]!.symbol
Expand Down
8 changes: 2 additions & 6 deletions lib/domain/repositories/tokens/tokens.repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:aewallet/infrastructure/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/modules/aeswap/domain/models/util/get_pool_list_response.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
Expand All @@ -7,17 +6,14 @@ import 'package:archethic_lib_dart/archethic_lib_dart.dart' as archethic;
abstract class TokensRepository {
Future<Map<String, archethic.Token>> getTokensFromAddresses(
List<String> addresses,
archethic.ApiService apiService,
);

Future<List<aedappfm.AEToken>> getTokensFromUserBalance(
String userGenesisAddress,
List<String> userTokenLocalAddresses,
archethic.ApiService apiService,
List<GetPoolListResponse> poolsListRaw,
aedappfm.Environment environment,
aedappfm.DefTokensRepositoryImpl defTokensRepositoryImpl,
TokensRepositoryImpl tokensRepositoryImpl, {
aedappfm.Environment environment, {
bool withUCO = true,
bool withVerified = true,
bool withLPToken = true,
bool withNotVerified = true,
Expand Down
28 changes: 15 additions & 13 deletions lib/infrastructure/repositories/tokens/tokens.repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutte
import 'package:archethic_lib_dart/archethic_lib_dart.dart' as archethic;

class TokensRepositoryImpl with TokenParser implements TokensRepository {
TokensRepositoryImpl({
required this.defTokensRepository,
required this.apiService,
required this.verifiedTokensRepository,
});

final aedappfm.DefTokensRepositoryInterface defTokensRepository;
final archethic.ApiService apiService;
final aedappfm.VerifiedTokensRepositoryImpl verifiedTokensRepository;

@override
Future<Map<String, archethic.Token>> getTokensFromAddresses(
List<String> addresses,
archethic.ApiService apiService,
) async {
final tokenMap = <String, archethic.Token>{};

Expand Down Expand Up @@ -62,11 +71,8 @@ class TokensRepositoryImpl with TokenParser implements TokensRepository {
Future<List<aedappfm.AEToken>> getTokensFromUserBalance(
String userGenesisAddress,
List<String> userTokenLocalAddresses,
archethic.ApiService apiService,
List<GetPoolListResponse> poolsListRaw,
aedappfm.Environment environment,
aedappfm.DefTokensRepositoryImpl defTokensRepositoryImpl,
TokensRepositoryImpl tokensRepositoryImpl, {
aedappfm.Environment environment, {
bool withUCO = true,
bool withVerified = true,
bool withLPToken = true,
Expand All @@ -80,7 +86,7 @@ class TokensRepositoryImpl with TokenParser implements TokensRepository {
}
if (withUCO) {
final defUCOToken =
await defTokensRepositoryImpl.getDefToken(environment, kUCOAddress);
await defTokensRepository.getDefToken(environment, kUCOAddress);
tokensList.add(
aedappfm.ucoToken.copyWith(
name: defUCOToken?.name ?? '',
Expand Down Expand Up @@ -109,13 +115,9 @@ class TokensRepositoryImpl with TokenParser implements TokensRepository {
if (tokenAddressList.isNotEmpty) {
final tokenMap = await getTokensFromAddresses(
tokenAddressList.toSet().toList(),
apiService,
);

final verifiedTokens = await aedappfm.VerifiedTokensRepositoryImpl(
apiService: apiService,
environment: environment,
).getVerifiedTokens();
final verifiedTokens = await verifiedTokensRepository.getVerifiedTokens();

final tokenBalances = balanceMap[userGenesisAddress]!.token;

Expand All @@ -130,8 +132,8 @@ class TokensRepositoryImpl with TokenParser implements TokensRepository {
poolsListRaw,
environment,
apiService,
defTokensRepositoryImpl,
tokensRepositoryImpl,
defTokensRepository,
this,
);

final matchingBalances = tokenBalances.where(
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/aeswap/application/pool/dex_pool.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ui/views/add_custom_token/bloc/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AddCustomTokenFormNotifier extends _$AddCustomTokenFormNotifier
.read(verifiedTokensRepositoryProvider)
.getVerifiedTokens();
final environment = ref.read(environmentProvider);
final tokensRepositoryImpl = ref.read(tokensRepositoryImplProvider);
final tokensRepositoryImpl = ref.read(tokensRepositoryProvider);
final defTokensRepositoryImpl =
ref.read(aedappfm.defTokensRepositoryImplProvider);

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/views/add_custom_token/bloc/provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ui/views/main/bloc/providers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/token_parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:aewallet/domain/models/token_parser.dart';
import 'package:aewallet/infrastructure/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/domain/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/modules/aeswap/domain/models/util/get_pool_list_response.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
Expand All @@ -12,9 +13,9 @@ import 'package:mockito/mockito.dart';

@GenerateNiceMocks(
[
MockSpec<aedappfm.DefTokensRepositoryImpl>(),
MockSpec<aedappfm.DefTokensRepositoryInterface>(),
MockSpec<archethic.ApiService>(),
MockSpec<TokensRepositoryImpl>(),
MockSpec<TokensRepository>(),
],
)
import 'token_parser_test.mocks.dart';
Expand All @@ -26,17 +27,17 @@ void main() {

group('TokenParser - tokenModelToAETokenModel', () {
late TokenParser tokenParser;
late MockTokensRepositoryImpl mockTokensRepository;
late MockDefTokensRepositoryImpl mockDefTokensRepository;
late MockTokensRepository mockTokensRepository;
late MockDefTokensRepositoryInterface mockDefTokensRepository;
late MockApiService mockApiService;
late aedappfm.Environment environment;
late List<String> verifiedTokens;
late List<GetPoolListResponse> poolsListRaw;

setUp(() {
Hive.init('${Directory.current.path}/test/tmp_data');
mockTokensRepository = MockTokensRepositoryImpl();
mockDefTokensRepository = MockDefTokensRepositoryImpl();
mockTokensRepository = MockTokensRepository();
mockDefTokensRepository = MockDefTokensRepositoryInterface();
mockApiService = MockApiService();
environment = aedappfm.Environment.testnet;
tokenParser = _TokenParserImpl();
Expand Down Expand Up @@ -179,7 +180,6 @@ void main() {
'00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4',
]),
),
any,
),
).thenAnswer(
(_) async => {
Expand Down
Loading

0 comments on commit d37c6a4

Please sign in to comment.