forked from GitHub-Mirror/riotX-android
Clear cache and rework Signout
This commit is contained in:
@ -16,8 +16,11 @@
|
||||
|
||||
package im.vector.riotredesign.features
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import im.vector.matrix.android.api.Matrix
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
import im.vector.riotredesign.core.platform.VectorBaseActivity
|
||||
import im.vector.riotredesign.features.home.HomeActivity
|
||||
import im.vector.riotredesign.features.login.LoginActivity
|
||||
@ -25,10 +28,52 @@ import im.vector.riotredesign.features.login.LoginActivity
|
||||
|
||||
class MainActivity : VectorBaseActivity() {
|
||||
|
||||
companion object {
|
||||
private const val EXTRA_CLEAR_CACHE = "EXTRA_CLEAR_CACHE"
|
||||
private const val EXTRA_CLEAR_CREDENTIALS = "EXTRA_CLEAR_CREDENTIALS"
|
||||
|
||||
// Special action to clear cache and/or clear credentials
|
||||
fun restartApp(activity: Activity, clearCache: Boolean = false, clearCredentials: Boolean = false) {
|
||||
val intent = Intent(activity, MainActivity::class.java)
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
||||
|
||||
intent.putExtra(EXTRA_CLEAR_CACHE, clearCache)
|
||||
intent.putExtra(EXTRA_CLEAR_CREDENTIALS, clearCredentials)
|
||||
activity.startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
||||
private val authenticator = Matrix.getInstance().authenticator()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val session = Matrix.getInstance().currentSession
|
||||
|
||||
val clearCache = intent.getBooleanExtra(EXTRA_CLEAR_CACHE, false)
|
||||
val clearCredentials = intent.getBooleanExtra(EXTRA_CLEAR_CREDENTIALS, false)
|
||||
|
||||
if (session == null) {
|
||||
start()
|
||||
} else {
|
||||
// Handle some wanted cleanup
|
||||
when {
|
||||
clearCredentials -> session.signOut(object : MatrixCallback<Unit> {
|
||||
override fun onSuccess(data: Unit) {
|
||||
start()
|
||||
}
|
||||
})
|
||||
clearCache -> session.clearCache(object : MatrixCallback<Unit> {
|
||||
override fun onSuccess(data: Unit) {
|
||||
start()
|
||||
}
|
||||
})
|
||||
else -> start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun start() {
|
||||
val intent = if (authenticator.hasActiveSessions()) {
|
||||
HomeActivity.newIntent(this)
|
||||
} else {
|
||||
@ -37,5 +82,4 @@ class MainActivity : VectorBaseActivity() {
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
}
|
@ -38,7 +38,7 @@ class EmptyState : MvRxState
|
||||
class HomeActivityViewModel(state: EmptyState,
|
||||
private val session: Session,
|
||||
roomSelectionRepository: RoomSelectionRepository
|
||||
) : VectorViewModel<EmptyState>(state) {
|
||||
) : VectorViewModel<EmptyState>(state), Session.Listener {
|
||||
|
||||
companion object : MvRxViewModelFactory<HomeActivityViewModel, EmptyState> {
|
||||
|
||||
@ -59,6 +59,8 @@ class HomeActivityViewModel(state: EmptyState,
|
||||
get() = _openRoomLiveData
|
||||
|
||||
init {
|
||||
session.addListener(this)
|
||||
|
||||
// TODO Move this else where, it's too late when we are here to change the filter
|
||||
session.setFilter(FilterService.FilterPreset.RiotFilter)
|
||||
|
||||
@ -100,5 +102,18 @@ class HomeActivityViewModel(state: EmptyState,
|
||||
})
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
||||
session.removeListener(this)
|
||||
}
|
||||
|
||||
/* ==========================================================================================
|
||||
* Session listener
|
||||
* ========================================================================================== */
|
||||
|
||||
override fun onInvalidToken() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -53,6 +53,7 @@ import im.vector.riotredesign.core.preference.ProgressBarPreference
|
||||
import im.vector.riotredesign.core.preference.UserAvatarPreference
|
||||
import im.vector.riotredesign.core.preference.VectorPreference
|
||||
import im.vector.riotredesign.core.utils.*
|
||||
import im.vector.riotredesign.features.MainActivity
|
||||
import im.vector.riotredesign.features.themes.ThemeUtils
|
||||
import org.koin.android.ext.android.inject
|
||||
import java.lang.ref.WeakReference
|
||||
@ -751,9 +752,8 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref
|
||||
*/
|
||||
|
||||
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||
notImplemented()
|
||||
// displayLoadingView()
|
||||
// TODO Matrix.getInstance(appContext).reloadSessions(appContext)
|
||||
displayLoadingView()
|
||||
MainActivity.restartApp(activity!!, clearCache = true, clearCredentials = false)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,7 @@
|
||||
|
||||
package im.vector.riotredesign.features.workers.signout
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
import im.vector.matrix.android.api.session.Session
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.platform.VectorBaseActivity
|
||||
@ -30,7 +28,7 @@ class SignOutUiWorker(val activity: VectorBaseActivity) {
|
||||
if (SignOutViewModel.doYouNeedToBeDisplayed(session)) {
|
||||
val signOutDialog = SignOutBottomSheetDialogFragment.newInstance(session.sessionParams.credentials.userId)
|
||||
signOutDialog.onSignOut = Runnable {
|
||||
doSignOut(session)
|
||||
doSignOut()
|
||||
}
|
||||
signOutDialog.show(activity.supportFragmentManager, "SO")
|
||||
} else {
|
||||
@ -39,31 +37,14 @@ class SignOutUiWorker(val activity: VectorBaseActivity) {
|
||||
.setTitle(R.string.action_sign_out)
|
||||
.setMessage(R.string.action_sign_out_confirmation_simple)
|
||||
.setPositiveButton(R.string.action_sign_out) { _, _ ->
|
||||
doSignOut(session)
|
||||
doSignOut()
|
||||
}
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun doSignOut(session: Session) {
|
||||
// TODO showWaitingView()
|
||||
|
||||
session.signOut(object : MatrixCallback<Unit> {
|
||||
|
||||
override fun onSuccess(data: Unit) {
|
||||
// Start MainActivity in a new task
|
||||
val intent = Intent(activity, MainActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
|
||||
activity.startActivity(intent)
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Throwable) {
|
||||
// TODO Notify user, or ignore?
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
private fun doSignOut() {
|
||||
MainActivity.restartApp(activity, clearCache = true, clearCredentials = true)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user