MO-Fuel-Tax-Back/lib/widgets/backup_reminder.dart

153 lines
6.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../screens/data_settings_screen.dart';
import '../screens/faq_screen.dart';
import '../services/onboarding_keys.dart';
import 'onboarding_tour_overlay.dart';
const _backedUpFaqQuestion = 'Is my data backed up?';
/// Shown right after a fuel entry is saved (see
/// `ConfirmFuelEntryScreen._save`) when no cloud backup is configured yet —
/// nudges the user toward Settings > Data before they forget and eventually
/// lose everything to a lost, damaged, or replaced device. Returns true if
/// they chose to set one up now.
Future<bool> showBackupReminderDialog(BuildContext context) async {
final wantsSetup = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text("Your Data Isn't Backed Up"),
content: const Text(
"Until you enable a backup method, everything you log — including this "
"receipt — only lives on this device. If it's lost, damaged, or "
"replaced, that data could be lost with it.\n\n"
'Would you like to set up a backup method now?',
),
actions: [
TextButton(
onPressed: () {
// Resolves as "not now" (they didn't choose to set backup up
// directly), but still takes them somewhere useful about it.
Navigator.of(context).pop(false);
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const FaqScreen(initiallyExpandedQuestion: _backedUpFaqQuestion),
),
);
},
child: const Text('Learn More'),
),
TextButton(onPressed: () => Navigator.of(context).pop(false), child: const Text('Not Now')),
FilledButton(onPressed: () => Navigator.of(context).pop(true), child: const Text('Set Up Backup')),
],
),
);
return wantsSetup ?? false;
}
/// Pushes Settings > Data with its cloud storage card spotlighted — the
/// same visual treatment the first-launch tour's "Back Up to the Cloud"
/// step uses (see [OnboardingTourOverlay] / `MainShell`), but as a one-off
/// standalone overlay rather than part of a multi-step tour.
Future<void> openCloudBackupSetup(BuildContext context) {
return Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const _SpotlightedDataSettingsScreen()),
);
}
class _SpotlightedDataSettingsScreen extends StatefulWidget {
const _SpotlightedDataSettingsScreen();
@override
State<_SpotlightedDataSettingsScreen> createState() => _SpotlightedDataSettingsScreenState();
}
class _SpotlightedDataSettingsScreenState extends State<_SpotlightedDataSettingsScreen> {
OverlayEntry? _entry;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _showSpotlight());
}
@override
void dispose() {
_entry?.remove();
super.dispose();
}
/// Mirrors `MainShell._waitForTargets`/`_showTourStep` for this one
/// target — duplicated rather than shared because that logic is
/// entangled with the tour's multi-step sequencing, which doesn't apply
/// to this standalone spotlight.
Future<void> _showSpotlight() async {
final key = OnboardingKeys.cloudStorageCard;
for (var attempt = 0; attempt < 30; attempt++) {
if (!mounted) return;
final renderObject = key.currentContext?.findRenderObject();
if (renderObject is RenderBox && renderObject.hasSize) break;
await Future.delayed(const Duration(milliseconds: 16));
}
if (!mounted) return;
key.currentContext?.findRenderObject()?.showOnScreen(duration: Duration.zero);
for (var i = 0; i < 5; i++) {
if (!mounted) return;
await Future.delayed(const Duration(milliseconds: 16));
}
if (!mounted) return;
// This screen was reached via Navigator.push (see openCloudBackupSetup
// below) — wait for that push transition to fully settle before
// capturing the card's position, or the spotlight ends up shifted by
// however far the slide-in hadn't yet finished. ModalRoute.of(context)
// correctly resolves to that pushed route here, since this State's
// context lives inside it.
await waitForRouteTransition(ModalRoute.of(context)?.animation);
if (!mounted) return;
// The route's own AnimationController reports AnimationStatus.completed
// at this point, but empirically the render tree's transforms (from
// FadeForwardsPageTransitionsBuilder's SlideTransition, the actual
// Android default as of Flutter 3.44) still reflect a mid-transition
// position for a few more frames after that — confirmed by walking the
// RenderObject ancestor chain and finding an active
// RenderFractionalTranslation still present, with a position matching
// the transition's *starting* offset rather than its resting
// Offset.zero. These extra frames give it time to actually settle
// before the spotlight measures anything. SchedulerBinding.endOfFrame
// (rather than a bare Future.delayed) so this genuinely waits on real
// frames — in a widget test, tester.pumpAndSettle() only keeps pumping
// while something has an actual frame scheduled, which a bare delay
// timer doesn't count as once the route's own transition has already
// finished.
for (var i = 0; i < 10; i++) {
if (!mounted) return;
await SchedulerBinding.instance.endOfFrame;
}
if (!mounted) return;
_entry = OverlayEntry(
builder: (_) => OnboardingTourOverlay(
targetKeys: [key],
title: 'Back Up to the Cloud',
description: 'Connect a cloud storage account here to back up every vehicle, receipt, '
"and photo you log — so nothing is lost if this device is lost, damaged, or "
'replaced.',
stepNumber: 1,
totalSteps: 1,
skipLabel: 'Close',
onNext: _dismiss,
onSkip: _dismiss,
),
);
Overlay.of(context, rootOverlay: true).insert(_entry!);
}
void _dismiss() {
_entry?.remove();
_entry = null;
}
@override
Widget build(BuildContext context) => const DataSettingsScreen();
}