Skip to content

Commit

Permalink
chore(ion-identity-client): Refactor GetWalletsService
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-tychon committed Oct 12, 2024
1 parent 1a7bcbc commit 73b34dc
Show file tree
Hide file tree
Showing 29 changed files with 1,157 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class UserWalletsDialog extends ConsumerWidget {

return SheetContent(
body: FutureBuilder(
future: ionClient(username: username).wallets.listWallets(),
future: ionClient(username: username).wallets.getWallets(),
builder: (context, snapshot) {
final isLoading = !snapshot.hasData;

Expand All @@ -199,10 +199,7 @@ class UserWalletsDialog extends ConsumerWidget {
);
}

final data = switch (snapshot.data) {
ListWalletsSuccess(wallets: final wallets) => wallets,
_ => <Wallet>[],
};
final data = snapshot.data ?? [];

return ListView.builder(
shrinkWrap: true,
Expand Down
1 change: 1 addition & 0 deletions packages/ion_identity_client/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ analyzer:
lines_longer_than_80_chars: ignore
public_member_api_docs: ignore
unnecessary_library_directive: ignore
invalid_annotation_target: ignore
plugins:
- custom_lint
exclude:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'user_wallets_provider.g.dart';

@riverpod
Future<ListWalletsResult> userWallets(UserWalletsRef ref, String username) async {
Future<List<Wallet>> userWallets(UserWalletsRef ref, String username) async {
final ionClient = await ref.watch(ionClientProvider.future);
return ionClient(username: username).wallets.listWallets();
return ionClient(username: username).wallets.getWallets();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,68 +33,73 @@ class _Body extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final username = ref.watch(currentUsernameNotifierProvider) ?? 'ERROR';

final expandedWallets = useState(<int, bool>{});

final walletsResult = ref.watch(userWalletsProvider(username));
if (walletsResult.isLoading) {
return const Center(

return walletsResult.when(
loading: () => const Center(
child: CircularProgressIndicator(),
);
}
),
error: (error, stack) => Center(
child: Text(error.toString()),
),
data: (wallets) => _SuccessState(
wallets: wallets,
),
);
}
}

if (walletsResult.hasError) {
return Center(
child: Text(walletsResult.error.toString()),
);
}
class _SuccessState extends HookWidget {
const _SuccessState({
required this.wallets,
});

final wallets = walletsResult.requireValue;
final List<Wallet> wallets;

return switch (wallets) {
ListWalletsSuccess(:final wallets) => SingleChildScrollView(
child: ExpansionPanelList(
expandedHeaderPadding: EdgeInsets.zero,
expansionCallback: (panelIndex, isExpanded) {
expandedWallets.value = {
...expandedWallets.value,
panelIndex: isExpanded,
};
},
children: wallets.indexed.map(
(e) {
final index = e.$1;
final wallet = e.$2;
@override
Widget build(BuildContext context) {
final expandedWallets = useState(<int, bool>{});

return ExpansionPanel(
isExpanded: expandedWallets.value[index] ?? false,
headerBuilder: (context, isExpanded) => ListTile(
title: Text('name: ${wallet.name}'),
subtitle: Text('network: ${wallet.network}'),
),
body: ListTile(
title: Row(
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => WalletAssetsPage(walletId: wallet.id),
),
);
},
child: const Text('Assets'),
),
],
return SingleChildScrollView(
child: ExpansionPanelList(
expandedHeaderPadding: EdgeInsets.zero,
expansionCallback: (panelIndex, isExpanded) {
expandedWallets.value = {
...expandedWallets.value,
panelIndex: isExpanded,
};
},
children: wallets.indexed.map(
(e) {
final index = e.$1;
final wallet = e.$2;

return ExpansionPanel(
isExpanded: expandedWallets.value[index] ?? false,
headerBuilder: (context, isExpanded) => ListTile(
title: Text('name: ${wallet.name}'),
subtitle: Text('network: ${wallet.network}'),
),
body: ListTile(
title: Row(
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => WalletAssetsPage(walletId: wallet.id),
),
);
},
child: const Text('Assets'),
),
),
);
},
).toList(),
),
),
_ => Center(
child: Text(walletsResult.error.toString()),
),
};
],
),
),
);
},
).toList(),
),
);
}
}
4 changes: 2 additions & 2 deletions packages/ion_identity_client/lib/ion_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

library ion_identity_client;

export 'src/auth/dtos/wallet.dart';
export 'src/auth/ion_auth.dart';
export 'src/auth/result_types/result_types.dart';
export 'src/core/types/ion_exception.dart';
export 'src/ion_api_client.dart';
export 'src/ion_client_config.dart';
export 'src/wallets/services/get_wallet_assets/exceptions/wallet_assets_exception.dart';
export 'src/wallets/services/get_wallet_assets/types/wallet_assets.dart';
export 'src/wallets/types/list_wallets_result.dart';
export 'src/wallets/types/wallet.dart';
export 'src/wallets/types/wallet_signing_key.dart';
1 change: 0 additions & 1 deletion packages/ion_identity_client/lib/src/auth/dtos/dtos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ export 'register_complete_wallet.dart';
export 'register_init_request.dart';
export 'signed_challenge.dart';
export 'user.dart';
export 'wallet.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,29 @@
import 'package:ion_identity_client/src/auth/dtos/authentication.dart';
import 'package:ion_identity_client/src/auth/dtos/credential.dart';
import 'package:ion_identity_client/src/auth/dtos/user.dart';
import 'package:ion_identity_client/src/auth/dtos/wallet.dart';
import 'package:ion_identity_client/src/core/types/types.dart';

class RegistrationCompleteResponse {
RegistrationCompleteResponse({
required this.credential,
required this.user,
required this.authentication,
required this.wallets,
});

factory RegistrationCompleteResponse.fromJson(JsonObject json) {
return RegistrationCompleteResponse(
credential: Credential.fromJson(json['credential'] as JsonObject),
user: User.fromJson(json['user'] as JsonObject),
authentication: Authentication.fromJson(json['authentication'] as JsonObject),
wallets: List<Wallet>.from(
(json['wallets'] as List<dynamic>).map<Wallet>(
(x) => Wallet.fromJson(x as JsonObject),
),
),
);
}

final Credential credential;
final User user;
final Authentication authentication;
final List<Wallet> wallets;

@override
String toString() {
return 'RegistrationCompleteResponse(credential: $credential, user: $user, authentication: $authentication, wallets: $wallets)';
return 'RegistrationCompleteResponse(credential: $credential, user: $user, authentication: $authentication)';
}
}
26 changes: 0 additions & 26 deletions packages/ion_identity_client/lib/src/auth/dtos/wallet.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'package:ion_identity_client/src/wallets/ion_wallets.dart';
import 'package:ion_identity_client/src/wallets/ion_wallets_data_source.dart';
import 'package:ion_identity_client/src/wallets/services/get_wallet_assets/data_sources/get_wallet_assets_data_source.dart';
import 'package:ion_identity_client/src/wallets/services/get_wallet_assets/get_wallet_assets_service.dart';
import 'package:ion_identity_client/src/wallets/services/get_wallets/data_sources/get_wallets_data_source.dart';
import 'package:ion_identity_client/src/wallets/services/get_wallets/get_wallets_service.dart';

class ClientsServiceLocator with _IonClient, _AuthClient, _WalletsClient, _UserActionSigner {
factory ClientsServiceLocator() {
Expand Down Expand Up @@ -182,6 +184,7 @@ mixin _WalletsClient {
config: config,
signer: signer,
),
getWalletsService: createGetWalletsService(username: username, config: config),
getWalletAssetsService: createGetWalletAssetsService(
username: username,
config: config,
Expand All @@ -201,6 +204,25 @@ mixin _WalletsClient {
);
}

GetWalletsService createGetWalletsService({
required String username,
required IonClientConfig config,
}) {
return GetWalletsService(
username,
createGetWalletsDataSource(config: config),
);
}

GetWalletsDataSource createGetWalletsDataSource({
required IonClientConfig config,
}) {
return GetWalletsDataSource(
IonServiceLocator.getNetworkClient2(config: config),
IonServiceLocator.getTokenStorage(),
);
}

GetWalletAssetsService createGetWalletAssetsService({
required String username,
required IonClientConfig config,
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 73b34dc

Please sign in to comment.