Improve code

This commit is contained in:
Benoit Marty 2020-05-10 23:58:21 +02:00
parent 4510aff00a
commit 062a21e39a
2 changed files with 53 additions and 61 deletions

View File

@ -17,6 +17,7 @@ package im.vector.riotx.features.discovery
import android.view.View import android.view.View
import com.airbnb.epoxy.TypedEpoxyController import com.airbnb.epoxy.TypedEpoxyController
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Incomplete import com.airbnb.mvrx.Incomplete
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
@ -66,8 +67,8 @@ class DiscoverySettingsController @Inject constructor(
buildIdentityServerSection(data) buildIdentityServerSection(data)
val hasIdentityServer = data.identityServer().isNullOrBlank().not() val hasIdentityServer = data.identityServer().isNullOrBlank().not()
if (hasIdentityServer) { if (hasIdentityServer) {
buildMailSection(data) buildEmailsSection(data.emailList)
buildPhoneNumberSection(data) buildMsisdnSection(data.phoneNumbersList)
} }
} }
} }
@ -105,15 +106,12 @@ class DiscoverySettingsController @Inject constructor(
settingsButtonItem { settingsButtonItem {
id("change") id("change")
colorProvider(colorProvider) colorProvider(colorProvider)
if (data.identityServer() != null) { if (data.identityServer() == null) {
buttonTitleId(R.string.change_identity_server)
} else {
buttonTitleId(R.string.add_identity_server) buttonTitleId(R.string.add_identity_server)
} else {
buttonTitleId(R.string.change_identity_server)
} }
buttonStyle(ButtonStyle.POSITIVE) buttonClickListener { listener?.onTapChangeIdentityServer() }
buttonClickListener(View.OnClickListener {
listener?.onTapChangeIdentityServer()
})
} }
if (data.identityServer() != null) { if (data.identityServer() != null) {
@ -126,39 +124,36 @@ class DiscoverySettingsController @Inject constructor(
colorProvider(colorProvider) colorProvider(colorProvider)
buttonTitleId(R.string.disconnect_identity_server) buttonTitleId(R.string.disconnect_identity_server)
buttonStyle(ButtonStyle.DESTRUCTIVE) buttonStyle(ButtonStyle.DESTRUCTIVE)
buttonClickListener(View.OnClickListener { buttonClickListener { listener?.onTapDisconnectIdentityServer() }
listener?.onTapDisconnectIdentityServer()
})
} }
} }
} }
private fun buildMailSection(data: DiscoverySettingsState) { private fun buildEmailsSection(emails: Async<List<PidInfo>>) {
settingsSectionTitleItem { settingsSectionTitleItem {
id("emails") id("emails")
titleResId(R.string.settings_discovery_emails_title) titleResId(R.string.settings_discovery_emails_title)
} }
when (data.emailList) { when (emails) {
is Incomplete -> { is Incomplete -> {
loadingItem { loadingItem {
id("mailLoading") id("emailsLoading")
} }
} }
is Fail -> { is Fail -> {
settingsInfoItem { settingsInfoItem {
id("mailListError") id("emailsError")
helperText(data.emailList.error.message) helperText(emails.error.message)
} }
} }
is Success -> { is Success -> {
val emails = data.emailList.invoke() if (emails().isEmpty()) {
if (emails.isEmpty()) {
settingsInfoItem { settingsInfoItem {
id("no_emails") id("emailsEmpty")
helperText(stringProvider.getString(R.string.settings_discovery_no_mails)) helperText(stringProvider.getString(R.string.settings_discovery_no_mails))
} }
} else { } else {
emails.forEach { buildEmail(it) } emails().forEach { buildEmail(it) }
} }
} }
} }
@ -175,6 +170,7 @@ class DiscoverySettingsController @Inject constructor(
buttonIndeterminate(true) buttonIndeterminate(true)
} }
is Fail -> { is Fail -> {
buttonType(ButtonType.NORMAL)
buttonStyle(ButtonStyle.DESTRUCTIVE) buttonStyle(ButtonStyle.DESTRUCTIVE)
buttonTitle(stringProvider.getString(R.string.global_retry)) buttonTitle(stringProvider.getString(R.string.global_retry))
iconMode(IconMode.ERROR) iconMode(IconMode.ERROR)
@ -183,8 +179,8 @@ class DiscoverySettingsController @Inject constructor(
is Success -> when (pidInfo.isShared()) { is Success -> when (pidInfo.isShared()) {
SharedState.SHARED, SharedState.SHARED,
SharedState.NOT_SHARED -> { SharedState.NOT_SHARED -> {
checked(pidInfo.isShared() == SharedState.SHARED)
buttonType(ButtonType.SWITCH) buttonType(ButtonType.SWITCH)
checked(pidInfo.isShared() == SharedState.SHARED)
switchChangeListener { _, checked -> switchChangeListener { _, checked ->
if (checked) { if (checked) {
listener?.onTapShare(pidInfo.threePid) listener?.onTapShare(pidInfo.threePid)
@ -205,55 +201,47 @@ class DiscoverySettingsController @Inject constructor(
} }
} }
if (pidInfo.isShared is Fail) { if (pidInfo.isShared is Fail) {
settingsInformationItem { buildSharedFail(pidInfo)
id("info${pidInfo.threePid.value}")
colorProvider(colorProvider)
textColorId(R.color.vector_error_color)
message(pidInfo.isShared.error.message ?: "")
}
buildContinueCancel(pidInfo.threePid) buildContinueCancel(pidInfo.threePid)
} } else if (pidInfo.isShared() == SharedState.BINDING_IN_PROGRESS) {
if (pidInfo.isShared() == SharedState.BINDING_IN_PROGRESS) {
when (pidInfo.finalRequest) { when (pidInfo.finalRequest) {
is Uninitialized -> { is Uninitialized,
is Loading ->
settingsInformationItem { settingsInformationItem {
id("info${pidInfo.threePid.value}") id("info${pidInfo.threePid.value}")
colorProvider(colorProvider) colorProvider(colorProvider)
message(stringProvider.getString(R.string.settings_discovery_confirm_mail, pidInfo.threePid.value)) message(stringProvider.getString(R.string.settings_discovery_confirm_mail, pidInfo.threePid.value))
} }
buildContinueCancel(pidInfo.threePid) is Fail ->
}
is Loading -> {
settingsInformationItem {
id("info${pidInfo.threePid.value}")
colorProvider(colorProvider)
message(stringProvider.getString(R.string.settings_discovery_confirm_mail, pidInfo.threePid.value))
}
settingsProgressItem {
id("progress${pidInfo.threePid.value}")
}
}
is Fail -> {
settingsInformationItem { settingsInformationItem {
id("info${pidInfo.threePid.value}") id("info${pidInfo.threePid.value}")
colorProvider(colorProvider) colorProvider(colorProvider)
message(stringProvider.getString(R.string.settings_discovery_confirm_mail_not_clicked, pidInfo.threePid.value)) message(stringProvider.getString(R.string.settings_discovery_confirm_mail_not_clicked, pidInfo.threePid.value))
textColorId(R.color.riotx_destructive_accent) textColorId(R.color.riotx_destructive_accent)
} }
is Success -> Unit /* Cannot happen */
}
when (pidInfo.finalRequest) {
is Uninitialized,
is Fail ->
buildContinueCancel(pidInfo.threePid) buildContinueCancel(pidInfo.threePid)
is Loading ->
settingsProgressItem {
id("progress${pidInfo.threePid.value}")
} }
is Success -> Unit /* Cannot happen */ is Success -> Unit /* Cannot happen */
} }
} }
} }
private fun buildPhoneNumberSection(data: DiscoverySettingsState) { private fun buildMsisdnSection(msisdns: Async<List<PidInfo>>) {
settingsSectionTitleItem { settingsSectionTitleItem {
id("msisdn") id("msisdn")
titleResId(R.string.settings_discovery_msisdn_title) titleResId(R.string.settings_discovery_msisdn_title)
} }
when (data.phoneNumbersList) { when (msisdns) {
is Incomplete -> { is Incomplete -> {
loadingItem { loadingItem {
id("msisdnLoading") id("msisdnLoading")
@ -262,18 +250,17 @@ class DiscoverySettingsController @Inject constructor(
is Fail -> { is Fail -> {
settingsInfoItem { settingsInfoItem {
id("msisdnListError") id("msisdnListError")
helperText(data.phoneNumbersList.error.message) helperText(msisdns.error.message)
} }
} }
is Success -> { is Success -> {
val phones = data.phoneNumbersList.invoke() if (msisdns().isEmpty()) {
if (phones.isEmpty()) {
settingsInfoItem { settingsInfoItem {
id("no_msisdn") id("no_msisdn")
helperText(stringProvider.getString(R.string.settings_discovery_no_msisdn)) helperText(stringProvider.getString(R.string.settings_discovery_no_msisdn))
} }
} else { } else {
phones.forEach { buildMsisdn(it) } msisdns().forEach { buildMsisdn(it) }
} }
} }
} }
@ -327,12 +314,7 @@ class DiscoverySettingsController @Inject constructor(
} }
} }
if (pidInfo.isShared is Fail) { if (pidInfo.isShared is Fail) {
settingsInformationItem { buildSharedFail(pidInfo)
id("info${pidInfo.threePid.value}")
colorProvider(colorProvider)
textColorId(R.color.vector_error_color)
message(pidInfo.isShared.error.message ?: "")
}
} }
when (pidInfo.isShared()) { when (pidInfo.isShared()) {
SharedState.BINDING_IN_PROGRESS -> { SharedState.BINDING_IN_PROGRESS -> {
@ -373,6 +355,15 @@ class DiscoverySettingsController @Inject constructor(
}.exhaustive }.exhaustive
} }
private fun buildSharedFail(pidInfo: PidInfo) {
settingsInformationItem {
id("info${pidInfo.threePid.value}")
colorProvider(colorProvider)
textColorId(R.color.vector_error_color)
message((pidInfo.isShared as? Fail)?.error?.message ?: "")
}
}
private fun buildContinueCancel(threePid: ThreePid) { private fun buildContinueCancel(threePid: ThreePid) {
settingsContinueCancelItem { settingsContinueCancelItem {
id("bottom${threePid.value}") id("bottom${threePid.value}")

View File

@ -15,15 +15,16 @@
*/ */
package im.vector.riotx.features.discovery package im.vector.riotx.features.discovery
import android.view.View
import android.widget.Button import android.widget.Button
import androidx.annotation.StringRes import androidx.annotation.StringRes
import com.airbnb.epoxy.EpoxyAttribute import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder import com.airbnb.epoxy.EpoxyModelWithHolder
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.epoxy.ClickListener
import im.vector.riotx.core.epoxy.VectorEpoxyHolder import im.vector.riotx.core.epoxy.VectorEpoxyHolder
import im.vector.riotx.core.epoxy.attributes.ButtonStyle import im.vector.riotx.core.epoxy.attributes.ButtonStyle
import im.vector.riotx.core.epoxy.onClick
import im.vector.riotx.core.extensions.setTextOrHide import im.vector.riotx.core.extensions.setTextOrHide
import im.vector.riotx.core.resources.ColorProvider import im.vector.riotx.core.resources.ColorProvider
@ -44,7 +45,7 @@ abstract class SettingsButtonItem : EpoxyModelWithHolder<SettingsButtonItem.Hold
var buttonStyle: ButtonStyle = ButtonStyle.POSITIVE var buttonStyle: ButtonStyle = ButtonStyle.POSITIVE
@EpoxyAttribute @EpoxyAttribute
var buttonClickListener: View.OnClickListener? = null var buttonClickListener: ClickListener? = null
override fun bind(holder: Holder) { override fun bind(holder: Holder) {
super.bind(holder) super.bind(holder)
@ -63,7 +64,7 @@ abstract class SettingsButtonItem : EpoxyModelWithHolder<SettingsButtonItem.Hold
} }
} }
holder.button.setOnClickListener(buttonClickListener) holder.button.onClick(buttonClickListener)
} }
class Holder : VectorEpoxyHolder() { class Holder : VectorEpoxyHolder() {