mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-29 06:38:45 +01:00
Part 1
This commit is contained in:
@@ -1,105 +1,81 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:cake_wallet/src/stores/user/user_store.dart';
|
||||
import 'package:cake_wallet/src/screens/pin_code/pin_code.dart';
|
||||
import 'package:cake_wallet/src/screens/base_page.dart';
|
||||
import 'package:cake_wallet/src/stores/settings/settings_store.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/src/screens/base_page.dart';
|
||||
import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart';
|
||||
import 'package:cake_wallet/view_model/setup_pin_code_view_model.dart';
|
||||
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
||||
|
||||
class SetupPinCodePage extends BasePage {
|
||||
SetupPinCodePage({this.onPinCodeSetup});
|
||||
SetupPinCodePage(this.pinCodeViewModel, {this.onSuccessfulPinSetup});
|
||||
|
||||
final Function(BuildContext, String) onPinCodeSetup;
|
||||
final SetupPinCodeViewModel pinCodeViewModel;
|
||||
final void Function(BuildContext, String) onSuccessfulPinSetup;
|
||||
|
||||
@override
|
||||
String get title => S.current.setup_pin;
|
||||
|
||||
@override
|
||||
Widget body(BuildContext context) =>
|
||||
SetupPinCodeForm(onPinCodeSetup: onPinCodeSetup, hasLengthSwitcher: true);
|
||||
}
|
||||
Widget body(BuildContext context) => PinCodeWidget(
|
||||
hasLengthSwitcher: true,
|
||||
onFullPin: (String pin, PinCodeState<PinCodeWidget> state) async {
|
||||
if (pinCodeViewModel.isOriginalPinCodeFull && !pinCodeViewModel.isRepeatedPinCodeFull) {
|
||||
state.title = S.current.enter_your_pin_again;
|
||||
state.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
class SetupPinCodeForm extends PinCodeWidget {
|
||||
SetupPinCodeForm(
|
||||
{@required this.onPinCodeSetup, @required bool hasLengthSwitcher})
|
||||
: super(hasLengthSwitcher: hasLengthSwitcher);
|
||||
if (!pinCodeViewModel.isPinCodeCorrect) {
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithOneAction(
|
||||
alertTitle: S.current.setup_pin,
|
||||
alertContent: S.of(context).pin_is_incorrect,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(context).pop());
|
||||
});
|
||||
pinCodeViewModel.reset();
|
||||
state.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
final Function(BuildContext, String) onPinCodeSetup;
|
||||
try {
|
||||
await pinCodeViewModel.setupPinCode();
|
||||
|
||||
@override
|
||||
_SetupPinCodeFormState createState() => _SetupPinCodeFormState();
|
||||
}
|
||||
|
||||
class _SetupPinCodeFormState<WidgetType extends SetupPinCodeForm>
|
||||
extends PinCodeState<WidgetType> {
|
||||
_SetupPinCodeFormState() {
|
||||
title = S.current.enter_your_pin;
|
||||
}
|
||||
|
||||
bool isEnteredOriginalPin() => _originalPin.isNotEmpty;
|
||||
Function(BuildContext) onPinCodeSetup;
|
||||
List<int> _originalPin = [];
|
||||
UserStore _userStore;
|
||||
SettingsStore _settingsStore;
|
||||
|
||||
@override
|
||||
void onPinCodeEntered(PinCodeState state) {
|
||||
if (!isEnteredOriginalPin()) {
|
||||
_originalPin = state.pin;
|
||||
state.title = S.current.enter_your_pin_again;
|
||||
state.clear();
|
||||
} else {
|
||||
if (listEquals<int>(state.pin, _originalPin)) {
|
||||
final String pin = state.pin.fold('', (ac, val) => ac + '$val');
|
||||
_userStore.set(password: pin);
|
||||
_settingsStore.setDefaultPinLength(pinLength: state.pinLength);
|
||||
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithOneAction(
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithOneAction(
|
||||
alertTitle: S.current.setup_pin,
|
||||
alertContent: S.of(context).setup_successful,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () {
|
||||
Navigator.of(context).pop();
|
||||
widget.onPinCodeSetup(context, pin);
|
||||
reset();
|
||||
onSuccessfulPinSetup(context, pin);
|
||||
state.reset();
|
||||
},
|
||||
alertBarrierDismissible: false,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithOneAction(
|
||||
);
|
||||
});
|
||||
} catch (e) {
|
||||
// FIXME: Add translation for alert content text.
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithOneAction(
|
||||
alertTitle: S.current.setup_pin,
|
||||
alertContent: S.of(context).pin_is_incorrect,
|
||||
alertContent:
|
||||
'Setup pin is failed with error: ${e.toString()}',
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(context).pop()
|
||||
);
|
||||
});
|
||||
|
||||
reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
clear();
|
||||
setTitle(S.current.enter_your_pin);
|
||||
_originalPin = [];
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_userStore = Provider.of<UserStore>(context);
|
||||
_settingsStore = Provider.of<SettingsStore>(context);
|
||||
|
||||
return body(context);
|
||||
}
|
||||
buttonAction: () => Navigator.of(context).pop(),
|
||||
alertBarrierDismissible: false,
|
||||
);
|
||||
});
|
||||
}
|
||||
},
|
||||
onChangedPin: (String pin) => pinCodeViewModel.pinCode = pin,
|
||||
onChangedPinLength: (int length) =>
|
||||
pinCodeViewModel.pinCodeLength = length,
|
||||
initialPinLength: pinCodeViewModel.pinCodeLength);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user