From b1d05c6fdd1c6088a014422e3af57a15c0292a4f Mon Sep 17 00:00:00 2001 From: "selim.toksal" Date: Sun, 9 Feb 2020 14:32:01 +0300 Subject: [PATCH] Add notify error with background --- libraries/suggestion-input-view/README.md | 3 +- libraries/suggestion-input-view/build.gradle | 6 +-- .../SuggestionInputItemViewState.kt | 13 +++++-- .../SuggestionInputView.kt | 39 +++++++++++++++---- ...shape_error_background_suggestion_item.xml | 9 +++++ .../src/main/res/layout/item_suggestion.xml | 1 - .../src/main/res/values/attrs.xml | 2 + .../SuggestionInputActivity.kt | 6 +++ .../res/layout/activity_suggestion_input.xml | 15 +++++++ 9 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 libraries/suggestion-input-view/src/main/res/drawable/shape_error_background_suggestion_item.xml diff --git a/libraries/suggestion-input-view/README.md b/libraries/suggestion-input-view/README.md index 3199b160..b5c303a0 100644 --- a/libraries/suggestion-input-view/README.md +++ b/libraries/suggestion-input-view/README.md @@ -1,6 +1,6 @@ -suggestionInputViewVersion = suggestion-input-view-1.0.0 [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) +suggestionInputViewVersion = suggestion-input-view-1.0.2 [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) ## SuggestionInputView SuggestionInputView allows selecting pre-selected options or entering a custom option @@ -41,6 +41,7 @@ To set width you can use `android:layout_width` attribute. To customize more you | `app:inputEditTextBackground` | `setInputEditTextBackground(Drawable)` | Drawable of input view's edit text background | shape_unselected_background_suggestion_item | app:inputEditTextBackground="@drawable/shape_unselected_background_suggestion_item"| | `app:inputSuffix` | `setInputSuffix(String)` | Suffix of input view's edit text | Empty String | app:inputSuffix="₺"| | `android:inputType` | `` | Input type of input view's edit text input type | TYPE_TEXT_VARIATION_NORMAL | android:inputType="number"| +| `app:shouldShowError` | `shouldShowError(Boolean)` | Change suggestion item's background | false | app:shouldShowError="@{true}"| # Contributors This library is maintained mainly by Trendyol Android Team members but also other Android lovers contributes. diff --git a/libraries/suggestion-input-view/build.gradle b/libraries/suggestion-input-view/build.gradle index cc9b7063..bc7892de 100644 --- a/libraries/suggestion-input-view/build.gradle +++ b/libraries/suggestion-input-view/build.gradle @@ -5,7 +5,7 @@ apply plugin: 'kotlin-kapt' apply plugin: 'com.github.dcendents.android-maven' group="com.trendyol.ui-components" -version="1.0.0" +version="1.0.2" android { compileSdkVersion 29 @@ -14,8 +14,8 @@ android { defaultConfig { minSdkVersion 17 targetSdkVersion 29 - versionCode 1 - versionName "1.0" + versionCode 3 + versionName "1.0.2" vectorDrawables.useSupportLibrary = true consumerProguardFiles 'consumer-rules.pro' diff --git a/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputItemViewState.kt b/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputItemViewState.kt index 7187bcf2..7eeac300 100644 --- a/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputItemViewState.kt +++ b/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputItemViewState.kt @@ -12,19 +12,24 @@ data class SuggestionInputItemViewState( val value: String, val selectedBackground: Drawable?, val unselectedBackground: Drawable?, + val errorBackground: Drawable?, val suffix: String, + val shouldShowError: Boolean, @ColorInt val selectedTextColor: Int, @ColorInt val unselectedTextColor: Int, @Dimension val textSize: Float, @Dimension val horizontalPadding: Float, @Dimension val verticalPadding: Float, @Dimension val minWidth: Float - ) { - fun getBackground(): Drawable? = - if(isSelected) selectedBackground else unselectedBackground +) { + fun getBackground(): Drawable? = when { + shouldShowError -> errorBackground + isSelected -> selectedBackground + else -> unselectedBackground + } fun getTextColor(): Int = - if(isSelected) selectedTextColor else unselectedTextColor + if (isSelected) selectedTextColor else unselectedTextColor fun getMinWidth(): Int = minWidth.toInt() } \ No newline at end of file diff --git a/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputView.kt b/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputView.kt index 90a963cb..73da0050 100644 --- a/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputView.kt +++ b/libraries/suggestion-input-view/src/main/java/com/trendyol/suggestioninputview/SuggestionInputView.kt @@ -25,6 +25,8 @@ class SuggestionInputView @JvmOverloads constructor( private var unselectedBackground: Drawable? = null + private var errorBackground: Drawable? = null + @ColorInt private var selectedTextColor: Int = 0 @@ -56,6 +58,8 @@ class SuggestionInputView @JvmOverloads constructor( private var inputType: Int = 0 + private var shouldShowError: Boolean = false + private val bindingSelectables: ViewSuggestionSelectablesBinding = inflate(R.layout.view_suggestion_selectables) private val bindingInput: ViewSuggestionInputBinding = @@ -81,6 +85,9 @@ class SuggestionInputView @JvmOverloads constructor( unselectedBackground = getDrawable(R.styleable.SuggestionInputView_unselectedBackground) ?: context.drawable(R.drawable.shape_unselected_background_suggestion_item) + errorBackground = + getDrawable(R.styleable.SuggestionInputView_errorBackground) + ?: context.drawable(R.drawable.shape_error_background_suggestion_item) selectedTextColor = getColor( R.styleable.SuggestionInputView_selectedTextColor, context.color(R.color.text_color_selected_suggestion_item) @@ -203,11 +210,18 @@ class SuggestionInputView @JvmOverloads constructor( setInitializeSelection() } + fun shouldShowError(shouldShowError: Boolean) { + this.shouldShowError = shouldShowError + notifyErrorToItems() + notifyAdapter() + } + fun setSuggestionItemClickListener(function: (SuggestionInputItem) -> (Unit)) { this.onSuggestionItemClickListener = function } private fun onSuggestionItemClicked(suggestionInputItemViewState: SuggestionInputItemViewState) { + shouldShowError(false) val itemType = suggestionInputItemViewState.type if (itemType == SuggestionItemType.SELECTABLE) { setSelectionToSuggestionItem(suggestionInputItemViewState) @@ -229,8 +243,8 @@ class SuggestionInputView @JvmOverloads constructor( } private fun setInitializeSelection() { - val selectedItem: SuggestionInputItemViewState? = items.firstOrNull { item -> - item.isSelected + val selectedItem: SuggestionInputItemViewState? = items.firstOrNull { item -> + item.isSelected } if (selectedItem != null) { @@ -245,8 +259,7 @@ class SuggestionInputView @JvmOverloads constructor( } items = updatedItems - bindingSelectables.recyclerViewSuggestionItems.adapter = itemsAdapter - itemsAdapter.submitList(items) + notifyAdapter() } private fun setSelectionToInput(suggestionInputItem: SuggestionInputItem) { @@ -260,8 +273,7 @@ class SuggestionInputView @JvmOverloads constructor( } items = updatedItems - bindingSelectables.recyclerViewSuggestionItems.adapter = itemsAdapter - itemsAdapter.submitList(items) + notifyAdapter() } private fun showInputView() { @@ -323,7 +335,9 @@ class SuggestionInputView @JvmOverloads constructor( horizontalPadding = horizontalPadding, verticalPadding = verticalPadding, minWidth = minWidth, - suffix = inputSuffix + suffix = inputSuffix, + errorBackground = errorBackground, + shouldShowError = shouldShowError ) } } @@ -369,6 +383,17 @@ class SuggestionInputView @JvmOverloads constructor( suffix = inputSuffix ) + private fun notifyErrorToItems() { + items = items.map { + it.copy(shouldShowError = shouldShowError) + } + } + + private fun notifyAdapter() { + bindingSelectables.recyclerViewSuggestionItems.adapter = itemsAdapter + itemsAdapter.submitList(items) + } + companion object { const val ID_FREE_TEXT = 192 } diff --git a/libraries/suggestion-input-view/src/main/res/drawable/shape_error_background_suggestion_item.xml b/libraries/suggestion-input-view/src/main/res/drawable/shape_error_background_suggestion_item.xml new file mode 100644 index 00000000..258b96e7 --- /dev/null +++ b/libraries/suggestion-input-view/src/main/res/drawable/shape_error_background_suggestion_item.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/libraries/suggestion-input-view/src/main/res/layout/item_suggestion.xml b/libraries/suggestion-input-view/src/main/res/layout/item_suggestion.xml index d335bca7..7cc1adae 100644 --- a/libraries/suggestion-input-view/src/main/res/layout/item_suggestion.xml +++ b/libraries/suggestion-input-view/src/main/res/layout/item_suggestion.xml @@ -10,7 +10,6 @@ + + \ No newline at end of file diff --git a/sample/src/main/java/com/trendyol/uicomponents/suggestioninput/SuggestionInputActivity.kt b/sample/src/main/java/com/trendyol/uicomponents/suggestioninput/SuggestionInputActivity.kt index 2f95858f..1a35cacf 100644 --- a/sample/src/main/java/com/trendyol/uicomponents/suggestioninput/SuggestionInputActivity.kt +++ b/sample/src/main/java/com/trendyol/uicomponents/suggestioninput/SuggestionInputActivity.kt @@ -21,6 +21,12 @@ class SuggestionInputActivity : AppCompatActivity() { binding.suggestionInputView.setSuggestionItemClickListener { onSuggestionItemClicked(it) } binding.suggestionInputView.setItems(createSuggestionInputItems()) + binding.buttonLoad.setOnClickListener { onLoadClicked() } + } + + private fun onLoadClicked() { + binding.shouldShowError = true + binding.executePendingBindings() } private fun onSuggestionItemClicked(suggestionInputItem: SuggestionInputItem) { diff --git a/sample/src/main/res/layout/activity_suggestion_input.xml b/sample/src/main/res/layout/activity_suggestion_input.xml index 453400ac..16cd1ace 100644 --- a/sample/src/main/res/layout/activity_suggestion_input.xml +++ b/sample/src/main/res/layout/activity_suggestion_input.xml @@ -4,6 +4,9 @@ xmlns:tools="http://schemas.android.com/tools"> + @@ -19,6 +22,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> + + +