Skip to content

Commit 7037378

Browse files
authored
Merge pull request #1162 from AppFlowy-IO/fix/1156
fix: calculate overlap
2 parents 882043e + ee46303 commit 7037378

File tree

10 files changed

+119
-105
lines changed

10 files changed

+119
-105
lines changed

frontend/app_flowy/packages/appflowy_board/lib/src/utils/log.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ class Log {
88

99
static void info(String? message) {
1010
if (enableLog) {
11-
debugPrint('ℹ️[Info]=> $message');
11+
debugPrint('AppFlowyBoard: ℹ️[Info]=> $message');
1212
}
1313
}
1414

1515
static void debug(String? message) {
1616
if (enableLog) {
17-
debugPrint('🐛[Debug] - ${DateTime.now().second}=> $message');
17+
debugPrint(
18+
'AppFlowyBoard: 🐛[Debug] - ${DateTime.now().second}=> $message');
1819
}
1920
}
2021

2122
static void warn(String? message) {
2223
if (enableLog) {
23-
debugPrint('🐛[Warn] - ${DateTime.now().second} => $message');
24+
debugPrint(
25+
'AppFlowyBoard: 🐛[Warn] - ${DateTime.now().second} => $message');
2426
}
2527
}
2628

2729
static void trace(String? message) {
2830
if (enableLog) {
29-
debugPrint('❗️[Trace] - ${DateTime.now().second}=> $message');
31+
debugPrint(
32+
'AppFlowyBoard: ❗️[Trace] - ${DateTime.now().second}=> $message');
3033
}
3134
}
3235
}

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/board_data.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ class AppFlowyBoardController extends ChangeNotifier
217217
final fromGroupItem = fromGroupController.removeAt(fromGroupIndex);
218218
if (toGroupController.items.length > toGroupIndex) {
219219
assert(toGroupController.items[toGroupIndex] is PhantomGroupItem);
220-
}
221220

222-
toGroupController.replace(toGroupIndex, fromGroupItem);
223-
onMoveGroupItemToGroup?.call(
224-
fromGroupId,
225-
fromGroupIndex,
226-
toGroupId,
227-
toGroupIndex,
228-
);
221+
toGroupController.replace(toGroupIndex, fromGroupItem);
222+
onMoveGroupItemToGroup?.call(
223+
fromGroupId,
224+
fromGroupIndex,
225+
toGroupId,
226+
toGroupIndex,
227+
);
228+
}
229229
}
230230

231231
@override

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/board_group/group.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class AppFlowyBoardGroup extends StatefulWidget {
112112
this.itemMargin = EdgeInsets.zero,
113113
this.cornerRadius = 0.0,
114114
this.backgroundColor = Colors.transparent,
115-
}) : config = const ReorderFlexConfig(setStateWhenEndDrag: false),
115+
}) : config = const ReorderFlexConfig(),
116116
super(key: key);
117117

118118
@override

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/board_group/group_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class AppFlowyGroupController extends ChangeNotifier with EquatableMixin {
124124
Log.debug('[$AppFlowyGroupController] $groupData add $newItem');
125125
} else {
126126
if (index >= groupData._items.length) {
127+
Log.warn(
128+
'[$AppFlowyGroupController] unexpected items length, index should less than the count of the items. Index: $index, items count: ${items.length}');
127129
return;
128130
}
129131

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/reorder_flex/drag_state.dart

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class FlexDragTargetData extends DragTargetData {
2222

2323
Size? get feedbackSize => _state.feedbackSize;
2424

25+
bool get isDragging => _state.isDragging();
26+
2527
final String dragTargetId;
2628

2729
Offset dragTargetOffset = Offset.zero;
@@ -48,39 +50,20 @@ class FlexDragTargetData extends DragTargetData {
4850

4951
bool isOverlapWithWidgets(List<GlobalObjectKey> widgetKeys) {
5052
final renderBox = dragTargetIndexKey.currentContext?.findRenderObject();
51-
5253
if (renderBox == null) return false;
5354
if (renderBox is! RenderBox) return false;
5455
final size = feedbackSize ?? Size.zero;
55-
final Rect rect = dragTargetOffset & size;
5656

57+
final Rect dragTargetRect = renderBox.localToGlobal(Offset.zero) & size;
5758
for (final widgetKey in widgetKeys) {
5859
final renderObject = widgetKey.currentContext?.findRenderObject();
5960
if (renderObject != null && renderObject is RenderBox) {
6061
Rect widgetRect =
6162
renderObject.localToGlobal(Offset.zero) & renderObject.size;
62-
// return rect.overlaps(widgetRect);
63-
if (rect.right <= widgetRect.left || widgetRect.right <= rect.left) {
64-
return false;
65-
}
66-
67-
if (rect.bottom <= widgetRect.top || widgetRect.bottom <= rect.top) {
68-
return false;
69-
}
70-
return true;
63+
return dragTargetRect.overlaps(widgetRect);
7164
}
7265
}
7366

74-
// final HitTestResult result = HitTestResult();
75-
// WidgetsBinding.instance.hitTest(result, position);
76-
// for (final HitTestEntry entry in result.path) {
77-
// final HitTestTarget target = entry.target;
78-
// if (target is RenderMetaData) {
79-
// print(target.metaData);
80-
// }
81-
// print(target);
82-
// }
83-
8467
return false;
8568
}
8669
}
@@ -113,7 +96,7 @@ class DraggingState {
11396
int currentIndex = -1;
11497

11598
/// The widget to move the dragging widget too after the current index.
116-
int nextIndex = 0;
99+
int nextIndex = -1;
117100

118101
/// Whether or not we are currently scrolling this view to show a widget.
119102
bool scrolling = false;
@@ -149,6 +132,7 @@ class DraggingState {
149132
dragStartIndex = -1;
150133
phantomIndex = -1;
151134
currentIndex = -1;
135+
nextIndex = -1;
152136
_draggingWidget = null;
153137
}
154138

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/reorder_flex/drag_target.dart

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:appflowy_board/src/utils/log.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter/scheduler.dart';
34
import 'package:provider/provider.dart';
@@ -184,8 +185,9 @@ class _ReorderDragTargetState<T extends DragTargetData>
184185
/// When the drag does not end inside a DragTarget widget, the
185186
/// drag fails, but we still reorder the widget to the last position it
186187
/// had been dragged to.
187-
onDraggableCanceled: (Velocity velocity, Offset offset) =>
188-
widget.onDragEnded(widget.dragTargetData),
188+
onDraggableCanceled: (Velocity velocity, Offset offset) {
189+
widget.onDragEnded(widget.dragTargetData);
190+
},
189191
child: widget.child,
190192
);
191193

@@ -221,8 +223,11 @@ class DragTargetAnimation {
221223
// where the widget used to be.
222224
late AnimationController phantomController;
223225

226+
// Uses to simulate the insert animation when card was moved from on group to
227+
// another group. Check out the [FakeDragTarget].
224228
late AnimationController insertController;
225229

230+
// Used to remove the phantom
226231
late AnimationController deleteController;
227232

228233
DragTargetAnimation({
@@ -238,7 +243,7 @@ class DragTargetAnimation {
238243
value: 0, vsync: vsync, duration: reorderAnimationDuration);
239244

240245
insertController = AnimationController(
241-
value: 0.0, vsync: vsync, duration: const Duration(milliseconds: 200));
246+
value: 0.0, vsync: vsync, duration: const Duration(milliseconds: 100));
242247

243248
deleteController = AnimationController(
244249
value: 0.0, vsync: vsync, duration: const Duration(milliseconds: 1));
@@ -423,7 +428,6 @@ abstract class FakeDragTargetEventData {
423428
}
424429

425430
class FakeDragTarget<T extends DragTargetData> extends StatefulWidget {
426-
final Duration animationDuration;
427431
final FakeDragTargetEventTrigger eventTrigger;
428432
final FakeDragTargetEventData eventData;
429433
final DragTargetOnStarted onDragStarted;
@@ -442,7 +446,6 @@ class FakeDragTarget<T extends DragTargetData> extends StatefulWidget {
442446
required this.insertAnimationController,
443447
required this.deleteAnimationController,
444448
required this.child,
445-
this.animationDuration = const Duration(milliseconds: 250),
446449
}) : super(key: key);
447450

448451
@override
@@ -468,6 +471,7 @@ class _FakeDragTargetState<T extends DragTargetData>
468471
// });
469472

470473
widget.eventTrigger.fakeOnDragEnded(() {
474+
Log.trace("[$FakeDragTarget] on drag end");
471475
WidgetsBinding.instance.addPostFrameCallback((_) {
472476
widget.onDragEnded(widget.eventData.dragTargetData as T);
473477
});
@@ -476,6 +480,13 @@ class _FakeDragTargetState<T extends DragTargetData>
476480
super.initState();
477481
}
478482

483+
@override
484+
void dispose() {
485+
widget.insertAnimationController
486+
.removeStatusListener(_onInsertedAnimationStatusChanged);
487+
super.dispose();
488+
}
489+
479490
@override
480491
Widget build(BuildContext context) {
481492
if (simulateDragging) {
@@ -503,14 +514,18 @@ class _FakeDragTargetState<T extends DragTargetData>
503514
WidgetsBinding.instance.addPostFrameCallback((_) {
504515
if (!mounted) return;
505516
setState(() {
506-
simulateDragging = true;
507-
widget.deleteAnimationController.reverse(from: 1.0);
508-
widget.onWillAccept(widget.eventData.dragTargetData as T);
509-
widget.onDragStarted(
510-
widget.child,
511-
widget.eventData.index,
512-
widget.eventData.feedbackSize,
513-
);
517+
if (widget.onWillAccept(widget.eventData.dragTargetData as T)) {
518+
Log.trace("[$FakeDragTarget] on drag start");
519+
simulateDragging = true;
520+
widget.deleteAnimationController.reverse(from: 1.0);
521+
widget.onDragStarted(
522+
widget.child,
523+
widget.eventData.index,
524+
widget.eventData.feedbackSize,
525+
);
526+
} else {
527+
Log.trace("[$FakeDragTarget] cancel start drag");
528+
}
514529
});
515530
});
516531
}

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/reorder_flex/drag_target_interceptor.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class DragTargetInterceptor {
3535

3636
abstract class OverlapDragTargetDelegate {
3737
void cancel();
38-
void moveTo(
38+
void dragTargetDidMoveToReorderFlex(
3939
String reorderFlexId,
4040
FlexDragTargetData dragTargetData,
4141
int dragTargetIndex,
@@ -99,7 +99,8 @@ class OverlappingDragTargetInterceptor extends DragTargetInterceptor {
9999
if (index != -1) {
100100
Log.trace(
101101
'[$OverlappingDragTargetInterceptor] move to $dragTargetId at $index');
102-
delegate.moveTo(dragTargetId, dragTargetData, index);
102+
delegate.dragTargetDidMoveToReorderFlex(
103+
dragTargetId, dragTargetData, index);
103104

104105
columnsState
105106
.getReorderFlexState(groupId: dragTargetId)
@@ -153,7 +154,7 @@ class CrossReorderFlexDragTargetInterceptor extends DragTargetInterceptor {
153154
/// it means the dragTarget is dragging on the top of its own list.
154155
/// Otherwise, it means the dargTarget was moved to another list.
155156
Log.trace(
156-
"[$CrossReorderFlexDragTargetInterceptor] $reorderFlexId accept ${dragTargetData.reorderFlexId} ${reorderFlexId != dragTargetData.reorderFlexId}");
157+
"[$CrossReorderFlexDragTargetInterceptor] $reorderFlexId should accept ${dragTargetData.reorderFlexId} : ${reorderFlexId != dragTargetData.reorderFlexId}");
157158
return reorderFlexId != dragTargetData.reorderFlexId;
158159
} else {
159160
Log.trace(

0 commit comments

Comments
 (0)