56 lines
2.0 KiB
Kotlin
56 lines
2.0 KiB
Kotlin
/* 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.navigation.Navigation
|
|
import androidx.preference.Preference
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.Job
|
|
import kotlinx.coroutines.launch
|
|
import org.mozilla.fenix.R
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
|
import org.mozilla.fenix.ext.requireComponents
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
private lateinit var job: Job
|
|
override val coroutineContext: CoroutineContext
|
|
get() = Dispatchers.Main + job
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
job = Job()
|
|
(activity as AppCompatActivity).title = getString(R.string.preferences_account_settings)
|
|
(activity as AppCompatActivity).supportActionBar?.show()
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
super.onDestroy()
|
|
job.cancel()
|
|
}
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
|
|
|
|
val signIn = context?.getPreferenceKey(R.string.pref_key_sign_out)
|
|
val preferenceSignOut = findPreference<Preference>(signIn)
|
|
preferenceSignOut.onPreferenceClickListener = getClickListenerForSignOut()
|
|
}
|
|
|
|
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
|
|
return Preference.OnPreferenceClickListener {
|
|
launch {
|
|
requireComponents.backgroundServices.accountManager.logoutAsync().await()
|
|
Navigation.findNavController(view!!).popBackStack()
|
|
}
|
|
true
|
|
}
|
|
}
|
|
}
|