Signout: propose the user to retry in case of error

This commit is contained in:
Benoit Marty 2019-09-17 10:33:27 +02:00
parent ced72aff4f
commit 20f53e9a58
1 changed files with 28 additions and 18 deletions

View File

@ -73,49 +73,59 @@ class MainActivity : VectorBaseActivity() {
// Handle some wanted cleanup // Handle some wanted cleanup
if (clearCache || clearCredentials) { if (clearCache || clearCredentials) {
GlobalScope.launch(Dispatchers.Main) { doCleanUp(clearCache, clearCredentials)
// On UI Thread } else {
Glide.get(this@MainActivity).clearMemory() start()
withContext(Dispatchers.IO) {
// On BG thread
Glide.get(this@MainActivity).clearDiskCache()
// Also clear cache (Logs, etc...)
deleteAllFiles(this@MainActivity.cacheDir)
}
}
} }
}
private fun doCleanUp(clearCache: Boolean, clearCredentials: Boolean) {
when { when {
clearCredentials -> sessionHolder.getActiveSession().signOut(object : MatrixCallback<Unit> { clearCredentials -> sessionHolder.getActiveSession().signOut(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
Timber.w("SIGN_OUT: success, start app") Timber.w("SIGN_OUT: success, start app")
sessionHolder.clearActiveSession() sessionHolder.clearActiveSession()
start() doLocalCleanupAndStart()
} }
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
displayErrorAndStart(failure) displayError(failure, clearCache, clearCredentials)
} }
}) })
clearCache -> sessionHolder.getActiveSession().clearCache(object : MatrixCallback<Unit> { clearCache -> sessionHolder.getActiveSession().clearCache(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
start() doLocalCleanupAndStart()
} }
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
displayErrorAndStart(failure) displayError(failure, clearCache, clearCredentials)
} }
}) })
else -> start()
} }
} }
private fun displayErrorAndStart(failure: Throwable) { private fun doLocalCleanupAndStart() {
GlobalScope.launch(Dispatchers.Main) {
// On UI Thread
Glide.get(this@MainActivity).clearMemory()
withContext(Dispatchers.IO) {
// On BG thread
Glide.get(this@MainActivity).clearDiskCache()
// Also clear cache (Logs, etc...)
deleteAllFiles(this@MainActivity.cacheDir)
}
}
start()
}
private fun displayError(failure: Throwable, clearCache: Boolean, clearCredentials: Boolean) {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.dialog_title_error) .setTitle(R.string.dialog_title_error)
.setMessage(errorFormatter.toHumanReadable(failure)) .setMessage(errorFormatter.toHumanReadable(failure))
.setPositiveButton(R.string.ok) { _, _ -> start() } .setPositiveButton(R.string.global_retry) { _, _ -> doCleanUp(clearCache, clearCredentials) }
.setNegativeButton(R.string.cancel) { _, _ -> start() }
.setCancelable(false) .setCancelable(false)
.show() .show()
} }