From 3fb984ea927ee92a90f45a0302855818fe6d7a07 Mon Sep 17 00:00:00 2001 From: Turtlepaw <81275769+Turtlepaw@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:42:18 -0400 Subject: [PATCH] fix: fitness data didn't after challenge creation --- lib/routes/create.dart | 6 +- lib/routes/settings.dart | 117 +++++++++++++++++++++++---------------- lib/utils/health.dart | 12 +++- pubspec.yaml | 2 +- 4 files changed, 84 insertions(+), 53 deletions(-) diff --git a/lib/routes/create.dart b/lib/routes/create.dart index 8671f13..30e9cd8 100644 --- a/lib/routes/create.dart +++ b/lib/routes/create.dart @@ -180,15 +180,15 @@ class _CreateDialogState extends State { }); if (mounted) { - Provider.of(context, listen: false) + await Provider.of(context, listen: false) .reloadChallenges(context); - Provider.of(context, listen: false) + await Provider.of(context, listen: false) .fetchHealthData(context: context); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( - content: Text('Challenge created'), + content: Text('Challenge created and synced'), ), ); Navigator.of(context).pop(); diff --git a/lib/routes/settings.dart b/lib/routes/settings.dart index 9d986b6..d34ea0a 100644 --- a/lib/routes/settings.dart +++ b/lib/routes/settings.dart @@ -25,7 +25,7 @@ class SettingsPage extends StatefulWidget { class _SettingsPageState extends State { bool _isLoading = false; bool _isAvailable = true; - bool _isHealthConnected = false; + bool _isSystemHealthAutorized = false; bool _isWatchLoading = false; bool _isSysHealthLoading = false; bool _isRefreshing = false; @@ -82,10 +82,11 @@ class _SettingsPageState extends State { if (result == true) { if (mounted) { debugPrint("Health connect permissions granted"); - Provider.of(context, listen: false) + HealthTypeManager().setHealthType(HealthType.systemManaged); + + await Provider.of(context, listen: false) .fetchHealthData(context: context); - HealthTypeManager().setHealthType(HealthType.systemManaged); setState(() { healthType = HealthType.systemManaged; }); @@ -102,8 +103,8 @@ class _SettingsPageState extends State { if (mounted) { final p = Provider.of(context, listen: false); - p.fetchHealthData(); - p.checkConnectionState(); + p.fetchHealthData(); + p.checkConnectionState(); } else { debugPrint("not mounted"); } @@ -114,12 +115,12 @@ class _SettingsPageState extends State { if (result == true) { setState(() { - _isHealthConnected = true; + _isSystemHealthAutorized = true; }); return true; } else { setState(() { - _isHealthConnected = false; + _isSystemHealthAutorized = false; }); return false; } @@ -160,7 +161,6 @@ class _SettingsPageState extends State { await Future.delayed(const Duration(seconds: 1)); setState(() { healthType = HealthType.watch; - if (allDataItems.isNotEmpty) _isHealthConnected = true; _isWatchLoading = false; }); return true; @@ -263,18 +263,16 @@ class _SettingsPageState extends State { }) : buildCard([ ConstrainedBox( - constraints: const BoxConstraints( - maxWidth: 300 + constraints: const BoxConstraints(maxWidth: 300), + child: Text( + _isAvailable + ? (healthType != null + ? "Health connected via ${HealthTypeManager.formatType(healthType)}" + : "Connect a health platform") + : "Health unavailable", + style: theme.textTheme.titleLarge, + textAlign: TextAlign.center, ), - child: Text( - _isAvailable - ? (healthType != null - ? "Health connected via ${HealthTypeManager.formatType(healthType)}" - : "Connect a health platform") - : "Health unavailable", - style: theme.textTheme.titleLarge, - textAlign: TextAlign.center, - ), ), const SizedBox(height: 5), if (healthType == null) @@ -304,7 +302,7 @@ class _SettingsPageState extends State { ), const SizedBox(width: 8), Text( - "Not synced", + getErrorText(), style: theme.textTheme.bodyLarge ?.copyWith(color: theme.colorScheme.error), ) @@ -318,18 +316,22 @@ class _SettingsPageState extends State { onSelected: (_isAvailable ? _connectSystemHealthPlatform : null), - label: Text(health.capabilities.contains(HealthType.systemManaged) + label: Text(health.capabilities + .contains(HealthType.systemManaged) ? ((health.isConnected && healthType == HealthType.systemManaged) ? "Connected" - : "System") + : HealthTypeManager.formatType( + HealthType.systemManaged)) : "Unavailable"), selected: healthType == HealthType.systemManaged, - avatar: _isSysHealthLoading && health.capabilities.contains(HealthType.systemManaged) + avatar: _isSysHealthLoading && + health.capabilities + .contains(HealthType.systemManaged) ? const CircularProgressIndicator( - strokeWidth: 3, - strokeCap: StrokeCap.round, - ) + strokeWidth: 3, + strokeCap: StrokeCap.round, + ) : null, showCheckmark: !_isSysHealthLoading, ), @@ -337,12 +339,16 @@ class _SettingsPageState extends State { width: 10, ), FilterChip( - label: const Text("Wear OS"), - onSelected: health.capabilities.contains(HealthType.watch) - ? ((isSelected) => _connectWearOS()) - : null, + label: Text( + HealthTypeManager.formatType(HealthType.watch)), + onSelected: + health.capabilities.contains(HealthType.watch) + ? ((isSelected) => _connectWearOS()) + : null, selected: healthType == HealthType.watch, - avatar: _isWatchLoading && health.capabilities.contains(HealthType.watch) + avatar: _isWatchLoading && + health.capabilities + .contains(HealthType.watch) ? const CircularProgressIndicator( strokeWidth: 3, strokeCap: StrokeCap.round, @@ -353,23 +359,30 @@ class _SettingsPageState extends State { const SizedBox( width: 5, ), - IconButton.outlined(onPressed: () async { - setState(() { - _isRefreshing = true; - }); - await health.fetchHealthData(); - await health.checkConnectionState(); - setState(() { - _isRefreshing = false; - }); - }, icon: _isRefreshing ? const SizedBox( - width: 15, - height: 15, - child: CircularProgressIndicator( - strokeWidth: 2, - strokeCap: StrokeCap.round, - ), - ) : Icon(Symbols.refresh_rounded, color: theme.colorScheme.onSurface,)) + IconButton.outlined( + onPressed: () async { + setState(() { + _isRefreshing = true; + }); + await health.fetchHealthData(); + await health.checkConnectionState(); + setState(() { + _isRefreshing = false; + }); + }, + icon: _isRefreshing + ? const SizedBox( + width: 15, + height: 15, + child: CircularProgressIndicator( + strokeWidth: 2, + strokeCap: StrokeCap.round, + ), + ) + : Icon( + Symbols.refresh_rounded, + color: theme.colorScheme.onSurface, + )) ], ) ]), @@ -380,6 +393,14 @@ class _SettingsPageState extends State { ); } + String getErrorText() { + if (!_isSystemHealthAutorized && healthType == HealthType.systemManaged) { + return "Permissions not granted"; + } else { + return "Not synced"; + } + } + double getWidth(BoxConstraints constraints) { if (constraints.maxWidth < 500) { return constraints.maxWidth - 10; // Fill the width on phones with margin diff --git a/lib/utils/health.dart b/lib/utils/health.dart index 3449b1f..9ca98e1 100644 --- a/lib/utils/health.dart +++ b/lib/utils/health.dart @@ -226,7 +226,7 @@ class HealthTypeManager { static String formatType(HealthType? type) { return switch (type) { - HealthType.systemManaged => "System", + HealthType.systemManaged => _getSystemType(), HealthType.watch => _getWatchType(), null => "Unknown", }; @@ -241,4 +241,14 @@ class HealthTypeManager { return "Watch"; } } + + static String _getSystemType() { + if (Platform.isAndroid) { + return "Health Connect"; + } else if (Platform.isIOS) { + return "Apple Health"; + } else { + return "Unavailable"; + } + } } diff --git a/pubspec.yaml b/pubspec.yaml index ef56391..b5be9ab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.0+7 +version: 1.0.0+8 environment: sdk: '>=3.4.3 <4.0.0'