Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified android/libs/ionnativeislands-android-release.aar
Comment thread
OS-pedrogustavobilro marked this conversation as resolved.
Resolved
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ import io.ionic.libs.iongeolocationlib.model.IONGLOCLocationOptions
import io.ionic.libs.iongeolocationlib.model.IONGLOCLocationResult
import io.ionic.libs.iongeolocationlib.view.IONGLOCLocationButtonRegistry
import io.ionic.libs.iongeolocationlib.view.IONGLOCLocationButtonPermissionRequester
import io.ionic.libs.ionnativeislandslib.NativeIslandsBridgeValidationError
import io.ionic.libs.ionnativeislandslib.NativeIslandsBridgeValidator
import io.ionic.libs.ionnativeislandslib.NativeIslandsCapabilities
import io.ionic.libs.ionnativeislandslib.NativeIslandsController
import io.ionic.libs.ionnativeislandslib.NativeIslandsScrollChannel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONObject

@CapacitorPlugin(
Expand All @@ -47,7 +43,9 @@ class GeolocationPlugin : Plugin() {
private lateinit var controller: IONGLOCController
private lateinit var coroutineScope: CoroutineScope
private lateinit var locationButtonPermissionLauncher: ActivityResultLauncher<Array<String>>
private var nativeIslandsController: NativeIslandsController? = null
private val nativeIslandsController = NativeIslandsController { event, payload ->
notifyListeners("nativeIslands:$event", JSObject.fromJSONObject(payload))
}
private val pendingLocationButtonPermissionResults = mutableListOf<(Boolean) -> Unit>()
private var locationButtonPermissionRequestInFlight = false
private val watchingCalls: MutableMap<String, PluginCall> = mutableMapOf()
Expand Down Expand Up @@ -90,12 +88,21 @@ class GeolocationPlugin : Plugin() {
::mapButtonErrorCode,
::mapButtonPosition,
)
nativeIslandsController.bind(bridge.webView, activity)
activity.runOnUiThread {
NativeIslandsScrollChannel.install(
bridge.webView,
"Geolocation",
bridge.allowedOriginRules,
) { nativeIslandsController?.receiveScrollOffsets(it) }
nativeIslandsController::receiveScrollOffsets,
nativeIslandsController::receiveDirect,
)
nativeIslandsController.ensureContainer()
if (activity.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
nativeIslandsController.onHostResume()
} else {
nativeIslandsController.onHostPause()
}
}
}

Expand Down Expand Up @@ -144,156 +151,32 @@ class GeolocationPlugin : Plugin() {

@PluginMethod
fun nativeIslandsApplyLayout(call: PluginCall) {
val envelope = call.data
if (!validateNativeIslands(call, NativeIslandsBridgeValidator.validateLayoutOperation(envelope))) {
return
}
val host = nativeIslandsHost()
val components = envelope.opt("components") as JSONArray
val order = envelope.opt("order") as JSONArray
val exclusions = envelope.opt("exclusions") as JSONObject
val cutouts = envelope.opt("cutouts") as JSONObject
val scrollContainers = envelope.opt("scrollContainers") as JSONArray
val documentRange = envelope.optDouble("documentRange", 0.0).toFloat()
val generations = NativeIslandsBridgeValidator.readGenerations(envelope, "layoutSeq", "offsetSeq")
if (generations == null) {
call.reject("layout generations must be non-negative integers sent together", "invalid_request")
return
}
val (layoutSeq, offsetSeq) = generations
host.validateLayout(
components,
order,
exclusions,
cutouts,
scrollContainers,
documentRange,
envelope.optJSONArray("motions"),
tints = envelope.optJSONObject("tints"),
)?.let { reason ->
call.reject(reason, "invalid_request")
return
}
host.applyLayout(
components,
order,
exclusions,
cutouts,
scrollContainers,
documentRange,
layoutSeq,
offsetSeq,
envelope.optString("canvasColor").takeIf(String::isNotEmpty),
envelope.optJSONArray("motions"),
(envelope.opt("sentAt") as? Number)?.toDouble(),
tints = envelope.optJSONObject("tints"),
failure = { code, message -> call.reject(message, code) },
) {
call.resolve(JSObject.fromJSONObject(NativeIslandsCapabilities.layoutAcknowledgement(host.lastLayoutAppliedAt)))
nativeIslandsController.submitLayout(call.data, failure = { code, message -> call.reject(message, code) }) {
call.resolve(JSObject.fromJSONObject(it))
}
}

@PluginMethod
fun nativeIslandsApplyScrollOffsets(call: PluginCall) {
val envelope = call.data
if (
!validateNativeIslands(
call,
NativeIslandsBridgeValidator.validateScrollOffsetsOperation(envelope),
)
) {
return
}
val generation = NativeIslandsBridgeValidator.readGenerations(envelope, "layoutSeq")
if (generation == null) {
call.reject("layoutSeq must be a non-negative integer", "invalid_request")
return
nativeIslandsController.submitScrollOffsets(call.data, failure = { code, message -> call.reject(message, code) }) {
call.resolve()
}
nativeIslandsHost().applyScrollOffsets(
sequence = (envelope.opt("sequence") as Number).toLong(),
offsets = envelope.opt("offsets") as JSONArray,
settled = envelope.optBoolean("settled", false),
layoutSeq = generation[0],
documentOffsetY = (envelope.opt("documentOffsetY") as? Number)?.toFloat(),
sentAt = (envelope.opt("sentAt") as? Number)?.toDouble(),
failure = { code, message -> call.reject(message, code) },
) { call.resolve() }
}

@PluginMethod
fun nativeIslandsCommand(call: PluginCall) {
val envelope = call.data
if (!validateNativeIslands(call, NativeIslandsBridgeValidator.validateCommandOperation(envelope))) {
return
}
val host = nativeIslandsHost()
val island = envelope.opt("island") as String
val component = envelope.opt("islandType") as String
val method = envelope.opt("method") as String
val params = envelope.opt("params") as? JSONObject ?: JSONObject()
host.dispatchCommand(
island,
component,
method,
NativeIslandsCarrierJson.objectToMap(params),
NativeIslandsBridgeValidator.commandInstance(envelope),
) { result ->
val code = result.optString("code")
if (code.isNotEmpty()) {
call.reject(result.optString("error", code), code)
} else {
call.resolve()
}
nativeIslandsController.submitCommand(call.data, failure = { code, message -> call.reject(message, code) }) {
call.resolve()
}
}

@PluginMethod
fun nativeIslandsReset(call: PluginCall) {
if (
!validateNativeIslands(
call,
NativeIslandsBridgeValidator.validateOperation("reset", call.data),
)
) {
return
}
nativeIslandsHost().reset {
call.resolve(JSObject.fromJSONObject(NativeIslandsCapabilities.sessionCapabilities()))
}
}

private fun nativeIslandsHost(): NativeIslandsController {
nativeIslandsController?.let { host ->
host.bind(bridge.webView, activity)
return host
}
val host = NativeIslandsController { event, payload ->
notifyListeners("nativeIslands:$event", JSObject.fromJSONObject(payload))
}
nativeIslandsController = host
host.bind(bridge.webView, activity)
activity.runOnUiThread {
host.ensureContainer()
if (activity.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
host.onHostResume()
} else {
host.onHostPause()
}
nativeIslandsController.submitReset(call.data, failure = { code, message -> call.reject(message, code) }) {
call.resolve(JSObject.fromJSONObject(it))
}
return host
}

private fun validateNativeIslands(
call: PluginCall,
error: NativeIslandsBridgeValidationError?,
): Boolean =
if (error == null) {
true
} else {
call.reject(error.message, error.code)
false
}

override fun handleOnDestroy() {
super.handleOnDestroy()
coroutineScope.cancel()
Expand All @@ -302,19 +185,18 @@ class GeolocationPlugin : Plugin() {
if (::locationButtonPermissionLauncher.isInitialized) {
locationButtonPermissionLauncher.unregister()
}
nativeIslandsController?.dispose()
nativeIslandsController = null
nativeIslandsController.dispose()
IONGLOCLocationButtonRegistry.unregister(activity)
}

override fun handleOnResume() {
super.handleOnResume()
nativeIslandsController?.onHostResume()
nativeIslandsController.onHostResume()
}

override fun handleOnPause() {
super.handleOnPause()
nativeIslandsController?.onHostPause()
nativeIslandsController.onHostPause()
}

@PluginMethod
Expand Down Expand Up @@ -643,24 +525,3 @@ class GeolocationPlugin : Plugin() {
private fun PluginCall.getNumber(name: String, defaultValue: Long): Long =
getLong(name) ?: getInt(name)?.toLong() ?: defaultValue
}

private object NativeIslandsCarrierJson {
fun objectToMap(value: JSONObject): Map<String, Any?> {
val result = HashMap<String, Any?>()
val keys = value.keys()
while (keys.hasNext()) {
val key = keys.next()
result[key] = normalize(value.opt(key))
}
return result
}

private fun normalize(value: Any?): Any? = when (value) {
null, JSONObject.NULL -> null
is JSONObject -> objectToMap(value)
is JSONArray -> List(value.length()) { index ->
normalize(value.opt(index))
}
else -> value
}
}
2 changes: 1 addition & 1 deletion example-app-location-button/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 11 additions & 25 deletions src/location-button.ts
Comment thread
OS-pedrogustavobilro marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,7 @@ function clampedPixelStyle(
return Number.isFinite(number) ? Math.min(maximum, Math.max(minimum, number)) : fallback;
}

function nonNegativeIntegerAttribute(
element: HTMLElement,
name: string,
fallback: number,
minimum = 0,
): number {
function nonNegativeIntegerAttribute(element: HTMLElement, name: string, fallback: number, minimum = 0): number {
const value = element.getAttribute(name);
if (value === null) return fallback;
const number = Number.parseInt(value, 10);
Expand Down Expand Up @@ -180,28 +175,19 @@ function dispatchPosition(element: HTMLElement, position: LocationPosition): voi

async function requestNativeFallback(element: HTMLElement, geolocationPlugin: GeolocationPlugin): Promise<void> {
try {
const status = await geolocationPlugin.requestPermissions({
permissions: ['location'],
const position = await geolocationPlugin.getCurrentPosition({
enableHighAccuracy: true,
timeout: nonNegativeIntegerAttribute(element, 'timeout', 10_000, 1),
maximumAge: nonNegativeIntegerAttribute(element, 'maximum-age', 0),
enableLocationFallback: booleanAttribute(element, 'enable-location-fallback', true),
});
const granted = status.location === 'granted';
dispatch<LocationButtonGrantDetail>(element, 'location-grant', { granted });
if (!granted) {
dispatch<LocationButtonErrorDetail>(element, 'location-error', {
reason: 'Location permission request was denied',
});
return;
}
dispatchPosition(
element,
await geolocationPlugin.getCurrentPosition({
enableHighAccuracy: true,
timeout: nonNegativeIntegerAttribute(element, 'timeout', 10_000, 1),
maximumAge: nonNegativeIntegerAttribute(element, 'maximum-age', 0),
enableLocationFallback: booleanAttribute(element, 'enable-location-fallback', true),
}),
);
dispatch<LocationButtonGrantDetail>(element, 'location-grant', { granted: true });
dispatchPosition(element, position);
} catch (error) {
const code = (error as { code?: unknown } | undefined)?.code;
if (code === 'OS-PLUG-GLOC-0003' || code === 'OS-PLUG-GLOC-0008') {
dispatch<LocationButtonGrantDetail>(element, 'location-grant', { granted: false });
}
dispatch<LocationButtonErrorDetail>(element, 'location-error', {
reason: error instanceof Error && error.message ? error.message : 'Location request failed',
code: typeof code === 'string' ? code : undefined,
Expand Down
Binary file modified vendor/capacitor-native-islands-1.0.0.tgz
Binary file not shown.
Loading