stack_wallet/lib/pages/paynym/paynym_claim_view.dart

164 lines
5.6 KiB
Dart
Raw Normal View History

2022-12-20 21:05:11 +00:00
import 'dart:async';
2022-12-20 20:48:31 +00:00
import 'package:flutter/material.dart';
2022-12-20 23:00:03 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/pages/paynym/dialogs/claiming_paynym_dialog.dart';
2022-12-21 23:02:14 +00:00
import 'package:stackwallet/pages/paynym/paynym_home_view.dart';
import 'package:stackwallet/pages/wallet_view/wallet_view.dart';
2022-12-20 23:00:03 +00:00
import 'package:stackwallet/providers/global/paynym_api_provider.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/services/coins/coin_paynym_extension.dart';
import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart';
2022-12-20 20:48:31 +00:00
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
2022-12-20 23:00:03 +00:00
class PaynymClaimView extends ConsumerStatefulWidget {
const PaynymClaimView({
Key? key,
required this.walletId,
}) : super(key: key);
2022-12-20 21:05:11 +00:00
2022-12-20 23:00:03 +00:00
final String walletId;
2022-12-20 20:48:31 +00:00
static const String routeName = "/claimPaynym";
@override
2022-12-20 23:00:03 +00:00
ConsumerState<PaynymClaimView> createState() => _PaynymClaimViewState();
2022-12-20 20:48:31 +00:00
}
2022-12-20 23:00:03 +00:00
class _PaynymClaimViewState extends ConsumerState<PaynymClaimView> {
2022-12-20 20:48:31 +00:00
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
final isDesktop = Util.isDesktop;
return MasterScaffold(
isDesktop: isDesktop,
appBar: AppBar(
leading: AppBarBackButton(
onPressed: () {
Navigator.of(context).pop();
},
),
titleSpacing: 0,
title: Text(
"PayNym",
style: STextStyles.navBarTitle(context),
overflow: TextOverflow.ellipsis,
),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
const Spacer(
flex: 1,
),
Image(
image: AssetImage(
Assets.png.stack,
),
width: MediaQuery.of(context).size.width / 2,
),
const SizedBox(
height: 20,
),
Text(
"You do not have a PayNym yet.\nClaim yours now!",
style: STextStyles.baseXS(context).copyWith(
color:
Theme.of(context).extension<StackColors>()!.textSubtitle1,
),
textAlign: TextAlign.center,
),
const Spacer(
flex: 2,
),
PrimaryButton(
label: "Claim",
2022-12-20 21:05:11 +00:00
onPressed: () async {
bool shouldCancel = false;
unawaited(
showDialog<bool?>(
context: context,
2022-12-21 19:46:50 +00:00
barrierDismissible: false,
2022-12-20 21:05:11 +00:00
builder: (context) => const ClaimingPaynymDialog(),
).then((value) => shouldCancel = value == true),
);
2022-12-21 16:17:53 +00:00
// ghet wallet to access paynym calls
2022-12-20 23:00:03 +00:00
final wallet = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as DogecoinWallet;
2022-12-21 16:17:53 +00:00
// get payment code
2022-12-20 23:00:03 +00:00
final pCode = await wallet.getPaymentCode();
2022-12-21 16:17:53 +00:00
// attempt to create new entry in paynym.is db
final created = await ref
2022-12-20 23:00:03 +00:00
.read(paynymAPIProvider)
.create(pCode.toString());
2022-12-21 23:02:14 +00:00
debugPrint("created:$created");
2022-12-21 16:17:53 +00:00
if (created.claimed) {
// payment code already claimed
debugPrint("pcode already claimed!!");
2022-12-21 23:02:14 +00:00
if (mounted) {
Navigator.of(context).popUntil(
ModalRoute.withName(
WalletView.routeName,
),
);
}
2022-12-21 16:17:53 +00:00
return;
}
2022-12-21 23:02:14 +00:00
final token =
await ref.read(paynymAPIProvider).token(pCode.toString());
2022-12-21 16:17:53 +00:00
// sign token with notification private key
2022-12-21 23:02:14 +00:00
final signature =
await wallet.signStringWithNotificationKey(token);
2022-12-21 16:17:53 +00:00
// claim paynym account
final claim =
await ref.read(paynymAPIProvider).claim(token, signature);
if (claim["claimed"] == pCode.toString()) {
2022-12-21 23:02:14 +00:00
final account =
await ref.read(paynymAPIProvider).nym(pCode.toString());
ref.read(myPaynymAccountStateProvider.state).state =
account!;
if (mounted) {
Navigator.of(context).popUntil(
ModalRoute.withName(
WalletView.routeName,
),
);
await Navigator.of(context).pushNamed(
PaynymHomeView.routeName,
arguments: widget.walletId,
);
}
} else if (mounted && !shouldCancel) {
2022-12-20 21:05:11 +00:00
Navigator.of(context).pop();
}
2022-12-20 20:48:31 +00:00
},
),
],
),
),
),
);
}
}