Skip to content

Commit

Permalink
Support RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
aitsuki committed Jun 7, 2022
1 parent a8d1b3c commit e2ce2f3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
15 changes: 0 additions & 15 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'kotlin-android'

group='com.github.aitsuki'
Expand Down Expand Up @@ -37,18 +36,4 @@ android {
dependencies {
compileOnly "androidx.recyclerview:recyclerview:$recyclerview_version"
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
from components.release
groupId = 'com.github.aitsuki'
artifactId = 'SwipeMenuRecyclerView'
version = '2.0.0'
}
}
}
}
87 changes: 72 additions & 15 deletions library/src/main/java/com/aitsuki/swipe/SwipeLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ class SwipeLayout @JvmOverloads constructor(
private const val PREVIEW_NONE = 0
private const val PREVIEW_LEFT = 1
private const val PREVIEW_RIGHT = 2
private const val PREVIEW_START = 3
private const val PREVIEW_END = 4

private const val FLAG_IS_OPENED = 0x1
private const val FLAG_IS_OPENING = 0x2
private const val FLAG_IS_CLOSING = 0x4

const val LEFT = 1
const val RIGHT = 1 shl 1
const val LEFT = 0x0001
const val RIGHT = 0x0002
const val RELATIVE_LAYOUT_DIRECTION = 0x1000
const val START = LEFT or RELATIVE_LAYOUT_DIRECTION
const val END = RIGHT or RELATIVE_LAYOUT_DIRECTION


const val STATE_IDLE = ViewDragHelper.STATE_IDLE
const val STATE_DRAGGING = ViewDragHelper.STATE_DRAGGING
Expand Down Expand Up @@ -71,11 +77,12 @@ class SwipeLayout @JvmOverloads constructor(
*/
var swipeFlags = LEFT or RIGHT
set(value) {
if ((value and (LEFT or RIGHT)) == 0) {
val direction = getAbsoluteDirection(value)
if ((direction and (LEFT or RIGHT)) == 0) {
closeActiveMenu()
} else if ((value and LEFT) == 0) {
} else if ((direction and LEFT) == 0) {
closeRightMenu()
} else if ((value and RIGHT) == 0) {
} else if ((direction and RIGHT) == 0) {
closeLeftMenu()
}
field = value
Expand Down Expand Up @@ -112,6 +119,14 @@ class SwipeLayout @JvmOverloads constructor(
}
}

fun closeStartMenu(animate: Boolean = true) {
if (isLayoutRTL()) closeRightMenu(animate) else closeLeftMenu(animate)
}

fun closeEndMenu(animate: Boolean = true) {
if (isLayoutRTL()) closeLeftMenu(animate) else closeRightMenu(animate)
}

fun isLeftMenuOpened(): Boolean {
val activeMenu = activeMenu ?: return false
return activeMenu == leftMenu && openState and FLAG_IS_OPENED == FLAG_IS_OPENED
Expand All @@ -122,6 +137,14 @@ class SwipeLayout @JvmOverloads constructor(
return activeMenu == rightMenu && openState and FLAG_IS_OPENED == FLAG_IS_OPENED
}

fun isStartMenuOpened():Boolean {
return if (isLayoutRTL()) isRightMenuOpened() else isLeftMenuOpened()
}

fun isEndMenuOpened(): Boolean {
return if (isLayoutRTL()) isLeftMenuOpened() else isRightMenuOpened()
}

fun openLeftMenu(animate: Boolean = true) {
autoClosePending = false
activeMenu = leftMenu
Expand All @@ -134,6 +157,14 @@ class SwipeLayout @JvmOverloads constructor(
openActiveMenu(animate)
}

fun openStartMenu(animate: Boolean = true) {
if (isLayoutRTL()) openRightMenu(animate) else openLeftMenu(animate)
}

fun openEndMenu(animate: Boolean = true) {
if (isLayoutRTL()) openLeftMenu(animate) else openRightMenu(animate)
}

fun addListener(listener: Listener) {
listeners.add(listener)
}
Expand Down Expand Up @@ -233,8 +264,10 @@ class SwipeLayout @JvmOverloads constructor(

val dx = ev.x.toInt() - downX
val dy = ev.y.toInt() - downY
val isRightDragging = dx > touchSlop && (swipeFlags and RIGHT) != 0 && dx > abs(dy)
val isLeftDragging = dx < -touchSlop && (swipeFlags and LEFT) != 0 &&abs(dx) > abs(dy)

val direction = getAbsoluteDirection(swipeFlags)
val isLeftDragging = dx < -touchSlop && (direction and LEFT) != 0 && abs(dx) > abs(dy)
val isRightDragging = dx > touchSlop && (direction and RIGHT) != 0 && dx > abs(dy)

if (openState and FLAG_IS_OPENED == FLAG_IS_OPENED
|| openState and FLAG_IS_OPENING == FLAG_IS_OPENING
Expand Down Expand Up @@ -529,13 +562,7 @@ class SwipeLayout @JvmOverloads constructor(

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
if (isInEditMode) {
if (preview == PREVIEW_LEFT) {
activeMenu = leftMenu
openActiveMenu(false)
} else if (preview == PREVIEW_RIGHT) {
activeMenu = rightMenu
openActiveMenu(false)
}
openPreview()
}
val parentLeft = paddingLeft
val parentRight = right - left - paddingRight
Expand Down Expand Up @@ -588,6 +615,36 @@ class SwipeLayout @JvmOverloads constructor(
}
}

private fun openPreview() {
activeMenu = when (preview) {
PREVIEW_LEFT -> leftMenu
PREVIEW_RIGHT -> rightMenu
PREVIEW_START -> if (isLayoutRTL()) rightMenu else leftMenu
PREVIEW_END -> if (isLayoutRTL()) leftMenu else rightMenu
else -> null
}
openActiveMenu(false)
}

private fun isLayoutRTL(): Boolean {
return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL
}

private fun getAbsoluteDirection(direction: Int): Int {
var result = direction
if ((result and RELATIVE_LAYOUT_DIRECTION) > 0) {
if ((result and START) == START) {
result = result and START.inv()
result = if (isLayoutRTL()) result or RIGHT else result or LEFT
} else if ((result and END) == END) {
result = result and END.inv()
result = if (isLayoutRTL()) result or LEFT else result or RIGHT
}
result = result and RELATIVE_LAYOUT_DIRECTION.inv()
}
return result
}

override fun generateDefaultLayoutParams(): ViewGroup.LayoutParams {
return LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
Expand Down Expand Up @@ -781,7 +838,7 @@ class SwipeLayout @JvmOverloads constructor(
child.layout(
childRight - child.width,
child.top,
childRight ,
childRight,
child.bottom
)
prevChild = child
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<enum name="none" value="0"/>
<enum name="left" value="1"/>
<enum name="right" value="2"/>
<enum name="start" value="3"/>
<enum name="end" value="4"/>
</attr>
<attr name="autoClose" format="boolean"/>
<attr name="designer" format="string"/>
Expand Down

0 comments on commit e2ce2f3

Please sign in to comment.