Ejemplo n.º 1
0
        public override void NativeCallback(string message)
        {
            const string VARIABLES_CHANGED = "VariablesChanged:";
            const string VARIABLES_CHANGED_NO_DOWNLOAD_PENDING = "VariablesChangedAndNoDownloadsPending:";
            const string STARTED = "Started:";
            const string VARIABLE_VALUE_CHANGED             = "VariableValueChanged:";
            const string FORCE_CONTENT_UPDATE_WITH_CALLBACK = "ForceContentUpdateWithCallback:";
            const string ACTION_RESPONDER = "ActionResponder:";

            if (message.StartsWith(VARIABLES_CHANGED))
            {
                VariablesChanged?.Invoke();
            }
            else if (message.StartsWith(VARIABLES_CHANGED_NO_DOWNLOAD_PENDING))
            {
                VariablesChangedAndNoDownloadsPending?.Invoke();
            }
            else if (message.StartsWith(STARTED))
            {
                if (Started != null)
                {
                    bool success = message.EndsWith("true") || message.EndsWith("True");
                    Started(success);
                }
            }
            else if (message.StartsWith(VARIABLE_VALUE_CHANGED))
            {
                // Drop the beginning of the message to get the name of the variable
                // Then dispatch to the correct variable
                LeanplumAndroid.VariableValueChanged(message.Substring(21));
            }
            else if (message.StartsWith(FORCE_CONTENT_UPDATE_WITH_CALLBACK))
            {
                int    key = Convert.ToInt32(message.Substring(FORCE_CONTENT_UPDATE_WITH_CALLBACK.Length));
                Action callback;
                if (ForceContentUpdateCallbackDictionary.TryGetValue(key, out callback))
                {
                    callback();
                    ForceContentUpdateCallbackDictionary.Remove(key);
                }
            }
            else if (message.StartsWith(ACTION_RESPONDER))
            {
                string key = message.Substring(ACTION_RESPONDER.Length);
                // {actionName:messageId}
                string actionName = key.Split(':')[0];

                ActionContext.ActionResponder callback;
                if (ActionRespondersDictionary.TryGetValue(actionName, out callback))
                {
                    var context = new ActionContextAndroid(key);
                    callback(context);
                }
            }

            if (Inbox != null)
            {
                Inbox.NativeCallback(message);
            }
        }
Ejemplo n.º 2
0
 public override ActionContext CreateActionContextForId(string actionId)
 {
     if (!string.IsNullOrEmpty(actionId))
     {
         string key       = NativeSDK.CallStatic <string>("createActionContextForId", actionId);
         string messageId = GetMessageIdFromMessageKey(key);
         var    context   = new ActionContextAndroid(key, messageId);
         ActionContextsDictionary[key] = context;
         return(context);
     }
     return(null);
 }
Ejemplo n.º 3
0
        public override void NativeCallback(string message)
        {
            const string VARIABLES_CHANGED = "VariablesChanged:";
            const string VARIABLES_CHANGED_NO_DOWNLOAD_PENDING = "VariablesChangedAndNoDownloadsPending:";
            const string STARTED = "Started:";
            const string VARIABLE_VALUE_CHANGED             = "VariableValueChanged:";
            const string FORCE_CONTENT_UPDATE_WITH_CALLBACK = "ForceContentUpdateWithCallback:";
            const string DEFINE_ACTION_RESPONDER            = "ActionResponder:";
            const string ON_ACTION_RESPONDER        = "OnAction:";
            const string RUN_ACTION_NAMED_RESPONDER = "OnRunActionNamed:";

            if (message.StartsWith(VARIABLES_CHANGED))
            {
                VariablesChanged?.Invoke();
            }
            else if (message.StartsWith(VARIABLES_CHANGED_NO_DOWNLOAD_PENDING))
            {
                VariablesChangedAndNoDownloadsPending?.Invoke();
            }
            else if (message.StartsWith(STARTED))
            {
                if (started != null)
                {
                    startSuccessful = message.EndsWith("true") || message.EndsWith("True");
                    started(startSuccessful);
                }
            }
            else if (message.StartsWith(VARIABLE_VALUE_CHANGED))
            {
                // Drop the beginning of the message to get the name of the variable
                // Then dispatch to the correct variable
                LeanplumAndroid.VariableValueChanged(message.Substring(21));
            }
            else if (message.StartsWith(FORCE_CONTENT_UPDATE_WITH_CALLBACK))
            {
                string[] values  = message.Substring(FORCE_CONTENT_UPDATE_WITH_CALLBACK.Length).Split(':');
                int      key     = Convert.ToInt32(values[0]);
                bool     success = values[1] == "1";
                if (ForceContentUpdateCallbacksDictionary.TryGetValue(key, out Leanplum.ForceContentUpdateHandler callback))
                {
                    callback(success);
                    ForceContentUpdateCallbacksDictionary.Remove(key);
                }
            }
            else if (message.StartsWith(DEFINE_ACTION_RESPONDER))
            {
                string key = message.Substring(DEFINE_ACTION_RESPONDER.Length);
                // {actionName:messageId}
                string actionName = key.Split(':')[0];

                ActionContext.ActionResponder callback;
                if (ActionRespondersDictionary.TryGetValue(actionName, out callback))
                {
                    string messageId = key.Length > actionName.Length ? key.Substring(actionName.Length + 1) : string.Empty;
                    var    context   = new ActionContextAndroid(key, messageId);
                    ActionContextsDictionary[key] = context;
                    callback(context);
                }
            }
            else if (message.StartsWith(ON_ACTION_RESPONDER))
            {
                string key        = message.Substring(ON_ACTION_RESPONDER.Length);
                string actionName = GetActionNameFromMessageKey(key);

                if (OnActionRespondersDictionary.TryGetValue(actionName, out List <ActionContext.ActionResponder> callbacks))
                {
                    if (!ActionContextsDictionary.ContainsKey(key))
                    {
                        string messageId  = GetMessageIdFromMessageKey(key);
                        var    newContext = new ActionContextAndroid(key, messageId);
                        ActionContextsDictionary[key] = newContext;
                    }

                    ActionContext context = ActionContextsDictionary[key];
                    foreach (var callback in callbacks)
                    {
                        callback(context);
                    }
                }
            }
            else if (message.StartsWith(RUN_ACTION_NAMED_RESPONDER))
            {
                char   keysSeparator = '|';
                string data          = message.Substring(RUN_ACTION_NAMED_RESPONDER.Length);

                string[] keys = data.Split(new char[] { keysSeparator }, StringSplitOptions.RemoveEmptyEntries);
                if (keys.Length != 2)
                {
                    return;
                }

                string parentKey = keys[0];
                string actionKey = keys[1];

                if (ActionContextsDictionary.TryGetValue(parentKey, out ActionContext parentContext))
                {
                    var context = new ActionContextAndroid(actionKey, GetMessageIdFromMessageKey(actionKey));
                    parentContext.TriggerActionNamedResponder(context);
                }
            }

            if (Inbox != null)
            {
                Inbox.NativeCallback(message);
            }
        }