Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOBILE-4239] Fix Airship Actions running #557

Merged
merged 7 commits into from
Apr 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,16 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}

@ReactMethod
override fun actionRun(name: String?, value: ReadableMap?, promise: Promise) {
override fun addCustomEvent(event: ReadableMap?, promise: Promise) {
promise.resolveResult {
proxy.analytics.addEvent(Utils.convertMap(event).toJsonValue())
}
}

@ReactMethod
override fun actionRun(action: ReadableMap, promise: Promise) {
promise.resolveDeferred<ActionValue> { callback ->
proxy.actions.runAction(requireNotNull(name), Utils.convertMap(value).toJsonValue())
proxy.actions.runAction(requireNotNull(action.getString("name")), Utils.convertDynamic(action.getDynamic("value")))
.addResultCallback { actionResult ->
if (actionResult != null && actionResult.status == ActionResult.STATUS_COMPLETED) {
callback(actionResult.value, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,17 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
promise: Promise
)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun addCustomEvent(
event: ReadableMap?,
promise: Promise
)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun actionRun(
name: String?,
value: ReadableMap?,
action: ReadableMap,
promise: Promise
)

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ PODS:
- React-Mapbuffer (0.73.4):
- glog
- React-debug
- react-native-airship (17.2.0):
- react-native-airship (17.2.1):
- AirshipFrameworkProxy (= 5.1.1)
- glog
- RCT-Folly (= 2022.05.16.00)
Expand Down Expand Up @@ -1407,7 +1407,7 @@ SPEC CHECKSUMS:
React-jsinspector: 9ac353eccf6ab54d1e0a33862ba91221d1e88460
React-logger: 0a57b68dd2aec7ff738195f081f0520724b35dab
React-Mapbuffer: 63913773ed7f96b814a2521e13e6d010282096ad
react-native-airship: ba50cd2630247d4896f65d6aeb9d7e94ec93ee08
react-native-airship: 876b0976076f1f85a8dc3722669db2702accfe68
react-native-safe-area-context: b97eb6f9e3b7f437806c2ce5983f479f8eb5de4b
React-nativeconfig: d7af5bae6da70fa15ce44f045621cf99ed24087c
React-NativeModulesApple: 0123905d5699853ac68519607555a9a4f5c7b3ac
Expand Down
14 changes: 11 additions & 3 deletions ios/AirshipReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,14 @@ public extension AirshipReactNative {
// Actions
@objc
public extension AirshipReactNative {
func actionsRun(actionName: String, actionValue: Any?) async throws-> Any? {
func actionsRun(action: [String: Any]) async throws-> Any? {
guard let name = action["name"] as? String else {
throw AirshipErrors.error("missing name")
}

return try await AirshipProxy.shared.action.runAction(
actionName,
value: try AirshipJSON.wrap(actionValue)
name,
value: action["value"] is NSNull ? nil : try AirshipJSON.wrap(action["value"])
)
}
}
Expand All @@ -350,6 +354,10 @@ public extension AirshipReactNative {
key: key
)
}

func addCustomEvent(_ json: Any) throws {
try AirshipProxy.shared.analytics.addEvent(json)
}
}

// Contact
Expand Down
16 changes: 13 additions & 3 deletions ios/RTNAirship.mm
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,10 @@ + (BOOL)requiresMainQueueSetup {
}

RCT_REMAP_METHOD(actionRun,
actionRun:(NSString *)name value:(NSDictionary *)value
actionRun:(NSDictionary *)action
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
[AirshipReactNative.shared actionsRunWithActionName:name
actionValue:value
[AirshipReactNative.shared actionsRunWithAction:action
completionHandler:^(id result , NSError *error) {


Expand Down Expand Up @@ -375,6 +374,17 @@ + (BOOL)requiresMainQueueSetup {
[self handleResult:nil error:error resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(addCustomEvent,
addCustomEvent:(NSDictionary *)event
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSError *error;
[AirshipReactNative.shared addCustomEvent:event
error:&error];

[self handleResult:nil error:error resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(contactEditAttributes,
contactEditAttributes:(NSArray *)operations
resolve:(RCTPromiseResolveBlock)resolve
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions src/AirshipActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export class AirshipActions {
/**
* Runs an Airship action.
*
* @param name The name of the action.
* @param value The action's value.
* @param actionName The name of the action.
* @param actionValue The action's value.
* @return A promise that returns the action result if the action
* successfully runs, or the Error if the action was unable to be run.
*/
public run(
Ulrico972 marked this conversation as resolved.
Show resolved Hide resolved
actionName: string,
actionValue?: JsonValue
): Promise<JsonValue | null | undefined> {
return this.module.actionRun(actionName, actionValue);
return this.module.actionRun({name: actionName, value: actionValue});
}
}
17 changes: 2 additions & 15 deletions src/AirshipAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ export class AirshipAnalytics {
* @return A promise that returns null if resolved, or an Error if the
* custom event is rejected.
*/
public addCustomEvent(event: CustomEvent): Promise<null | Error> {
const actionArg = {
Ulrico972 marked this conversation as resolved.
Show resolved Hide resolved
event_name: event._name,
event_value: event._value,
transaction_id: event._transactionId,
properties: event._properties
}

return new Promise((resolve, reject) => {
this.module.actionRun("add_custom_event_action", actionArg).then(() => {
resolve(null)
}, (error: Error) => {
reject(error)
})
})
public addCustomEvent(event: CustomEvent): Promise<void> {
return this.module.addCustomEvent(event.toJsonValue());
}
}
54 changes: 54 additions & 0 deletions src/CustomEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class CustomEvent {
_value?: number;
_properties: JsonObject;
_transactionId?: string;
_interactionId?: string;
_interactionType?: string;

/**
* Custom event constructor.
Expand Down Expand Up @@ -39,6 +41,34 @@ export class CustomEvent {
this._transactionId = value;
}

/**
* Gets the event's interaction ID.
*/
get interactionId(): string | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not particularly, I noticed we handle those in the proxy so I just added them

return this._interactionId;
}

/**
* Sets the event's interaction ID.
*/
set interactionId(value: string | undefined) {
this._interactionId = value;
}

/**
* Gets the event's interaction Type.
*/
get interactionType(): string | undefined {
return this._interactionType;
}

/**
* Sets the event's interaction Type.
*/
set interactionType(value: string | undefined) {
this._interactionType = value;
}

/**
* Adds a property to the custom event.
*
Expand All @@ -48,4 +78,28 @@ export class CustomEvent {
addProperty(name: string, value: JsonValue) {
this._properties[name] = value;
}

/**
* Converts a CustomEvent into a JsonValue.
*
* @returns A JsonValue.
*/
toJsonValue(): JsonValue {
let jsonObject: JsonObject = {};
jsonObject.eventName = this._name;
if (this._value) {
jsonObject.eventValue = this._value;
}
jsonObject.properties = this._properties;
if (this._transactionId) {
jsonObject.transactionId = this._transactionId;
}
if (this._interactionId) {
jsonObject.interactionId = this._interactionId;
}
if (this._interactionType) {
jsonObject.interactionType = this._interactionType;
}
return jsonObject;
}
}
3 changes: 2 additions & 1 deletion src/NativeRTNAirship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ export interface Spec extends TurboModule {
// Analytics
analyticsTrackScreen(screen?: string): Promise<void>;
analyticsAssociateIdentifier(key: string, identifier?: string): Promise<void>;
addCustomEvent(event: Object): Promise<void>;

// Action
actionRun(name: string, value?: Object): Promise<Object | Error>;
actionRun(action: Object): Promise<Object | Error>;

// Privacy Manager
privacyManagerSetEnabledFeatures(features: string[]): Promise<void>;
Expand Down
Loading