Skip to content

Commit

Permalink
Merge pull request #8 from lytics/intermediary/interface/6-bridge-imp…
Browse files Browse the repository at this point in the history
…rovements

Native Bridge Improvements
  • Loading branch information
mgacy authored Jan 17, 2024
2 parents bf58295 + 33183a1 commit a0a6d73
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 44 deletions.
29 changes: 24 additions & 5 deletions android/src/main/java/com/lytics/react_native/LyticsModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lytics.react_native

import android.content.Context
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
Expand All @@ -14,26 +15,40 @@ import com.lytics.android.logging.LogLevel
import com.lytics.android.Lytics
import com.lytics.android.LyticsConfiguration
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.cancel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class LyticsModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
ReactContextBaseJavaModule(reactContext), LifecycleEventListener {

private val context: Context

private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

init {
context = reactContext.applicationContext
reactContext.addLifecycleEventListener(this)
}

override fun getName(): String {
return NAME
}

override fun onHostResume() {
// noop - overridden to implement `LifecycleEventListener`
}

override fun onHostPause() {
// noop - overridden to implement `LifecycleEventListener`
}

override fun onHostDestroy() {
scope.cancel()
}

// Properties

@ReactMethod
Expand Down Expand Up @@ -191,12 +206,16 @@ class LyticsModule(reactContext: ReactApplicationContext) :
// Personalization

@ReactMethod
fun getProfile(name: String? = null, value: String? = null, promise: Promise) {
fun getProfile(
identifierName: String? = null,
identifierValue: String? = null,
promise: Promise
) {
var identifier: EntityIdentifier? = null
if (name != null && value != null) {
if (identifierName != null && identifierValue != null) {
identifier = EntityIdentifier(
name = name,
value = value
name = identifierName,
value = identifierValue
)
}
scope.launch {
Expand Down
20 changes: 10 additions & 10 deletions ios/LyticsBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ @interface RCT_EXTERN_MODULE(LyticsBridge, NSObject)
#pragma mark - Properties

RCT_EXTERN_METHOD(hasStarted:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(isOptedIn:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(isTrackingEnabled:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(user:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

#pragma mark - Configuration

RCT_EXTERN_METHOD(start:(NSDictionary<NSString *,id> *) configuration
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)

#pragma mark - Events

Expand Down Expand Up @@ -60,8 +60,8 @@ @interface RCT_EXTERN_MODULE(LyticsBridge, NSObject)

RCT_EXTERN_METHOD(getProfile:(NSString *) identifierName
identifierValue:(NSString *) identifierValue
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)

#pragma mark - Tracking

Expand All @@ -70,14 +70,14 @@ @interface RCT_EXTERN_MODULE(LyticsBridge, NSObject)
RCT_EXTERN_METHOD(optOut)

RCT_EXTERN_METHOD(requestTrackingAuthorization:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(disableTracking)

#pragma mark - Utility

RCT_EXTERN_METHOD(identifier:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(dispatch)

Expand Down
67 changes: 38 additions & 29 deletions ios/LyticsBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ public final class LyticsBridge: NSObject {

// MARK: - Properties

@objc(hasStarted:reject:)
public func hasStarted(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
@objc(hasStarted:rejecter:)
public func hasStarted(
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) {
resolve(lytics.hasStarted)
}

@objc(isOptedIn:reject:)
public func isOptedIn(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
@objc(isOptedIn:rejecter:)
public func isOptedIn(
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) {
resolve(lytics.isOptedIn)
}

@objc(isTrackingEnabled:reject:)
public func isTrackingEnabled(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
@objc(isTrackingEnabled:rejecter:)
public func isTrackingEnabled(
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) {
resolve(lytics.isIDFAEnabled)
}

@objc(user:reject:)
@objc(user:rejecter:)
public func user(
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
Task {
let user = await lytics.user
Expand All @@ -56,11 +65,11 @@ public final class LyticsBridge: NSObject {

// MARK: - Configuration

@objc(start:resolve:reject:)
@objc(start:resolver:rejecter:)
public func start(
configuration: [String: Any],
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
guard let apiToken = configuration["apiToken"] as? String else {
reject("invalid_api_token", "Missing or Invalid API Token", nil)
Expand Down Expand Up @@ -146,8 +155,8 @@ public final class LyticsBridge: NSObject {

@objc(track:name:identifiers:properties:)
public func track(
stream: String? = nil,
name: String? = nil,
stream: String?,
name: String?,
identifiers: [String: Any],
properties: [String: Any]
) {
Expand All @@ -160,8 +169,8 @@ public final class LyticsBridge: NSObject {

@objc(identify:name:identifiers:attributes:shouldSend:)
public func identify(
stream: String? = nil,
name: String? = nil,
stream: String?,
name: String?,
identifiers: [String: Any],
attributes: [String: Any],
shouldSend: Bool = true
Expand All @@ -176,8 +185,8 @@ public final class LyticsBridge: NSObject {

@objc(consent:name:identifiers:attributes:consent:shouldSend:)
public func consent(
stream: String? = nil,
name: String? = nil,
stream: String?,
name: String?,
identifiers: [String: Any],
attributes: [String: Any],
consent: [String: Any],
Expand All @@ -193,8 +202,8 @@ public final class LyticsBridge: NSObject {

@objc(screen:name:identifiers:properties:)
public func screen(
stream: String? = nil,
name: String? = nil,
stream: String?,
name: String?,
identifiers: [String: Any],
properties: [String: Any]
) {
Expand All @@ -207,12 +216,12 @@ public final class LyticsBridge: NSObject {

// MARK: - Personalization

@objc(getProfile:identifierValue:resolve:reject:)
@objc(getProfile:identifierValue:resolver:rejecter:)
public func getProfile(
identifierName: String?,
identifierValue: String?,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
Task {
do {
Expand Down Expand Up @@ -243,10 +252,10 @@ public final class LyticsBridge: NSObject {
lytics.optOut()
}

@objc(requestTrackingAuthorization:reject:)
@objc(requestTrackingAuthorization:rejecter:)
public func requestTrackingAuthorization(
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
Task {
resolve(await lytics.requestTrackingAuthorization())
Expand All @@ -260,10 +269,10 @@ public final class LyticsBridge: NSObject {

// MARK: - Utility

@objc(identifier:reject:)
@objc(identifier:rejecter:)
public func identifier(
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
resolve(lytics.identifier())
}
Expand Down

0 comments on commit a0a6d73

Please sign in to comment.