diff --git a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SimpleSwipeLayout.kt b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SimpleSwipeLayout.kt index 980f5b5..025ced0 100644 --- a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SimpleSwipeLayout.kt +++ b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SimpleSwipeLayout.kt @@ -3,6 +3,7 @@ package com.github.chantsune.swipetoaction.views import android.content.Context import android.content.res.TypedArray import android.util.AttributeSet +import android.view.View import android.view.ViewGroup import androidx.core.content.res.use import com.github.chantsune.swipetoaction.R @@ -25,9 +26,6 @@ open class SimpleSwipeLayout(c: Context, attrs: AttributeSet? = null) : SwipeLay var leftTexts: Array = arrayOf() var rightTexts: Array = arrayOf() - private val rightItems: MutableList = mutableListOf() - private val leftItems: MutableList = mutableListOf() - private var iconSize = 0 private var textSize = 0f @@ -201,50 +199,4 @@ open class SimpleSwipeLayout(c: Context, attrs: AttributeSet? = null) : SwipeLay ) } } - - private fun createSwipeItem( - swipeItem: SwipeItem - ): SwipeItem { - swipeItem.view = SwipeItemView(context).also { - it.update(swipeItem) - it.setOnTouchListener(this) - } - return swipeItem - } - - private fun addSwipeItem(swipeItem: SwipeItem) { - if (swipeItem.left) { - leftItems.add(swipeItem) - setLeftSwipeItems(leftItems.map { it.view }) - } else { - rightItems.add(swipeItem) - setRightSwipeItems(rightItems.map { it.view }) - } - } - - private fun removeSwipeItem(swipeItem: SwipeItem) { - if (swipeItem.left) { - leftItems.remove(swipeItem) - setLeftSwipeItems(leftItems.map { it.view }) - } else { - rightItems.remove(swipeItem) - setRightSwipeItems(rightItems.map { it.view }) - } - } - - internal class SwipeItem( - val icon: Int?, - val iconColor: Int?, - val backgroundColor: Int?, - val text: String?, - val textColor: Int?, - val left: Boolean, - // internal params - val itemWidth: Int, - val iconSize: Int, - val textSize: Float, - ) { - lateinit var view: SwipeItemView - internal set - } } diff --git a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeItemView.kt b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeItemView.kt index be576a6..4aeb94a 100644 --- a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeItemView.kt +++ b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeItemView.kt @@ -24,7 +24,7 @@ internal class SwipeItemView( foreground = rippleDrawable } - private fun getImageViewInternal(swipeItem: SimpleSwipeLayout.SwipeItem): ImageView? = + private fun getImageViewInternal(swipeItem: SwipeLayout.SwipeItem): ImageView? = swipeItem.icon?.let { icon -> imageView.also { imageView -> imageView.setImageDrawable( @@ -37,7 +37,7 @@ internal class SwipeItemView( } } - private fun getTextViewInternal(swipeItem: SimpleSwipeLayout.SwipeItem): TextView? = + private fun getTextViewInternal(swipeItem: SwipeLayout.SwipeItem): TextView? = swipeItem.text?.let { text -> textView.also { textView -> textView.maxLines = 2 @@ -53,7 +53,7 @@ internal class SwipeItemView( } } - fun update(swipeItem: SimpleSwipeLayout.SwipeItem) { + fun update(swipeItem: SwipeLayout.SwipeItem) { removeAllViews() // NOTE: clear views val imageView = getImageViewInternal(swipeItem) val textView = getTextViewInternal(swipeItem) diff --git a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeLayout.kt b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeLayout.kt index 1981325..c8268eb 100644 --- a/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeLayout.kt +++ b/swipe-to-action/src/main/java/com/github/chantsune/swipetoaction/views/SwipeLayout.kt @@ -1,12 +1,12 @@ package com.github.chantsune.swipetoaction.views +import android.annotation.SuppressLint import android.content.Context import android.os.Handler import android.os.Looper import android.util.AttributeSet import android.util.Log import android.view.* -import android.view.View.OnTouchListener import android.view.animation.Animation import android.widget.* import androidx.core.content.res.use @@ -27,7 +27,7 @@ open class SwipeLayout( ) : FrameLayout( context, attrs, defStyleAttr, defStyleRes - ), OnTouchListener { + ), View.OnTouchListener { constructor(context: Context): this(context, null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this( @@ -700,6 +700,62 @@ open class SwipeLayout( fun onSwipeItemClick(view: View, left: Boolean, index: Int) } + private val rightItems: MutableList = mutableListOf() + private val leftItems: MutableList = mutableListOf() + + internal fun createSwipeItem( + swipeItem: SwipeItem + ): SwipeItem { + swipeItem.view = SwipeItemView(context) + swipeItem.update(this) + return swipeItem + } + + internal fun addSwipeItem(swipeItem: SwipeItem) { + if (swipeItem.left) { + leftItems.add(swipeItem) + setLeftSwipeItems(leftItems.map { it.customView ?: it.view }) + } else { + rightItems.add(swipeItem) + setRightSwipeItems(rightItems.map { it.customView ?: it.view }) + } + } + + internal fun removeSwipeItem(swipeItem: SwipeItem) { + if (swipeItem.left) { + leftItems.remove(swipeItem) + setLeftSwipeItems(leftItems.map { it.customView ?: it.view }) + } else { + rightItems.remove(swipeItem) + setRightSwipeItems(rightItems.map { it.customView ?: it.view }) + } + } + + internal class SwipeItem( + val icon: Int?, + val iconColor: Int?, + val backgroundColor: Int?, + val text: String?, + val textColor: Int?, + val left: Boolean, + // internal params + val itemWidth: Int, + val iconSize: Int, + val textSize: Float, + ) { + lateinit var view: SwipeItemView + internal set + var customView: View? = null + + @SuppressLint("ClickableViewAccessibility") + internal fun update(l: View.OnTouchListener) { + customView?.setOnTouchListener(l) ?: kotlin.run { + view.update(this) + view.setOnTouchListener(l) + } + } + } + companion object { val TAG = SwipeLayout::class.simpleName const val NO_ID = 0