Skip to content

Commit

Permalink
Move SwipeItem SimpleSwipeLayout to SwipeLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed May 4, 2021
1 parent be239d3 commit c8a4208
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,9 +26,6 @@ open class SimpleSwipeLayout(c: Context, attrs: AttributeSet? = null) : SwipeLay
var leftTexts: Array<String> = arrayOf()
var rightTexts: Array<String> = arrayOf()

private val rightItems: MutableList<SwipeItem> = mutableListOf()
private val leftItems: MutableList<SwipeItem> = mutableListOf()

private var iconSize = 0
private var textSize = 0f

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -700,6 +700,62 @@ open class SwipeLayout(
fun onSwipeItemClick(view: View, left: Boolean, index: Int)
}

private val rightItems: MutableList<SwipeItem> = mutableListOf()
private val leftItems: MutableList<SwipeItem> = 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
Expand Down

0 comments on commit c8a4208

Please sign in to comment.