From b3d9ba6e1f0c88450cd042bb3f42efe08e89f8f8 Mon Sep 17 00:00:00 2001 From: PuPha Date: Thu, 7 Dec 2023 12:07:46 +0700 Subject: [PATCH] Return to sender --- .../claim_empty_postcard_screen.dart | 2 +- .../pay_to_mint_postcard_screen.dart | 14 ++-- .../interactive_postcard/design_stamp.dart | 9 +-- .../hand_signature_page.dart | 5 +- .../postcard_detail_page.dart | 4 +- .../postcard_explain.dart | 1 + .../interactive_postcard/prompt_page.dart | 4 +- .../interactive_postcard/stamp_preview.dart | 40 ++++++----- .../receive_postcard_page.dart | 66 +++++++----------- lib/service/navigation_service.dart | 10 ++- lib/service/postcard_service.dart | 67 ++++++++++++------- 11 files changed, 119 insertions(+), 103 deletions(-) diff --git a/lib/screen/interactive_postcard/claim_empty_postcard/claim_empty_postcard_screen.dart b/lib/screen/interactive_postcard/claim_empty_postcard/claim_empty_postcard_screen.dart index 35f2d08f0..dc30f6135 100644 --- a/lib/screen/interactive_postcard/claim_empty_postcard/claim_empty_postcard_screen.dart +++ b/lib/screen/interactive_postcard/claim_empty_postcard/claim_empty_postcard_screen.dart @@ -56,7 +56,7 @@ class _ClaimEmptyPostCardScreenState extends State { listener: (context, state) { if (state.isClaimed == true) { unawaited(injector() - .selectPromptsThenStamp(context, state.assetToken!)); + .selectPromptsThenStamp(context, state.assetToken!, null)); } if (state.error != null) { _handleError(state.error!); diff --git a/lib/screen/interactive_postcard/claim_empty_postcard/pay_to_mint_postcard_screen.dart b/lib/screen/interactive_postcard/claim_empty_postcard/pay_to_mint_postcard_screen.dart index cc136dde4..5302c83d6 100644 --- a/lib/screen/interactive_postcard/claim_empty_postcard/pay_to_mint_postcard_screen.dart +++ b/lib/screen/interactive_postcard/claim_empty_postcard/pay_to_mint_postcard_screen.dart @@ -79,12 +79,14 @@ class _PayToMintPostcardScreenState extends State { enabled: state.isClaiming != true, isProcessing: state.isClaiming == true, onTap: () { - unawaited(injector() - .selectPromptsThenStamp( - context, - state.assetToken!.copyWith( - owner: widget.claimRequest.address, - tokenId: widget.claimRequest.tokenId))); + unawaited( + injector().selectPromptsThenStamp( + context, + state.assetToken!.copyWith( + owner: widget.claimRequest.address, + tokenId: widget.claimRequest.tokenId), + null, + )); }, color: POSTCARD_GREEN_BUTTON_COLOR, ), diff --git a/lib/screen/interactive_postcard/design_stamp.dart b/lib/screen/interactive_postcard/design_stamp.dart index ee5d87f9b..f711c1cf9 100644 --- a/lib/screen/interactive_postcard/design_stamp.dart +++ b/lib/screen/interactive_postcard/design_stamp.dart @@ -270,9 +270,9 @@ class _DesignStampPageState extends State { await Navigator.of(context).pushNamed( HandSignaturePage.handSignaturePage, arguments: HandSignaturePayload( - bytes!, - widget.payload.asset, - )); + bytes!, + widget.payload.asset, + widget.payload.shareCode)); setState(() { _line = true; @@ -438,6 +438,7 @@ class StampPainter extends CustomPainter { class DesignStampPayload { final AssetToken asset; final bool allowPop; + final String? shareCode; - DesignStampPayload(this.asset, this.allowPop); + DesignStampPayload(this.asset, this.allowPop, this.shareCode); } diff --git a/lib/screen/interactive_postcard/hand_signature_page.dart b/lib/screen/interactive_postcard/hand_signature_page.dart index 1555e86c2..e6914aca1 100644 --- a/lib/screen/interactive_postcard/hand_signature_page.dart +++ b/lib/screen/interactive_postcard/hand_signature_page.dart @@ -274,7 +274,8 @@ class _HandSignaturePageState extends State { imagePath: imageDataFile.path, metadataPath: metadataFile.path, asset: asset, - location: geoLocation.position))); + location: geoLocation.position, + shareCode: widget.payload.shareCode))); }, color: AppColor.momaGreen, ), @@ -295,9 +296,11 @@ class _HandSignaturePageState extends State { class HandSignaturePayload { final Uint8List image; final AssetToken asset; + final String? shareCode; HandSignaturePayload( this.image, this.asset, + this.shareCode, ); } diff --git a/lib/screen/interactive_postcard/postcard_detail_page.dart b/lib/screen/interactive_postcard/postcard_detail_page.dart index 62cf7a316..76fbb6b00 100644 --- a/lib/screen/interactive_postcard/postcard_detail_page.dart +++ b/lib/screen/interactive_postcard/postcard_detail_page.dart @@ -654,7 +654,7 @@ class ClaimedPostcardDetailPageState extends State onTap: () async { unawaited(injector().popAndPushNamed( AppRouter.designStamp, - arguments: DesignStampPayload(asset, false))); + arguments: DesignStampPayload(asset, false, null))); }, color: AppColor.momaGreen, ); @@ -665,7 +665,7 @@ class ClaimedPostcardDetailPageState extends State )); } else { unawaited(injector() - .selectPromptsThenStamp(context, asset)); + .selectPromptsThenStamp(context, asset, null)); } }, color: MoMAColors.moMA8, diff --git a/lib/screen/interactive_postcard/postcard_explain.dart b/lib/screen/interactive_postcard/postcard_explain.dart index c78cbd80c..14eb52bb3 100644 --- a/lib/screen/interactive_postcard/postcard_explain.dart +++ b/lib/screen/interactive_postcard/postcard_explain.dart @@ -448,6 +448,7 @@ class _PostcardExplainState extends State { postcardAspectRatio, child: PostcardViewWidget( assetToken: asset, + withPreviewStamp: true, ), ), ), diff --git a/lib/screen/interactive_postcard/prompt_page.dart b/lib/screen/interactive_postcard/prompt_page.dart index 09ce3fa1a..87d36dd6c 100644 --- a/lib/screen/interactive_postcard/prompt_page.dart +++ b/lib/screen/interactive_postcard/prompt_page.dart @@ -96,8 +96,8 @@ class _PromptPageState extends State { Prompt.getUserPrompt(_controller.text.trim())); await Navigator.of(context).pushNamed( AppRouter.designStamp, - arguments: - DesignStampPayload(assetWithPrompt, true)); + arguments: DesignStampPayload(assetWithPrompt, true, + widget.payload.shareCode)); }, )), Flexible( diff --git a/lib/screen/interactive_postcard/stamp_preview.dart b/lib/screen/interactive_postcard/stamp_preview.dart index 0b8d57542..5f597faf0 100644 --- a/lib/screen/interactive_postcard/stamp_preview.dart +++ b/lib/screen/interactive_postcard/stamp_preview.dart @@ -65,23 +65,31 @@ class _StampPreviewState extends State with AfterLayoutMixin { setState(() { confirming = true; }); - await _postcardService - .finalizeStamp(widget.payload.asset, widget.payload.imagePath, - widget.payload.metadataPath, widget.payload.location) - .then((final bool isStampSuccess) async { - _setTimer(); - if (mounted) { - setState(() { - confirming = false; - }); - if (!isStampSuccess) { - await UIHelper.showPostcardStampFailed(context); - } + try { + await _postcardService + .finalizeStamp( + asset: widget.payload.asset, + imagePath: widget.payload.imagePath, + metadataPath: widget.payload.metadataPath, + location: widget.payload.location, + shareCode: widget.payload.shareCode) + .then((final bool isStampSuccess) async { + _setTimer(); if (mounted) { - _onClose(context); + setState(() { + confirming = false; + }); + if (!isStampSuccess) { + await UIHelper.showPostcardStampFailed(context); + } + if (mounted) { + _onClose(context); + } } - } - }); + }); + } catch (e) { + log.info(e); + } } void _onClose(BuildContext context) { @@ -200,6 +208,7 @@ class StampPreviewPayload { final String imagePath; final String metadataPath; final Location location; + final String? shareCode; // constructor StampPreviewPayload({ @@ -207,6 +216,7 @@ class StampPreviewPayload { required this.imagePath, required this.metadataPath, required this.location, + required this.shareCode, }); } diff --git a/lib/screen/send_receive_postcard/receive_postcard_page.dart b/lib/screen/send_receive_postcard/receive_postcard_page.dart index 8f14e28d2..3bda534b2 100644 --- a/lib/screen/send_receive_postcard/receive_postcard_page.dart +++ b/lib/screen/send_receive_postcard/receive_postcard_page.dart @@ -11,10 +11,8 @@ import 'package:autonomy_flutter/service/navigation_service.dart'; import 'package:autonomy_flutter/service/postcard_service.dart'; import 'package:autonomy_flutter/util/asset_token_ext.dart'; import 'package:autonomy_flutter/util/constants.dart'; -import 'package:autonomy_flutter/util/ui_helper.dart'; import 'package:autonomy_flutter/util/wallet_utils.dart'; import 'package:autonomy_flutter/view/postcard_button.dart'; -import 'package:dio/dio.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -42,6 +40,8 @@ class ReceivePostCardPage extends StatefulWidget { class _ReceivePostCardPageState extends State { final metricClient = injector.get(); final tokenService = injector.get(); + final accountService = injector(); + final postcardService = injector(); late bool _isProcessing; late bool _isConfirming; late AssetToken assetToken; @@ -103,13 +103,9 @@ class _ReceivePostCardPageState extends State { } } - Future _receivePostcard( - BuildContext context, AssetToken asset) async { - final geoLocation = internetUserGeoLocation; - final accountService = injector(); - final addresses = await accountService.getAddress(asset.blockchain); + Future _getAddress(String blockchain) async { + final addresses = await accountService.getAddress(blockchain); String? address; - AssetToken? pendingToken; if (addresses.isEmpty) { final defaultPersona = await accountService.getOrCreateDefaultPersona(); final walletAddress = @@ -133,42 +129,28 @@ class _ReceivePostCardPageState extends State { }, ); } + return address; + } + + Future _receivePostcard( + BuildContext context, AssetToken asset) async { + AssetToken? pendingToken; + final address = await _getAddress(asset.blockchain); if (address != null) { - try { - pendingToken = - await injector.get().claimSharedPostcardToAddress( - address: address, - assetToken: asset, - location: geoLocation.position, - shareCode: widget.shareCode, - ); - if (!mounted) { - return null; - } - unawaited(Navigator.of(context).pushNamedAndRemoveUntil( - AppRouter.homePage, - (route) => false, - )); - unawaited(Navigator.of(context).pushNamed(AppRouter.designStamp, - arguments: DesignStampPayload(pendingToken, false))); - } catch (e) { - if (e is DioException) { - if (!mounted) { - return null; - } - await UIHelper.showAlreadyClaimedPostcard( - context, - e, - ); - if (!mounted) { - return null; - } - unawaited(Navigator.of(context).pushNamedAndRemoveUntil( - AppRouter.homePage, - (route) => false, - )); - } + pendingToken = postcardService.getPendingTokenAfterClaimShare( + address: address, + assetToken: asset, + ); + if (!mounted) { + return null; } + unawaited(Navigator.of(context).pushNamedAndRemoveUntil( + AppRouter.homePage, + (route) => false, + )); + unawaited(Navigator.of(context).pushNamed(AppRouter.designStamp, + arguments: + DesignStampPayload(pendingToken, false, widget.shareCode))); } setState(() { _isProcessing = false; diff --git a/lib/service/navigation_service.dart b/lib/service/navigation_service.dart index 891d02b99..1380551b8 100644 --- a/lib/service/navigation_service.dart +++ b/lib/service/navigation_service.dart @@ -30,9 +30,7 @@ import 'package:autonomy_flutter/util/ui_helper.dart'; import 'package:autonomy_theme/autonomy_theme.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; -import 'package:nft_collection/models/asset_token.dart'; - -// ignore: implementation_imports +import 'package:nft_collection/models/asset_token.dart'; // ignore: implementation_imports import 'package:overlay_support/src/overlay_state_finder.dart'; class NavigationService { @@ -86,7 +84,7 @@ class NavigationService { } Future selectPromptsThenStamp( - BuildContext context, AssetToken asset) async { + BuildContext context, AssetToken asset, String? shareCode) async { final prompts = await injector().getPrompts(asset.tokenId!); if (!mounted) { @@ -94,11 +92,11 @@ class NavigationService { } if (prompts.isEmpty) { await popAndPushNamed(AppRouter.promptPage, - arguments: DesignStampPayload(asset, true)); + arguments: DesignStampPayload(asset, true, shareCode)); } else { final assetWithPrompt = asset.setAssetPrompt(prompts.first); await popAndPushNamed(AppRouter.designStamp, - arguments: DesignStampPayload(assetWithPrompt, true)); + arguments: DesignStampPayload(assetWithPrompt, true, shareCode)); } } diff --git a/lib/service/postcard_service.dart b/lib/service/postcard_service.dart index 3d5e1a1e9..70e335a70 100644 --- a/lib/service/postcard_service.dart +++ b/lib/service/postcard_service.dart @@ -100,15 +100,18 @@ abstract class PostcardService { {required String address, required RequestPostcardResponse requestPostcardResponse}); - Future claimSharedPostcardToAddress({ - required String address, + AssetToken getPendingTokenAfterClaimShare({ required AssetToken assetToken, - required String shareCode, - required Location location, + required String address, }); - Future finalizeStamp(AssetToken asset, String imagePath, - String metadataPath, Location location); + Future finalizeStamp({ + required AssetToken asset, + required String imagePath, + required String metadataPath, + required Location location, + required String? shareCode, + }); Future> getPrompts(String tokenId); } @@ -574,16 +577,8 @@ class PostcardServiceImpl extends PostcardService { } @override - Future claimSharedPostcardToAddress( - {required String address, - required AssetToken assetToken, - required String shareCode, - required Location location}) async { - await receivePostcard( - shareCode: shareCode, - location: location, - address: address, - ); + AssetToken getPendingTokenAfterClaimShare( + {required AssetToken assetToken, required String address}) { var postcardMetadata = assetToken.postcardMetadata; log.info( 'claimSharedPostcardToAddress metadata ${postcardMetadata.toJson()}'); @@ -597,18 +592,16 @@ class PostcardServiceImpl extends PostcardService { balance: 1, owners: newOwners, ); - - await _tokensService.setCustomTokens([pendingToken]); - unawaited(_tokensService.reindexAddresses([address])); - NftCollectionBloc.eventController.add( - GetTokensByOwnerEvent(pageKey: PageKey.init()), - ); return pendingToken; } @override - Future finalizeStamp(AssetToken asset, String imagePath, - String metadataPath, Location location) async { + Future finalizeStamp( + {required AssetToken asset, + required String imagePath, + required String metadataPath, + required Location location, + required String? shareCode}) async { File imageFile = File(imagePath); File metadataFile = File(metadataPath); @@ -636,6 +629,32 @@ class PostcardServiceImpl extends PostcardService { await _configurationService.setProcessingStampPostcard([ processingStampPostcard, ]); + if (shareCode != null) { + try { + await receivePostcard( + shareCode: shareCode, + location: location, + address: address, + ); + } catch (e) { + if (e is DioException) { + if (!mounted) { + return null; + } + await UIHelper.showAlreadyClaimedPostcard( + context, + e, + ); + if (!mounted) { + return null; + } + unawaited(Navigator.of(context).pushNamedAndRemoveUntil( + AppRouter.homePage, + (route) => false, + )); + } + } + } final isStampSuccess = await stampPostcard( tokenId, walletIndex.first,