From cfe136bfc5d9618b82cdf0a486884e7c8c419bda Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Fri, 14 Aug 2020 16:20:52 +0300 Subject: [PATCH] Fixed a navigation bug.| #716 --- .../fragment/AddOtherAccountFragment.kt | 84 +++++++++++-------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/AddOtherAccountFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/AddOtherAccountFragment.kt index 0b42f1f965..d82396ae1b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/AddOtherAccountFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/AddOtherAccountFragment.kt @@ -159,7 +159,6 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour, updateView(authCreds) setupPrivateKeysViewModel() - setupOAuth2AuthCredentialsViewModel() } override fun onPause() { @@ -293,13 +292,7 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour, if (authResponse != null) { val code = authResponse.authorizationCode if (code != null) { - authRequest?.let { request -> - when (schema) { - OAuth2Helper.MICROSOFT_OAUTH2_SCHEMA -> { - oAuth2AuthCredentialsViewModel.getMicrosoftOAuth2Token(authorizeCode = code, authRequest = request) - } - } - } + getOAuthToken(schema, code) } else { showInfoDialog( dialogTitle = "", @@ -419,7 +412,7 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour, buttonSignInWithOutlook = view.findViewById(R.id.buttonSignInWithOutlook) buttonSignInWithOutlook?.setOnClickListener { it.isEnabled = false - oAuth2AuthCredentialsViewModel.getAuthorizationRequestForProvider( + getAuthorizationRequestForProvider( requestCode = REQUEST_CODE_FETCH_MICROSOFT_OPENID_CONFIGURATION, provider = OAuth2Helper.Provider.MICROSOFT) } @@ -578,7 +571,7 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour, it?.let { when (it.status) { Result.Status.LOADING -> { - showProgress() + showProgress(progressMsg = getString(R.string.saving_prv_keys)) } Result.Status.SUCCESS -> { @@ -603,63 +596,82 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour, }) } - private fun setupOAuth2AuthCredentialsViewModel() { - oAuth2AuthCredentialsViewModel.authorizationRequestLiveData.observe(viewLifecycleOwner, Observer { + private fun getOAuthToken(schema: String?, code: String) { + authRequest?.let { request -> + when (schema) { + OAuth2Helper.MICROSOFT_OAUTH2_SCHEMA -> { + observeMicrosoftOAuth2TokenLiveData() + oAuth2AuthCredentialsViewModel.getMicrosoftOAuth2Token(authorizeCode = code, authRequest = request) + } + } + } + } + + private fun observeMicrosoftOAuth2TokenLiveData() { + oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.observe(viewLifecycleOwner, Observer { when (it.status) { Result.Status.LOADING -> { - showProgress(progressMsg = getString(R.string.loading_oauth_server_configuration)) + showProgress(progressMsg = getString(R.string.loading_account_details)) } Result.Status.SUCCESS -> { - buttonSignInWithOutlook?.isEnabled = true - it.data?.let { authorizationRequest -> - authRequest = authorizationRequest - authRequest?.let { request -> - AuthorizationService(requireContext()) - .performAuthorizationRequest( - request, - PendingIntent.getActivity(requireContext(), 0, Intent(requireContext(), SignInActivity::class.java), 0)) - } - showContent() + oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.removeObservers(viewLifecycleOwner) + it.data?.let { authCredentials -> + authCreds = authCredentials + val account = AccountEntity(authCredentials) + val nextFrag = AuthorizeAndSearchBackupsFragment.newInstance(account) + activity?.supportFragmentManager?.beginTransaction() + ?.replace(R.id.fragmentContainerView, nextFrag, AuthorizeAndSearchBackupsFragment::class.java.simpleName) + ?.addToBackStack(null) + ?.commit() } } Result.Status.ERROR, Result.Status.EXCEPTION -> { - buttonSignInWithOutlook?.isEnabled = true + oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.removeObservers(viewLifecycleOwner) showContent() showInfoDialog( dialogMsg = it.exception?.message ?: it.exception?.javaClass?.simpleName - ?: "Couldn't load the server configuration") + ?: "Couldn't fetch token") } } }) + } - oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.observe(viewLifecycleOwner, Observer { + private fun getAuthorizationRequestForProvider(requestCode: Long, provider: OAuth2Helper.Provider) { + oAuth2AuthCredentialsViewModel.authorizationRequestLiveData.observe(viewLifecycleOwner, Observer { when (it.status) { Result.Status.LOADING -> { - showProgress(progressMsg = getString(R.string.loading_account_details)) + showProgress(progressMsg = getString(R.string.loading_oauth_server_configuration)) } Result.Status.SUCCESS -> { - it.data?.let { authCredentials -> - authCreds = authCredentials - val account = AccountEntity(authCredentials) - val nextFrag = AuthorizeAndSearchBackupsFragment.newInstance(account) - activity?.supportFragmentManager?.beginTransaction() - ?.replace(R.id.fragmentContainerView, nextFrag, AuthorizeAndSearchBackupsFragment::class.java.simpleName) - ?.addToBackStack(null) - ?.commit() + oAuth2AuthCredentialsViewModel.authorizationRequestLiveData.removeObservers(viewLifecycleOwner) + buttonSignInWithOutlook?.isEnabled = true + it.data?.let { authorizationRequest -> + authRequest = authorizationRequest + authRequest?.let { request -> + AuthorizationService(requireContext()) + .performAuthorizationRequest( + request, + PendingIntent.getActivity(requireContext(), 0, Intent(requireContext(), SignInActivity::class.java), 0)) + } + showContent() } } Result.Status.ERROR, Result.Status.EXCEPTION -> { + oAuth2AuthCredentialsViewModel.authorizationRequestLiveData.removeObservers(viewLifecycleOwner) + buttonSignInWithOutlook?.isEnabled = true showContent() showInfoDialog( dialogMsg = it.exception?.message ?: it.exception?.javaClass?.simpleName - ?: "Couldn't fetch token") + ?: "Couldn't load the server configuration") } } }) + + oAuth2AuthCredentialsViewModel.getAuthorizationRequestForProvider(requestCode, provider) } /**