1
0
Fork 0

For #2021 - Update custom preferences' views with email/account changes

master
ekager 2019-08-01 19:14:40 -07:00 committed by Emily Kager
parent 97f305612f
commit 5edc9d0b4e
3 changed files with 68 additions and 43 deletions

View File

@ -11,13 +11,15 @@ import android.widget.TextView
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import org.mozilla.fenix.R import org.mozilla.fenix.R
import kotlin.properties.Delegates
class AccountAuthErrorPreference @JvmOverloads constructor( class AccountAuthErrorPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
attributeSetId: Int = android.R.attr.preferenceStyle attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) { ) : Preference(context, attrs, attributeSetId) {
var email: String? = null private var emailView: TextView? = null
var email: String? by Delegates.observable<String?>(null) { _, _, new -> updateEmailView(new) }
init { init {
layoutResource = R.layout.account_auth_error_preference layoutResource = R.layout.account_auth_error_preference
@ -25,9 +27,13 @@ class AccountAuthErrorPreference @JvmOverloads constructor(
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
val emailView = holder.findViewById(R.id.email) as TextView emailView = holder.findViewById(R.id.email) as TextView
emailView.text = email.orEmpty() updateEmailView(email)
emailView.visibility = when (email.isNullOrEmpty()) { }
private fun updateEmailView(email: String?) {
emailView?.text = email.orEmpty()
emailView?.visibility = when (email.isNullOrEmpty()) {
true -> View.GONE true -> View.GONE
false -> View.VISIBLE false -> View.VISIBLE
} }

View File

@ -11,13 +11,21 @@ import android.widget.TextView
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import org.mozilla.fenix.R import org.mozilla.fenix.R
import kotlin.properties.Delegates
class AccountPreference @JvmOverloads constructor( class AccountPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null attrs: AttributeSet? = null
) : Preference(context, attrs) { ) : Preference(context, attrs) {
var displayName: String? = null private var emailView: TextView? = null
var email: String? = null private var displayNameView: TextView? = null
var displayName: String? by Delegates.observable<String?>(null) { _, _, new ->
updateDisplayName(new)
}
var email: String? by Delegates.observable<String?>(null) { _, _, new ->
new?.let { updateEmailText(it) }
}
init { init {
layoutResource = R.layout.account_preference layoutResource = R.layout.account_preference
@ -25,18 +33,26 @@ class AccountPreference @JvmOverloads constructor(
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
val displayNameView = holder.findViewById(R.id.displayName) as TextView displayNameView = holder.findViewById(R.id.displayName) as TextView
val emailView = holder.findViewById(R.id.email) as TextView emailView = holder.findViewById(R.id.email) as TextView
displayNameView.text = displayName.orEmpty() updateDisplayName(displayName)
displayNameView.visibility = when (displayName.isNullOrEmpty()) {
true -> View.GONE
false -> View.VISIBLE
}
// There is a potential for a race condition here. We might not have the user profile by the time we display // There is a potential for a race condition here. We might not have the user profile by the time we display
// this field, in which case we won't have the email address (or the display name, but that we may just not have // this field, in which case we won't have the email address (or the display name, but that we may just not have
// at all even after fetching the profile). We don't hide the email field or change its text if email is missing // at all even after fetching the profile). We don't hide the email field or change its text if email is missing
// because in the layout a default value ("Firefox Account") is specified, which will be displayed instead. // because in the layout a default value ("Firefox Account") is specified, which will be displayed instead.
email?.let { emailView.text = it } email?.let { emailView?.text = it }
}
private fun updateEmailText(email: String) {
emailView?.text = email
}
private fun updateDisplayName(name: String?) {
displayNameView?.text = name.orEmpty()
displayNameView?.visibility = when (displayName.isNullOrEmpty()) {
true -> View.GONE
false -> View.VISIBLE
}
} }
} }

View File

@ -2,27 +2,27 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public <!-- 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 - 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/. --> - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/account_preference_background" android:id="@+id/account_preference_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:gravity="center_vertical" android:gravity="center_vertical"
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="16dp" android:paddingEnd="16dp">
android:foreground="?android:attr/selectableItemBackground">
<FrameLayout <FrameLayout
android:id="@+id/icon_frame" android:id="@+id/icon_frame"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<androidx.preference.internal.PreferenceImageView <androidx.preference.internal.PreferenceImageView
android:id="@android:id/icon" android:id="@android:id/icon"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:maxWidth="48dp" app:maxHeight="48dp"
app:maxHeight="48dp"/> app:maxWidth="48dp" />
</FrameLayout> </FrameLayout>
<RelativeLayout <RelativeLayout
@ -30,35 +30,38 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="15dp" android:layout_marginStart="15dp"
android:layout_marginEnd="6dp" android:layout_marginEnd="6dp"
android:layout_weight="1"
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingTop="16dp" android:paddingTop="16dp"
android:paddingBottom="16dp" android:paddingBottom="16dp">
android:layout_weight="1">
<TextView android:id="@+id/displayName" <TextView
android:layout_width="wrap_content" android:id="@+id/displayName"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textColor="?primaryText" android:layout_height="wrap_content"
android:textSize="16sp" android:ellipsize="marquee"
android:ellipsize="marquee" android:fadingEdge="horizontal"
android:fadingEdge="horizontal" android:textColor="?primaryText"
android:visibility="gone"/> android:textSize="16sp"
android:visibility="gone" />
<TextView android:id="@+id/email" <TextView
android:layout_width="wrap_content" android:id="@+id/email"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@id/displayName" android:layout_height="wrap_content"
android:layout_alignStart="@id/displayName" android:layout_below="@id/displayName"
android:textColor="?primaryText" android:layout_alignStart="@id/displayName"
android:text="@string/preferences_account_default_name" android:maxLines="4"
android:maxLines="4"/> android:text="@string/preferences_account_default_name"
android:textColor="?primaryText" />
</RelativeLayout> </RelativeLayout>
<LinearLayout android:id="@android:id/widget_frame" <LinearLayout
android:layout_width="wrap_content" android:id="@android:id/widget_frame"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:gravity="center_vertical" android:layout_height="wrap_content"
android:orientation="vertical"/> android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout> </LinearLayout>