mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-20 23:12:21 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
parent
78c41bd167
commit
38fb4f3763
@ -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<HomeScreen> {
|
||||
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<HomeScreen> {
|
||||
// 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<HomeScreen> {
|
||||
),
|
||||
onPressed: () => Navigator.push<void>(
|
||||
context,
|
||||
MaterialPageRoute<void>(builder: (_) => const FreeDaysScreen()),
|
||||
appRoute<void>(context, (_) => const FreeDaysScreen()),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
@ -191,7 +199,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
onPressed: () => Navigator.push<void>(
|
||||
context,
|
||||
MaterialPageRoute<void>(builder: (_) => const SickDaysScreen()),
|
||||
appRoute<void>(context, (_) => const SickDaysScreen()),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
@ -204,8 +212,9 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
onPressed: () async {
|
||||
await Navigator.push<void>(
|
||||
context,
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => SettingsScreen(alarmService: _service),
|
||||
appRoute<void>(
|
||||
context,
|
||||
(_) => SettingsScreen(alarmService: _service),
|
||||
),
|
||||
);
|
||||
// Publish too, not just reload: a tag registered in Settings
|
||||
|
||||
@ -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<void>(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => SyncSettingsScreen(
|
||||
appRoute<void>(
|
||||
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<void>(
|
||||
MaterialPageRoute(builder: (_) => const RoomProofScreen()),
|
||||
appRoute<void>(context, (_) => const RoomProofScreen()),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
@ -152,7 +154,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => Navigator.of(context).push<void>(
|
||||
MaterialPageRoute(builder: (_) => const SessionTagsScreen()),
|
||||
appRoute<void>(context, (_) => const SessionTagsScreen()),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
@ -164,7 +166,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => Navigator.of(context).push<void>(
|
||||
MaterialPageRoute(builder: (_) => const HomeLocationScreen()),
|
||||
appRoute<void>(context, (_) => const HomeLocationScreen()),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
@ -187,7 +189,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
onTap: () => Navigator.of(context).push<void>(
|
||||
MaterialPageRoute(builder: (_) => const GitHubMirrorScreen()),
|
||||
appRoute<void>(context, (_) => const GitHubMirrorScreen()),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@ -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<void> registerWithClockApp(int hour, int minute) async {
|
||||
///
|
||||
/// Returns true only if the launch completed before the timeout.
|
||||
Future<bool> registerWithClockApp(int hour, int minute) async {
|
||||
final intent = AndroidIntent(
|
||||
action: 'android.intent.action.SET_ALARM',
|
||||
arguments: <String, Object>{
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 ✓';
|
||||
}
|
||||
|
||||
@ -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<StatusCue>
|
||||
void initState() {
|
||||
super.initState();
|
||||
_shakeCtrl = AnimationController(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
duration: MotionDuration.slow,
|
||||
vsync: this,
|
||||
);
|
||||
_shakeAnim = Tween<double>(
|
||||
|
||||
27
phone_app/lib/ui/motion_route.dart
Normal file
27
phone_app/lib/ui/motion_route.dart
Normal file
@ -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<T> appRoute<T>(BuildContext context, WidgetBuilder builder) {
|
||||
final duration = motionDuration(context, MotionDuration.slow);
|
||||
return PageRouteBuilder<T>(
|
||||
transitionDuration: duration,
|
||||
reverseTransitionDuration: duration,
|
||||
pageBuilder: (context, _, _) => builder(context),
|
||||
transitionsBuilder: (context, animation, secondary, child) {
|
||||
final slide = Tween<Offset>(begin: const Offset(1, 0), end: Offset.zero)
|
||||
.animate(
|
||||
CurvedAnimation(
|
||||
parent: animation,
|
||||
curve: MotionCurve.decelerate,
|
||||
reverseCurve: MotionCurve.accelerate,
|
||||
),
|
||||
);
|
||||
return SlideTransition(position: slide, child: child);
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -121,3 +121,36 @@ class DayMarkColors extends ThemeExtension<DayMarkColors> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
@ -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 ✓');
|
||||
});
|
||||
}
|
||||
|
||||
46
phone_app/test/ui/motion_test.dart
Normal file
46
phone_app/test/ui/motion_test.dart
Normal file
@ -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<Duration> _routeDuration(WidgetTester t, {required bool off}) async {
|
||||
late Duration d;
|
||||
await t.pumpWidget(
|
||||
MediaQuery(
|
||||
data: MediaQueryData(disableAnimations: off),
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
d = appRoute<void>(
|
||||
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);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user