Skip to content

Commit

Permalink
Availability improvements
Browse files Browse the repository at this point in the history
From this commit: cph-cachet#799
  • Loading branch information
maxbeech committed Oct 15, 2023
1 parent 6bb1510 commit cddcbf7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions packages/health/ios/Classes/SwiftHealthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions packages/health/lib/src/health_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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<bool> checkAvailability() async {
if (!Platform.isAndroid) return true;

final result = await _channel.invokeMethod<bool>('checkAvailability');
return result ?? false;
}

/// Requests permissions to access data types in Apple Health or Google Fit.
///
Expand Down

0 comments on commit cddcbf7

Please sign in to comment.