Exemplo n.º 1
0
        internal static bool UpdateApiConfig(object jsonResponse)
        {
            try
            {
                int numResponses = NumResponses(jsonResponse);

                for (int i = 0; i < numResponses; i++)
                {
                    IDictionary <string, object> response = GetResponseAt(jsonResponse, i) as IDictionary <string, object>;

                    if (IsResponseSuccess(response))
                    {
                        continue;
                    }

                    string apiHost       = Util.GetValueOrDefault(response, Constants.Params.API_HOST) as string;
                    string apiPath       = Util.GetValueOrDefault(response, Constants.Params.API_PATH) as string;
                    string devServerHost = Util.GetValueOrDefault(response, Constants.Params.DEV_SERVER_HOST) as string;
                    // Prevent setting the same API config and request retry loop
                    bool configUpdated = false;

                    bool hasNewApiHost    = !string.IsNullOrEmpty(apiHost) && !apiHost.Equals(Leanplum.ApiConfig.ApiHost);
                    bool hasNewApiPath    = !string.IsNullOrEmpty(apiPath) && !apiHost.Equals(Leanplum.ApiConfig.ApiHost);
                    bool hasNewSocketHost = !string.IsNullOrEmpty(devServerHost) && !apiHost.Equals(Leanplum.ApiConfig.SocketHost);

                    // API config
                    if (hasNewApiHost || hasNewApiPath)
                    {
                        configUpdated = true;
                        if (string.IsNullOrEmpty(apiHost))
                        {
                            apiHost = Leanplum.ApiConfig.ApiHost;
                        }
                        if (string.IsNullOrEmpty(apiPath))
                        {
                            apiPath = Leanplum.ApiConfig.ApiPath;
                        }

                        LeanplumNative.CompatibilityLayer.LogDebug($"Changing API endpoint to {apiHost}/{apiPath}");
                        Leanplum.SetApiConnectionSettings(apiHost, apiPath, Leanplum.ApiConfig.ApiSSL);
                    }

                    // Socket config
                    if (hasNewSocketHost)
                    {
                        configUpdated = true;
                        int socketPort = Leanplum.ApiConfig.SocketPort;
                        LeanplumNative.CompatibilityLayer.LogDebug($"Changing socket to {devServerHost}:{socketPort}");
                        Leanplum.SetSocketConnectionSettings(devServerHost, socketPort);
                    }

                    return(configUpdated);
                }
            }
            catch (Exception e)
            {
                LeanplumNative.CompatibilityLayer.LogError("Error parsing response for API config", e);
            }
            return(false);
        }
        public static void DefineGenericDefinition()
        {
            string     configVars = $"{Constants.Args.GENERIC_DEFINITION_CONFIG}.vars";
            ActionArgs args       = new ActionArgs()
                                    .With <IDictionary <string, object> >(Constants.Args.GENERIC_DEFINITION_CONFIG, null)
                                    .With <IDictionary <string, object> >(configVars, null);

            ActionContext.ActionResponder responder = new ActionContext.ActionResponder((context) =>
            {
                var messageConfig                 = context.GetObjectNamed <Dictionary <string, object> >(Constants.Args.GENERIC_DEFINITION_CONFIG);
                var messageVars                   = context.GetObjectNamed <Dictionary <string, object> >(configVars);
                StringBuilder builder             = new StringBuilder();
                NativeActionContext nativeContext = context as NativeActionContext;
                if (nativeContext != null && !string.IsNullOrEmpty(nativeContext.Id))
                {
                    builder.AppendLine($"Message Id: {nativeContext.Id}");
                }
                BuildString("message",
                            messageConfig, builder, 0);

                EditorUtility.DisplayDialog(context.Name, builder.ToString(), null);
            });

            Leanplum.DefineAction(Constants.Args.GENERIC_DEFINITION_NAME, Constants.ActionKind.MESSAGE, args, null, responder);
        }
        public static void DefineConfirm()
        {
            ActionArgs actionArgs = new ActionArgs();

            actionArgs.With <string>(Constants.Args.MESSAGE, "Confirm message");
            actionArgs.With <string>(Constants.Args.ACCEPT_TEXT, "Accept");
            actionArgs.With <string>(Constants.Args.CANCEL_TEXT, "Cancel");

            actionArgs.WithAction <object>(Constants.Args.ACCEPT_ACTION, null)
            .WithAction <object>(Constants.Args.CANCEL_ACTION, null);

            ActionContext.ActionResponder responder = new ActionContext.ActionResponder((context) =>
            {
                if (EditorUtility.DisplayDialog(Constants.Args.CONFIRM_NAME,
                                                context.GetStringNamed(Constants.Args.MESSAGE),
                                                context.GetStringNamed(Constants.Args.ACCEPT_TEXT),
                                                context.GetStringNamed(Constants.Args.CANCEL_TEXT)))
                {
                    context.RunTrackedActionNamed(Constants.Args.ACCEPT_ACTION);
                }
                else
                {
                    context.RunActionNamed(Constants.Args.CANCEL_ACTION);
                }
            });

            Leanplum.DefineAction(Constants.Args.CONFIRM_NAME, Constants.ActionKind.MESSAGE, actionArgs, null, responder);
        }
Exemplo n.º 4
0
        private void Update()
        {
            // Workaround so that CheckVarsUpdate() is invoked on Unity's main thread.
            // This is called by Unity on every frame.
            if (VarCache.VarsNeedUpdate && developerModeEnabled && Leanplum.HasStarted)
            {
                Leanplum.ForceContentUpdate();
            }

            // Run deferred actions.
            List <Action> actions = null;

            lock (delayed)
            {
                if (delayed.Count > 0)
                {
                    actions = new List <Action>(delayed);
                    delayed.Clear();
                }
            }
            if (actions != null)
            {
                foreach (Action action in actions)
                {
                    action();
                }
            }
        }
        public static void DefineOpenURL()
        {
            ActionArgs actionArgs = new ActionArgs();

            actionArgs.With <string>(Constants.Args.URL, "https://www.example.com");

            ActionContext.ActionResponder responder = new ActionContext.ActionResponder((context) =>
            {
                string url = context.GetStringNamed(Constants.Args.URL);
                if (!string.IsNullOrEmpty(url))
                {
                    Application.OpenURL(url);
                }
            });

            Leanplum.DefineAction(Constants.Args.OPEN_URL, Constants.ActionKind.ACTION, actionArgs, null, responder);
        }