Skip to content

Commit

Permalink
API 21 and greater support
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigoriy Martirosov committed Feb 12, 2019
1 parent b0fec6f commit 53b35a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion easydnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ group='com.github.STRENCH0'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 24
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
Expand Down
10 changes: 5 additions & 5 deletions easydnd/src/main/java/ru/checka/easydnd/ConfigObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ru.checka.easydnd

import android.view.View

public abstract class BaseConfig<S : DragAssignment, R : DragAssignment> {
abstract class BaseConfig<S : DragAssignment, R : DragAssignment> internal constructor(){

/**
* Calls when dropping is performed. Sender and Receiver objects of type [DragAndDropObject] will be passed as params
Expand Down Expand Up @@ -48,7 +48,7 @@ public abstract class BaseConfig<S : DragAssignment, R : DragAssignment> {
/**
* Default configuration class
*/
public class DragAndDropDefaultConfig<S : DragAssignment, R : DragAssignment> : BaseConfig<S, R>() {
class DragAndDropDefaultConfig<S : DragAssignment, R : DragAssignment> : BaseConfig<S, R>() {

/**
* Calls when start dragging sender. Sender [DragAndDropObject] will be passed as param
Expand All @@ -71,7 +71,7 @@ public class DragAndDropDefaultConfig<S : DragAssignment, R : DragAssignment> :
var userAction: UserAction = UserAction.TOUCH

/**
* Override default [ShadowBuilder]
* Override default [View.DragShadowBuilder]
*/
var shadowBuilder: (View, DragAssignment) -> View.DragShadowBuilder = { view, _ -> DefaultShadowBuilder(view) }

Expand Down Expand Up @@ -100,13 +100,13 @@ public class DragAndDropDefaultConfig<S : DragAssignment, R : DragAssignment> :
/**
* Additional configuration which can override default behavior of [DragAndDropDefaultConfig]
*/
public class DragAndDropLocalConfig<S : DragAssignment, R : DragAssignment> : BaseConfig<S, R>()
class DragAndDropLocalConfig<S : DragAssignment, R : DragAssignment> : BaseConfig<S, R>()


/**
* User actions
*/
public enum class UserAction {
enum class UserAction {
/**
* Drag must be performed on user's touch action
*/
Expand Down
26 changes: 14 additions & 12 deletions easydnd/src/main/java/ru/checka/easydnd/DragAndDropManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ class DragAndDropManager<S : DragAssignment, R : DragAssignment> {
receivers.addAll(newReceivers)
for (receiver in newReceivers) {
for (sender in newSenders) {
receiverSenderMap.compute(receiver.assignedObject.tag) { _, v ->
val senderTag = sender.assignedObject.tag
v?.apply { add(senderTag) } ?: mutableSetOf(senderTag)
}
// val receiverTag = receiver.assignedObject.tag
// val senderTag = sender.assignedObject.tag
// var senders = receiverSenderMap[receiverTag]
// senders = senders?.apply { add(senderTag) } ?: mutableSetOf(senderTag)
// receiverSenderMap[receiverTag] = senders
val receiverTag = receiver.assignedObject.tag
val senderTag = sender.assignedObject.tag
receiverSenderMap
.getOrPut(receiverTag, ::mutableSetOf)
.add(senderTag)
}
}
//save custom config
Expand Down Expand Up @@ -79,9 +75,10 @@ class DragAndDropManager<S : DragAssignment, R : DragAssignment> {
private fun checkSenderReceiver(sender: DragAndDropObject<S>?, receiver: DragAndDropObject<R>?): Boolean {
if (sender == null || receiver == null)
return false
val set = receiverSenderMap[receiver.assignedObject.tag]
if (defaultConfig.selfDrop || sender.assignedObject.tag != receiver.assignedObject.tag) {
return set != null && set.contains(sender.assignedObject.tag)
return receiverSenderMap[receiver.assignedObject.tag]
?.contains(sender.assignedObject.tag)
?: false
}
return false
}
Expand Down Expand Up @@ -112,6 +109,7 @@ class DragAndDropManager<S : DragAssignment, R : DragAssignment> {
}

}

DragEvent.ACTION_DRAG_EXITED -> {
val sender = senders.find { event.clipDescription.label == it.assignedObject.tag }
val receiver = receivers.find { view.tag == it.assignedObject.tag }
Expand Down Expand Up @@ -157,7 +155,11 @@ class DragAndDropManager<S : DragAssignment, R : DragAssignment> {
val action: (View, S) -> Boolean = { view, obj ->
val item = ClipData.Item("DragAndDrop" as CharSequence)
val dragData = ClipData(obj.tag, arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN), item)
view.startDragAndDrop(dragData, sbBuilder(view, obj), null, dragFlags)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
view.startDragAndDrop(dragData, sbBuilder(view, obj), null, dragFlags)
} else {
view.startDrag(dragData, sbBuilder(view, obj), null, dragFlags)
}
true

}
Expand Down

0 comments on commit 53b35a3

Please sign in to comment.