Skip to content

Commit

Permalink
Merge pull request #24 from selimtoksal/errorView
Browse files Browse the repository at this point in the history
Add notify error with background
  • Loading branch information
selimtoksal authored Feb 9, 2020
2 parents a546c6b + 8c38f14 commit 679fa93
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 16 deletions.
3 changes: 2 additions & 1 deletion libraries/suggestion-input-view/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="https://raw.githubusercontent.com/Trendyol/android-ui-components/master/images/suggestion-input-view-1.gif" width="240"/>

$suggestionInputViewVersion = suggestion-input-view-1.0.1 [![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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions libraries/suggestion-input-view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ apply plugin: 'kotlin-kapt'
apply plugin: 'com.github.dcendents.android-maven'

group="com.trendyol.ui-components"
version="1.0.1"

version="1.0.2"

android {
compileSdkVersion 29
Expand All @@ -14,8 +15,9 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 29
versionCode 2
versionName "1.0.1"
versionCode 3
versionName "1.0.2"

vectorDrawables.useSupportLibrary = true

consumerProguardFiles 'consumer-rules.pro'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SuggestionInputView @JvmOverloads constructor(

private var unselectedBackground: Drawable? = null

private var errorBackground: Drawable? = null

@ColorInt
private var selectedTextColor: Int = 0

Expand Down Expand Up @@ -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 =
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -245,8 +259,7 @@ class SuggestionInputView @JvmOverloads constructor(
}

items = updatedItems
bindingSelectables.recyclerViewSuggestionItems.adapter = itemsAdapter
itemsAdapter.submitList(items)
notifyAdapter()
}

private fun setSelectionToInput(suggestionInputItem: SuggestionInputItem) {
Expand All @@ -260,8 +273,7 @@ class SuggestionInputView @JvmOverloads constructor(
}

items = updatedItems
bindingSelectables.recyclerViewSuggestionItems.adapter = itemsAdapter
itemsAdapter.submitList(items)
notifyAdapter()
}

private fun showInputView() {
Expand Down Expand Up @@ -323,7 +335,9 @@ class SuggestionInputView @JvmOverloads constructor(
horizontalPadding = horizontalPadding,
verticalPadding = verticalPadding,
minWidth = minWidth,
suffix = inputSuffix
suffix = inputSuffix,
errorBackground = errorBackground,
shouldShowError = shouldShowError
)
}
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="#ff0000"/>
<stroke android:width="1dp" android:color="#999999"/>
<corners android:radius="8dp"/>

</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</data>

<androidx.appcompat.widget.AppCompatTextView
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions libraries/suggestion-input-view/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
<attr name="inputSuffix" format="string" />
<!-- input type for input view's edit text -->
<attr name="android:inputType" />
<!-- error background -->
<attr name="errorBackground" format="reference" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions sample/src/main/res/layout/activity_suggestion_input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="shouldShowError"
type="Boolean" />

</data>

Expand All @@ -19,6 +22,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.trendyol.suggestioninputview.SuggestionInputView
android:clickable="false"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:id="@+id/suggestionInputView"
Expand All @@ -36,11 +40,22 @@
app:selectedBackground="@drawable/shape_selected_background_suggestion_item"
app:selectedTextColor="@color/text_color_selected_suggestion_item"
app:textSize="@dimen/text_size_suggestion_input"
app:shouldShowError="@{shouldShowError}"
app:errorBackground="@drawable/shape_error_background_suggestion_item"
app:unselectedBackground="@drawable/shape_unselected_background_suggestion_item"
app:unselectedTextColor="@color/text_color_unselected_suggestion_item" />
</androidx.cardview.widget.CardView>


<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonLoad"
android:layout_margin="16dp"
android:gravity="center"
android:text="LOAD"
android:layout_width="match_parent"
android:layout_height="50dp"/>



<LinearLayout
android:visibility="gone"
Expand Down

0 comments on commit 679fa93

Please sign in to comment.