Skip to content

Commit

Permalink
refactor: Move logic from helper function to enum or model
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Aug 18, 2023
1 parent f2ad82b commit 1733756
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 114 deletions.
72 changes: 0 additions & 72 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,6 @@ import 'package:key_generator/key_generator.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:secure_storage/secure_storage.dart';

Future<void> openBlockchainExplorer(
BlockchainNetwork network,
String txHash,
) async {
if (network is TezosNetwork) {
await LaunchUrl.launch(
'https://tzkt.io/$txHash',
);
} else if (network is PolygonNetwork) {
await LaunchUrl.launch(
'https://polygonscan.com/tx/$txHash',
);
} else if (network is BinanceNetwork) {
await LaunchUrl.launch(
'https://www.bscscan.com/tx/$txHash',
);
} else if (network is FantomNetwork) {
await LaunchUrl.launch(
'https://ftmscan.com/tx/$txHash',
);
} else if (network is EthereumNetwork) {
await LaunchUrl.launch(
'https://etherscan.io/tx/$txHash',
);
} else {
UnimplementedError();
}
}

Future<void> openAddressBlockchainExplorer(
BlockchainNetwork network,
String address,
) async {
if (network is TezosNetwork) {
await LaunchUrl.launch(
'https://tzkt.io/$address/operations',
);
} else if (network is PolygonNetwork) {
await LaunchUrl.launch(
'https://polygonscan.com/address/$address',
);
} else if (network is BinanceNetwork) {
await LaunchUrl.launch(
'https://www.bscscan.com/address/$address',
);
} else if (network is FantomNetwork) {
await LaunchUrl.launch(
'https://ftmscan.com/address/$address',
);
} else if (network is EthereumNetwork) {
await LaunchUrl.launch(
'https://etherscan.io/address/$address',
);
} else {
UnimplementedError();
}
}

String generateDefaultAccountName(
int accountIndex,
List<String> accountNameList,
Expand Down Expand Up @@ -104,10 +46,6 @@ String getIssuerDid({required Uri uriToCheck}) {
return did;
}

bool isPolygonssuer(CredentialModel credentialModel) {
return credentialModel.issuer.contains('did:polygonid');
}

bool isValidPrivateKey(String value) {
bool isEthereumPrivateKey = false;
if (RegExp(r'^(0x)?[0-9a-f]{64}$', caseSensitive: false).hasMatch(value)) {
Expand Down Expand Up @@ -331,16 +269,6 @@ Future<String> getRandomP256PrivateKey(
}
}

bool isVerifiableDiplomaType(CredentialModel credentialModel) {
return credentialModel.credentialPreview.type.contains('VerifiableDiploma');
}

bool isPolygonIdCard(CredentialModel credentialModel) {
return credentialModel.credentialPreview.credentialSubjectModel.id
?.contains('did:polygonid:polygon:') ??
false;
}

Map<String, dynamic> decodePayload({
required JWTDecode jwtDecode,
required String token,
Expand Down
52 changes: 52 additions & 0 deletions lib/app/shared/models/blockchain_network/blockchain_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,56 @@ class BlockchainNetwork extends Equatable {
'rpcNodeUrl: $rpcNodeUrl, apiKey: $apiKey ,title:$title '
',subTitle:$subTitle, type: $type}';
}

Future<void> openBlockchainExplorer(String txHash) async {
if (this is TezosNetwork) {
await LaunchUrl.launch(
'https://tzkt.io/$txHash',
);
} else if (this is PolygonNetwork) {
await LaunchUrl.launch(
'https://polygonscan.com/tx/$txHash',
);
} else if (this is BinanceNetwork) {
await LaunchUrl.launch(
'https://www.bscscan.com/tx/$txHash',
);
} else if (this is FantomNetwork) {
await LaunchUrl.launch(
'https://ftmscan.com/tx/$txHash',
);
} else if (this is EthereumNetwork) {
await LaunchUrl.launch(
'https://etherscan.io/tx/$txHash',
);
} else {
UnimplementedError();
}
}

Future<void> openAddressBlockchainExplorer(String address) async {
if (this is TezosNetwork) {
await LaunchUrl.launch(
'https://tzkt.io/$address/operations',
);
} else if (this is PolygonNetwork) {
await LaunchUrl.launch(
'https://polygonscan.com/address/$address',
);
} else if (this is BinanceNetwork) {
await LaunchUrl.launch(
'https://www.bscscan.com/address/$address',
);
} else if (this is FantomNetwork) {
await LaunchUrl.launch(
'https://ftmscan.com/address/$address',
);
} else if (this is EthereumNetwork) {
await LaunchUrl.launch(
'https://etherscan.io/address/$address',
);
} else {
UnimplementedError();
}
}
}
17 changes: 5 additions & 12 deletions lib/credentials/cubit/credentials_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ class CredentialsCubit extends Cubit<CredentialsState> {

/// manually categorizing default credential
for (final credential in savedCredentials) {
final isDefaultCredential = credential
.credentialPreview.credentialSubjectModel.credentialSubjectType ==
CredentialSubjectType.defaultCredential;

if (isDefaultCredential && isVerifiableDiplomaType(credential)) {
if (credential.isDefaultCredential &&
credential.isVerifiableDiplomaType) {
final updatedCredential = credential.copyWith(
credentialPreview: credential.credentialPreview.copyWith(
credentialSubjectModel:
Expand All @@ -74,7 +71,7 @@ class CredentialsCubit extends Cubit<CredentialsState> {
),
);
updatedCredentials.add(updatedCredential);
} else if (isDefaultCredential && isPolygonIdCard(credential)) {
} else if (credential.isDefaultCredential && credential.isPolygonIdCard) {
final updatedCredential = credential.copyWith(
credentialPreview: credential.credentialPreview.copyWith(
credentialSubjectModel:
Expand Down Expand Up @@ -206,11 +203,7 @@ class CredentialsCubit extends Cubit<CredentialsState> {
}) async {
late final List<CredentialModel> credentials;

final isDefaultCredential = credential
.credentialPreview.credentialSubjectModel.credentialSubjectType ==
CredentialSubjectType.defaultCredential;

if (isDefaultCredential && isVerifiableDiplomaType(credential)) {
if (credential.isDefaultCredential && credential.isVerifiableDiplomaType) {
final updatedCredential = credential.copyWith(
credentialPreview: credential.credentialPreview.copyWith(
credentialSubjectModel:
Expand All @@ -222,7 +215,7 @@ class CredentialsCubit extends Cubit<CredentialsState> {
await replaceCredential(credential: updatedCredential);
await credentialsRepository.insert(updatedCredential);
credentials = List.of(state.credentials)..add(updatedCredential);
} else if (isDefaultCredential && isPolygonIdCard(credential)) {
} else if (credential.isDefaultCredential && credential.isPolygonIdCard) {
final updatedCredential = credential.copyWith(
credentialPreview: credential.credentialPreview.copyWith(
credentialSubjectModel:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CredentialDetailsCubit extends Cubit<CredentialDetailsState> {
status: AppStatus.idle,
),
);
} else if (isPolygonssuer(item)) {
} else if (item.isPolygonssuer) {
final mnemonic =
await secureStorageProvider.get(SecureStorageKeys.ssiMnemonic);
await polygonIdCubit.initialise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ class CredentialModel extends Equatable {
return map['id'] as String?;
}

bool get isPolygonssuer => issuer.contains('did:polygonid');

bool get isVerifiableDiplomaType =>
credentialPreview.type.contains('VerifiableDiploma');

bool get isPolygonIdCard =>
credentialPreview.credentialSubjectModel.id
?.contains('did:polygonid:polygon:') ??
false;

bool get isDefaultCredential =>
credentialPreview.credentialSubjectModel.credentialSubjectType ==
CredentialSubjectType.defaultCredential;

@override
List<Object?> get props => [
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CredentialDisplay extends StatelessWidget {
}

case CredentialSubjectType.defaultCredential:
if (isPolygonIdCard(credentialModel)) {
if (credentialModel.isPolygonIdCard) {
return DefaultPolygonIdCardWidget(credentialModel: credentialModel);
} else {
switch (credDisplayType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CredentialManifestCard extends StatelessWidget {

@override
Widget build(BuildContext context) {
final textColor = isVerifiableDiplomaType(credentialModel)
final textColor = credentialModel.isVerifiableDiplomaType
? Colors.white
: getColorFromCredential(outputDescriptor.styles?.text, Colors.black);
final credential = Credential.fromJsonOrDummy(credentialModel.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DefaultCredentialDetailWidget extends StatelessWidget {
decoration: BaseBoxDecoration(
borderRadius: BorderRadius.circular(Sizes.credentialBorderRadius),
color: backgroundColor,
gradient: isVerifiableDiplomaType(credentialModel)
gradient: credentialModel.isVerifiableDiplomaType
? const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DefaultCredentialListWidget extends StatelessWidget {
child: DecoratedBox(
decoration: BaseBoxDecoration(
color: backgroundColor,
gradient: isVerifiableDiplomaType(credentialModel)
gradient: credentialModel.isVerifiableDiplomaType
? const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
Expand Down
45 changes: 25 additions & 20 deletions lib/dashboard/home/tab_bar/nft/view/nft_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ class _NftDetailsViewState extends State<NftDetailsView> {
size: Sizes.icon,
),
onPressed: () {
openAddressBlockchainExplorer(
context.read<ManageNetworkCubit>().state.network,
nftModel.contractAddress,
);
context
.read<ManageNetworkCubit>()
.state
.network
.openAddressBlockchainExplorer(nftModel.contractAddress);
},
),
],
Expand Down Expand Up @@ -265,10 +266,11 @@ class _NftDetailsViewState extends State<NftDetailsView> {
size: Sizes.icon,
),
onPressed: () {
openAddressBlockchainExplorer(
context.read<ManageNetworkCubit>().state.network,
nftModel.creators!.first,
);
context
.read<ManageNetworkCubit>()
.state
.network
.openAddressBlockchainExplorer(nftModel.creators!.first);
},
),
],
Expand Down Expand Up @@ -296,10 +298,11 @@ class _NftDetailsViewState extends State<NftDetailsView> {
size: Sizes.icon,
),
onPressed: () {
openAddressBlockchainExplorer(
context.read<ManageNetworkCubit>().state.network,
nftModel.publishers!.first,
);
context
.read<ManageNetworkCubit>()
.state
.network
.openAddressBlockchainExplorer(nftModel.publishers!.first);
},
),
],
Expand Down Expand Up @@ -348,10 +351,11 @@ class _NftDetailsViewState extends State<NftDetailsView> {
size: Sizes.icon,
),
onPressed: () {
openAddressBlockchainExplorer(
context.read<ManageNetworkCubit>().state.network,
nftModel.contractAddress,
);
context
.read<ManageNetworkCubit>()
.state
.network
.openAddressBlockchainExplorer(nftModel.contractAddress);
},
),
],
Expand All @@ -378,10 +382,11 @@ class _NftDetailsViewState extends State<NftDetailsView> {
size: Sizes.icon,
),
onPressed: () {
openAddressBlockchainExplorer(
context.read<ManageNetworkCubit>().state.network,
nftModel.minterAddress!,
);
context
.read<ManageNetworkCubit>()
.state
.network
.openAddressBlockchainExplorer(nftModel.minterAddress!);
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class _ConfirmWithdrawalViewState extends State<ConfirmWithdrawalView> {
onTrasactionHashTap: () {
final network = context.read<ManageNetworkCubit>().state.network;
if (state.transactionHash != null) {
openBlockchainExplorer(network, state.transactionHash!);
network.openBlockchainExplorer(state.transactionHash!);
}
},
onDoneButtonClick: () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ class RecentTransactions extends StatelessWidget {
.read<ManageNetworkCubit>()
.state
.network;
openBlockchainExplorer(
network,
operations[index].hash,
);
network
.openBlockchainExplorer(operations[index].hash);
},
),
separatorBuilder: (_, __) {
Expand Down

0 comments on commit 1733756

Please sign in to comment.