Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sang/postcard/stamping faster #1334

Merged
merged 8 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets
7 changes: 3 additions & 4 deletions lib/screen/interactive_postcard/postcard_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PostcardDetailBloc
.where((element) => element.id == event.identity.id)
.toList();
if (assetToken.isNotEmpty) {
final paths = getUpdatingPath(state, assetToken.first);
final paths = getUpdatingPath(assetToken.first);
emit(state.copyWith(
assetToken: assetToken.first,
provenances: assetToken.first.provenance,
Expand All @@ -88,7 +88,7 @@ class PostcardDetailBloc
await tokenService.reindexAddresses([event.identity.owner]);
final assetToken = await _assetTokenDao.findAssetTokenByIdAndOwner(
event.identity.id, event.identity.owner);
final paths = getUpdatingPath(state, assetToken);
final paths = getUpdatingPath(assetToken);
emit(state.copyWith(
assetToken: assetToken,
imagePath: paths.first,
Expand Down Expand Up @@ -179,8 +179,7 @@ class PostcardDetailBloc
}
}

Pair<String?, String?> getUpdatingPath(
PostcardDetailState state, AssetToken? asset) {
Pair<String?, String?> getUpdatingPath(AssetToken? asset) {
String? imagePath;
String? metadataPath;
if (asset != null) {
Expand Down
25 changes: 2 additions & 23 deletions lib/screen/interactive_postcard/postcard_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import 'package:autonomy_flutter/util/share_helper.dart';
import 'package:autonomy_flutter/util/string_ext.dart';
import 'package:autonomy_flutter/util/ui_helper.dart';
import 'package:autonomy_flutter/view/artwork_common_widget.dart';
import 'package:autonomy_flutter/view/dot_loading_indicator.dart';
import 'package:autonomy_flutter/view/external_link.dart';
import 'package:autonomy_flutter/view/postcard_button.dart';
import 'package:autonomy_flutter/view/postcard_chat.dart';
Expand Down Expand Up @@ -368,10 +367,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
.any((element) =>
element.id == assetToken.id &&
element.owner == assetToken.owner);
if (!state.isPostcardUpdating &&
!state.isPostcardUpdatingOnBlockchain &&
state.isStamped &&
!alreadyShowPostcardUpdate) {
if (!alreadyShowPostcardUpdate) {
if (_configurationService.isNotificationEnabled() != true) {
_postcardUpdated(context);
}
Expand Down Expand Up @@ -605,24 +601,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
isViewOnly != false) {
return const SizedBox();
}
if (state.isPostcardUpdatingOnBlockchain || state.isPostcardUpdating) {
return PostcardCustomButton(
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
"updating_token".tr(),
style: theme.textTheme.moMASans700Black14,
),
const Padding(
padding: EdgeInsets.only(bottom: 5),
child: DotsLoading(),
),
],
),
);
}
if (!state.isStamped) {
if (!(asset.isStamping || asset.isStamped)) {
return PostcardButton(
text: "stamp_postcard".tr(),
onTap: () {
Expand Down
4 changes: 0 additions & 4 deletions lib/screen/interactive_postcard/postcard_detail_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ extension PostcardDetailStateExtension on PostcardDetailState {
(isStamping() && postcardValue!.stamped == false);
}

bool get isStamped {
return postcardValue?.stamped ?? false;
}

bool get isLastOwner {
if (postcardValue == null) {
return true;
Expand Down
167 changes: 65 additions & 102 deletions lib/screen/interactive_postcard/stamp_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:autonomy_flutter/common/environment.dart';
import 'package:autonomy_flutter/common/injector.dart';
import 'package:autonomy_flutter/model/postcard_metadata.dart';
import 'package:autonomy_flutter/screen/app_router.dart';
import 'package:autonomy_flutter/screen/detail/artwork_detail_page.dart';
import 'package:autonomy_flutter/screen/interactive_postcard/postcard_detail_bloc.dart';
import 'package:autonomy_flutter/screen/interactive_postcard/postcard_detail_page.dart';
import 'package:autonomy_flutter/screen/interactive_postcard/postcard_detail_state.dart';
Expand All @@ -22,7 +21,6 @@ import 'package:autonomy_flutter/util/moma_style_color.dart';
import 'package:autonomy_flutter/util/share_helper.dart';
import 'package:autonomy_flutter/util/ui_helper.dart';
import 'package:autonomy_flutter/view/back_appbar.dart';
import 'package:autonomy_flutter/view/dot_loading_indicator.dart';
import 'package:autonomy_flutter/view/postcard_button.dart';
import 'package:autonomy_flutter/view/responsive.dart';
import 'package:autonomy_theme/autonomy_theme.dart';
Expand Down Expand Up @@ -74,26 +72,10 @@ class _StampPreviewState extends State<StampPreview> {
super.dispose();
}

void _refreshPostcard() {
log.info("Refresh postcard");
context.read<PostcardDetailBloc>().add(PostcardDetailGetInfoEvent(
ArtworkIdentity(widget.payload.asset.id, widget.payload.asset.owner),
));
}

void _setTimer() {
timer?.cancel();
const duration = Duration(seconds: 10);
timer = Timer.periodic(duration, (timer) {
if (mounted) {
_refreshPostcard();
}
});
}

Future<void> showOptions(BuildContext context,
{required AssetToken assetToken, Function()? callBack}) async {
final theme = Theme.of(context);
bool isProcessing = false;
final options = [
OptionItem(
title: "stamp_minted".tr(),
Expand Down Expand Up @@ -124,6 +106,7 @@ class _StampPreviewState extends State<StampPreview> {
),
),
onTap: () async {
isProcessing = true;
shareToTwitter(token: assetToken);
Navigator.of(context).pop();
await callBack?.call();
Expand All @@ -146,6 +129,7 @@ class _StampPreviewState extends State<StampPreview> {
),
),
onTap: () async {
isProcessing = true;
try {
await _postcardService.downloadStamp(
tokenId: assetToken.tokenId!,
Expand All @@ -171,41 +155,45 @@ class _StampPreviewState extends State<StampPreview> {
}
},
),
OptionItem(
title: 'download_postcard'.tr(),
icon: SvgPicture.asset(
'assets/images/download.svg',
OptionItem(
title: 'download_postcard'.tr(),
icon: SvgPicture.asset(
'assets/images/download.svg',
width: 24,
height: 24,
),
iconOnProcessing: SvgPicture.asset('assets/images/download.svg',
width: 24,
height: 24,
),
iconOnProcessing: SvgPicture.asset('assets/images/download.svg',
width: 24,
height: 24,
colorFilter: const ColorFilter.mode(
AppColor.disabledColor, BlendMode.srcIn)),
onTap: () async {
try {
await _postcardService.downloadPostcard(assetToken.tokenId!);
if (!mounted) return;
Navigator.of(context).pop();
await UIHelper.showPostcardSaved(context);
} catch (e) {
log.info("Download postcard failed: error ${e.toString()}");
if (!mounted) return;
Navigator.of(context).pop();
switch (e.runtimeType) {
case MediaPermissionException:
await UIHelper.showPostcardPhotoAccessFailed(context);
break;
default:
if (!mounted) return;
await UIHelper.showPostcardSavedFailed(context);
}
colorFilter: const ColorFilter.mode(
AppColor.disabledColor, BlendMode.srcIn)),
onTap: () async {
isProcessing = true;
try {
await _postcardService.downloadPostcard(assetToken.tokenId!);
if (!mounted) return;
Navigator.of(context).pop();
await UIHelper.showPostcardSaved(context);
} catch (e) {
log.info("Download postcard failed: error ${e.toString()}");
if (!mounted) return;
Navigator.of(context).pop();
switch (e.runtimeType) {
case MediaPermissionException:
await UIHelper.showPostcardPhotoAccessFailed(context);
break;
default:
if (!mounted) return;
await UIHelper.showPostcardSavedFailed(context);
}
},
),
}
},
),
];
await UIHelper.showPostcardDrawerAction(context, options: options);
await UIHelper.showPostcardDrawerAction(context, options: options)
.then((value) {
if (!isProcessing) callBack?.call();
});
}

@override
Expand Down Expand Up @@ -250,31 +238,7 @@ class _StampPreviewState extends State<StampPreview> {
statusBarColor: backgroundColor,
),
body: BlocConsumer<PostcardDetailBloc, PostcardDetailState>(
listener: (context, state) {
if (!(state.isPostcardUpdatingOnBlockchain ||
state.isPostcardUpdating)) {
final assetToken = state.assetToken;
if (assetToken == null) {
return;
}
timer?.cancel();
if (alreadyShowPopup) {
return;
}
alreadyShowPopup = true;
showOptions(context, assetToken: assetToken, callBack: () {
log.info("Popup closed");
_navigationService.popUntilHomeOrSettings();
if (!mounted) return;
Navigator.of(context).pushNamed(
AppRouter.claimedPostcardDetailsPage,
arguments:
PostcardDetailPagePayload([assetToken.identity], 0),
);
_configurationService.setAutoShowPostcard(true);
});
}
},
listener: (context, state) {},
builder: (context, state) {
final assetToken = widget.payload.asset;
final imagePath = widget.payload.imagePath;
Expand Down Expand Up @@ -305,45 +269,45 @@ class _StampPreviewState extends State<StampPreview> {
);
}

Future<void> onConfirmed(AssetToken assetToken) async {
if (alreadyShowPopup) {
return;
}
alreadyShowPopup = true;
showOptions(context, assetToken: assetToken, callBack: () {
log.info("Popup closed");
if (!mounted) return;
_navigationService.popUntilHomeOrSettings();
Navigator.of(context).pushNamed(
AppRouter.claimedPostcardDetailsPage,
arguments: PostcardDetailPagePayload([assetToken.identity], 0),
);
_configurationService.setAutoShowPostcard(true);
});
}

Widget _postcardAction(PostcardDetailState state) {
final theme = Theme.of(context);
if (!confirming) {
return PostcardAsyncButton(
text: widget.payload.asset.isCompleted
? "complete_postcard_journey_".tr()
: "confirm_your_design".tr(),
fontSize: 18,
onTap: () async {
final isSuccess = await _onConfirm();
if (mounted && isSuccess) {
final assetToken = await _onConfirm();
if (mounted && assetToken != null) {
setState(() {
confirming = true;
});
await onConfirmed(assetToken);
}
},
);
}
if (state.isPostcardUpdatingOnBlockchain || state.isPostcardUpdating) {
return PostcardCustomButton(
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
"updating_token".tr(),
style: theme.textTheme.moMASans700Black14.copyWith(fontSize: 18),
),
const Padding(
padding: EdgeInsets.only(bottom: 5),
child: DotsLoading(),
),
],
),
);
}
return const SizedBox();
}

Future<bool> _onConfirm() async {
Future<AssetToken?> _onConfirm() async {
final imagePath = widget.payload.imagePath;
final metadataPath = widget.payload.metadataPath;
File imageFile = File(imagePath);
Expand All @@ -358,7 +322,7 @@ class _StampPreviewState extends State<StampPreview> {
final walletIndex = await asset.getOwnerWallet();
if (walletIndex == null) {
log.info("[POSTCARD] Wallet index not found");
return false;
return null;
}

final isStampSuccess = await _postcardService.stampPostcard(
Expand All @@ -373,7 +337,7 @@ class _StampPreviewState extends State<StampPreview> {
if (!isStampSuccess) {
log.info("[POSTCARD] Stamp failed");
injector<NavigationService>().popUntilHomeOrSettings();
return false;
return null;
} else {
log.info("[POSTCARD] Stamp success");
_metricClientService.addEvent(MixpanelEvent.postcardStamp, data: {
Expand All @@ -389,23 +353,22 @@ class _StampPreviewState extends State<StampPreview> {
counter: counter,
)
]);

AssetToken? pendingToken;
if (widget.payload.location != null) {
var postcardMetadata = asset.postcardMetadata;
final stampedLocation = widget.payload.location!;
postcardMetadata.locationInformation.add(stampedLocation);
var newAsset = asset.asset;
newAsset?.artworkMetadata = jsonEncode(postcardMetadata.toJson());
final pendingToken = asset.copyWith(asset: newAsset);
pendingToken = asset.copyWith(asset: newAsset);
await _tokenService.setCustomTokens([pendingToken]);
_tokenService.reindexAddresses([address]);
NftCollectionBloc.eventController.add(
GetTokensByOwnerEvent(pageKey: PageKey.init()),
);
_setTimer();
}
return pendingToken;
}
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class _PostcardTravelInfoState extends State<PostcardTravelInfo> {
final theme = Theme.of(context);
final travelInfoWithoutInternetUser =
asset.postcardMetadata.listTravelInfoWithoutLocationName;
final currentStampNumber = asset.getArtists.length;
final stampingPostcard = asset.isStamping ? 1 : 0;
final currentStampNumber = asset.getArtists.length + stampingPostcard;
final numberFormatter = NumberFormat();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down
Loading
Loading