From cddcbf72c380f7dd174d2bc059e08ef63686490b Mon Sep 17 00:00:00 2001 From: maxedbeech Date: Sun, 15 Oct 2023 19:27:24 +0100 Subject: [PATCH] Availability improvements From this commit: https://github.com/cph-cachet/flutter-plugins/pull/799 --- .../cachet/plugins/health/HealthPlugin.kt | 17 ++++++++++++++- .../ios/Classes/SwiftHealthPlugin.swift | 8 +++++++ packages/health/lib/src/health_factory.dart | 21 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt index cfc288fba..e04c0d71e 100644 --- a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt +++ b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt @@ -1393,6 +1393,8 @@ class HealthPlugin(private var channel: MethodChannel? = null) : override fun onMethodCall(call: MethodCall, result: Result) { when (call.method) { "useHealthConnectIfAvailable" -> useHealthConnectIfAvailable(call, result) + "openSystemSettings" -> openSystemSettings(call, result) + "checkAvailability" -> checkAvailability(call, result) "hasPermissions" -> hasPermissions(call, result) "requestAuthorization" -> requestAuthorization(call, result) "revokePermissions" -> revokePermissions(call, result) @@ -1436,9 +1438,22 @@ class HealthPlugin(private var channel: MethodChannel? = null) : var healthConnectAvailable = false var healthConnectStatus = HealthConnectClient.SDK_UNAVAILABLE - fun checkAvailability() { + fun checkAvailability(call: MethodCall? = null, result: Result? = null) { healthConnectStatus = HealthConnectClient.getSdkStatus(context!!) healthConnectAvailable = healthConnectStatus == HealthConnectClient.SDK_AVAILABLE + result?.success(healthConnectAvailable) + } + + fun openSystemSettings(call: MethodCall, result: Result) { + try { + val intent = Intent() + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.action = HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS + context?.startActivity(intent) + result.success(true) + } catch (e: Throwable) { + result.error("UNABLE_TO_START_ACTIVITY", e.message, e) + } } fun useHealthConnectIfAvailable(call: MethodCall, result: Result) { diff --git a/packages/health/ios/Classes/SwiftHealthPlugin.swift b/packages/health/ios/Classes/SwiftHealthPlugin.swift index dd2ed7f06..06fae1140 100644 --- a/packages/health/ios/Classes/SwiftHealthPlugin.swift +++ b/packages/health/ios/Classes/SwiftHealthPlugin.swift @@ -138,6 +138,10 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin { else if call.method.elementsEqual("requestAuthorization") { try! requestAuthorization(call: call, result: result) } + + else if call.method.elementsEqual("openSystemSettings") { + openSystemSettings(call: call, result: result) + } /// Handle getData else if call.method.elementsEqual("getData") { @@ -184,6 +188,10 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin { func checkIfHealthDataAvailable(call: FlutterMethodCall, result: @escaping FlutterResult) { result(HKHealthStore.isHealthDataAvailable()) } + + func openSystemSettings(call: FlutterMethodCall, result: @escaping FlutterResult) { + UIApplication.shared.open(URL(string: "x-apple-health://")!) + } func hasPermissions(call: FlutterMethodCall, result: @escaping FlutterResult) throws { let arguments = call.arguments as? NSDictionary diff --git a/packages/health/lib/src/health_factory.dart b/packages/health/lib/src/health_factory.dart index 58ccc85ed..46900a45e 100644 --- a/packages/health/lib/src/health_factory.dart +++ b/packages/health/lib/src/health_factory.dart @@ -94,6 +94,27 @@ class HealthFactory { print(e); } } + + /// Opens native system settings for: + /// - Health on iOS + /// - Health Connect on Android + /// + /// Throws if the application is not installed on the device. + Future openSystemSettings() async { + await _channel.invokeMethod('openSystemSettings'); + } + + /// Checks the availability of Health Connect on Android platform. + /// + /// Returns a bool indicating whether Health Connect is available. + /// + /// On other platforms than Android it returns always true. + Future checkAvailability() async { + if (!Platform.isAndroid) return true; + + final result = await _channel.invokeMethod('checkAvailability'); + return result ?? false; + } /// Requests permissions to access data types in Apple Health or Google Fit. ///