Skip to content

Commit

Permalink
Fixed a navigation bug.| #716
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 14, 2020
1 parent 149acfc commit cfe136b
Showing 1 changed file with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour,
updateView(authCreds)

setupPrivateKeysViewModel()
setupOAuth2AuthCredentialsViewModel()
}

override fun onPause() {
Expand Down Expand Up @@ -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 = "",
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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)
}

/**
Expand Down

0 comments on commit cfe136b

Please sign in to comment.