mirror of
https://github.com/bitfireAT/davx5-ose
synced 2024-07-23 19:50:18 +00:00
[Google OAuth] fix Re-authentication crash (bitfireAT/davx5#319)
* Hide/Show auth settings instead of removing them prevents NPE * Provide original OAuth login email as default value for GoogleLoginFragment started from account settings * Account settings: explicitly trim empty user name to null --------- Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
parent
17ccb305f9
commit
c7ff42c03f
|
@ -218,7 +218,6 @@ class SettingsActivity: AppCompatActivity() {
|
|||
}
|
||||
|
||||
// preference group: authentication
|
||||
val authCategory: PreferenceCategory = findPreference("authentication")!!
|
||||
val prefUserName = findPreference<EditTextPreference>(getString(R.string.settings_username_key))!!
|
||||
val prefPassword = findPreference<EditTextPreference>(getString(R.string.settings_password_key))!!
|
||||
val prefCertAlias = findPreference<Preference>(getString(R.string.settings_certificate_alias_key))!!
|
||||
|
@ -227,25 +226,34 @@ class SettingsActivity: AppCompatActivity() {
|
|||
model.credentials.observe(viewLifecycleOwner) { credentials ->
|
||||
if (credentials.authState != null) {
|
||||
// using OAuth, hide other settings
|
||||
authCategory.removePreference(prefUserName)
|
||||
authCategory.removePreference(prefPassword)
|
||||
authCategory.removePreference(prefCertAlias)
|
||||
prefOAuth.isVisible = true
|
||||
prefUserName.isVisible = false
|
||||
prefPassword.isVisible = false
|
||||
prefCertAlias.isVisible = false
|
||||
|
||||
prefOAuth.setOnPreferenceClickListener {
|
||||
parentFragmentManager.beginTransaction()
|
||||
.replace(android.R.id.content, GoogleLoginFragment(), null)
|
||||
.replace(android.R.id.content, GoogleLoginFragment(account.name), null)
|
||||
.addToBackStack(null)
|
||||
.commit()
|
||||
true
|
||||
}
|
||||
} else {
|
||||
// not using OAuth, hide OAuth setting
|
||||
authCategory.removePreference(prefOAuth)
|
||||
// not using OAuth, hide OAuth setting, show the others
|
||||
prefOAuth.isVisible = false
|
||||
prefUserName.isVisible = true
|
||||
prefPassword.isVisible = true
|
||||
prefCertAlias.isVisible = true
|
||||
|
||||
prefUserName.summary = credentials.userName
|
||||
prefUserName.text = credentials.userName
|
||||
prefUserName.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newUserName ->
|
||||
model.updateCredentials(Credentials(newUserName as String, credentials.password, credentials.certificateAlias))
|
||||
val newUserNameOrNull = StringUtils.trimToNull(newUserName as String)
|
||||
model.updateCredentials(Credentials(
|
||||
userName = newUserNameOrNull,
|
||||
password = credentials.password,
|
||||
certificateAlias = credentials.certificateAlias)
|
||||
)
|
||||
false
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ import java.util.logging.Level
|
|||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class GoogleLoginFragment: Fragment() {
|
||||
class GoogleLoginFragment(private val defaultEmail: String? = null): Fragment() {
|
||||
|
||||
companion object {
|
||||
|
||||
|
@ -126,7 +126,7 @@ class GoogleLoginFragment: Fragment() {
|
|||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
val view = ComposeView(requireActivity()).apply {
|
||||
setContent {
|
||||
GoogleLogin(onLogin = { accountEmail, clientId ->
|
||||
GoogleLogin(defaultEmail = defaultEmail, onLogin = { accountEmail, clientId ->
|
||||
loginModel.baseURI = googleBaseUri(accountEmail)
|
||||
loginModel.suggestedAccountName = accountEmail
|
||||
|
||||
|
@ -198,6 +198,7 @@ class GoogleLoginFragment: Fragment() {
|
|||
|
||||
@Composable
|
||||
fun GoogleLogin(
|
||||
defaultEmail: String?,
|
||||
onLogin: (accountEmail: String, clientId: String?) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
@ -230,7 +231,7 @@ fun GoogleLogin(
|
|||
}
|
||||
}
|
||||
|
||||
val email = rememberSaveable { mutableStateOf("") }
|
||||
val email = rememberSaveable { mutableStateOf(defaultEmail ?: "") }
|
||||
val emailError = remember { mutableStateOf(false) }
|
||||
OutlinedTextField(
|
||||
email.value,
|
||||
|
@ -288,5 +289,5 @@ fun GoogleLogin(
|
|||
@Composable
|
||||
@Preview
|
||||
fun PreviewGoogleLogin() {
|
||||
GoogleLogin(onLogin = { _, _ -> })
|
||||
GoogleLogin(null) { _, _ -> }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue