Skip to content

Commit 3c5bd4a

Browse files
committed
improve tab reorder logic
1 parent d41d56c commit 3c5bd4a

File tree

9 files changed

+83
-12
lines changed

9 files changed

+83
-12
lines changed

app/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ targets:
1515
lexo_rank_next: "text (int, text null)"
1616
lexo_rank_previous: "text (int, text null)"
1717
lexo_rank_reorder_after: "text (text null, text null)"
18+
lexo_rank_reorder_before: "text (text null, text null)"
1819
modules:
1920
- json1
2021
- fts5

app/lib/data/database/functions/lexo_rank_functions.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ String _reorderAfter(List<Object?> args) {
4040
}
4141
}
4242

43+
String _reorderBefore(List<Object?> args) {
44+
final first = args[0].mapNotNull((arg) => LexoRank.parse(arg as String));
45+
final last = args[1].mapNotNull((arg) => LexoRank.parse(arg as String));
46+
47+
if (first == null) {
48+
throw Exception('Tab not found');
49+
} else if (last == null) {
50+
return first.genPrev().value;
51+
} else {
52+
return last.genBetween(first).value;
53+
}
54+
}
55+
4356
void registerLexorankFunctions(CommonDatabase database) {
4457
database.createFunction(
4558
functionName: 'lexo_rank_next',
@@ -56,4 +69,9 @@ void registerLexorankFunctions(CommonDatabase database) {
5669
argumentCount: const AllowedArgumentCount(2),
5770
function: _reorderAfter,
5871
);
72+
database.createFunction(
73+
functionName: 'lexo_rank_reorder_before',
74+
argumentCount: const AllowedArgumentCount(2),
75+
function: _reorderBefore,
76+
);
5977
}

app/lib/features/geckoview/features/browser/presentation/widgets/sheets/view_tabs.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ class ViewTabsSheetWidget extends HookConsumerWidget {
300300
Expanded(
301301
child: HookConsumer(
302302
builder: (context, ref, child) {
303+
final gridViewKey = useMemoized(() => GlobalKey());
304+
303305
final container = ref.watch(selectedContainerProvider);
304306

305307
final filteredTabEntities = ref.watch(
@@ -387,10 +389,11 @@ class ViewTabsSheetWidget extends HookConsumerWidget {
387389

388390
return Padding(
389391
padding: const EdgeInsets.symmetric(horizontal: 4.0),
390-
child: ReorderableBuilder(
392+
child: ReorderableBuilder.builder(
391393
//Rebuild when cross axis count changes
392394
key: ValueKey(crossAxisCount),
393395
scrollController: sheetScrollController,
396+
itemCount: tabs.length,
394397
onDragStarted: (index) {
395398
ref.read(willAcceptDropProvider.notifier).clear();
396399
},
@@ -423,19 +426,27 @@ class ViewTabsSheetWidget extends HookConsumerWidget {
423426
containerId,
424427
);
425428
} else {
426-
final orderAfterIndex = newIndex;
427-
key = await containerRepository.getOrderKeyAfterTab(
428-
filteredTabEntities.value[orderAfterIndex].tabId,
429-
containerId,
430-
);
429+
if (newIndex < oldIndex) {
430+
key = await containerRepository.getOrderKeyAfterTab(
431+
filteredTabEntities.value[newIndex - 1].tabId,
432+
containerId,
433+
);
434+
} else {
435+
key = await containerRepository
436+
.getOrderKeyBeforeTab(
437+
filteredTabEntities.value[newIndex + 1].tabId,
438+
containerId,
439+
);
440+
}
431441
}
432442

433443
await ref
434444
.read(tabDataRepositoryProvider.notifier)
435445
.assignOrderKey(tabId, key);
436446
},
437-
builder: (children) {
447+
childBuilder: (itemBuilder) {
438448
return GridView.builder(
449+
key: gridViewKey,
439450
controller: sheetScrollController,
440451
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
441452
//Sync values for itemHeight calculation _calculateItemHeight
@@ -444,11 +455,11 @@ class ViewTabsSheetWidget extends HookConsumerWidget {
444455
crossAxisSpacing: 8.0,
445456
crossAxisCount: crossAxisCount,
446457
),
447-
itemCount: children.length,
448-
itemBuilder: (context, index) => children[index],
458+
itemCount: tabs.length,
459+
itemBuilder: (context, index) =>
460+
itemBuilder(tabs[index], index),
449461
);
450462
},
451-
children: tabs,
452463
),
453464
);
454465
},

app/lib/features/geckoview/features/tabs/data/database/daos/container.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,11 @@ class ContainerDao extends DatabaseAccessor<TabDatabase>
9696
) {
9797
return db.orderKeyAfterTab(containerId: containerId, tabId: tabId);
9898
}
99+
100+
SingleSelectable<String> generateOrderKeyBeforeTabId(
101+
String? containerId,
102+
String tabId,
103+
) {
104+
return db.orderKeyBeforeTab(containerId: containerId, tabId: tabId);
105+
}
99106
}

app/lib/features/geckoview/features/tabs/data/database/database.drift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ orderKeyAfterTab(:tab_id AS TEXT, :container_id AS TEXT OR NULL):
118118
FROM ordered_table
119119
WHERE id = :tab_id;
120120

121+
orderKeyBeforeTab(:tab_id AS TEXT, :container_id AS TEXT OR NULL):
122+
WITH ordered_table AS (
123+
SELECT id,
124+
order_key,
125+
LAG(order_key) OVER (ORDER BY order_key) AS prev_order_key
126+
FROM tab
127+
WHERE container_id IS :container_id
128+
)
129+
SELECT lexo_rank_reorder_before(order_key, prev_order_key)
130+
FROM ordered_table
131+
WHERE id = :tab_id;
132+
121133
queryTabsBasic WITH TabQueryResult:
122134
WITH weights AS (
123135
SELECT

app/lib/features/geckoview/features/tabs/data/database/database.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/lib/features/geckoview/features/tabs/domain/repositories/container.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ class ContainerRepository extends _$ContainerRepository {
8383
.getSingle();
8484
}
8585

86+
Future<String> getOrderKeyBeforeTab(String tabId, String? containerId) {
87+
return ref
88+
.read(tabDatabaseProvider)
89+
.containerDao
90+
.generateOrderKeyBeforeTabId(containerId, tabId)
91+
.getSingle();
92+
}
93+
8694
Future<Color> unusedRandomContainerColor() async {
8795
final allColors = colorTypes.flattened.toList();
8896
final usedColors = await getDistinctColors();

app/lib/features/geckoview/features/tabs/domain/repositories/container.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ dependencies:
2424
flutter_material_design_icons: ^1.1.7447
2525
flutter_mozilla_components:
2626
path: ../packages/flutter_mozilla_components
27-
flutter_reorderable_grid_view: ^5.5.0
27+
flutter_reorderable_grid_view:
28+
git:
29+
url: https://github.com/FaFre/flutter-reorderable-grid-view.git
30+
ref: fix_builder_assert
2831
flutter_secure_storage: ^10.0.0-beta.4
2932
flutter_slidable: ^4.0.0
3033
flutter_svg: ^2.2.0

0 commit comments

Comments
 (0)