1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/settings/AccountProblemFragment.kt

82 lines
3.3 KiB
Kotlin
Raw Normal View History

2019-05-24 23:18:27 +02:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.settings
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
2019-05-24 23:18:27 +02:00
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.concept.sync.AccountObserver
2019-09-04 22:56:22 +02:00
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
2019-05-24 23:18:27 +02:00
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.nav
2019-05-24 23:18:27 +02:00
import org.mozilla.fenix.ext.requireComponents
class AccountProblemFragment : PreferenceFragmentCompat(), AccountObserver {
2019-05-24 23:18:27 +02:00
private val signInClickListener = Preference.OnPreferenceClickListener {
requireComponents.services.accountsAuthFeature.beginAuthentication(requireContext())
// TODO The sign-in web content populates session history,
// so pressing "back" after signing in won't take us back into the settings screen, but rather up the
// session history stack.
// We could auto-close this tab once we get to the end of the authentication process?
// Via an interceptor, perhaps.
true
}
private val signOutClickListener = Preference.OnPreferenceClickListener {
nav(
R.id.accountProblemFragment,
AccountProblemFragmentDirections.actionAccountProblemFragmentToSignOutFragment()
)
true
}
2019-05-24 23:18:27 +02:00
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.sync_reconnect)
(activity as AppCompatActivity).supportActionBar?.show()
val accountManager = requireComponents.backgroundServices.accountManager
accountManager.register(this, owner = this)
// We may have fixed our auth problem, in which case close this fragment.
if (accountManager.authenticatedAccount() != null && !accountManager.accountNeedsReauth()) {
findNavController().popBackStack()
return
}
2019-05-24 23:18:27 +02:00
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.sync_problem, rootKey)
val preferenceSignIn =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_sync_sign_in))
val preferenceSignOut =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_sign_out))
preferenceSignIn?.onPreferenceClickListener = signInClickListener
preferenceSignOut?.onPreferenceClickListener = signOutClickListener
2019-05-24 23:18:27 +02:00
}
// We're told our auth problems have been fixed; close this fragment.
2019-09-04 22:56:22 +02:00
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) = closeFragment()
// We're told there are no more auth problems since there is no more account; close this fragment.
override fun onLoggedOut() = closeFragment()
private fun closeFragment() {
lifecycleScope.launch(Dispatchers.Main) {
findNavController()
.popBackStack(R.id.accountProblemFragment, true)
}
}
2019-05-24 23:18:27 +02:00
}