mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 01:50:30 +00:00
Merge mobile back navigation and exit confirmation into beta
This commit is contained in:
commit
d330d8bea8
7 files changed
+307
-66
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
@@ -15,6 +16,7 @@ import '../../features/tasks/domain/task_record.dart';
|
||||
import '../../features/tasks/presentation/pages/task_details_page.dart';
|
||||
import '../../features/tasks/presentation/pages/task_files_page.dart';
|
||||
import 'shells/main_shell.dart';
|
||||
import 'mobile_exit_guard.dart';
|
||||
|
||||
class AppRouter {
|
||||
static final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>();
|
||||
@@ -27,6 +29,34 @@ class AppRouter {
|
||||
}
|
||||
|
||||
static GoRouter build(WebAuthController webAuthController) {
|
||||
// Keep desktop and web sections as peers; native mobile retains home below them.
|
||||
final isMobile =
|
||||
!kIsWeb && (defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS);
|
||||
final sectionRoutes = [
|
||||
GoRoute(
|
||||
path: isMobile ? 'extensions' : '/extensions',
|
||||
builder: (context, state) => const ExtensionsPage(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ':id',
|
||||
builder: (context, state) => ExtensionDetailsPage(
|
||||
extensionId: state.pathParameters['id'] ?? '',
|
||||
initialItem: state.extra is ExtensionListItem ? state.extra as ExtensionListItem : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: isMobile ? 'settings' : '/settings',
|
||||
builder: (context, state) => const SettingsPage(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ':section',
|
||||
builder: (context, state) => SettingsPage(sectionKey: state.pathParameters['section']),
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
return GoRouter(
|
||||
navigatorKey: rootNavigatorKey,
|
||||
refreshListenable: webAuthController,
|
||||
@@ -48,7 +78,7 @@ class AppRouter {
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
builder: (context, state) => const HomePage(),
|
||||
builder: (context, state) => const MobileExitGuard(child: HomePage()),
|
||||
routes: [
|
||||
GoRoute(path: 'create', builder: (context, state) => const CreateTaskWindowPage()),
|
||||
GoRoute(
|
||||
@@ -67,31 +97,10 @@ class AppRouter {
|
||||
),
|
||||
],
|
||||
),
|
||||
if (isMobile) ...sectionRoutes,
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: '/extensions',
|
||||
builder: (context, state) => const ExtensionsPage(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ':id',
|
||||
builder: (context, state) => ExtensionDetailsPage(
|
||||
extensionId: state.pathParameters['id'] ?? '',
|
||||
initialItem: state.extra is ExtensionListItem ? state.extra as ExtensionListItem : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings',
|
||||
builder: (context, state) => const SettingsPage(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ':section',
|
||||
builder: (context, state) => SettingsPage(sectionKey: state.pathParameters['section']),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (!isMobile) ...sectionRoutes,
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../shared/navigation/app_exit_confirmation_controller.dart';
|
||||
|
||||
/// Confirms app exit only on the home route; child routes pop normally.
|
||||
class MobileExitGuard extends StatefulWidget {
|
||||
const MobileExitGuard({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<MobileExitGuard> createState() => _MobileExitGuardState();
|
||||
}
|
||||
|
||||
class _MobileExitGuardState extends State<MobileExitGuard> with WidgetsBindingObserver {
|
||||
final _controller = AppExitConfirmationController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state != AppLifecycleState.resumed) _controller.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) return widget.child;
|
||||
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop) unawaited(_controller.handleBack(context));
|
||||
},
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,6 @@ import '../../../features/tasks/application/tasks_controller.dart';
|
||||
import '../../../features/tasks/presentation/widgets/pending_update_dialog.dart';
|
||||
import '../../../l10n/l10n.dart';
|
||||
import '../../../core/window/app_window_launcher.dart';
|
||||
import '../../../core/utils/breakpoints.dart';
|
||||
import '../../../shared/navigation/app_exit_confirmation_controller.dart';
|
||||
import '../../../shared/widgets/app_toast.dart';
|
||||
|
||||
class MainShell extends ConsumerStatefulWidget {
|
||||
@@ -34,7 +32,6 @@ class MainShell extends ConsumerStatefulWidget {
|
||||
class _MainShellState extends ConsumerState<MainShell> {
|
||||
String? _shownUpdateVersion;
|
||||
bool _handlingPendingUpdateRequest = false;
|
||||
final _exitConfirmationController = AppExitConfirmationController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -82,23 +79,7 @@ class _MainShellState extends ConsumerState<MainShell> {
|
||||
return widget.child;
|
||||
},
|
||||
);
|
||||
return _buildMobileBackGuard(context, content);
|
||||
}
|
||||
|
||||
Widget _buildMobileBackGuard(BuildContext context, Widget child) {
|
||||
final isAndroidMobile =
|
||||
!kIsWeb &&
|
||||
defaultTargetPlatform == TargetPlatform.android &&
|
||||
MediaQuery.sizeOf(context).width < Breakpoints.mobile;
|
||||
if (!isAndroidMobile || context.canPop()) return child;
|
||||
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop) unawaited(_exitConfirmationController.handleBack(context));
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
return content;
|
||||
}
|
||||
|
||||
void _schedulePendingUpdateRequest(PendingUpdateRequest request) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
@@ -7,18 +9,37 @@ import '../widgets/app_toast.dart';
|
||||
class AppExitConfirmationController {
|
||||
static const confirmationWindow = Duration(seconds: 2);
|
||||
|
||||
DateTime? _lastBackAt;
|
||||
OverlayEntry? _toast;
|
||||
Timer? _timer;
|
||||
|
||||
Future<void> handleBack(BuildContext context) async {
|
||||
final now = DateTime.now();
|
||||
final lastBackAt = _lastBackAt;
|
||||
if (lastBackAt != null && now.difference(lastBackAt) <= confirmationWindow) {
|
||||
_lastBackAt = null;
|
||||
if (_toast != null) {
|
||||
dispose();
|
||||
await SystemNavigator.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
_lastBackAt = now;
|
||||
showAppToast(context, context.l10n.pressBackAgainToExit);
|
||||
final message = context.l10n.pressBackAgainToExit;
|
||||
final themes = InheritedTheme.capture(from: context, to: Overlay.of(context, rootOverlay: true).context);
|
||||
_toast = OverlayEntry(
|
||||
builder: (_) => IgnorePointer(
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: themes.wrap(AppToastContent(message: message)),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
Overlay.of(context, rootOverlay: true).insert(_toast!);
|
||||
_timer = Timer(confirmationWindow, dispose);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_toast?.remove();
|
||||
_toast?.dispose();
|
||||
_toast = null;
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,6 @@ enum AppToastType { info, success, error }
|
||||
/// short messages stay compact while long messages wrap before becoming wider
|
||||
/// than the configured maximum. Errors and normal notices use the same layout.
|
||||
void showAppToast(BuildContext context, String message, {AppToastType type = AppToastType.info}) {
|
||||
final palette = AppPalette.of(context);
|
||||
final borderColor = switch (type) {
|
||||
AppToastType.error => palette.error.withValues(alpha: 0.55),
|
||||
AppToastType.success || AppToastType.info => palette.border,
|
||||
};
|
||||
shad.showToast(
|
||||
context: context,
|
||||
location: shad.ToastLocation.topCenter,
|
||||
@@ -23,16 +18,34 @@ void showAppToast(BuildContext context, String message, {AppToastType type = App
|
||||
alignment: Alignment.topCenter,
|
||||
widthFactor: 1,
|
||||
heightFactor: 1,
|
||||
child: Container(
|
||||
key: const ValueKey('app-toast-content'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: palette.cardBg,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: borderColor),
|
||||
),
|
||||
child: Text(message, style: TextStyle(color: palette.textPrimary, fontSize: 13, height: 1.2)),
|
||||
),
|
||||
child: AppToastContent(message: message, type: type),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Shared toast surface for edge notifications and centered exit prompts.
|
||||
class AppToastContent extends StatelessWidget {
|
||||
const AppToastContent({super.key, required this.message, this.type = AppToastType.info});
|
||||
|
||||
final String message;
|
||||
final AppToastType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
final borderColor = switch (type) {
|
||||
AppToastType.error => palette.error.withValues(alpha: 0.55),
|
||||
AppToastType.success || AppToastType.info => palette.border,
|
||||
};
|
||||
return Container(
|
||||
key: const ValueKey('app-toast-content'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: palette.cardBg,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: borderColor),
|
||||
),
|
||||
child: Text(message, style: TextStyle(color: palette.textPrimary, fontSize: 13, height: 1.2)),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user