Skip to content

Commit

Permalink
Add quick actions for pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Nov 29, 2023
1 parent 5c00727 commit 11954fb
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 75 deletions.
56 changes: 32 additions & 24 deletions app/lib/pages/pages_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class _PageTile extends HookConsumerWidget {
icon: FontAwesomeIcons.pen,
onTap: () => showDialog(
context: context,
builder: (_) => _RenamePageDialogue(old: pageId),
builder: (_) => RenamePageDialogue(old: pageId),
),
),
ContextMenuTile.button(
Expand All @@ -369,22 +369,7 @@ class _PageTile extends HookConsumerWidget {
title: "Delete",
icon: FontAwesomeIcons.trash,
color: Colors.redAccent,
onTap: () => showConfirmationDialogue(
context: context,
title: "Delete ${pageId.formatted}?",
content:
"This will delete the page and all its content.\nTHIS CANNOT BE UNDONE.",
delayConfirm: 3.seconds,
confirmText: "Delete",
confirmIcon: FontAwesomeIcons.trash,
onConfirm: () async {
await ref.read(bookProvider.notifier).deletePage(pageId);
if (!isSelected) return;
unawaited(
ref.read(appRouter).replace(const EmptyPageEditorRoute()),
);
},
),
onTap: () => showPageDeletionDialogue(context, ref.passing, pageId),
),
];
}
Expand Down Expand Up @@ -657,9 +642,10 @@ class AddPageDialogue extends HookConsumerWidget {
}
}

class _RenamePageDialogue extends HookConsumerWidget {
const _RenamePageDialogue({
class RenamePageDialogue extends HookConsumerWidget {
const RenamePageDialogue({
required this.old,
super.key,
});

final String old;
Expand Down Expand Up @@ -716,7 +702,7 @@ class _RenamePageDialogue extends HookConsumerWidget {
onSubmitted: (value) async {
final navigator = Navigator.of(context);
await _renamePage(ref, value);
navigator.pop();
navigator.pop(true);
},
),
actions: [
Expand All @@ -726,15 +712,15 @@ class _RenamePageDialogue extends HookConsumerWidget {
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).textTheme.bodySmall?.color,
),
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(context).pop(false),
),
FilledButton.icon(
onPressed: !isNameValid.value
? null
: () async {
final navigator = Navigator.of(context);
await _renamePage(ref, controller.text);
navigator.pop();
navigator.pop(true);
},
label: const Text("Rename"),
icon: const Icon(FontAwesomeIcons.pen),
Expand Down Expand Up @@ -766,7 +752,7 @@ class ChangeChapterDialogue extends HookConsumerWidget {

final navigator = Navigator.of(context);
await ref.read(pageProvider(pageId))?.changeChapter(ref, newName);
navigator.pop();
navigator.pop(true);
}

@override
Expand Down Expand Up @@ -808,7 +794,7 @@ class ChangeChapterDialogue extends HookConsumerWidget {
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).textTheme.bodySmall?.color,
),
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(context).pop(false),
),
FilledButton.icon(
onPressed: () async => _changeChapter(
Expand All @@ -825,3 +811,25 @@ class ChangeChapterDialogue extends HookConsumerWidget {
);
}
}

Future<bool> showPageDeletionDialogue(
BuildContext context,
PassingRef ref,
String pageId,
) {
return showConfirmationDialogue(
context: context,
title: "Delete ${pageId.formatted}?",
content:
"This will delete the page and all its content.\nTHIS CANNOT BE UNDONE.",
delayConfirm: 3.seconds,
confirmText: "Delete",
confirmIcon: FontAwesomeIcons.trash,
onConfirm: () async {
await ref.read(bookProvider.notifier).deletePage(pageId);
unawaited(
ref.read(appRouter).replace(const EmptyPageEditorRoute()),
);
},
);
}
44 changes: 23 additions & 21 deletions app/lib/utils/popups.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ConfirmationDialogue extends HookWidget {
icon: Icon(cancelIcon),
label: Text(cancelText),
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pop(false);
onCancel?.call();
},
style: TextButton.styleFrom(
Expand All @@ -90,7 +90,7 @@ class ConfirmationDialogue extends HookWidget {
color: confirmColor,
onPressed: canConfirm
? () {
Navigator.of(context).pop();
Navigator.of(context).pop(true);
onConfirm();
}
: null,
Expand All @@ -100,7 +100,7 @@ class ConfirmationDialogue extends HookWidget {
}
}

void showConfirmationDialogue({
Future<bool> showConfirmationDialogue({
required BuildContext context,
required Function onConfirm,
String title = "Are you sure?",
Expand All @@ -112,28 +112,30 @@ void showConfirmationDialogue({
String cancelText = "Cancel",
IconData cancelIcon = FontAwesomeIcons.xmark,
Function? onCancel,
}) {
}) async {
// If the user has its shift key pressed, we skip the confirmation dialogue.
// But only if the delay is 0.
final hasShiftDown = RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftLeft);
final hasShiftDown =
RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.shiftLeft);
if (hasShiftDown && delayConfirm.inSeconds == 0) {
onConfirm();
return;
return true;
}

showDialog(
context: context,
builder: (context) => ConfirmationDialogue(
onConfirm: onConfirm,
title: title,
content: content,
confirmText: confirmText,
confirmIcon: confirmIcon,
confirmColor: confirmColor,
delayConfirm: delayConfirm,
cancelText: cancelText,
cancelIcon: cancelIcon,
onCancel: onCancel,
),
);
return await showDialog<bool>(
context: context,
builder: (context) => ConfirmationDialogue(
onConfirm: onConfirm,
title: title,
content: content,
confirmText: confirmText,
confirmIcon: confirmIcon,
confirmColor: confirmColor,
delayConfirm: delayConfirm,
cancelText: cancelText,
cancelIcon: cancelIcon,
onCancel: onCancel,
),
) ??
false;
}
20 changes: 15 additions & 5 deletions app/lib/widgets/components/app/entry_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "package:typewriter/utils/passing_reference.dart";
import "package:typewriter/utils/smart_single_activator.dart";
import "package:typewriter/widgets/components/app/page_search.dart";
import "package:typewriter/widgets/components/app/search_bar.dart";
import "package:typewriter/widgets/components/general/toasts.dart";
import "package:typewriter/widgets/inspector/inspector.dart";

part "entry_search.g.dart";
Expand Down Expand Up @@ -202,8 +201,13 @@ class NewEntryFetcher extends SearchFetcher {
final results = fuzzy.search(search.query);

return results
.map((result) =>
AddEntrySearchElement(result.item, onAdd: onAdd, onAdded: onAdded))
.map(
(result) => AddEntrySearchElement(
result.item,
onAdd: onAdd,
onAdded: onAdded,
),
)
.toList();
}

Expand Down Expand Up @@ -320,7 +324,10 @@ class EntrySearchElement extends SearchElement {
"Open Wiki",
FontAwesomeIcons.book,
SmartSingleActivator(LogicalKeyboardKey.keyO, control: true),
onTrigger: (_, __) => blueprint.openWiki(),
onTrigger: (_, __) {
blueprint.openWiki();
return false;
},
),
];
}
Expand Down Expand Up @@ -371,7 +378,10 @@ class AddEntrySearchElement extends SearchElement {
"Open Wiki",
FontAwesomeIcons.book,
SmartSingleActivator(LogicalKeyboardKey.keyO, control: true),
onTrigger: (_, __) => blueprint.openWiki(),
onTrigger: (_, __) {
blueprint.openWiki();
return false;
},
),
];
}
Expand Down
43 changes: 41 additions & 2 deletions app/lib/widgets/components/app/page_search.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import "package:collection/collection.dart";
import "package:flutter/material.dart" hide Page;
import "package:flutter/services.dart";
import "package:font_awesome_flutter/font_awesome_flutter.dart";
import "package:fuzzy/fuzzy.dart";
import "package:riverpod_annotation/riverpod_annotation.dart";
import "package:typewriter/app_router.dart";
import "package:typewriter/models/page.dart";
import "package:typewriter/pages/pages_list.dart";
import "package:typewriter/utils/extensions.dart";
import "package:typewriter/utils/passing_reference.dart";
import "package:typewriter/utils/smart_single_activator.dart";
import "package:typewriter/widgets/components/app/search_bar.dart";

part "page_search.g.dart";
Expand Down Expand Up @@ -177,6 +179,39 @@ class PageSearchElement extends SearchElement {
Icons.open_in_new,
SingleActivator(LogicalKeyboardKey.enter),
),
SearchAction(
"Rename",
FontAwesomeIcons.pencil,
SmartSingleActivator(LogicalKeyboardKey.keyR, control: true),
onTrigger: (context, __) async =>
await showDialog<bool>(
context: context,
builder: (_) => RenamePageDialogue(old: page.pageName),
) ??
false,
),
SearchAction(
"Change Chapter",
FontAwesomeIcons.bookBookmark,
SmartSingleActivator(LogicalKeyboardKey.keyC, control: true),
onTrigger: (context, __) async =>
await showDialog<bool>(
context: context,
builder: (_) => ChangeChapterDialogue(
pageId: page.pageName,
chapter: page.chapter,
),
) ??
false,
),
SearchAction(
"Delete",
FontAwesomeIcons.trash,
SmartSingleActivator(LogicalKeyboardKey.backspace, control: true),
color: Colors.red,
onTrigger: (context, ref) =>
showPageDeletionDialogue(context, ref, page.pageName),
),
];
}

Expand All @@ -186,8 +221,12 @@ class PageSearchElement extends SearchElement {
return await onSelect?.call(page) ?? true;
}

await ref.read(appRouter).navigateToPage(ref, page.pageName);
return true;
final navigator = ref.read(appRouter);

ref.read(searchProvider.notifier).endSearch();

await navigator.navigateToPage(ref, page.pageName);
return false;
}
}

Expand Down
Loading

0 comments on commit 11954fb

Please sign in to comment.