Skip to content

Commit

Permalink
Merge pull request #448 from nyarian/bugfix/backup_and_restore_error_…
Browse files Browse the repository at this point in the history
…after_gdrive_permission_revocation

Bugfix/backup and restore error after gdrive permission revocation
  • Loading branch information
kukabi authored Jun 19, 2020
2 parents cd2342d + aa9db16 commit 699389a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@
<string name="back_up_wallet_backing_up_error_title">Backup error</string>
<string name="back_up_wallet_backing_up_error_desc">Error occurred during backup: %s</string>
<string name="back_up_wallet_back_up_is_in_progress_error">Backup already in progress</string>
<string name="back_up_wallet_status_check_unknown_error">Unknown error</string>

<string name="back_up_wallet_status_check_authentication_cancellation">Authentication was cancelled</string>
<!-- Wallet back up with seed phrase -->
<string name="back_up_seed_phrase_page_title">Back Up With Seed Phrase</string>
<string name="back_up_seed_phrase_desc">Carefully write these 12 words down in order (left to right) on a piece of paper, and hide it somewhere others won’t access it. Do not take a screenshot.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
*/
package com.tari.android.wallet.ui.fragment.restore

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
import com.orhanobut.logger.Logger
import com.tari.android.wallet.R.string.restore_wallet_with_cloud_error_desc
import com.tari.android.wallet.R.string.restore_wallet_with_cloud_error_title
import com.tari.android.wallet.R.string.*
import com.tari.android.wallet.databinding.FragmentWalletRestoringBinding
import com.tari.android.wallet.infrastructure.backup.WalletRestoration
import com.tari.android.wallet.ui.activity.restore.WalletRestoreRouter
Expand Down Expand Up @@ -79,30 +81,60 @@ UI tree rebuild on configuration changes"""
state = ViewModelProvider(requireActivity()).get()
state.state.observe(viewLifecycleOwner, Observer {
if (it.status == RestorationStatus.FAILURE) {
ErrorDialog(
requireContext(),
title = string(restore_wallet_with_cloud_error_title),
description = string(
restore_wallet_with_cloud_error_desc,
it.exception?.message ?: ""
),
cancelable = false,
canceledOnTouchOutside = false,
onClose = {
blockingBackPressDispatcher.isEnabled = false
requireActivity().onBackPressed()
state.reset()
}
).show()
handleRestorationException(it)
} else if (it.status == RestorationStatus.SUCCESS) {
(requireActivity() as WalletRestoreRouter).onBackupCompleted()
}
})
}

private fun handleRestorationException(state: RestorationState) {
val exception = state.exception!!
if (exception is UserRecoverableAuthIOException) {
startActivityForResult(exception.intent, REQUEST_CODE_REAUTH)
} else {
showUnrecoverableExceptionDialog(
exception.message ?: string(
back_up_wallet_status_check_unknown_error
)
)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE_REAUTH) {
if (resultCode == Activity.RESULT_OK) {
state.reset()
state.restoreWallet()
} else {
showUnrecoverableExceptionDialog(
string(back_up_wallet_status_check_authentication_cancellation)
)
}
}
}


private fun showUnrecoverableExceptionDialog(message: String) {
ErrorDialog(
requireContext(),
title = string(restore_wallet_with_cloud_error_title),
description = string(restore_wallet_with_cloud_error_desc, message),
cancelable = false,
canceledOnTouchOutside = false,
onClose = {
blockingBackPressDispatcher.isEnabled = false
requireActivity().onBackPressed()
this.state.reset()
}
).show()
}

companion object {
@Suppress("DEPRECATION")
fun newInstance() = RestorationWithCloudFragment()

private const val REQUEST_CODE_REAUTH = 11222
}

class RestorationWithCloudState(private val restoration: WalletRestoration) : ViewModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class StorageBackupViewModel(private val storage: BackupStorage, private val bac
private val currentState get() = _state.value!!

init {
checkBackupStatus()
}

fun checkBackupStatus() {
if (_state.value?.backupStatus == StorageBackupStatus.CHECKING_STATUS) return
_state.value = StorageBackupState.checkingBackupStatus()
viewModelScope.launch(Dispatchers.Main) {
try {
Expand All @@ -61,7 +66,7 @@ class StorageBackupViewModel(private val storage: BackupStorage, private val bac
Logger.e(e, "Error occurred during backup check")
_state.value = currentState.copy(
backupStatus = StorageBackupStatus.STATUS_CHECK_FAILURE,
processException = e
statusCheckException = e
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
*/
package com.tari.android.wallet.ui.fragment.settings.backup

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -45,6 +47,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
import com.orhanobut.logger.Logger
import com.tari.android.wallet.R.string.*
import com.tari.android.wallet.databinding.FragmentWalletBackupSettingsBinding
Expand Down Expand Up @@ -113,8 +116,7 @@ framework for UI tree rebuild on configuration changes"""
ui.cloudBackUpStatusProgressView.visible()
}
StorageBackupStatus.STATUS_CHECK_FAILURE -> {
displayStatusCheckFailureDialog(state.statusCheckException)
vm.clearStatusCheckFailure()
handleStatusCheckFailure(state)
}
StorageBackupStatus.BACKED_UP -> ui.cloudBackUpStatusSuccessView.visible()
StorageBackupStatus.NOT_BACKED_UP, StorageBackupStatus.UNKNOWN -> {
Expand All @@ -123,13 +125,39 @@ framework for UI tree rebuild on configuration changes"""
}
}

private fun displayStatusCheckFailureDialog(e: Exception?) {
private fun handleStatusCheckFailure(state: StorageBackupState) {
val exception = state.statusCheckException!!
if (exception is UserRecoverableAuthIOException) {
startActivityForResult(exception.intent, REQUEST_CODE_REAUTH)
} else {
displayStatusCheckFailureDialog(
exception.message ?: string(back_up_wallet_status_check_unknown_error)
)
vm.clearStatusCheckFailure()
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE_REAUTH) {
if (resultCode == Activity.RESULT_OK) {
vm.clearStatusCheckFailure()
vm.checkBackupStatus()
} else {
displayStatusCheckFailureDialog(
string(back_up_wallet_status_check_authentication_cancellation)
)
vm.clearStatusCheckFailure()
}
}
}

private fun displayStatusCheckFailureDialog(message: String) {
ErrorDialog(
requireContext(),
title = string(back_up_wallet_back_up_check_error_title),
description = string(
back_up_wallet_back_up_check_error_desc,
e?.message ?: ""
message
)
).show()
}
Expand Down Expand Up @@ -222,6 +250,8 @@ framework for UI tree rebuild on configuration changes"""
companion object {
@Suppress("DEPRECATION")
fun newInstance() = WalletBackupSettingsFragment()

private const val REQUEST_CODE_REAUTH = 1355
}

}

0 comments on commit 699389a

Please sign in to comment.