Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions open_wearable/lib/apps/widgets/apps_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ final List<AppInfo> _apps = [
accentColor: _appAccentColor,
widget: SelectEarableView(
supportedDevicePrefixes: _postureSupportedDevices,
startApp: (wearable, sensorConfigProvider) {
startApp: (wearable, sensorConfigProvider) async {
return PostureTrackerView(
EarableAttitudeTracker(
wearable.requireCapability<SensorManager>(),
sensorConfigProvider,
wearable.name.endsWith("L"),
wearable.hasCapability<StereoDevice>() &&
await wearable.requireCapability<StereoDevice>().position == DevicePosition.left,
),
);
},
Expand All @@ -110,7 +111,7 @@ final List<AppInfo> _apps = [
accentColor: _appAccentColor,
widget: SelectEarableView(
supportedDevicePrefixes: _heartSupportedDevices,
startApp: (wearable, _) {
startApp: (wearable, _) async {
if (wearable.hasCapability<SensorManager>()) {
final sensors = wearable.requireCapability<SensorManager>().sensors;
Sensor? ppgSensor;
Expand Down
89 changes: 75 additions & 14 deletions open_wearable/lib/apps/widgets/select_earable_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import 'package:open_wearable/widgets/devices/wearable_icon.dart';
import 'package:provider/provider.dart';

class SelectEarableView extends StatefulWidget {
final Widget Function(Wearable, SensorConfigurationProvider) startApp;
final Future<Widget> Function(
Wearable,
SensorConfigurationProvider,
) startApp;
final List<String> supportedDevicePrefixes;

const SelectEarableView({
Expand All @@ -28,6 +31,7 @@ class _SelectEarableViewState extends State<SelectEarableView> {
Wearable? _selectedWearable;
Future<List<WearableDisplayGroup>>? _groupsFuture;
String _groupFingerprint = '';
bool _isStartingApp = false;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -68,14 +72,19 @@ class _SelectEarableViewState extends State<SelectEarableView> {
child: SizedBox(
width: double.infinity,
child: PlatformElevatedButton(
onPressed: hasSelectedCompatibleWearable
onPressed: hasSelectedCompatibleWearable && !_isStartingApp
? () => _startSelectedApp(
context,
wearablesProvider,
compatibleWearables,
)
: null,
child: PlatformText('Start App'),
child: _isStartingApp
? const SizedBox(
height: 20,
width: 20,
child: PlatformCircularProgressIndicator(),
)
: PlatformText('Start App'),
),
),
),
Expand Down Expand Up @@ -216,11 +225,10 @@ class _SelectEarableViewState extends State<SelectEarableView> {
return indexed.map((entry) => entry.value).toList(growable: false);
}

void _startSelectedApp(
BuildContext context,
Future<void> _startSelectedApp(
WearablesProvider wearablesProvider,
List<Wearable> compatibleWearables,
) {
) async {
final selectedId = _selectedWearable?.deviceId;
if (selectedId == null) {
return;
Expand All @@ -236,18 +244,71 @@ class _SelectEarableViewState extends State<SelectEarableView> {

final sensorConfigProvider =
wearablesProvider.getSensorConfigurationProvider(selectedWearable);
final navigator = Navigator.of(context);

setState(() {
_isStartingApp = true;
});

Navigator.push(
context,
navigator.push(
platformPageRoute(
context: context,
builder: (context) => ChangeNotifierProvider.value(
value: sensorConfigProvider,
child: widget.startApp(
selectedWearable,
sensorConfigProvider,
builder: (context) => const _AppStartupLoadingScreen(),
),
);

try {
final app = await widget.startApp(
selectedWearable,
sensorConfigProvider,
);

if (!mounted) {
return;
}

navigator.pushReplacement(
platformPageRoute(
context: context,
builder: (context) => ChangeNotifierProvider.value(
value: sensorConfigProvider,
child: app,
),
),
);
} catch (_) {
if (navigator.canPop()) {
navigator.pop();
}
rethrow;
} finally {
if (mounted) {
setState(() {
_isStartingApp = false;
});
}
}
}
}

class _AppStartupLoadingScreen extends StatelessWidget {
const _AppStartupLoadingScreen();

@override
Widget build(BuildContext context) {
return PlatformScaffold(
appBar: PlatformAppBar(
title: const Text('Starting App'),
),
body: const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
PlatformCircularProgressIndicator(),
SizedBox(height: 12),
Text('Preparing app...'),
],
),
),
);
}
Expand Down