Repository navigation
Expand file tree
/
Copy pathmain.dart
More file actions
433 lines (402 loc) · 14 KB
/
Copy pathmain.dart
File metadata and controls
433 lines (402 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import 'package:flutter/material.dart';
import 'package:flutter_workoutkit/flutter_workoutkit.dart';
import 'sample_workout.dart';
void main() {
runApp(const MyApp());
}
/// Samples shown in the example app.
final Map<String, Workout> samples = {
'[Custom] 5K Training Run': sampleWorkout,
'[Custom] Tempo Run (pace alerts)': sampleTempoPaceRun,
'[Custom] Pool Intervals (iOS 18)': samplePoolSwimIntervals,
'[Custom] Speed Cycling': sampleSpeedCyclingWorkout,
'[Custom] Timed Workout': sampleCustomWorkout,
'[Custom] 10k Training Run': sample10kTrainingRun,
'[Custom] 21k Stepped Run': sample21kmTrainingRun,
'[Pacer] 2 Hour Cycle': samplePacerHoursWorkout,
'[Pacer] 30 Minute Run': samplePacerMinutesWorkout,
'[Single] Timed Workout': sampleSingleGoalTimedWorkout,
'[Single] Energy Workout': sampleSingleGoalEnergyWorkout,
'[Single] Open Workout': sampleSingleOpenWorkout,
'[Single] Swimming Workout': sampleSingleGoalSwimmingWorkout,
'[SwimBikeRun] Triathlon Training': sampleSwimBikeRunWorkout,
};
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter WorkoutKit Example'),
bottom: const TabBar(isScrollable: true, tabs: [
Tab(text: 'Preview'),
Tab(text: 'Validate'),
Tab(text: 'Customize'),
Tab(text: 'Schedule'),
]),
),
body: const TabBarView(children: [
PreviewTab(),
ValidateTab(),
CustomizeTab(),
ScheduleTab(),
]),
),
),
);
}
}
/// One preview entry per sample, either as the native `WorkoutPreviewButton`
/// or as a Flutter button calling `WorkoutKit.presentPreview`.
class PreviewTab extends StatefulWidget {
const PreviewTab({super.key});
@override
State<PreviewTab> createState() => _PreviewTabState();
}
class _PreviewTabState extends State<PreviewTab> {
bool _useNativeButton = true;
void _showError(String name, WorkoutkitException error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$name: ${error.message}')),
);
}
Future<void> _present(String name, Workout workout) async {
try {
await WorkoutKit.presentPreview(workout);
} on WorkoutkitException catch (e) {
if (mounted) _showError(name, e);
}
}
@override
Widget build(BuildContext context) {
return ListView(
padding: const EdgeInsets.symmetric(vertical: 8),
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: SegmentedButton<bool>(
segments: const [
ButtonSegment(value: true, label: Text('Native button')),
ButtonSegment(value: false, label: Text('presentPreview')),
],
selected: {_useNativeButton},
onSelectionChanged: (v) =>
setState(() => _useNativeButton = v.first),
),
),
for (final entry in samples.entries)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: _useNativeButton
? WorkoutPreviewButton(
workout: entry.value,
buttonTitle: entry.key,
sfSymbol: 'applewatch',
onError: (error) => _showError(entry.key, error),
)
: SizedBox(
height: 54,
child: FilledButton.icon(
icon: const Icon(Icons.watch),
label: Text(entry.key),
onPressed: () => _present(entry.key, entry.value),
),
),
),
],
);
}
}
/// Runs `WorkoutKit.validate` on every sample.
class ValidateTab extends StatefulWidget {
const ValidateTab({super.key});
@override
State<ValidateTab> createState() => _ValidateTabState();
}
class _ValidateTabState extends State<ValidateTab> {
late final Future<Map<String, WorkoutValidationResult>> _results = () async {
return {
for (final entry in {...samples, ...unsupportedExamples}.entries)
entry.key: await WorkoutKit.validate(entry.value),
};
}();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _results,
builder: (context, snapshot) {
if (snapshot.hasError) return Center(child: Text('${snapshot.error}'));
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
return ListView(
children: [
for (final entry in snapshot.data!.entries)
ListTile(
leading: Icon(
entry.value.isValid ? Icons.check_circle : Icons.error,
color: entry.value.isValid ? Colors.green : Colors.red,
),
title: Text(entry.key),
// Structured issues: a code and a path you can use to
// localize the message or highlight the offending step.
subtitle: entry.value.isValid
? null
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final issue in entry.value.details)
Text('${issue.code.name}'
'${issue.path == null ? '' : ' @ ${issue.path}'}'
'\n${issue.message}'),
],
),
),
],
);
},
);
}
}
/// Workouts WorkoutKit rejects, to show structured validation issues.
final Map<String, Workout> unsupportedExamples = {
'[Invalid] Archery pacer': samplePacerMinutesWorkout.copyWith(
activityType: WorkoutActivityType.archery,
),
'[Invalid] Pool swim goal on a run': sampleTempoPaceRun.copyWith(
displayName: 'Run with a pool swim step',
blocks: [
IntervalBlock(iterations: 1, steps: [
IntervalStep(
purpose: IntervalStepPurpose.work,
goal: const WorkoutGoal.distance(1, WorkoutGoalUnit.kilometers),
),
IntervalStep(
purpose: IntervalStepPurpose.work,
goal: const WorkoutGoal.poolSwimDistanceWithTime(
distance: 100,
distanceUnit: WorkoutGoalUnit.meters,
duration: Duration(minutes: 2),
),
),
]),
],
),
};
/// Builds a variant of the tempo run with `copyWith`, compares it with `==`,
/// round-trips it through WorkoutKit's export format and opens it with
/// `WorkoutKit.presentPreview`.
class CustomizeTab extends StatefulWidget {
const CustomizeTab({super.key});
@override
State<CustomizeTab> createState() => _CustomizeTabState();
}
class _CustomizeTabState extends State<CustomizeTab> {
int _repeats = 3;
int _fastestPaceSeconds = 300;
String? _status;
CustomWorkout get _workout {
final base = sampleTempoPaceRun;
final block = base.blocks.first;
final work = block.steps.first;
final pace = work.alert! as PaceRangeAlert;
return base.copyWith(
displayName: 'Tempo Run ($_repeats×2 km)',
blocks: [
block.copyWith(
iterations: _repeats,
steps: [
work.copyWith(
alert: pace.copyWith(
fastestPace: Duration(seconds: _fastestPaceSeconds),
slowestPace: Duration(seconds: _fastestPaceSeconds + 30),
),
),
...block.steps.skip(1),
],
),
],
);
}
Future<void> _run(Future<String> Function() action) async {
try {
final status = await action();
if (mounted) setState(() => _status = status);
} on WorkoutkitException catch (e) {
if (mounted) setState(() => _status = '${e.code}: ${e.message}');
}
}
String _formatPace(int seconds) =>
'${seconds ~/ 60}:${(seconds % 60).toString().padLeft(2, '0')} /km';
@override
Widget build(BuildContext context) {
final workout = _workout;
final unchanged = workout.copyWith(displayName: 'Tempo Run') ==
sampleTempoPaceRun.copyWith(displayName: 'Tempo Run');
return ListView(
padding: const EdgeInsets.all(16),
children: [
Text(workout.displayName,
style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 8),
Text('Repeats: $_repeats'),
Slider(
value: _repeats.toDouble(),
min: 1,
max: 8,
divisions: 7,
onChanged: (v) => setState(() => _repeats = v.round()),
),
Text('Pace: ${_formatPace(_fastestPaceSeconds)} – '
'${_formatPace(_fastestPaceSeconds + 30)}'),
Slider(
value: _fastestPaceSeconds.toDouble(),
min: 210,
max: 420,
divisions: 14,
onChanged: (v) => setState(() => _fastestPaceSeconds = v.round()),
),
ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(unchanged ? Icons.drag_handle : Icons.edit),
title: Text(unchanged
? 'Same steps as the sample (==)'
: 'Different from the sample (==)'),
),
const SizedBox(height: 8),
FilledButton.icon(
icon: const Icon(Icons.watch),
label: const Text('Preview with presentPreview'),
onPressed: () => _run(() async {
await WorkoutKit.presentPreview(workout);
return 'Preview closed';
}),
),
const SizedBox(height: 8),
OutlinedButton.icon(
icon: const Icon(Icons.swap_vert),
label: const Text('Export & import'),
onPressed: () => _run(() async {
final data = await WorkoutKit.exportWorkout(workout);
final imported = await WorkoutKit.importWorkout(data);
// Pace alerts come back as speed alerts, so compare the rest.
final sameShape = imported is CustomWorkout &&
imported.displayName == workout.displayName &&
imported.blocks.first.iterations == _repeats;
return 'Exported ${data.length} bytes; imported '
'${sameShape ? 'the same workout' : 'a different workout'}';
}),
),
if (_status != null) ...[
const SizedBox(height: 16),
Text(_status!),
],
],
);
}
}
/// Authorizes and schedules workouts on the paired Apple Watch.
class ScheduleTab extends StatefulWidget {
const ScheduleTab({super.key});
@override
State<ScheduleTab> createState() => _ScheduleTabState();
}
class _ScheduleTabState extends State<ScheduleTab> {
bool? _supported;
SchedulerAuthorizationState? _state;
List<ScheduledWorkout> _scheduled = [];
String? _error;
@override
void initState() {
super.initState();
_refresh();
}
Future<void> _run(Future<void> Function() action) async {
try {
await action();
_error = null;
} on WorkoutkitException catch (e) {
_error = '${e.code}: ${e.message}';
}
await _refresh();
}
Future<void> _refresh() async {
try {
final supported = await WorkoutScheduler.isSupported();
final state = await WorkoutScheduler.authorizationState();
final scheduled = state == SchedulerAuthorizationState.authorized
? await WorkoutScheduler.scheduledWorkouts()
: <ScheduledWorkout>[];
if (!mounted) return;
setState(() {
_supported = supported;
_state = state;
_scheduled = scheduled;
});
} on WorkoutkitException catch (e) {
if (mounted) setState(() => _error = '${e.code}: ${e.message}');
}
}
@override
Widget build(BuildContext context) {
final authorized = _state == SchedulerAuthorizationState.authorized;
return ListView(
padding: const EdgeInsets.all(16),
children: [
Text('Supported: ${_supported ?? '…'}'),
Text('Authorization: ${_state?.name ?? '…'}'),
if (_error != null)
Text(_error!, style: const TextStyle(color: Colors.red)),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () => _run(WorkoutScheduler.requestAuthorization),
child: const Text('Request authorization'),
),
ElevatedButton(
onPressed: authorized
? () => _run(() async {
final tomorrow =
DateTime.now().add(const Duration(days: 1));
await WorkoutScheduler.schedule(
sampleTempoPaceRun,
DateTime(tomorrow.year, tomorrow.month, tomorrow.day, 7),
);
})
: null,
child: const Text('Schedule "Tempo Run" tomorrow at 07:00'),
),
ElevatedButton(
onPressed: authorized ? () => _run(WorkoutScheduler.removeAll) : null,
child: const Text('Remove all'),
),
const Divider(),
for (final scheduled in _scheduled)
ListTile(
title: Text(_title(scheduled.workout)),
subtitle: Text('${scheduled.date}'
'${scheduled.complete ? ' (complete)' : ''}'),
trailing: Wrap(children: [
IconButton(
icon: const Icon(Icons.check),
onPressed: () =>
_run(() => WorkoutScheduler.markComplete(scheduled)),
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: () => _run(() => WorkoutScheduler.remove(scheduled)),
),
]),
),
],
);
}
String _title(Workout workout) => switch (workout) {
CustomWorkout(:final displayName) => displayName,
SwimBikeRunWorkout(:final displayName) =>
displayName ?? workout.workoutType.label,
_ => workout.workoutType.label,
};
}