Skip to content

Commit

Permalink
mac address formatting and better ip address error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
herzhenr committed Jul 3, 2023
1 parent 5a17673 commit 2a47ddc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 78 deletions.
8 changes: 4 additions & 4 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class AppConstants {

// Form Elements
static const formIcon = Icons.done_rounded;
static const nameValidationRegex = r'^.{1,200}$';
static const nameValidationRegex = r'^.{1,100}$';
static const ipValidationRegex =
r'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b';
r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$';
static const ipSubStringValidationRegex =
r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})\.){0,3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})(?:\.[0-9]{0,3})?$';
static const macValidationRegex =
r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$';
r'^(?:[0-9A-Fa-f]{2}([-:]))(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$';
static const macSubStringValidationRegex =
r'^(?:(?:[0-9A-Fa-f]{2}:){0,4}[0-9A-Fa-f]{2})?(?::[0-9A-Fa-f]{0,2}|[0-9A-Fa-f])?$';
r'^(?:[0-9A-Fa-f]{2}(?:([-:])|$)){0,5}[0-9A-Fa-f]{0,2}$';
static const portValidationRegex =
r'^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$';
static const formWrongFormatIcon = Icons.assignment_outlined;
Expand Down
129 changes: 55 additions & 74 deletions lib/services/form_input_formatters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,94 @@ import 'package:flutter/services.dart';
import 'package:simple_wake_on_lan/constants.dart';

class CustomSeparatorFormatter extends TextInputFormatter {
final String? separator;
final String separators;
final bool allowPasteWithoutFormatting;
final RegExp allowedInput;

String preferredSeparator;

final bool autoSeparate;

CustomSeparatorFormatter(
{this.separator,
{this.separators = "",
this.allowPasteWithoutFormatting = true,
this.autoSeparate = true,
required this.allowedInput});
required this.allowedInput,
this.preferredSeparator = ""});

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
assert(autoSeparate == false || separator!.length == 1);
//assert(autoSeparate == false || separator!.length == 1);
final int currentLength = oldValue.text.length;
final int inputLength = newValue.text.length;
final String inputText = newValue.text;

// // if an separator already exists in the input, set it as preferred separator
// if (inputLength != 0) {
// for (int i = 0; i < separators.length; i++) {
// if (inputText.contains(separators.substring(i, i + 1))) {
// preferredSeparator = separators.substring(i, i + 1);
// break;
// }
// }
// }

// if something longer than 1 char is pasted, just return it (allows copy paste)
if (allowPasteWithoutFormatting && inputLength > currentLength + 1) {
return newValue;
}

// user inputs one char
if (inputLength == currentLength + 1) {
// if input is separator, set preferred separator to this separator and return old value
if (separators.contains(inputText.substring(inputLength - 1))) {
preferredSeparator = inputText.substring(inputLength - 1);
// replace every occurrence of any separator with preferred separator
final String newText = oldValue.text
.replaceAll(RegExp('[$separators]'), preferredSeparator);
return TextEditingValue(
text: newText,
selection: TextSelection.collapsed(
offset: oldValue.selection.end,
),
);
}

// check if current input is valid
if (!allowedInput.hasMatch(inputText)) {
return oldValue;
}

// add separator automatically if necessary as next char
if (autoSeparate && allowedInput.hasMatch('$inputText$separator')) {
return TextEditingValue(
text: '$inputText$separator',
selection: TextSelection.collapsed(
offset: newValue.selection.end + 1,
),
);
if (autoSeparate) {
if (allowedInput.hasMatch('$inputText$preferredSeparator')) {
return TextEditingValue(
text: '$inputText$preferredSeparator',
selection: TextSelection.collapsed(
offset: newValue.selection.end + 1,
),
);
}
for (int i = 0; i < separators.length; i++) {
if (allowedInput
.hasMatch('$inputText${separators.substring(i, i + 1)}')) {
return TextEditingValue(
text: '$inputText$separators',
selection: TextSelection.collapsed(
offset: newValue.selection.end + 1,
),
);
}
}
}
}
// user delete chars
else if (inputLength == currentLength - 1) {
// delete char before separator automatically if user deletes separator
if (autoSeparate &&
currentLength > 1 &&
oldValue.text.substring(currentLength - 1) == separator) {
oldValue.text.substring(currentLength - 1) == preferredSeparator) {
final selectionIndex = newValue.selection.end - 1;
return TextEditingValue(
text: newValue.text.substring(0, newValue.text.length - 1),
Expand All @@ -67,7 +108,8 @@ class CustomSeparatorFormatter extends TextInputFormatter {
class MACAddressFormatter extends CustomSeparatorFormatter {
MACAddressFormatter({bool allowPasteWithoutFormatting = true})
: super(
separator: ':',
separators: ':-',
preferredSeparator: ':',
allowPasteWithoutFormatting: allowPasteWithoutFormatting,
allowedInput: RegExp(AppConstants.macSubStringValidationRegex),
);
Expand All @@ -81,64 +123,3 @@ class IPAddressFormatter extends CustomSeparatorFormatter {
autoSeparate: false,
);
}

// class MacAddressFormatter extends TextInputFormatter {
// @override
// TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
// // if something longer than 1 char is pasted, just return it (allows copy paste)
// if (newValue.text.length > oldValue.text.length + 1) {
// return newValue;
// }
//
// // input can't be longer than 17 characters
// if (newValue.text.length > 17) {
// return oldValue;
// }
//
// // if user entered text (and didn't delete), add colon after every 2 characters
// if (newValue.text.length > oldValue.text.length) {
// // if courser is where a colon should be next, allow user to input it (can occur when a char was deleted)
// if (newValue.text.length % 3 == 0 && newValue.text.substring(newValue.text.length - 1) == ':') {
// return newValue;
// }
//
// // if new char isn't a hex char, return old value
// if (!RegExp(r'[a-fA-F0-9]').hasMatch(newValue.text.substring(newValue.text.length - 1))) {
// return oldValue;
// }
//
// if (newValue.text.length > 2 && newValue.text.length % 3 == 0 && newValue.text.length < 17) {
// final selectionIndex = newValue.selection.end + 1;
// return TextEditingValue(
// text: '${oldValue.text}:${newValue.text.substring(newValue.text.length - 1, newValue.text.length)}',
// selection: TextSelection.collapsed(
// offset: selectionIndex,
// ),
// );
// }
//
// // Add colon after every 2 characters (except after the last one)
// if (newValue.text.length % 3 == 2 && newValue.text.length < 17) {
// final selectionIndex = newValue.selection.end + 1;
// return TextEditingValue(
// text: '${newValue.text}:',
// selection: TextSelection.collapsed(
// offset: selectionIndex,
// ),
// );
// }
// }
// if (newValue.text.length < oldValue.text.length) {
// if (newValue.text.length > 1 && newValue.text.length % 3 == 2 && newValue.text.length < 17) {
// final selectionIndex = newValue.selection.end - 1;
// return TextEditingValue(
// text: newValue.text.substring(0, newValue.text.length - 1),
// selection: TextSelection.collapsed(
// offset: selectionIndex,
// ),
// );
// }
// }
// return newValue;
// }
// }

0 comments on commit 2a47ddc

Please sign in to comment.