From 38fb4f3763742c609a895af9ee9bf9d1f0db5dfc Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Mon, 16 Nov 2026 08:09:48 +0100 Subject: [PATCH] Add motion tokens and haptics to the phone app Motion tokens hand-copied from DOCS-motion.md into theme.dart; every page route goes through appRoute() (slow, decelerate in, accelerate out, instant under Remove animations). mediumImpact on the picker confirm, heavyImpact on a refused time. The Clock app line shows only when the SET_ALARM handoff finished before its 3 s timeout. Sound is skipped on purpose. Co-Authored-By: Claude Opus 5.5 --- phone_app/lib/screens/home_screen.dart | 17 +++++-- phone_app/lib/screens/settings_screen.dart | 14 +++--- phone_app/lib/services/alarm_arming.dart | 11 ++++- phone_app/lib/services/alarm_publisher.dart | 13 +++++- phone_app/lib/ui/cues.dart | 5 +- phone_app/lib/ui/motion_route.dart | 27 +++++++++++ phone_app/lib/ui/theme.dart | 33 +++++++++++++ .../test/services/alarm_publisher_test.dart | 8 ++++ phone_app/test/ui/motion_test.dart | 46 +++++++++++++++++++ 9 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 phone_app/lib/ui/motion_route.dart create mode 100644 phone_app/test/ui/motion_test.dart diff --git a/phone_app/lib/screens/home_screen.dart b/phone_app/lib/screens/home_screen.dart index d2ae6cb..5516995 100644 --- a/phone_app/lib/screens/home_screen.dart +++ b/phone_app/lib/screens/home_screen.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:developer' as developer; +import 'package:flutter/services.dart'; import 'package:material_ui/material_ui.dart'; import 'package:wake_alarm_sync/screens/free_days_screen.dart'; @@ -14,6 +15,7 @@ import 'package:wake_alarm_sync/services/alarm_sync_service.dart'; import 'package:wake_alarm_sync/services/day_pool_refresh.dart'; import 'package:wake_alarm_sync/services/firebase_backend.dart'; import 'package:wake_alarm_sync/services/sync_settings.dart'; +import 'package:wake_alarm_sync/ui/motion_route.dart'; /// Main screen: displays the current alarm time and syncs it to the PC via /// the shared crdt_sync transport. @@ -117,9 +119,12 @@ class _HomeScreenState extends State { helpText: '', ); if (picked == null) return; + // The tap that sets the alarm: felt now, before any await or intent. + unawaited(HapticFeedback.mediumImpact()); final accepted = await AlarmPolicy.setEarlier(picked.hour, picked.minute); if (!mounted) return; if (!accepted) { + unawaited(HapticFeedback.heavyImpact()); setState( () => _status = 'That is not earlier than $current. The alarm can only be ' @@ -134,6 +139,9 @@ class _HomeScreenState extends State { // publish attempt was the one at startup, so an alarm moved earlier here // never reached the PC at all. await _syncToPc(); + if (!mounted) return; + // Keep the armed time in the confirmation; the sync result follows it. + setState(() => _status = 'Armed for ${_fmt(_hour, _minute)}. $_status'); } /// Re-reads the policy and arms the alarm to match it. @@ -180,7 +188,7 @@ class _HomeScreenState extends State { ), onPressed: () => Navigator.push( context, - MaterialPageRoute(builder: (_) => const FreeDaysScreen()), + appRoute(context, (_) => const FreeDaysScreen()), ), ), IconButton( @@ -191,7 +199,7 @@ class _HomeScreenState extends State { ), onPressed: () => Navigator.push( context, - MaterialPageRoute(builder: (_) => const SickDaysScreen()), + appRoute(context, (_) => const SickDaysScreen()), ), ), IconButton( @@ -204,8 +212,9 @@ class _HomeScreenState extends State { onPressed: () async { await Navigator.push( context, - MaterialPageRoute( - builder: (_) => SettingsScreen(alarmService: _service), + appRoute( + context, + (_) => SettingsScreen(alarmService: _service), ), ); // Publish too, not just reload: a tag registered in Settings diff --git a/phone_app/lib/screens/settings_screen.dart b/phone_app/lib/screens/settings_screen.dart index 65e3f1c..76d303b 100644 --- a/phone_app/lib/screens/settings_screen.dart +++ b/phone_app/lib/screens/settings_screen.dart @@ -14,6 +14,7 @@ import 'package:wake_alarm_sync/services/alarm_sync_service.dart'; import 'package:wake_alarm_sync/services/data_transfer.dart'; import 'package:wake_alarm_sync/services/firebase_backend.dart'; import 'package:wake_alarm_sync/services/google_sign_in_backend.dart'; +import 'package:wake_alarm_sync/ui/motion_route.dart'; import 'package:wake_alarm_sync/ui/theme.dart'; /// Settings screen: links to the two sync surfaces. @@ -121,8 +122,9 @@ class SettingsScreen extends StatelessWidget { ), trailing: const Icon(Icons.chevron_right), onTap: () => Navigator.of(context).push( - MaterialPageRoute( - builder: (_) => SyncSettingsScreen( + appRoute( + context, + (_) => SyncSettingsScreen( accountLoader: loadAccount, accountSaver: saveAccount, accountClearer: clearAccount, @@ -140,7 +142,7 @@ class SettingsScreen extends StatelessWidget { leading: const Icon(Icons.nfc, semanticLabel: 'Room proof'), trailing: const Icon(Icons.chevron_right), onTap: () => Navigator.of(context).push( - MaterialPageRoute(builder: (_) => const RoomProofScreen()), + appRoute(context, (_) => const RoomProofScreen()), ), ), ListTile( @@ -152,7 +154,7 @@ class SettingsScreen extends StatelessWidget { ), trailing: const Icon(Icons.chevron_right), onTap: () => Navigator.of(context).push( - MaterialPageRoute(builder: (_) => const SessionTagsScreen()), + appRoute(context, (_) => const SessionTagsScreen()), ), ), ListTile( @@ -164,7 +166,7 @@ class SettingsScreen extends StatelessWidget { ), trailing: const Icon(Icons.chevron_right), onTap: () => Navigator.of(context).push( - MaterialPageRoute(builder: (_) => const HomeLocationScreen()), + appRoute(context, (_) => const HomeLocationScreen()), ), ), ListTile( @@ -187,7 +189,7 @@ class SettingsScreen extends StatelessWidget { ], ), onTap: () => Navigator.of(context).push( - MaterialPageRoute(builder: (_) => const GitHubMirrorScreen()), + appRoute(context, (_) => const GitHubMirrorScreen()), ), ), ], diff --git a/phone_app/lib/services/alarm_arming.dart b/phone_app/lib/services/alarm_arming.dart index 9d5ec65..9dcd570 100644 --- a/phone_app/lib/services/alarm_arming.dart +++ b/phone_app/lib/services/alarm_arming.dart @@ -37,7 +37,9 @@ class AlarmArming { /// /// android_intent_plus v5 uses startActivityForResult and the Clock app /// never sends a result, so this times out after 3s rather than hang. - Future registerWithClockApp(int hour, int minute) async { + /// + /// Returns true only if the launch completed before the timeout. + Future registerWithClockApp(int hour, int minute) async { final intent = AndroidIntent( action: 'android.intent.action.SET_ALARM', arguments: { @@ -47,6 +49,11 @@ class AlarmArming { 'android.intent.extra.alarm.MESSAGE': 'Wake Alarm', }, ); - await intent.launch().timeout(const Duration(seconds: 3), onTimeout: () {}); + var completed = true; + await intent.launch().timeout( + const Duration(seconds: 3), + onTimeout: () => completed = false, + ); + return completed; } } diff --git a/phone_app/lib/services/alarm_publisher.dart b/phone_app/lib/services/alarm_publisher.dart index e29579e..096e2dd 100644 --- a/phone_app/lib/services/alarm_publisher.dart +++ b/phone_app/lib/services/alarm_publisher.dart @@ -53,10 +53,19 @@ class AlarmPublisher { await applySyncedSettings(service); final problem = await arming.arm(hour, minute); if (problem != null) return problem; - await arming.registerWithClockApp(hour, minute); - return 'Synced to PC ✓'; + final inClock = await arming.registerWithClockApp(hour, minute); + return syncedStatus(hour, minute, inClock: inClock); } on Exception catch (e) { return 'Error: $e'; } } } + +/// The success line; names the Clock app only when the handoff to it +/// completed before its timeout, so a silent timeout never claims it. +String syncedStatus(int hour, int minute, {required bool inClock}) { + final time = + '${hour.toString().padLeft(2, '0')}:' + '${minute.toString().padLeft(2, '0')}'; + return inClock ? 'Synced to PC ✓ Clock app set $time ✓' : 'Synced to PC ✓'; +} diff --git a/phone_app/lib/ui/cues.dart b/phone_app/lib/ui/cues.dart index 37195ff..981d4c0 100644 --- a/phone_app/lib/ui/cues.dart +++ b/phone_app/lib/ui/cues.dart @@ -7,7 +7,8 @@ import 'dart:math' show pi, sin; import 'package:flutter/services.dart'; import 'package:material_ui/material_ui.dart'; -import 'package:wake_alarm_sync/ui/theme.dart' show AppStatusColors; +import 'package:wake_alarm_sync/ui/theme.dart' + show AppStatusColors, MotionDuration; export 'package:wake_alarm_sync/ui/spoken_label.dart'; @@ -127,7 +128,7 @@ class _StatusCueState extends State void initState() { super.initState(); _shakeCtrl = AnimationController( - duration: const Duration(milliseconds: 300), + duration: MotionDuration.slow, vsync: this, ); _shakeAnim = Tween( diff --git a/phone_app/lib/ui/motion_route.dart b/phone_app/lib/ui/motion_route.dart new file mode 100644 index 0000000..a9e89b1 --- /dev/null +++ b/phone_app/lib/ui/motion_route.dart @@ -0,0 +1,27 @@ +/// The app's one page route: slow slide, decelerate in, accelerate out. +library; + +import 'package:material_ui/material_ui.dart'; +import 'package:wake_alarm_sync/ui/theme.dart'; + +/// A route to [builder]'s page using the shared motion tokens; instant under +/// the OS "Remove animations" setting. +PageRoute appRoute(BuildContext context, WidgetBuilder builder) { + final duration = motionDuration(context, MotionDuration.slow); + return PageRouteBuilder( + transitionDuration: duration, + reverseTransitionDuration: duration, + pageBuilder: (context, _, _) => builder(context), + transitionsBuilder: (context, animation, secondary, child) { + final slide = Tween(begin: const Offset(1, 0), end: Offset.zero) + .animate( + CurvedAnimation( + parent: animation, + curve: MotionCurve.decelerate, + reverseCurve: MotionCurve.accelerate, + ), + ); + return SlideTransition(position: slide, child: child); + }, + ); +} diff --git a/phone_app/lib/ui/theme.dart b/phone_app/lib/ui/theme.dart index 24b018e..f433746 100644 --- a/phone_app/lib/ui/theme.dart +++ b/phone_app/lib/ui/theme.dart @@ -121,3 +121,36 @@ class DayMarkColors extends ThemeExtension { ); } } + +/// Motion durations, hand-copied from the shared `DOCS-motion.md`. +abstract final class MotionDuration { + /// No animation; the reduced-motion target. + static const Duration instant = Duration.zero; + + /// State on an element already under the finger. + static const fast = Duration(milliseconds: 120); + + /// Default: anything entering or leaving within the current view. + static const base = Duration(milliseconds: 200); + + /// Full-surface change: page transitions. The ceiling. + static const slow = Duration(milliseconds: 320); +} + +/// Motion curves, hand-copied from the shared `DOCS-motion.md`. +abstract final class MotionCurve { + /// Default start-and-end-on-screen curve. + static const standard = Cubic(0.2, 0, 0, 1); + + /// Entering the screen. + static const decelerate = Cubic(0, 0, 0, 1); + + /// Leaving the screen. + static const accelerate = Cubic(0.3, 0, 1, 1); +} + +/// [token], or zero when the OS "Remove animations" setting is on. +Duration motionDuration(BuildContext context, Duration token) => + (MediaQuery.maybeDisableAnimationsOf(context) ?? false) + ? MotionDuration.instant + : token; diff --git a/phone_app/test/services/alarm_publisher_test.dart b/phone_app/test/services/alarm_publisher_test.dart index 371ce98..7fe72ce 100644 --- a/phone_app/test/services/alarm_publisher_test.dart +++ b/phone_app/test/services/alarm_publisher_test.dart @@ -38,4 +38,12 @@ void main() { expect(message, isNot(contains('Connect a sync backend'))); expect(message, contains('Sync store unavailable')); }); + + test('the Clock line names the time only when the handoff completed', () { + expect( + syncedStatus(6, 5, inClock: true), + 'Synced to PC ✓ Clock app set 06:05 ✓', + ); + expect(syncedStatus(6, 5, inClock: false), 'Synced to PC ✓'); + }); } diff --git a/phone_app/test/ui/motion_test.dart b/phone_app/test/ui/motion_test.dart new file mode 100644 index 0000000..505806e --- /dev/null +++ b/phone_app/test/ui/motion_test.dart @@ -0,0 +1,46 @@ +/// Tests for the motion tokens and the app route. +library; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:wake_alarm_sync/ui/motion_route.dart'; +import 'package:wake_alarm_sync/ui/theme.dart'; + +Future _routeDuration(WidgetTester t, {required bool off}) async { + late Duration d; + await t.pumpWidget( + MediaQuery( + data: MediaQueryData(disableAnimations: off), + child: Builder( + builder: (context) { + d = appRoute( + context, + (_) => const SizedBox(), + ).transitionDuration; + return const SizedBox(); + }, + ), + ), + ); + return d; +} + +void main() { + test('duration tokens match DOCS-motion', () { + expect(MotionDuration.instant, Duration.zero); + expect(MotionDuration.fast, const Duration(milliseconds: 120)); + expect(MotionDuration.base, const Duration(milliseconds: 200)); + expect(MotionDuration.slow, const Duration(milliseconds: 320)); + }); + + test('curves match DOCS-motion', () { + expect(MotionCurve.decelerate, const Cubic(0, 0, 0, 1)); + expect(MotionCurve.accelerate, const Cubic(0.3, 0, 1, 1)); + expect(MotionCurve.standard, const Cubic(0.2, 0, 0, 1)); + }); + + testWidgets('route is slow, instant under disableAnimations', (t) async { + expect(await _routeDuration(t, off: false), MotionDuration.slow); + expect(await _routeDuration(t, off: true), Duration.zero); + }); +}