Closed thomasvidas closed 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!
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)
master
the 3.x branch and put the existing code into a 2.x
branch. Makes it easy to backport critical fixes/features!npm publish --tag next
to prevent users from accidentally installing the version with Capacitor 3 changes until Capacitor 3 has hit full release.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 😅)
call.success()
should be call.resolve()
and call.error()
should be call.reject()
to better match their JS counterparts.self.bridge
is now a weak reference, but using self.bridge?.func()
should probably be fine.keepAlive
pattern for plugin calls. This allows your to use call.resolve()
multiple times. The docs on that are here. You can keep using the existing saveCall
function but it doesn't seem to fit your use case. It might look like this@objc func GetBackgroundLocation(_ call: CAPPluginCall) {
call.keepAlive = true
// ... do things! ...
call.resolve()
// ... do things! ...
call.resolve()
}
keepAlive
stuff as above, but on Android it is call.setKeepAlive(true)
.@NativePlugin
to @CapacitorPlugin
. You don't need the permission request codes anymore! Whenever you want to handle a permission you use the @PermissionCallback or
@ActivityCallbackannotation above your callback rather than handling it in
handleRequestPermissionsResult`. It might look something like this.@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
}
}
Thanks, I am working through these issues.
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!