Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
finkmoritz committed Jul 16, 2021
1 parent 28e3516 commit 4820108
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 163 deletions.
5 changes: 2 additions & 3 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ void main(List<String> args) {
CommandLine.printHeader();

var amplifyDir = CommandLine.readArg(args, '--amplifyDir') ?? './amplify/';
var targetDir = CommandLine.readArg(args, '--targetDir') ?? './lib/generated_auth_classes/';
var targetDir = CommandLine.readArg(args, '--targetDir') ??
'./lib/generated_auth_classes/';

CommandLine.printMessage('Using following parameters:');
CommandLine.printMessage('\t--amplifyDir=$amplifyDir');
Expand All @@ -19,5 +20,3 @@ void main(List<String> args) {
targetDir: targetDir,
);
}


4 changes: 2 additions & 2 deletions lib/src/auth_config/auth_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class AuthConfig {
static AuthConfig fromJson(dynamic json) {
return AuthConfig(
allowUnauthenticatedIdentities: json['allowUnauthenticatedIdentities'],
requiredAttributes: _getAsList(json,'requiredAttributes'),
usernameAttributes: _getAsList(json,'usernameAttributes'),
requiredAttributes: _getAsList(json, 'requiredAttributes'),
usernameAttributes: _getAsList(json, 'usernameAttributes'),
authProvidersUserPool: _getAsList(json, 'authProvidersUserPool'),
);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/src/auth_config/auth_config_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ class AuthConfigReader {
static AuthConfig readAuthConfig({required String amplifyDir}) {
var file = _findParametersJsonFile(amplifyDir: amplifyDir);
var authConfig = AuthConfig.fromJson(json.decode(file.readAsStringSync()));
CommandLine.printInfo('Successfully read Amplify configuration:\n$authConfig');
CommandLine.printInfo(
'Successfully read Amplify configuration:\n$authConfig');
return authConfig;
}

static File _findParametersJsonFile({required String amplifyDir}) {
var authDir = Directory(path.join(amplifyDir, 'backend', 'auth'));
for(var file in authDir.listSync(recursive: true)) {
if(file.path.endsWith('parameters.json')) {
CommandLine.printMessage('Reading Amplify Auth configuration from ${file.path}\n');
for (var file in authDir.listSync(recursive: true)) {
if (file.path.endsWith('parameters.json')) {
CommandLine.printMessage(
'Reading Amplify Auth configuration from ${file.path}\n');
return File(file.path);
}
};
}
;
throw FlutterAmplifyAuthUiException('''
Could not find "parameters.json"!
Please check if you have already configured Amplify Auth for this project.''');
Expand Down
8 changes: 3 additions & 5 deletions lib/src/flutter_amplify_auth_ui_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import 'package:path/path.dart' as path;
import 'auth_config/auth_config.dart';

class FlutterAmplifyAuthUIGenerator {
static void generateClassesFromConfig({
required AuthConfig authConfig,
required String targetDir
}) {
static void generateClassesFromConfig(
{required AuthConfig authConfig, required String targetDir}) {
Directory(targetDir).createSync();

CommandLine.printMessage('Generating classes...');
Expand Down Expand Up @@ -46,7 +44,7 @@ class FlutterAmplifyAuthUIGenerator {
AuthConfig? authConfig,
}) async {
var template = await Template.byName(templateName: templateName);
if(templateHandler != null && authConfig != null) {
if (templateHandler != null && authConfig != null) {
templateHandler.modifyTemplate(
template: template,
authConfig: authConfig,
Expand Down
20 changes: 12 additions & 8 deletions lib/src/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class Template {
}

static Future<Template> byName({required String templateName}) async {
var uri = Uri.parse('package:flutter_amplify_auth_ui/templates/$templateName');
var uri =
Uri.parse('package:flutter_amplify_auth_ui/templates/$templateName');
var resolvedUri = await Isolate.resolvePackageUri(uri);
if(resolvedUri == null) {
if (resolvedUri == null) {
throw Exception('Could not resolve path to $uri');
} else {
return Template._(pathToTemplate: resolvedUri.toFilePath());
Expand All @@ -34,16 +35,19 @@ class Template {
}

void remove({required String identifier}) {
while(_content.contains(delimiterStart + identifier)) {
var startIndex = _content.lastIndexOf('\n', _content.indexOf(delimiterStart + identifier));
var endIndex = _content.indexOf('\n', _content.indexOf(delimiterEnd + identifier));
_content = _content.substring(0, startIndex) + _content.substring(endIndex);
while (_content.contains(delimiterStart + identifier)) {
var startIndex = _content.lastIndexOf(
'\n', _content.indexOf(delimiterStart + identifier));
var endIndex =
_content.indexOf('\n', _content.indexOf(delimiterEnd + identifier));
_content =
_content.substring(0, startIndex) + _content.substring(endIndex);
}
}

void removeAllIdentifiers() {
for(var delimiter in delimiters) {
while(_content.contains(delimiter)) {
for (var delimiter in delimiters) {
while (_content.contains(delimiter)) {
_removeLineAt(_content.indexOf(delimiter));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ class PasswordResetPageTemplateHandler extends TemplateHandler {
];

@override
void modifyTemplate({required Template template, required AuthConfig authConfig}) {
void modifyTemplate(
{required Template template, required AuthConfig authConfig}) {
_handleUsernameAttributes(template: template, authConfig: authConfig);
}

void _handleUsernameAttributes({required Template template, required AuthConfig authConfig}) {
if(authConfig.usernameAttributes.isEmpty) {
void _handleUsernameAttributes(
{required Template template, required AuthConfig authConfig}) {
if (authConfig.usernameAttributes.isEmpty) {
configurableUsernameAttributes.forEach((attribute) {
template.remove(identifier: 'usernameAttributes[$attribute]');
});
} else {
template.remove(identifier: 'usernameAttributes[username]');
configurableUsernameAttributes.forEach((attribute) {
if(!authConfig.usernameAttributes.contains(attribute)) {
if (!authConfig.usernameAttributes.contains(attribute)) {
template.remove(identifier: 'usernameAttributes[$attribute]');
}
});
Expand Down
22 changes: 13 additions & 9 deletions lib/src/template_handlers/impl/sign_in_page_template_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,44 @@ class SignInPageTemplateHandler extends TemplateHandler {
];

@override
void modifyTemplate({required Template template, required AuthConfig authConfig}) {
void modifyTemplate(
{required Template template, required AuthConfig authConfig}) {
_handleUsernameAttributes(template: template, authConfig: authConfig);
_handleGuestSignIn(template: template, authConfig: authConfig);
_handleSignInWithWebUI(template: template, authConfig: authConfig);
}

void _handleUsernameAttributes({required Template template, required AuthConfig authConfig}) {
if(authConfig.usernameAttributes.isEmpty) {
void _handleUsernameAttributes(
{required Template template, required AuthConfig authConfig}) {
if (authConfig.usernameAttributes.isEmpty) {
configurableUsernameAttributes.forEach((attribute) {
template.remove(identifier: 'usernameAttributes[$attribute]');
});
} else {
template.remove(identifier: 'usernameAttributes[username]');
configurableUsernameAttributes.forEach((attribute) {
if(!authConfig.usernameAttributes.contains(attribute)) {
if (!authConfig.usernameAttributes.contains(attribute)) {
template.remove(identifier: 'usernameAttributes[$attribute]');
}
});
}
}

void _handleGuestSignIn({required Template template, required AuthConfig authConfig}) {
if(!authConfig.allowUnauthenticatedIdentities) {
void _handleGuestSignIn(
{required Template template, required AuthConfig authConfig}) {
if (!authConfig.allowUnauthenticatedIdentities) {
template.remove(identifier: 'allowUnauthenticatedIdentities');
}
}

void _handleSignInWithWebUI({required Template template, required AuthConfig authConfig}) {
void _handleSignInWithWebUI(
{required Template template, required AuthConfig authConfig}) {
configurableAuthProviders.forEach((provider) {
if(!authConfig.authProvidersUserPool.contains(provider)) {
if (!authConfig.authProvidersUserPool.contains(provider)) {
template.remove(identifier: 'authProvidersUserPool[$provider]');
}
});
if(authConfig.authProvidersUserPool.isEmpty) {
if (authConfig.authProvidersUserPool.isEmpty) {
template.remove(identifier: 'authProvidersUserPool[any]');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,31 @@ class SignUpPageTemplateHandler extends TemplateHandler {
];

@override
void modifyTemplate({required Template template, required AuthConfig authConfig}) {
void modifyTemplate(
{required Template template, required AuthConfig authConfig}) {
_handleRequiredAttributes(template: template, authConfig: authConfig);
_handleUsernameAttributes(template: template, authConfig: authConfig);
}

void _handleRequiredAttributes({required Template template, required AuthConfig authConfig}) {
void _handleRequiredAttributes(
{required Template template, required AuthConfig authConfig}) {
configurableRequiredAttributes.forEach((attribute) {
if(!authConfig.requiredAttributes.contains(attribute)) {
if (!authConfig.requiredAttributes.contains(attribute)) {
template.remove(identifier: 'requiredAttributes[$attribute]');
}
});
}

void _handleUsernameAttributes({required Template template, required AuthConfig authConfig}) {
if(authConfig.usernameAttributes.isEmpty) {
void _handleUsernameAttributes(
{required Template template, required AuthConfig authConfig}) {
if (authConfig.usernameAttributes.isEmpty) {
configurableUsernameAttributes.forEach((attribute) {
template.remove(identifier: 'usernameAttributes[$attribute]');
});
} else {
template.remove(identifier: 'usernameAttributes[username]');
configurableUsernameAttributes.forEach((attribute) {
if(!authConfig.usernameAttributes.contains(attribute)) {
if (!authConfig.usernameAttributes.contains(attribute)) {
template.remove(identifier: 'usernameAttributes[$attribute]');
}
});
Expand Down
15 changes: 7 additions & 8 deletions lib/src/util/command_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CommandLine {

static void printMessage(String s, {String? color}) {
s = _prepend(s, 'MSG');
if(color == null) {
if (color == null) {
print(s);
} else {
print('$color$s$RESET');
Expand Down Expand Up @@ -41,9 +41,9 @@ class CommandLine {
static void printFancy(String s) {
s = _prepend(s, 'MSG');
var rand = Random();
for(var i=s.length-1; i>=0; --i) {
for (var i = s.length - 1; i >= 0; --i) {
var r = rand.nextInt(6) + 1;
s = '${s.substring(0,i)}\x1B[3${r}m${s.substring(i)}';
s = '${s.substring(0, i)}\x1B[3${r}m${s.substring(i)}';
}
print('$s$RESET');
}
Expand All @@ -54,16 +54,15 @@ class CommandLine {
printFancy('###############################');
printMessage('');

printMessage('Generate Flutter widgets from your AWS Amplify CLI configuration.\n');
printMessage(
'Generate Flutter widgets from your AWS Amplify CLI configuration.\n');

printMessage('Author: https://github.com/finkmoritz\n');
}

static String? readArg(List<String> args, String key) {
String? value = args.firstWhere(
(element) => element.startsWith('$key='),
orElse: () => ''
);
String? value = args.firstWhere((element) => element.startsWith('$key='),
orElse: () => '');
return value.isEmpty ? null : value.replaceAll('$key=', '');
}

Expand Down
27 changes: 10 additions & 17 deletions lib/templates/password_management/password_reset_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
final TextEditingController _phoneNumberController = TextEditingController();
/*+++END usernameAttributes[phone_number]+++*/
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _confirmationCodeController = TextEditingController();
final TextEditingController _confirmationCodeController =
TextEditingController();

int _stepIndex = 0;

Expand Down Expand Up @@ -151,8 +152,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.person_outline),
hintText: 'Enter your username',
labelText: 'Username'
),
labelText: 'Username'),
),
/*+++END usernameAttributes[username]+++*/
/*+++START usernameAttributes[email]+++*/
Expand All @@ -162,8 +162,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.mail_outline),
hintText: 'Enter your email address',
labelText: 'Email address'
),
labelText: 'Email address'),
),
/*+++END usernameAttributes[email]+++*/
/*+++START usernameAttributes[phone_number]+++*/
Expand All @@ -173,8 +172,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.phone_outlined),
hintText: 'Enter your phone number',
labelText: 'Phone number'
),
labelText: 'Phone number'),
),
/*+++END usernameAttributes[phone_number]+++*/
],
Expand All @@ -193,8 +191,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.person_outline),
hintText: 'Enter your username',
labelText: 'Username'
),
labelText: 'Username'),
),
/*+++END usernameAttributes[username]+++*/
/*+++START usernameAttributes[email]+++*/
Expand All @@ -204,8 +201,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.mail_outline),
hintText: 'Enter your email address',
labelText: 'Email address'
),
labelText: 'Email address'),
),
/*+++END usernameAttributes[email]+++*/
/*+++START usernameAttributes[phone_number]+++*/
Expand All @@ -215,8 +211,7 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.phone_outlined),
hintText: 'Enter your phone number',
labelText: 'Phone number'
),
labelText: 'Phone number'),
),
/*+++END usernameAttributes[phone_number]+++*/
TextFormField(
Expand All @@ -225,17 +220,15 @@ class _PasswordResetPageState extends State<PasswordResetPage> {
decoration: InputDecoration(
icon: Icon(Icons.lock_outline),
hintText: 'Enter your new password',
labelText: 'New password'
),
labelText: 'New password'),
),
TextFormField(
controller: _confirmationCodeController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
icon: Icon(Icons.check_outlined),
hintText: 'Enter confirmation code',
labelText: 'Confirmation code'
),
labelText: 'Confirmation code'),
),
],
);
Expand Down
Loading

0 comments on commit 4820108

Please sign in to comment.