mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-20 06:08:34 +02:00
Changed way for validation of seed phrase. Added multi words to text field on restore from seed screen.
This commit is contained in:
@@ -48,17 +48,12 @@ class RestoreFromSeedForm extends StatefulWidget {
|
||||
|
||||
class _RestoreFromSeedFormState extends State<RestoreFromSeedForm> {
|
||||
final _seedKey = GlobalKey<SeedWidgetState>();
|
||||
bool _reactionSet = false;
|
||||
|
||||
void clear() => _seedKey.currentState.clear();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final walletRestorationStore = Provider.of<WalletRestorationStore>(context);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
(_) => _setReactions(context, walletRestorationStore));
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () =>
|
||||
SystemChannels.textInput.invokeMethod<void>('TextInput.hide'),
|
||||
@@ -74,22 +69,4 @@ class _RestoreFromSeedFormState extends State<RestoreFromSeedForm> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _setReactions(BuildContext context, WalletRestorationStore store) {
|
||||
if (_reactionSet) {
|
||||
return;
|
||||
}
|
||||
|
||||
reaction((_) => store.errorMessage, (String errorMessage) {
|
||||
if (errorMessage == null || errorMessage.isEmpty) {
|
||||
_seedKey.currentState.validated();
|
||||
} else {
|
||||
_seedKey.currentState.invalidate();
|
||||
}
|
||||
|
||||
_seedKey.currentState.setErrorMessage(errorMessage);
|
||||
});
|
||||
|
||||
_reactionSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,11 @@ class PrimaryButton extends StatelessWidget {
|
||||
@required this.text,
|
||||
@required this.color,
|
||||
@required this.borderColor,
|
||||
|
||||
this.isDisabled = false});
|
||||
this.isDisabled = false,
|
||||
this.onDisabledPressed});
|
||||
|
||||
final VoidCallback onPressed;
|
||||
final VoidCallback onDisabledPressed;
|
||||
final Color color;
|
||||
final Color borderColor;
|
||||
final String text;
|
||||
@@ -23,15 +24,19 @@ class PrimaryButton extends StatelessWidget {
|
||||
minWidth: double.infinity,
|
||||
height: 56.0,
|
||||
child: FlatButton(
|
||||
onPressed: isDisabled ? null : onPressed,
|
||||
color: isDisabled ? Colors.grey : color,
|
||||
onPressed: isDisabled
|
||||
? (onDisabledPressed != null ? onDisabledPressed : null)
|
||||
: onPressed,
|
||||
color: isDisabled ? Colors.transparent : color,
|
||||
shape: RoundedRectangleBorder(
|
||||
side: BorderSide(color: borderColor),
|
||||
borderRadius: BorderRadius.circular(10.0)),
|
||||
child: Text(text,
|
||||
style: TextStyle(
|
||||
fontSize: 16.0,
|
||||
color: isDisabled ? Palette.darkGrey : Theme.of(context).primaryTextTheme.button.color)),
|
||||
color: isDisabled
|
||||
? Palette.darkGrey
|
||||
: Theme.of(context).primaryTextTheme.button.color)),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,9 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
bool isValid;
|
||||
String errorMessage;
|
||||
|
||||
MnemoticItem currentMnemotic;
|
||||
List<MnemoticItem> currentMnemotics;
|
||||
bool isCurrentMnemoticValid;
|
||||
String _errorMessage;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -49,14 +50,6 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
void deleteMnemotic(MnemoticItem item) {
|
||||
setState(() => items.remove(item));
|
||||
|
||||
if (widget.onMnemoticChange != null) {
|
||||
widget.onMnemoticChange(items);
|
||||
}
|
||||
}
|
||||
|
||||
void mnemoticFromText(String text) {
|
||||
final splitted = text.split(' ');
|
||||
|
||||
@@ -78,7 +71,7 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
void selectMnemotic(MnemoticItem item) {
|
||||
setState(() {
|
||||
selectedItem = item;
|
||||
currentMnemotic = item;
|
||||
currentMnemotics = [item];
|
||||
|
||||
_seedController
|
||||
..text = item.text
|
||||
@@ -106,18 +99,6 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
void onFieldSubmitted(String text) {
|
||||
if (text.isEmpty || text == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedItem != null) {
|
||||
editTextOfSelectedMnemotic(text);
|
||||
} else {
|
||||
addMnemotic(text);
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
setState(() {
|
||||
items = [];
|
||||
@@ -149,31 +130,68 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
|
||||
void changeCurrentMnemotic(String text) {
|
||||
setState(() {
|
||||
final trimmedText = text.trim();
|
||||
final splitted = trimmedText.split(' ');
|
||||
_errorMessage = null;
|
||||
|
||||
if (text == null) {
|
||||
currentMnemotic = null;
|
||||
currentMnemotics = [];
|
||||
isCurrentMnemoticValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
currentMnemotic = MnemoticItem(text: text, dic: EnglishMnemonics.words);
|
||||
isCurrentMnemoticValid = currentMnemotic.isCorrect();
|
||||
currentMnemotics = splitted
|
||||
.map((text) => MnemoticItem(text: text, dic: EnglishMnemonics.words))
|
||||
.toList();
|
||||
|
||||
bool isValid = true;
|
||||
|
||||
for (final word in currentMnemotics) {
|
||||
print('word ${word.text}');
|
||||
isValid = word.isCorrect();
|
||||
|
||||
if (!isValid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
isCurrentMnemoticValid = isValid;
|
||||
});
|
||||
}
|
||||
|
||||
void saveCurrentMnemoticToItems() {
|
||||
setState(() {
|
||||
if (selectedItem != null) {
|
||||
selectedItem.changeText(currentMnemotic.text);
|
||||
selectedItem.changeText(currentMnemotics.first.text.trim());
|
||||
selectedItem = null;
|
||||
} else {
|
||||
items.add(currentMnemotic);
|
||||
items.addAll(currentMnemotics);
|
||||
}
|
||||
|
||||
currentMnemotic = null;
|
||||
currentMnemotics = [];
|
||||
_seedController.text = '';
|
||||
});
|
||||
}
|
||||
|
||||
void showErrorIfExist() {
|
||||
setState(() => _errorMessage =
|
||||
!isCurrentMnemoticValid ? S.current.incorrect_seed : null);
|
||||
}
|
||||
|
||||
bool isSeedValid() {
|
||||
bool isValid;
|
||||
|
||||
for (final item in items) {
|
||||
isValid = item.isCorrect();
|
||||
|
||||
if (!isValid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
@@ -258,6 +276,7 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
hintStyle:
|
||||
TextStyle(color: Theme.of(context).hintColor),
|
||||
hintText: S.of(context).restore_from_seed_placeholder,
|
||||
errorText: _errorMessage,
|
||||
focusedBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Palette.cakeGreen, width: 2.0)),
|
||||
@@ -273,6 +292,7 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
child: (selectedItem == null && items.length == maxLength)
|
||||
? PrimaryButton(
|
||||
text: S.of(context).restore_next,
|
||||
isDisabled: !isSeedValid(),
|
||||
onPressed: () => widget.onFinish != null
|
||||
? widget.onFinish()
|
||||
: null,
|
||||
@@ -291,6 +311,7 @@ class SeedWidgetState extends State<SeedWidget> {
|
||||
onPressed: () => isCurrentMnemoticValid
|
||||
? saveCurrentMnemoticToItems()
|
||||
: null,
|
||||
onDisabledPressed: () => showErrorIfExist(),
|
||||
isDisabled: !isCurrentMnemoticValid,
|
||||
color: PaletteDark.darkThemeBlueButton,
|
||||
borderColor: Palette.brightBlue))
|
||||
|
||||
Reference in New Issue
Block a user