This commit is contained in:
Hillel Coren 2021-05-13 13:52:58 +03:00
parent fbf954bbc9
commit 87a659087a
3 changed files with 71 additions and 58 deletions

View File

@ -51,8 +51,12 @@ class _UserDetailsState extends State<UserDetails>
TabController _controller; TabController _controller;
bool autoValidate = false; bool autoValidate = false;
bool _isConnectingGmail = false; static const GMAIL_DEFAULT = 0;
bool _isSignedInToGmail = false; static const GMAIL_1_SIGN_IN = 1;
static const GMAIL_2_REQUEST_SCOPE = 2;
static const GMAIL_3_AUTHORIZE_OFFLINE = 4;
int _connectGmailStep = GMAIL_DEFAULT;
String _password; String _password;
final _firstNameController = TextEditingController(); final _firstNameController = TextEditingController();
@ -131,6 +135,46 @@ class _UserDetailsState extends State<UserDetails>
}); });
} }
void _connectToGmail() {
if (_connectGmailStep == GMAIL_DEFAULT) {
print('## STEP 0');
GoogleOAuth.signOut();
passwordCallback(
context: context,
skipOAuth: true,
callback: (password, idToken) async {
setState(() {
_connectGmailStep = GMAIL_1_SIGN_IN;
_password = password;
});
});
} else if (_connectGmailStep == GMAIL_1_SIGN_IN) {
print('## STEP 1');
GoogleOAuth.signIn((idToken, accessToken) {
print('## $idToken, $accessToken');
if (idToken.isEmpty || accessToken.isEmpty) {
GoogleOAuth.signOut();
setState(() {
_connectGmailStep = GMAIL_2_REQUEST_SCOPE;
});
}
});
} else if (_connectGmailStep == GMAIL_2_REQUEST_SCOPE) {
print('## STEP 2');
GoogleOAuth.requestGmailScope();
setState(() {
_connectGmailStep = GMAIL_3_AUTHORIZE_OFFLINE;
});
} else if (_connectGmailStep == GMAIL_3_AUTHORIZE_OFFLINE) {
print('## STEP 3');
final completer = Completer<Null>();
completer.future.catchError((Object error) {
showErrorDialog(context: context, message: error);
});
widget.viewModel.onConnectGmailPressed(context, completer, _password);
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final localization = AppLocalization.of(context); final localization = AppLocalization.of(context);
@ -139,6 +183,17 @@ class _UserDetailsState extends State<UserDetails>
final state = viewModel.state; final state = viewModel.state;
final settings = viewModel.state.userCompany.settings; final settings = viewModel.state.userCompany.settings;
String gmailButtonLabel = localization.connectGmail;
if (state.user.isConnectedToGmail) {
gmailButtonLabel = localization.disconnectGmail;
} else if (_connectGmailStep == GMAIL_1_SIGN_IN) {
gmailButtonLabel = localization.step1SignIn;
} else if (_connectGmailStep == GMAIL_2_REQUEST_SCOPE) {
gmailButtonLabel = localization.step2RequestScope;
} else if (_connectGmailStep == GMAIL_3_AUTHORIZE_OFFLINE) {
gmailButtonLabel = localization.step3AuthorizeOffline;
}
return EditScaffold( return EditScaffold(
title: localization.userDetails, title: localization.userDetails,
onSavePressed: (context) { onSavePressed: (context) {
@ -244,14 +299,7 @@ class _UserDetailsState extends State<UserDetails>
SizedBox(width: kTableColumnGap), SizedBox(width: kTableColumnGap),
Expanded( Expanded(
child: OutlineButton( child: OutlineButton(
child: Text((_isConnectingGmail child: Text(gmailButtonLabel.toUpperCase()),
? (_isSignedInToGmail
? localization.step2Authorize
: localization.step1SignIn)
: state.user.isConnectedToGmail
? localization.disconnectGmail
: localization.connectGmail)
.toUpperCase()),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5)), borderRadius: BorderRadius.circular(5)),
onPressed: !state.user.isConnectedToGoogle onPressed: !state.user.isConnectedToGoogle
@ -267,49 +315,8 @@ class _UserDetailsState extends State<UserDetails>
if (state.user.isConnectedToGmail) { if (state.user.isConnectedToGmail) {
viewModel.onDisconnectGmailPressed(context); viewModel.onDisconnectGmailPressed(context);
} else if (_isConnectingGmail) {
if (_isSignedInToGmail) {
print('## onPressed: 1');
final completer = Completer<Null>();
completer.future
.catchError((Object error) {
print('## onPressed: 2');
showErrorDialog(
context: context, message: error);
});
print('## onPressed: 3');
viewModel.onConnectGmailPressed(
context, completer, _password);
print('## onPressed: 4');
} else { } else {
GoogleOAuth.signIn( _connectToGmail();
(idToken, accessToken) {
if (idToken.isEmpty ||
accessToken.isEmpty) {
GoogleOAuth.signOut();
setState(() {
_isConnectingGmail = false;
_isSignedInToGmail = false;
});
} else {
setState(() {
_isSignedInToGmail = true;
});
}
});
}
} else {
GoogleOAuth.signOut();
passwordCallback(
context: context,
skipOAuth: true,
callback: (password, idToken) async {
setState(() {
_isConnectingGmail = true;
_isSignedInToGmail = false;
_password = password;
});
});
} }
}, },
), ),

View File

@ -60,11 +60,13 @@ class UserDetailsVM {
print('## onConnectGmailPressed: 2'); print('## onConnectGmailPressed: 2');
final signedIn = await GoogleOAuth.grantOfflineAccess( final signedIn = await GoogleOAuth.grantOfflineAccess(
(idToken, accessToken, serverAuthCode) { (idToken, accessToken, serverAuthCode) {
print(
'## onConnectGmailPressed: 3 $idToken, $accessToken, $serverAuthCode');
if (idToken.isEmpty || if (idToken.isEmpty ||
accessToken.isEmpty || accessToken.isEmpty ||
serverAuthCode.isEmpty) { serverAuthCode.isEmpty) {
print( print('## onConnectGmailPressed: 4');
'## onConnectGmailPressed: $idToken, $accessToken, $serverAuthCode');
completer.completeError( completer.completeError(
AppLocalization.of(context).anErrorOccurredTryAgain); AppLocalization.of(context).anErrorOccurredTryAgain);
} else { } else {

View File

@ -16,7 +16,8 @@ mixin LocalizationsProvider on LocaleCodeAware {
'en': { 'en': {
// STARTER: lang key - do not remove comment // STARTER: lang key - do not remove comment
'step_1_sign_in': 'Step 1: Sign In', 'step_1_sign_in': 'Step 1: Sign In',
'step_2_authorize': 'Step 2: Authorize', 'step_2_request_scope': 'Step 2: Request Scope',
'step_3_authorize_offline': 'Step 3: Authorize',
'account_id': 'Account ID', 'account_id': 'Account ID',
'migration_not_yet_completed': 'The migration has not yet completed', 'migration_not_yet_completed': 'The migration has not yet completed',
'wizard_warning': 'wizard_warning':
@ -60364,10 +60365,13 @@ mixin LocalizationsProvider on LocaleCodeAware {
_localizedValues[localeCode]['step_1_sign_in'] ?? _localizedValues[localeCode]['step_1_sign_in'] ??
_localizedValues['en']['step_1_sign_in']; _localizedValues['en']['step_1_sign_in'];
String get step2Authorize => String get step2RequestScope =>
_localizedValues[localeCode]['step_2_authorize'] ?? _localizedValues[localeCode]['step_2_request_scope'] ??
_localizedValues['en']['step_2_authorize']; _localizedValues['en']['step_2_request_scope'];
String get step3AuthorizeOffline =>
_localizedValues[localeCode]['step_3_authorize_offline'] ??
_localizedValues['en']['step_3_authorize_offline'];
String lookup(String key) { String lookup(String key) {
final lookupKey = toSnakeCase(key); final lookupKey = toSnakeCase(key);