Skip to content

Commit df39ada

Browse files
authored
Merge pull request #90 from Leanplum/inbox-open-action
[SDK-293] Implement Native Inbox Open Action
2 parents 93f092c + 374f656 commit df39ada

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

Leanplum-Unity-SDK/Assets/LeanplumSDK/Native/LeanplumInboxNative.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,7 @@ public override void Read(string messageId)
8888

8989
if (msg != null)
9090
{
91-
msg.IsRead = true;
92-
93-
var param = new Dictionary<string, string>
94-
{
95-
[Constants.Params.INBOX_MESSAGE_ID] = messageId
96-
};
97-
98-
LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.MARK_INBOX_MESSAGE_AS_READ, param);
99-
request.SendIfConnected();
91+
Read(msg);
10092
}
10193
}
10294
}
@@ -105,26 +97,39 @@ public override void Read(Message message)
10597
{
10698
if (message != null)
10799
{
108-
Read(message.Id);
100+
MarkAsRead(message);
101+
Open(message);
109102
}
110103
}
111104

112105
internal override void MarkAsRead(string messageId)
113106
{
114107
if (messageId != null)
115108
{
116-
117-
}
109+
var msg = _messages.Find(message => message.Id == messageId);
118110

119-
InboxChanged?.Invoke();
111+
if (msg != null)
112+
{
113+
MarkAsRead(msg);
114+
}
115+
}
120116
}
121117

122118
internal override void MarkAsRead(Message message)
123119
{
124120
if (message != null)
125121
{
126-
MarkAsRead(message.Id);
122+
message.IsRead = true;
123+
124+
var param = new Dictionary<string, string>
125+
{
126+
[Constants.Params.INBOX_MESSAGE_ID] = message.Id
127+
};
128+
129+
LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.MARK_INBOX_MESSAGE_AS_READ, param);
130+
request.SendIfConnected();
127131
}
132+
InboxChanged?.Invoke();
128133
}
129134

130135
public override void Remove(string messageId)
@@ -179,7 +184,6 @@ public override void DownloadMessages(OnForceContentUpdate completedHandler)
179184
var response = Util.GetLastResponse(data) as IDictionary<string, object>;
180185
var messages = Util.GetValueOrDefault(response, Constants.Keys.INBOX_MESSAGES) as IDictionary<string, object>;
181186
var inboxMessages = new List<Message>();
182-
183187
if (messages != null)
184188
{
185189
foreach (var pair in messages)
@@ -271,6 +275,22 @@ public override void DownloadMessages(OnForceContentUpdate completedHandler)
271275
request.SendIfConnected();
272276
}
273277

278+
internal void Open(Message message)
279+
{
280+
var messageVars = message.ActionContext;
281+
var openAction = Util.GetValueOrDefault(messageVars, Constants.Args.OPEN_ACTION) as IDictionary<string, object>;
282+
if (openAction != null)
283+
{
284+
// The inbox message id is with format <messageId>##<instance>
285+
string id = message.Id.Split(new string[] { "##" }, StringSplitOptions.RemoveEmptyEntries)[0];
286+
287+
string actionName = Util.GetValueOrDefault(messageVars, Constants.Args.ACTION_NAME, string.Empty)?.ToString();
288+
289+
var actionContext = new NativeActionContext(id, actionName, messageVars);
290+
actionContext.RunTrackedActionNamed(Constants.Args.OPEN_ACTION);
291+
}
292+
}
293+
274294
internal void UpdateMessages(List<Message> messages)
275295
{
276296
lock(_messages)

Leanplum-Unity-SDK/Assets/LeanplumSDK/Native/NativeActionContext.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,30 @@ public override void MuteForFutureMessagesOfSameKind()
128128

129129
public override void RunActionNamed(string name)
130130
{
131-
IDictionary<object, object> action = Traverse(name) as IDictionary<object, object>;
132-
if (action != null)
131+
object actionObject = Traverse(name);
132+
133+
Dictionary<string, object> actionData = null;
134+
if (actionObject != null)
133135
{
134-
var actionName = action[Constants.Args.ACTION_NAME];
135-
if (!string.IsNullOrEmpty(actionName?.ToString()))
136+
if (actionObject is IDictionary<object, object>)
136137
{
137-
Dictionary<string, object> actions = action.ToDictionary(kv => kv.Key.ToString(),
138-
kv => kv.Value);
138+
var actionDataObj = actionObject as IDictionary<object, object>;
139+
actionData = actionDataObj.ToDictionary(kv => kv.Key.ToString(), kv => kv.Value);
140+
}
141+
else if (actionObject is IDictionary<string, object>)
142+
{
143+
actionData = actionObject as Dictionary<string, object>;
144+
}
145+
}
139146

140-
if (actionName.Equals(Constants.Args.CHAIN_TO_EXISTING) && actions.ContainsKey(Constants.Args.CHAIN_MESSAGE))
147+
if (actionData != null)
148+
{
149+
var actionName = actionData[Constants.Args.ACTION_NAME];
150+
if (!string.IsNullOrEmpty(actionName?.ToString()))
151+
{
152+
if (actionName.Equals(Constants.Args.CHAIN_TO_EXISTING) && actionData.ContainsKey(Constants.Args.CHAIN_MESSAGE))
141153
{
142-
string messageId = actions[Constants.Args.CHAIN_MESSAGE]?.ToString();
154+
string messageId = actionData[Constants.Args.CHAIN_MESSAGE]?.ToString();
143155
if (!Leanplum.ShowMessage(messageId))
144156
{
145157
// Try to fetch the chained message if not on the device
@@ -151,8 +163,12 @@ public override void RunActionNamed(string name)
151163
}
152164
else
153165
{
154-
NativeActionContext actionContext = new NativeActionContext(null, actionName.ToString(), actions);
155-
LeanplumActionManager.TriggerAction(actionContext, actions);
166+
// The Actions default values are not merged when the message is merged
167+
// Merge now when the action is to be triggered
168+
var mergedActions = VarCache.MergeMessage(actionData);
169+
170+
NativeActionContext actionContext = new NativeActionContext(null, actionName.ToString(), mergedActions);
171+
LeanplumActionManager.TriggerAction(actionContext, mergedActions);
156172
}
157173
}
158174
}

Leanplum-Unity-SDK/Assets/LeanplumSDK/Native/VarCache.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ internal static void MergeMessages(IDictionary<string, object> messages)
396396
var messageConfig = Util.GetValueOrDefault(values, Constants.Args.VARS) as Dictionary<string, object>;
397397
if (messageConfig != null)
398398
{
399+
// Merges the default argument values defined in the Action Definition with the values coming from the API.
400+
// The API returns only the modified values from the dashboard.
401+
// It does NOT merge Actions which could be part of the Action Definition since
402+
// the Actions are themselves Action Definitions.
403+
// i.e Confirm -> Accept action -> Open URL
404+
// The Confirm Action Definition contains the Accept action ->
405+
// The Accept action itself has an Action Definition with default values.
406+
// The last are not merged here but when the action is triggered.
399407
var mergedVars = MergeHelper(actionDefinitions[name].Vars, messageConfig);
400408
var mergedVarsDict = mergedVars as Dictionary<object, object>;
401409
var newVars = mergedVarsDict.ToDictionary(item => item.Key.ToString(), item => item.Value);

Leanplum-Unity-SDK/Assets/LeanplumSDK/Utilities/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public class Args
175175
public const string CLOSE_URL = "Close URL";
176176
public const string HAS_DISMISS_BUTTON = "Has dismiss button";
177177
public const string OPEN_URL = "Open URL";
178+
public const string OPEN_ACTION = "Open action";
178179
public const string TRACK_URL = "Track URL";
179180
public const string ACTION_URL = "Action URL";
180181
public const string TRACK_ACTION_URL = "Track Action URL";

0 commit comments

Comments
 (0)