From 87a659087abf48dabbb836c95323f29d15de97ea Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 13 May 2021 13:52:58 +0300 Subject: [PATCH] Gmail --- lib/ui/settings/user_details.dart | 111 ++++++++++++++------------- lib/ui/settings/user_details_vm.dart | 6 +- lib/utils/i18n.dart | 12 ++- 3 files changed, 71 insertions(+), 58 deletions(-) diff --git a/lib/ui/settings/user_details.dart b/lib/ui/settings/user_details.dart index 0d5933cb5..8c4e4e482 100644 --- a/lib/ui/settings/user_details.dart +++ b/lib/ui/settings/user_details.dart @@ -51,8 +51,12 @@ class _UserDetailsState extends State TabController _controller; bool autoValidate = false; - bool _isConnectingGmail = false; - bool _isSignedInToGmail = false; + static const GMAIL_DEFAULT = 0; + 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; final _firstNameController = TextEditingController(); @@ -131,6 +135,46 @@ class _UserDetailsState extends State }); } + 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(); + completer.future.catchError((Object error) { + showErrorDialog(context: context, message: error); + }); + widget.viewModel.onConnectGmailPressed(context, completer, _password); + } + } + @override Widget build(BuildContext context) { final localization = AppLocalization.of(context); @@ -139,6 +183,17 @@ class _UserDetailsState extends State final state = viewModel.state; 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( title: localization.userDetails, onSavePressed: (context) { @@ -244,14 +299,7 @@ class _UserDetailsState extends State SizedBox(width: kTableColumnGap), Expanded( child: OutlineButton( - child: Text((_isConnectingGmail - ? (_isSignedInToGmail - ? localization.step2Authorize - : localization.step1SignIn) - : state.user.isConnectedToGmail - ? localization.disconnectGmail - : localization.connectGmail) - .toUpperCase()), + child: Text(gmailButtonLabel.toUpperCase()), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5)), onPressed: !state.user.isConnectedToGoogle @@ -267,49 +315,8 @@ class _UserDetailsState extends State if (state.user.isConnectedToGmail) { viewModel.onDisconnectGmailPressed(context); - } else if (_isConnectingGmail) { - if (_isSignedInToGmail) { - print('## onPressed: 1'); - final completer = Completer(); - 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 { - GoogleOAuth.signIn( - (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; - }); - }); + _connectToGmail(); } }, ), diff --git a/lib/ui/settings/user_details_vm.dart b/lib/ui/settings/user_details_vm.dart index de1735d16..270dcbc1b 100644 --- a/lib/ui/settings/user_details_vm.dart +++ b/lib/ui/settings/user_details_vm.dart @@ -60,11 +60,13 @@ class UserDetailsVM { print('## onConnectGmailPressed: 2'); final signedIn = await GoogleOAuth.grantOfflineAccess( (idToken, accessToken, serverAuthCode) { + print( + '## onConnectGmailPressed: 3 $idToken, $accessToken, $serverAuthCode'); + if (idToken.isEmpty || accessToken.isEmpty || serverAuthCode.isEmpty) { - print( - '## onConnectGmailPressed: $idToken, $accessToken, $serverAuthCode'); + print('## onConnectGmailPressed: 4'); completer.completeError( AppLocalization.of(context).anErrorOccurredTryAgain); } else { diff --git a/lib/utils/i18n.dart b/lib/utils/i18n.dart index c549006ae..c949d8e53 100644 --- a/lib/utils/i18n.dart +++ b/lib/utils/i18n.dart @@ -16,7 +16,8 @@ mixin LocalizationsProvider on LocaleCodeAware { 'en': { // STARTER: lang key - do not remove comment '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', 'migration_not_yet_completed': 'The migration has not yet completed', 'wizard_warning': @@ -60364,10 +60365,13 @@ mixin LocalizationsProvider on LocaleCodeAware { _localizedValues[localeCode]['step_1_sign_in'] ?? _localizedValues['en']['step_1_sign_in']; - String get step2Authorize => - _localizedValues[localeCode]['step_2_authorize'] ?? - _localizedValues['en']['step_2_authorize']; + String get step2RequestScope => + _localizedValues[localeCode]['step_2_request_scope'] ?? + _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) { final lookupKey = toSnakeCase(key);