Show a dialog if the server can't check username

parent 14c6b20f
......@@ -30,6 +30,7 @@ class Strings {
final password = 'Password';
final confirm = 'Confirm';
final login = 'Login';
final register = 'Register';
final next = 'Next';
final homeserver = 'Homeserver';
final identityServer = 'Identity server';
......@@ -82,6 +83,10 @@ class Strings {
final hostnameInvalidError = 'Invalid hostname';
final unknownError = 'An unknown error occured';
final failedUsernameCheckAvailableError =
'Failed to check if the username is available';
final wouldYouLikeLoginOrRegister = 'Would you like to login or register?';
// StartPage: PasswordPage //
final enterPassword = 'Enter password';
final wrongPasswordError = 'Wrong password. Please try again';
......
......@@ -24,6 +24,7 @@ import 'package:pattle/src/app.dart';
import 'package:pattle/src/ui/resources/localizations.dart';
import 'package:pattle/src/ui/start/start_bloc.dart';
import 'package:pattle/src/ui/util/lower_case_text_formatter.dart';
import 'package:chopper/chopper.dart';
class UsernamePage extends StatefulWidget {
@override
......@@ -55,7 +56,14 @@ class UsernamePageState extends State<UsernamePage> {
if (state == RequestState.success) {
Navigator.pushNamed(context, Routes.startPassword);
}
});
})
..onError((error) {
// Show a dialog with a choice between login and register if this
// homeserver does not support checking for a username.
if (error is Response<dynamic> && error.statusCode == 405) {
_showCantCheckUsernameDialog(context);
}
});
}
@override
......@@ -112,14 +120,14 @@ class UsernamePageState extends State<UsernamePage> {
String errorText;
if (snapshot.hasError) {
if (snapshot.error is InvalidUsernameException) {
final error = snapshot.error;
if (error is InvalidUsernameException) {
errorText = l(context).usernameInvalidError;
} else if (snapshot.error
is InvalidHostnameException) {
} else if (error is InvalidHostnameException) {
errorText = l(context).hostnameInvalidError;
} else if (snapshot.error is InvalidUserIdException) {
} else if (error is InvalidUserIdException) {
errorText = l(context).userIdInvalidError;
} else if (snapshot.error is SocketException) {
} else if (error is SocketException) {
errorText = l(context).connectionFailed;
} else {
errorText = l(context).unknownError;
......@@ -189,4 +197,32 @@ class UsernamePageState extends State<UsernamePage> {
),
);
}
Future<void> _showCantCheckUsernameDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(l(context).failedUsernameCheckAvailableError),
content: SingleChildScrollView(
child: Text(l(context).wouldYouLikeLoginOrRegister),
),
actions: <Widget>[
FlatButton(
child: Text(l(context).register.toUpperCase()),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text(l(context).login.toUpperCase()),
onPressed: () {
Navigator.popAndPushNamed(context, Routes.startPassword);
},
),
],
);
},
);
}
}
......@@ -118,8 +118,6 @@ class StartBloc extends Bloc {
}
Future<void> checkUsernameAvailability(String username) async {
var user;
await _do(
subject: _isUsernameAvailableSubj,
validate: (addError) {
......@@ -154,7 +152,7 @@ class StartBloc extends Bloc {
return false;
}
user = UserId(username).username;
_username = UserId(username).username;
} on FormatException {
addError(InvalidHostnameException());
return false;
......@@ -171,16 +169,14 @@ class StartBloc extends Bloc {
return false;
}
user = Username(username);
_username = Username(username);
return true;
}
},
request: (addError) {
homeserver.isUsernameAvailable(user).then((available) {
homeserver.isUsernameAvailable(_username).then((available) {
_isUsernameAvailableSubj.add(RequestSuccessState(data: available));
_username = user;
}).catchError((error) => _isUsernameAvailableSubj.addError(error));
},
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment