YesterdaySync to BackgroundSync
To simplify the SDK features the following components where removed
- HCRookYesterdaySyncManager
These components needed a semi-permanent notification, relied on a mixture of Android and Health Connect of permissions and had no scheduling support.
HCRookBackgroundSync does not need any notification, only uses Health Connect related permissions and supports
scheduling every 1 hour using the Work Manager API.
Migration process
Remove all usages of the removed components and follow steps below:
Check that the device supports background reads with HCRookHealthPermissionsManager.checkBackgroundReadStatus, this
will return a HCBackgroundReadStatus:
enum HCBackgroundReadStatus {
/// Background read is not available on this device. Try asking the user to update their Health Connect application.
unavailable,
/// Background read permission is not granted. Try requesting background read permission.
permissionNotGranted,
/// Background read permission is granted.
permissionGranted,
}
Most devices should support background reads, check the Legacy section for alternatives when unavailable is
returned.
When the user clicks on the "Connect" button schedule an hourly sync:
Future<void> connectToHealthConnect() async {
// We are assuming that you've already check for HC availability
final backgroundReadStatus = await HCRookHealthPermissionsManager
.checkBackgroundReadStatus();
if (backgroundReadStatus == HCBackgroundReadStatus.unavailable) {
// TODO: Manage not supported case
return;
}
await HCRookBackgroundSync.enableBackground(enableNativeLogs: isDebug);
await preferences.toggleHealthConnect(true);
}
Update the "Connect" button using the Boolean returned by:
void example() async {
final connected = await HCRookBackgroundSync.isScheduled();
}
Or if you want to receive real time updates:
void example() {
subscription = HCRookBackgroundSync.isScheduledUpdates.listen((isScheduled) {
// Update UI
});
}
Finally go to the main method and add a call to enableBackground, this allows to re-schedule the background sync if is
killed by the system:
void main() {
// Ensure that the plugin is ready
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) {
enableAndroidBackgroundSync();
}
runApp(App());
}
void enableAndroidBackgroundSync() async {
try {
final userAllowedBackgroundSync = await AppPreferences().getUserAllowedBackgroundSync();
if (userAllowedBackgroundSync) {
await HCRookBackgroundSync.enableBackground(enableNativeLogs: isDebug);
}
} catch (error) {
// Log
}
}
When the user clicks on the "Disconnect" button cancel any scheduled sync:
Future<void> disconnectFromHealthConnect() async {
await HCRookBackgroundSync.disableBackground();
await preferences.toggleHealthConnect(false);
}
Go to the background sync documentation to learn more.
Check the best practices documentation to learn how improve Background Sync behaviour on devices with energy limitations.
Legacy
Most devices should be compatible with the background read feature of Health Connect, however there will be instances where is not supported that's why we recommend to always check for feature availability:
void example() async {
try {
final backgroundReadStatus =
await HCRookHealthPermissionsManager.checkBackgroundReadStatus();
switch (backgroundReadStatus) {
case HCBackgroundReadStatus.unavailable:
// TODO
break;
case HCBackgroundReadStatus.permissionNotGranted:
// TODO
break;
case HCBackgroundReadStatus.permissionGranted:
// TODO
break;
}
} catch (error) {
// Handle error
}
}
If HCBackgroundReadStatus.unavailable is returned you can hide the Health Connect option from your users and show an
alternative like the Steps Counter.