capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
190 stars 57 forks source link

Capacitor 3 Support #16

Closed thomasvidas closed 3 years ago

thomasvidas commented 3 years ago

Is your feature request related to a problem? Please describe.

The Capacitor 3 RC is out! 🎉 This library should support the new version

Describe the solution you'd like

Follow the plugin upgrade guide here: https://capacitorjs.com/docs/v3/updating/plugins/3-0

Additional context

The migration is pretty easy, I've done it for a couple plugins. Happy to assist if you need assisting!

diachedelic commented 3 years ago

Yes I would like to, can I assume that any changes will be backward compatible with Capacitor v2?

If you wouldn't mind reviewing the source and suggesting the required changes, I would be most grateful. I expect the following source files will receive most or all of the required changes:

Thanks!

thomasvidas commented 3 years ago

Capacitor 3 has breaking changes for 2.x plugins, with the biggest ones being the import style and Android permissions, so it is difficult to maintain backwards compatibility. Here is what I would suggest (and have seen other developers do)

Looking over your code quickly here are the things I see that need to be addressed. (Sorry if it isn't perfect syntax wise, I'm just winging it 😅)

iOS

@objc func GetBackgroundLocation(_ call: CAPPluginCall) {
    call.keepAlive = true

    // ... do things! ...

    call.resolve()

    // ... do things! ...

    call.resolve()
}

Android

@CapacitorPlugin(
    name = "BackgroundGeolocation",
    permissions = {
        @Permission(strings = { Manifest.permission.ACCESS_FINE_LOCATION }, alias = "location_permission")
    }
)
public class BackgroundGeolocationPlugin {
    @PluginMethod()
    public void GetBackgroundLocation(PluginCall call) {
        if (getPermissionState("location_permission") != PermissionState.GRANTED) {
            requestPermissionForAlias("location_permission", call, "myCoolPermissionCallback");
        } else {
            getLocationData(call);
        }
    }

    @PermissionCallback
    private void myCoolPermissionCallback(PluginCall call) {
        // This happens after OS level permission check
        if (getPermissionState("location_permission") == PermissionState.GRANTED) {
            getLocationData(call);
        } else {
            call.reject("Permission is required to get background location");
        }
    }

    private void getLocationData(PluginCall call) {
      // Get the location data
    }
}
diachedelic commented 3 years ago

Thanks, I am working through these issues.