| 1 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | import 'package:flutter/cupertino.dart'; |
| 4 | import 'package:cake_wallet/generated/i18n.dart'; |
| 5 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 6 | import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; |
| 7 | import 'package:cake_wallet/view_model/setup_pin_code_view_model.dart'; |
| 8 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 9 | |
| 10 | class SetupPinCodePage extends BasePage { |
| 11 | SetupPinCodePage(this.pinCodeViewModel, {this.onSuccessfulPinSetup, this.isDuressPin = false}) |
| 12 | : pinCodeStateKey = GlobalKey<PinCodeState>(); |
| 13 | |
| 14 | final SetupPinCodeViewModel pinCodeViewModel; |
| 15 | final void Function(PinCodeState<PinCodeWidget>, String)? onSuccessfulPinSetup; |
| 16 | final bool isDuressPin; |
| 17 | final GlobalKey<PinCodeState> pinCodeStateKey; |
| 18 | |
| 19 | @override |
| 20 | String get title => isDuressPin ? S.current.durres_PIN : S.current.setup_pin; |
| 21 | |
| 22 | @override |
| 23 | Widget body(BuildContext context) => PinCodeWidget( |
| 24 | key: pinCodeStateKey, |
| 25 | hasLengthSwitcher: true, |
| 26 | onFullPin: (String pin, PinCodeState<PinCodeWidget> state) async { |
| 27 | if (pinCodeViewModel.isOriginalPinCodeFull && !pinCodeViewModel.isRepeatedPinCodeFull) { |
| 28 | state.title = S.current.enter_your_pin_again; |
| 29 | state.clear(); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (!pinCodeViewModel.isPinCodeCorrect) { |
| 34 | await showPopUp<void>( |
| 35 | context: context, |
| 36 | builder: (BuildContext context) { |
| 37 | return AlertWithOneAction( |
| 38 | alertTitle: S.current.setup_pin, |
| 39 | alertContent: S.of(context).pin_is_incorrect, |
| 40 | buttonText: S.of(context).ok, |
| 41 | buttonAction: () => Navigator.of(context).pop()); |
| 42 | }); |
| 43 | pinCodeViewModel.reset(); |
| 44 | state.reset(); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | await pinCodeViewModel.setupPinCode(); |
| 50 | |
| 51 | await showPopUp<void>( |
| 52 | context: context, |
| 53 | builder: (BuildContext context) { |
| 54 | return AlertWithOneAction( |
| 55 | buttonKey: ValueKey('setup_pin_code_success_button_key'), |
| 56 | alertTitle: isDuressPin ? S.current.durres_PIN : S.current.setup_pin, |
| 57 | alertContent: isDuressPin |
| 58 | ? S.current.durres_PIN_set_up_successfully |
| 59 | : S.current.setup_successful, |
| 60 | buttonText: S.of(context).ok, |
| 61 | buttonAction: () { |
| 62 | Navigator.of(context).pop(); |
| 63 | if (pinCodeStateKey.currentState != null) { |
| 64 | onSuccessfulPinSetup?.call(pinCodeStateKey.currentState!, pin); |
| 65 | } |
| 66 | |
| 67 | state.reset(); |
| 68 | }, |
| 69 | alertBarrierDismissible: false, |
| 70 | ); |
| 71 | }); |
| 72 | } catch (e) { |
| 73 | await showPopUp<void>( |
| 74 | context: context, |
| 75 | builder: (BuildContext context) { |
| 76 | return AlertWithOneAction( |
| 77 | alertTitle: isDuressPin ? S.current.durres_PIN : S.current.setup_pin, |
| 78 | alertContent: '${S.current.setup_pin_is_failed} ${e.toString()}', |
| 79 | buttonText: S.of(context).ok, |
| 80 | buttonAction: () => Navigator.of(context).pop(), |
| 81 | alertBarrierDismissible: false, |
| 82 | ); |
| 83 | }); |
| 84 | } |
| 85 | }, |
| 86 | onChangedPin: (String pin) async { |
| 87 | try { |
| 88 | await pinCodeViewModel.setPinCode(pin); |
| 89 | } catch (e) { |
| 90 | await showPopUp<void>( |
| 91 | context: context, |
| 92 | builder: (BuildContext context) { |
| 93 | return AlertWithOneAction( |
| 94 | alertTitle: S.current.durres_PIN, |
| 95 | alertContent: e.toString(), |
| 96 | buttonText: S.of(context).ok, |
| 97 | buttonAction: () { |
| 98 | Navigator.of(context).pop(); |
| 99 | }, |
| 100 | alertBarrierDismissible: false, |
| 101 | ); |
| 102 | }, |
| 103 | ); |
| 104 | |
| 105 | pinCodeStateKey.currentState?.reset(); |
| 106 | pinCodeViewModel.reset(); |
| 107 | } |
| 108 | }, |
| 109 | onChangedPinLength: (int length) => pinCodeViewModel.pinCodeLength = length, |
| 110 | initialPinLength: pinCodeViewModel.pinCodeLength); |
| 111 | } |