diff --git a/base-filters/README.md b/base-filters/README.md index 99de784..c68f7e0 100644 --- a/base-filters/README.md +++ b/base-filters/README.md @@ -95,14 +95,14 @@ val selectorView = ListSelectionView(newContext) ### Как использовать ``` kotlin binding.tagItemLayout - .setSpacing(16) - .setSelectionType(SelectionType.MULTI_SELECT) // по умолчанию - .isSingleLine(false) // по умолчанию - .onPropertySelectedAction { filterProperty: FilterProperty -> - //Do something - } - .build(getFilterItem()) + .Builder(getFilterItem()) + .setSpacing(16) + .setSelectionType(SelectionType.MULTI_SELECT) // по умолчанию + .isSingleLine(false) // по умолчанию + .onPropertySelectedAction { filterProperty: FilterProperty -> + //Do something } + .build() ``` ### Конфигурации * метод `setSelectionType(SelectionType)` конфигурирует тип выбора: @@ -112,6 +112,7 @@ binding.tagItemLayout * `setTagLayout(Int)` устанавливает разметку для тега. Если не задано - то используется дефолтная разметка `layout_default_tag.xml` * `setMaxTagCount(Int)` позволяет ограничить количество отображаемых тегов. По умолчанию ограничения нет. * `setMoreTagLayout(Int, String)` устанавливает разметку для тега, который отображается для дополнительного тега. Если не указана - то тег не будет создан -* `setSpacing(Int)`, `setSpacingHorizontal(Int`) и мsetSpacingVertical(Int)` можно использовать для настройки расстояния между тегами. По умолчанию - 0 +* `setSpacing(Int)`, `setSpacingHorizontal(Int)` и `setSpacingVertical(Int)` можно использовать для настройки расстояния между тегами. По умолчанию - 0 * `onMoreValuesAction(FilterMoreAction)` и `onPropertySelectedAction(PropertySelectedAction)` используются для передачи колбэков на клик по тегу типа "Еще" и обычного тега соответственно -* после вызова конфигурационных методов обязательно необходимо вызать метод `build(FilterItem)` +* после вызова конфигурационных методов обязательно необходимо вызать метод `build()` +* в Builder необходимо передать объект `filterItem: FilterItem` diff --git a/base-filters/src/main/java/ru/touchin/roboswag/base_filters/tags/TagLayoutView.kt b/base-filters/src/main/java/ru/touchin/roboswag/base_filters/tags/TagLayoutView.kt index a81dfbc..8c169b5 100644 --- a/base-filters/src/main/java/ru/touchin/roboswag/base_filters/tags/TagLayoutView.kt +++ b/base-filters/src/main/java/ru/touchin/roboswag/base_filters/tags/TagLayoutView.kt @@ -2,16 +2,13 @@ package ru.touchin.roboswag.base_filters.tags import android.content.Context import android.util.AttributeSet -import android.view.LayoutInflater import android.view.View import android.widget.FrameLayout import android.widget.TextView import androidx.annotation.LayoutRes -import androidx.core.view.isVisible import com.google.android.material.chip.ChipGroup import ru.touchin.roboswag.base_filters.R import ru.touchin.roboswag.base_filters.SelectionType -import ru.touchin.roboswag.base_filters.databinding.TagSelectionLayoutBinding import ru.touchin.roboswag.base_filters.tags.model.FilterItem import ru.touchin.roboswag.base_filters.tags.model.FilterProperty import ru.touchin.roboswag.components.utils.UiUtils @@ -27,10 +24,9 @@ class TagLayoutView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr) { - private val binding = TagSelectionLayoutBinding.inflate(LayoutInflater.from(context), this) private var filterItem: FilterItem by Delegates.notNull() - private var tagsContainer: ChipGroup = binding.multiLineTagGroup + private var tagsContainer: ChipGroup by Delegates.notNull() private var propertySelectedAction: PropertySelectedAction? = null private var moreValuesAction: FilterMoreAction? = null @@ -44,82 +40,16 @@ class TagLayoutView @JvmOverloads constructor( private var tagLayout: Int = R.layout.layout_default_tag private var moreTagText: String = "" - private var maxTagCount: Int? = null + private var maxTagCount = Int.MAX_VALUE @LayoutRes - private var moreTagLayout: Int? = null + private var moreTagLayout: Int = tagLayout - fun onMoreValuesAction(action: FilterMoreAction) = apply { - moreValuesAction = action - } - - fun onPropertySelectedAction(action: PropertySelectedAction) = apply { - propertySelectedAction = action - } - - fun setMaxTagCount(count: Int) = apply { - maxTagCount = count - } - - fun setSpacingHorizontal(horizontalSpacingDp: Int) = apply { - tagSpacingHorizontalDp = horizontalSpacingDp - } - - fun setSpacingVertical(verticalSpacingDp: Int) = apply { - tagSpacingVerticalDp = verticalSpacingDp - } - - fun setSpacing(value: Int) = apply { - tagSpacingHorizontalDp = value - tagSpacingVerticalDp = value - } - - fun setSelectionType(type: SelectionType) = apply { - selectionType = type - } - - fun isSingleLine(value: Boolean) = apply { - isSingleLine = value - } - - fun setTagLayout(@LayoutRes layoutId: Int) = apply { - tagLayout = layoutId - } - - fun setMoreTagLayout(@LayoutRes layoutId: Int, text: String) = apply { - moreTagLayout = layoutId - moreTagText = text - } - - fun build(filterItem: FilterItem) { - this.filterItem = filterItem - tagsContainer = getTagView(isSingleLine) - - with(tagsContainer) { - removeAllViews() - - this.isSingleLine = isSingleLine - - chipSpacingHorizontal = tagSpacingHorizontalDp.px - chipSpacingVertical = tagSpacingVerticalDp.px - - val properties = maxTagCount - ?.let { count -> filterItem.properties.take(count) } - ?: filterItem.properties - properties.forEach { property -> - addView(createTag(property)) - } - - if (maxTagCount != null && filterItem.properties.size > maxTagCount!!) { - createMoreChip(filterItem)?.let { addView(it) } - } - } - } - - private fun getTagView(isSingleLine: Boolean): ChipGroup { - binding.lineTagContainer.isVisible = isSingleLine - binding.multiLineTagGroup.isVisible = !isSingleLine - return if (isSingleLine) binding.singleLineTagGroup else binding.multiLineTagGroup + private fun inflateAndGetChipGroup(isSingleLine: Boolean): ChipGroup { + val layoutId = if (isSingleLine) R.layout.layout_single_line_tag_group else R.layout.layout_multi_line_tag_group + return UiUtils.inflate(layoutId, this) + .also { addView(it) } + .findViewById(R.id.tag_group) } private fun createTag(property: FilterProperty): TagView = @@ -138,12 +68,11 @@ class TagLayoutView @JvmOverloads constructor( } } ?: throw IllegalArgumentException("Layout for tag must be extended from TagView") - private fun createMoreChip(filter: FilterItem): View? = moreTagLayout?.let { - (UiUtils.inflate(it, this) as? TextView)?.apply { - text = moreTagText - setOnClickListener { moreValuesAction?.invoke(filter) } - } - } + private fun createMoreChip(filter: FilterItem): View? = + (UiUtils.inflate(moreTagLayout, this) as? TextView)?.apply { + text = moreTagText + setOnClickListener { moreValuesAction?.invoke(filter) } + } private fun clearCheck(selectedId: Int) { for (i in 0 until tagsContainer.childCount) { @@ -164,4 +93,71 @@ class TagLayoutView @JvmOverloads constructor( } } } + + inner class Builder(private val filterItem: FilterItem) { + + fun onMoreValuesAction(action: FilterMoreAction) = apply { + moreValuesAction = action + } + + fun onPropertySelectedAction(action: PropertySelectedAction) = apply { + propertySelectedAction = action + } + + fun setMaxTagCount(count: Int) = apply { + maxTagCount = count + } + + fun setSpacingHorizontal(horizontalSpacingDp: Int) = apply { + tagSpacingHorizontalDp = horizontalSpacingDp + } + + fun setSpacingVertical(verticalSpacingDp: Int) = apply { + tagSpacingVerticalDp = verticalSpacingDp + } + + fun setSpacing(value: Int) = apply { + tagSpacingHorizontalDp = value + tagSpacingVerticalDp = value + } + + fun setSelectionType(type: SelectionType) = apply { + selectionType = type + } + + fun isSingleLine(value: Boolean) = apply { + isSingleLine = value + } + + fun setTagLayout(@LayoutRes layoutId: Int) = apply { + tagLayout = layoutId + } + + fun setMoreTagLayout(@LayoutRes layoutId: Int, text: String) = apply { + moreTagLayout = layoutId + moreTagText = text + } + + fun build() { + this@TagLayoutView.filterItem = filterItem + tagsContainer = inflateAndGetChipGroup(isSingleLine) + + with(tagsContainer) { + removeAllViews() + + this.isSingleLine = isSingleLine + + chipSpacingHorizontal = tagSpacingHorizontalDp.px + chipSpacingVertical = tagSpacingVerticalDp.px + + filterItem.properties.take(maxTagCount).forEach { property -> + addView(createTag(property)) + } + + if (filterItem.properties.size > maxTagCount) { + createMoreChip(filterItem)?.let { addView(it) } + } + } + } + } } diff --git a/base-filters/src/main/res/layout/layout_multi_line_tag_group.xml b/base-filters/src/main/res/layout/layout_multi_line_tag_group.xml new file mode 100644 index 0000000..27118fd --- /dev/null +++ b/base-filters/src/main/res/layout/layout_multi_line_tag_group.xml @@ -0,0 +1,11 @@ + + diff --git a/base-filters/src/main/res/layout/layout_single_line_tag_group.xml b/base-filters/src/main/res/layout/layout_single_line_tag_group.xml new file mode 100644 index 0000000..3a32ef3 --- /dev/null +++ b/base-filters/src/main/res/layout/layout_single_line_tag_group.xml @@ -0,0 +1,18 @@ + + + + + + + diff --git a/base-filters/src/main/res/layout/tag_selection_layout.xml b/base-filters/src/main/res/layout/tag_selection_layout.xml deleted file mode 100644 index 1701e7c..0000000 --- a/base-filters/src/main/res/layout/tag_selection_layout.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - -