Skip to content

Commit

Permalink
fix: fitness data didn't after challenge creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Turtlepaw committed Oct 12, 2024
1 parent 49b41e3 commit 3fb984e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 53 deletions.
6 changes: 3 additions & 3 deletions lib/routes/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ class _CreateDialogState extends State<CreateDialog> {
});

if (mounted) {
Provider.of<ChallengeProvider>(context, listen: false)
await Provider.of<ChallengeProvider>(context, listen: false)
.reloadChallenges(context);

Provider.of<HealthManager>(context, listen: false)
await Provider.of<HealthManager>(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();
Expand Down
117 changes: 69 additions & 48 deletions lib/routes/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SettingsPage extends StatefulWidget {
class _SettingsPageState extends State<SettingsPage> {
bool _isLoading = false;
bool _isAvailable = true;
bool _isHealthConnected = false;
bool _isSystemHealthAutorized = false;
bool _isWatchLoading = false;
bool _isSysHealthLoading = false;
bool _isRefreshing = false;
Expand Down Expand Up @@ -82,10 +82,11 @@ class _SettingsPageState extends State<SettingsPage> {
if (result == true) {
if (mounted) {
debugPrint("Health connect permissions granted");
Provider.of<HealthManager>(context, listen: false)
HealthTypeManager().setHealthType(HealthType.systemManaged);

await Provider.of<HealthManager>(context, listen: false)
.fetchHealthData(context: context);

HealthTypeManager().setHealthType(HealthType.systemManaged);
setState(() {
healthType = HealthType.systemManaged;
});
Expand All @@ -102,8 +103,8 @@ class _SettingsPageState extends State<SettingsPage> {

if (mounted) {
final p = Provider.of<HealthManager>(context, listen: false);
p.fetchHealthData();
p.checkConnectionState();
p.fetchHealthData();
p.checkConnectionState();
} else {
debugPrint("not mounted");
}
Expand All @@ -114,12 +115,12 @@ class _SettingsPageState extends State<SettingsPage> {

if (result == true) {
setState(() {
_isHealthConnected = true;
_isSystemHealthAutorized = true;
});
return true;
} else {
setState(() {
_isHealthConnected = false;
_isSystemHealthAutorized = false;
});
return false;
}
Expand Down Expand Up @@ -160,7 +161,6 @@ class _SettingsPageState extends State<SettingsPage> {
await Future.delayed(const Duration(seconds: 1));
setState(() {
healthType = HealthType.watch;
if (allDataItems.isNotEmpty) _isHealthConnected = true;
_isWatchLoading = false;
});
return true;
Expand Down Expand Up @@ -263,18 +263,16 @@ class _SettingsPageState extends State<SettingsPage> {
})
: 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)
Expand Down Expand Up @@ -304,7 +302,7 @@ class _SettingsPageState extends State<SettingsPage> {
),
const SizedBox(width: 8),
Text(
"Not synced",
getErrorText(),
style: theme.textTheme.bodyLarge
?.copyWith(color: theme.colorScheme.error),
)
Expand All @@ -318,31 +316,39 @@ class _SettingsPageState extends State<SettingsPage> {
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,
),
const SizedBox(
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,
Expand All @@ -353,23 +359,30 @@ class _SettingsPageState extends State<SettingsPage> {
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,
))
],
)
]),
Expand All @@ -380,6 +393,14 @@ class _SettingsPageState extends State<SettingsPage> {
);
}

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
Expand Down
12 changes: 11 additions & 1 deletion lib/utils/health.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand All @@ -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";
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 3fb984e

Please sign in to comment.