Skip to content

Fix single mode #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions swipy/src/main/java/com/dotloop/swipy/SwipableCellHandler.kt
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
package com.dotloop.swipy

import com.dotloop.swipy.SwipableCellHandler.Mode.Single
import java.util.*

class SwipableCellHandler {

private val openPositions: MutableSet<Int> = HashSet()
private var mode = Single
class SwipableCellHandler() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the empty constructor can be removed


private val openHistory: MutableMap<Int, SwipeLayout> = HashMap()
var mode = Single

fun bindSwipableCell(swipeLayout: SwipeLayout, position: Int) {
swipeLayout.tag = position
swipeLayout.onSwipeListeners = (object : OnSwipeListener {
override fun onClose(layout: SwipeLayout) {
openPositions.remove(layout.tag as Int)
openHistory.remove(layout.tag as Int)
}

override fun onStartOpen(layout: SwipeLayout) {
val position = layout.tag as Int
/**if (mode == Attributes.Mode.Single &&
* !openPositions.isEmpty() &&
* !openPositions.contains(position))
* adapter.notifyItemChanged(position); */
val pos = layout.tag as Int
if (mode == Single && !openHistory.isEmpty() && !openHistory.containsKey(pos)) {
openHistory.keys.forEach { openHistory[it]?.close(smooth = true, notify = true) }
}
}

override fun onOpen(layout: SwipeLayout) {
openPositions.add(layout.tag as Int)
openHistory[layout.tag as Int] = layout
}
})

swipeLayout.simpleOnLayoutChangeListenerListener = (object : SimpleOnLayoutChangeListener {
swipeLayout.simpleOnLayoutChangeListener = (object : SimpleOnLayoutChangeListener {
override fun onLayoutChange(layout: SwipeLayout) =
if (isOpen(layout.tag as Int)) layout.open(false, false)
else layout.close(false, false)
if (isOpen(layout.tag as Int)) layout.open(smooth = false, notify = false)
else layout.close(smooth = false, notify = false)
})

}

@JvmOverloads
fun reset(position: Int? = null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method being used? If not, can you remove it?

Copy link
Contributor Author

@elodieferrais elodieferrais Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it's not used. You don't think it could be useful to have a method to programmatically close all the "open" cells? Meaning cell being swiped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine to leave it.

when (position) {
null -> openPositions.clear()
else -> openPositions.remove(position)
null -> openHistory.clear()
else -> openHistory.remove(position)
}
}

fun isOpen(position: Int): Boolean {
return openPositions.contains(position)
}

fun setMode(mode: Mode) {
this.mode = mode
return openHistory.containsKey(position)
}

enum class Mode {
Expand Down
4 changes: 2 additions & 2 deletions swipy/src/main/java/com/dotloop/swipy/SwipeLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SwipeLayout @JvmOverloads constructor(
private var isBeingDragged: Boolean = false

var onSwipeListeners: OnSwipeListener? = null
var simpleOnLayoutChangeListenerListener: SimpleOnLayoutChangeListener? = null
var simpleOnLayoutChangeListener: SimpleOnLayoutChangeListener? = null
private var clickListener: View.OnClickListener? = null

private var viewBoundCache: HashMap<View, Rect> = HashMap()//save all children's bound, restore in onLayoutChange
Expand Down Expand Up @@ -357,7 +357,7 @@ class SwipeLayout @JvmOverloads constructor(

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
updateActionViews()
simpleOnLayoutChangeListenerListener?.onLayoutChange(this)
simpleOnLayoutChangeListener?.onLayoutChange(this)
}

@JvmOverloads
Expand Down