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 2 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,9 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}

@ReactMethod
override fun actionRun(name: String?, value: ReadableMap?, promise: Promise) {
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 @@ -240,8 +240,7 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
@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
6 changes: 3 additions & 3 deletions ios/AirshipReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ 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? {
return try await AirshipProxy.shared.action.runAction(
actionName,
value: try AirshipJSON.wrap(actionValue)
action["_name"] as! String,
value: action["_value"] is NSNull ? nil : try AirshipJSON.wrap(action["_value"])
Copy link
Contributor Author

@Ulrico972 Ulrico972 Apr 5, 2024

Choose a reason for hiding this comment

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

I had to make sure I pass nil if it's a NSNull.
Otherwise the JSON wrapping fails: the NSNull Object don't go through the guard else, is that normal @crow ?

public static func wrap(_ value: Any?, encoder: JSONEncoder = AirshipJSON.defaultEncoder) throws -> AirshipJSON {
    guard let value = value else {
        return .null
    }
    .
    .
    .
}

)
}
}
Expand Down
5 changes: 2 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
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.

34 changes: 34 additions & 0 deletions src/Action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright Airship and Contributors */

'use strict';

import { JsonValue } from './types';

/**
* Airship Action Object.
* This is used to encapsulate the Action name and the Action value.
*/
export class Action {
Ulrico972 marked this conversation as resolved.
Show resolved Hide resolved
_name: string;
_value?: JsonValue;

/**
* Airship Action constructor.
*
* @param name The action name.
* @param value The action value.
*/
constructor(name: string, value?: JsonValue) {
this._name = name;
this._value = value;
}

/**
* Sets the action value.
*
* @param value The action value.
*/
setValue(value?: JsonValue) {
this._value = value;
}
}
9 changes: 4 additions & 5 deletions src/AirshipActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JsonValue } from './types';
import { Action } from './Action';

/**
* Airship actions.
Expand All @@ -9,15 +10,13 @@ export class AirshipActions {
/**
* Runs an Airship action.
*
* @param name The name of the action.
* @param value The action's value.
* @param action The Airship Action.
* @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
action: Action
): Promise<JsonValue | null | undefined> {
return this.module.actionRun(actionName, actionValue);
return this.module.actionRun(action);
}
}
10 changes: 3 additions & 7 deletions src/AirshipAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Action } from "./Action";
import { CustomEvent } from "./CustomEvent";

/**
Expand Down Expand Up @@ -33,15 +34,10 @@ export class AirshipAnalytics {
* 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
}
let action = new Action("add_custom_event_action", event.toJsonValue())

return new Promise((resolve, reject) => {
this.module.actionRun("add_custom_event_action", actionArg).then(() => {
this.module.actionRun(action).then(() => {
resolve(null)
}, (error: Error) => {
reject(error)
Expand Down
18 changes: 18 additions & 0 deletions src/CustomEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ 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.event_name = this._name;
if (this._value) {
jsonObject.event_value = this._value;
}
jsonObject.properties = this._properties;
if (this._transactionId) {
jsonObject.transaction_id = this._transactionId;
}
return jsonObject;
}
}
2 changes: 1 addition & 1 deletion src/NativeRTNAirship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface Spec extends TurboModule {
analyticsAssociateIdentifier(key: string, identifier?: string): 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
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { SubscriptionListEditor } from './SubscriptionListEditor';
export { TagGroupEditor } from './TagGroupEditor';
export { ScopedSubscriptionListEditor } from './ScopedSubscriptionListEditor';
export { AttributeEditor } from './AttributeEditor';
export { Action } from './Action';

export * from './types';
export * from './MessageView';
Expand Down
Loading