Identity: Extract enum for Epoxy Items

This commit is contained in:
Benoit Marty 2020-05-10 23:19:47 +02:00
parent bdfcf5c67c
commit 7822660ce7
6 changed files with 77 additions and 36 deletions

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.core.epoxy.attributes
enum class ButtonStyle {
POSITIVE,
DESTRUCTIVE
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.core.epoxy.attributes
enum class ButtonType {
NO_BUTTON,
NORMAL,
SWITCH
}

View File

@ -14,10 +14,10 @@
* limitations under the License.
*/
package im.vector.riotx.features.discovery
package im.vector.riotx.core.epoxy.attributes
enum class IconMode {
None,
Info,
Error
NONE,
INFO,
ERROR
}

View File

@ -27,6 +27,9 @@ import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.session.identity.SharedState
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.riotx.R
import im.vector.riotx.core.epoxy.attributes.ButtonStyle
import im.vector.riotx.core.epoxy.attributes.ButtonType
import im.vector.riotx.core.epoxy.attributes.IconMode
import im.vector.riotx.core.epoxy.loadingItem
import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.exhaustive
@ -107,7 +110,7 @@ class DiscoverySettingsController @Inject constructor(
} else {
buttonTitleId(R.string.add_identity_server)
}
buttonStyle(SettingsTextButtonSingleLineItem.ButtonStyle.POSITIVE)
buttonStyle(ButtonStyle.POSITIVE)
buttonClickListener(View.OnClickListener {
listener?.onTapChangeIdentityServer()
})
@ -122,7 +125,7 @@ class DiscoverySettingsController @Inject constructor(
id("remove")
colorProvider(colorProvider)
buttonTitleId(R.string.disconnect_identity_server)
buttonStyle(SettingsTextButtonSingleLineItem.ButtonStyle.DESTRUCTIVE)
buttonStyle(ButtonStyle.DESTRUCTIVE)
buttonClickListener(View.OnClickListener {
listener?.onTapDisconnectIdentityServer()
})
@ -172,16 +175,16 @@ class DiscoverySettingsController @Inject constructor(
buttonIndeterminate(true)
}
is Fail -> {
buttonStyle(SettingsTextButtonSingleLineItem.ButtonStyle.DESTRUCTIVE)
buttonStyle(ButtonStyle.DESTRUCTIVE)
buttonTitle(stringProvider.getString(R.string.global_retry))
iconMode(IconMode.Error)
iconMode(IconMode.ERROR)
buttonClickListener { listener?.onTapRetryToRetrieveBindings() }
}
is Success -> when (pidInfo.isShared()) {
SharedState.SHARED,
SharedState.NOT_SHARED -> {
checked(pidInfo.isShared() == SharedState.SHARED)
buttonType(SettingsTextButtonSingleLineItem.ButtonType.SWITCH)
buttonType(ButtonType.SWITCH)
switchChangeListener { _, checked ->
if (checked) {
listener?.onTapShare(pidInfo.threePid)
@ -191,11 +194,11 @@ class DiscoverySettingsController @Inject constructor(
}
}
SharedState.BINDING_IN_PROGRESS -> {
buttonType(SettingsTextButtonSingleLineItem.ButtonType.NO_BUTTON)
buttonType(ButtonType.NO_BUTTON)
when (pidInfo.finalRequest) {
is Incomplete -> iconMode(IconMode.Info)
is Fail -> iconMode(IconMode.Error)
else -> iconMode(IconMode.None)
is Incomplete -> iconMode(IconMode.INFO)
is Fail -> iconMode(IconMode.ERROR)
else -> iconMode(IconMode.NONE)
}
}
}
@ -300,17 +303,17 @@ class DiscoverySettingsController @Inject constructor(
buttonIndeterminate(true)
}
is Fail -> {
buttonType(SettingsTextButtonSingleLineItem.ButtonType.NORMAL)
buttonStyle(SettingsTextButtonSingleLineItem.ButtonStyle.DESTRUCTIVE)
buttonType(ButtonType.NORMAL)
buttonStyle(ButtonStyle.DESTRUCTIVE)
buttonTitle(stringProvider.getString(R.string.global_retry))
iconMode(IconMode.Error)
iconMode(IconMode.ERROR)
buttonClickListener { listener?.onTapRetryToRetrieveBindings() }
}
is Success -> when (pidInfo.isShared()) {
SharedState.SHARED,
SharedState.NOT_SHARED -> {
checked(pidInfo.isShared() == SharedState.SHARED)
buttonType(SettingsTextButtonSingleLineItem.ButtonType.SWITCH)
buttonType(ButtonType.SWITCH)
switchChangeListener { _, checked ->
if (checked) {
listener?.onTapShare(pidInfo.threePid)
@ -320,7 +323,7 @@ class DiscoverySettingsController @Inject constructor(
}
}
SharedState.BINDING_IN_PROGRESS -> {
buttonType(SettingsTextButtonSingleLineItem.ButtonType.NO_BUTTON)
buttonType(ButtonType.NO_BUTTON)
}
}
}

View File

@ -23,6 +23,7 @@ import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder
import im.vector.riotx.R
import im.vector.riotx.core.epoxy.VectorEpoxyHolder
import im.vector.riotx.core.epoxy.attributes.ButtonStyle
import im.vector.riotx.core.extensions.setTextOrHide
import im.vector.riotx.core.resources.ColorProvider
@ -40,7 +41,7 @@ abstract class SettingsButtonItem : EpoxyModelWithHolder<SettingsButtonItem.Hold
var buttonTitleId: Int? = null
@EpoxyAttribute
var buttonStyle: SettingsTextButtonSingleLineItem.ButtonStyle = SettingsTextButtonSingleLineItem.ButtonStyle.POSITIVE
var buttonStyle: ButtonStyle = ButtonStyle.POSITIVE
@EpoxyAttribute
var buttonClickListener: View.OnClickListener? = null
@ -54,10 +55,10 @@ abstract class SettingsButtonItem : EpoxyModelWithHolder<SettingsButtonItem.Hold
}
when (buttonStyle) {
SettingsTextButtonSingleLineItem.ButtonStyle.POSITIVE -> {
ButtonStyle.POSITIVE -> {
holder.button.setTextColor(colorProvider.getColor(R.color.riotx_accent))
}
SettingsTextButtonSingleLineItem.ButtonStyle.DESTRUCTIVE -> {
ButtonStyle.DESTRUCTIVE -> {
holder.button.setTextColor(colorProvider.getColor(R.color.riotx_destructive_accent))
}
}

View File

@ -30,6 +30,9 @@ import com.airbnb.epoxy.EpoxyModelWithHolder
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.attributes.ButtonStyle
import im.vector.riotx.core.epoxy.attributes.ButtonType
import im.vector.riotx.core.epoxy.attributes.IconMode
import im.vector.riotx.core.epoxy.onClick
import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.extensions.setTextOrHide
@ -40,17 +43,6 @@ import im.vector.riotx.features.themes.ThemeUtils
@EpoxyModelClass(layout = R.layout.item_settings_button_single_line)
abstract class SettingsTextButtonSingleLineItem : EpoxyModelWithHolder<SettingsTextButtonSingleLineItem.Holder>() {
enum class ButtonStyle {
POSITIVE,
DESTRUCTIVE
}
enum class ButtonType {
NO_BUTTON,
NORMAL,
SWITCH
}
@EpoxyAttribute
lateinit var colorProvider: ColorProvider
@ -65,7 +57,7 @@ abstract class SettingsTextButtonSingleLineItem : EpoxyModelWithHolder<SettingsT
var titleResId: Int? = null
@EpoxyAttribute
var iconMode: IconMode = IconMode.None
var iconMode: IconMode = IconMode.NONE
@EpoxyAttribute
var buttonTitle: String? = null
@ -146,17 +138,17 @@ abstract class SettingsTextButtonSingleLineItem : EpoxyModelWithHolder<SettingsT
when (iconMode) {
IconMode.None -> {
IconMode.NONE -> {
holder.textView.setCompoundDrawables(null, null, null, null)
}
IconMode.Info -> {
IconMode.INFO -> {
val errorColor = colorProvider.getColor(R.color.notification_accent_color)
ContextCompat.getDrawable(holder.view.context, R.drawable.ic_notification_privacy_warning)?.apply {
ThemeUtils.tintDrawableWithColor(this, errorColor)
holder.textView.setCompoundDrawablesWithIntrinsicBounds(this, null, null, null)
}
}
IconMode.Error -> {
IconMode.ERROR -> {
val errorColor = colorProvider.getColor(R.color.vector_error_color)
ContextCompat.getDrawable(holder.view.context, R.drawable.ic_notification_privacy_warning)?.apply {
ThemeUtils.tintDrawableWithColor(this, errorColor)