Skip to content

Commit 8befaec

Browse files
committed
refactor(dnd): centralize import validation and simplify touch handling
1 parent 86ff9e1 commit 8befaec

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

app/src/main/java/com/itsaky/androidide/dnd/GitUrlDropTarget.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ internal class GitUrlDropTarget(
3939
}
4040

4141
private fun handleDroppedRepositoryContent(payload: ContentInfoCompat): ContentInfoCompat? {
42-
if (!shouldAcceptDrop()) {
43-
return payload
44-
}
42+
if (!shouldAcceptDrop()) return payload
4543

46-
val droppedText = extractDroppedText(payload) ?: return payload
47-
val repositoryUrl = parseGitRepositoryUrl(droppedText) ?: return payload
44+
val repositoryUrl = extractRepositoryUrl(payload) ?: return payload
4845

4946
onRepositoryDropped(repositoryUrl)
5047
return null
@@ -99,14 +96,15 @@ internal class GitUrlDropTarget(
9996
}
10097
}
10198

102-
private fun extractDroppedText(payload: ContentInfoCompat): String? {
99+
private fun extractRepositoryUrl(payload: ContentInfoCompat): String? {
103100
val clip = payload.clip
104101
for (index in 0 until clip.itemCount) {
105102
val item = clip.getItemAt(index)
106103
val text = item.uri?.toString() ?: item.coerceToText(context)?.toString()
107-
if (!text.isNullOrBlank()) {
108-
return text
109-
}
104+
105+
if (text.isNullOrBlank()) continue
106+
107+
parseGitRepositoryUrl(text)?.let { return it }
110108
}
111109

112110
return null

app/src/main/java/com/itsaky/androidide/fragments/sidebar/FileTreeDropController.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class FileTreeDropController(
4545
}
4646

4747
private fun handleDragEvent(view: View, target: DropTarget, event: DragEvent): Boolean {
48-
val canHandleDrop = event.hasDroppedContent()
48+
val canHandleDrop = event.hasImportableContent(activity)
4949

5050
return when (event.action) {
5151
DragEvent.ACTION_DRAG_STARTED -> canHandleDrop
@@ -135,10 +135,7 @@ internal class FileTreeDropController(
135135

136136
var importedCount = 0
137137
for (index in 0 until clipData.itemCount) {
138-
val uri = clipData.getItemAt(index).toExternalUri() ?: continue
139-
if (uri.isInternalDragUri(context)) {
140-
continue
141-
}
138+
val uri = clipData.getItemAt(index).toImportableExternalUri(context) ?: continue
142139

143140
val defaultName = context.getString(R.string.msg_file_tree_drop_default_name)
144141
val rawName = uri.getFileName(context).ifBlank { defaultName }
@@ -180,6 +177,24 @@ internal class FileTreeDropController(
180177
}
181178
}
182179

180+
private fun ClipData.Item.toImportableExternalUri(context: Context): Uri? {
181+
val resolvedUri = toExternalUri() ?: return null
182+
return resolvedUri.takeUnless { it.isInternalDragUri(context) }
183+
}
184+
185+
private fun DragEvent.hasImportableContent(context: Context): Boolean {
186+
if (localState != null) return false
187+
188+
val clip = clipData ?: return false
189+
for (index in 0 until clip.itemCount) {
190+
if (clip.getItemAt(index).toImportableExternalUri(context) != null) {
191+
return true
192+
}
193+
}
194+
195+
return false
196+
}
197+
183198
private fun ClipData.Item.toExternalUri(): Uri? {
184199
return uri
185200
?: text?.toString()
@@ -209,11 +224,6 @@ internal class FileTreeDropController(
209224
}
210225
}
211226

212-
private fun DragEvent.hasDroppedContent(): Boolean {
213-
if (localState != null) return false
214-
return clipDescription != null || (clipData?.itemCount ?: 0) > 0
215-
}
216-
217227
private fun createAvailableTargetFile(directory: File, originalName: String): File {
218228
val dotIndex = originalName.lastIndexOf('.')
219229
val hasExtension = dotIndex > 0 && dotIndex < originalName.lastIndex

treeview/src/main/java/com/unnamed/b/atv/view/NodeTouchHandler.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,32 @@ class NodeTouchHandler implements View.OnTouchListener {
3333
public boolean onTouch(View v, MotionEvent event) {
3434
gestureDetector.onTouchEvent(event);
3535

36-
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
37-
isAwaitingDrag = false;
38-
view.setPressed(false);
39-
} else if (event.getAction() == MotionEvent.ACTION_MOVE && isAwaitingDrag) {
40-
isAwaitingDrag = false;
41-
dispatchDrag();
36+
switch (event.getAction()) {
37+
case MotionEvent.ACTION_UP:
38+
case MotionEvent.ACTION_CANCEL:
39+
resetTouchState();
40+
break;
41+
42+
case MotionEvent.ACTION_MOVE:
43+
handleMove();
44+
break;
4245
}
4346

4447
return true;
4548
}
4649

50+
private void resetTouchState() {
51+
isAwaitingDrag = false;
52+
view.setPressed(false);
53+
}
54+
55+
private void handleMove() {
56+
if (!isAwaitingDrag) return;
57+
58+
isAwaitingDrag = false;
59+
dispatchDrag();
60+
}
61+
4762
private void dispatchDrag() {
4863
TreeNode.TreeNodeDragListener listener = node.getDragListener() != null
4964
? node.getDragListener()

0 commit comments

Comments
 (0)