Exemple #1
0
        /// <summary>
        /// Transforms the part of the textbox string "API{url,data}" to the data from the API
        /// </summary>
        /// <param name="reminderText">The reminder text</param>
        /// <returns>The reminder text with the API{} replaced with the actual API value</returns>
        private async void TransformAPITextToValue(string color, string reminderText, float previewSize = 0)
        {
            float size = previewSize == 0 ? BLLocalDatabase.PopupDimension.GetPopupDimensions().FontNoteSize : previewSize;

            if (!reminderText.Contains("API{"))
            {
                return;
            }

            try
            {
                int retryCount = 0;
                htmlLblText.Text = "<p style=\"color: " + color + "; font-size: " + Math.Round(size * 1.28) + "px;\">Loading...</p>";
                new Thread(async() =>
                {
                    startMethod:



                    try
                    {
                        //if !hasinternetaccess Thread.Sleep(250); , 8 times, then error

                        if (!BLIO.HasInternetAccess())
                        {
                            retryCount++;

                            if (retryCount <= 8)
                            {
                                Thread.Sleep(250);
                                goto startMethod;
                            }
                            else
                            {
                                htmlLblText.Invoke((MethodInvoker)(() =>
                                {
                                    htmlLblText.Text = "An error occured." + rem.Note;
                                }));

                                return;
                            }
                        }

                        retryCount = 0;

                        //Interner access!

                        int startIndex = reminderText.IndexOf("API{");
                        int endIndex   = -1;

                        bool found = false;
                        int count  = 1;
                        while (!found)
                        {
                            if (reminderText[startIndex + count] == '}')
                            {
                                endIndex = startIndex + count;
                                found    = true;
                            }
                            else
                            {
                                count++;
                            }
                        }

                        //[url, dataToPick]
                        string[] data    = (reminderText.Substring(startIndex + 4, endIndex - (startIndex + 4))).Split(',');
                        JObject response = await BLIO.HttpRequest("GET", data[0]);

                        //This is the API value the user is requesting. Replace API{url,data} with this.
                        string value = response.SelectTokens(data[1]).Select(t => t.Value <string>()).ToList()[0];

                        StringBuilder stringBuilder = new StringBuilder(reminderText);
                        stringBuilder.Remove(startIndex, endIndex - (startIndex) + 1);
                        stringBuilder.Insert(startIndex, value);
                        reminderText = stringBuilder.ToString();

                        //TODO if response.status != 200   stringBuilder.Insert(startIndex, "Error occured");
                        //Bitcoin: $60,001
                        //Superfarm: Error
                        //Reef: $0,04
                        //

                        //Still contains another API{} ? again...
                        if (reminderText.Contains("API{"))
                        {
                            goto startMethod;
                        }

                        htmlLblText.Invoke((MethodInvoker)(() =>
                        {
                            htmlLblText.Text = "<p style=\"color: " + color + "; font-size: " + Math.Round(size * 1.28) + "px;\">" + reminderText + "</p>";
                        }));
                        //return reminderText;
                    }
                    catch (Exception ex)
                    {
                        retryCount++;

                        if (retryCount <= 8)
                        {
                            Thread.Sleep(250);
                            goto startMethod;
                        }
                        else
                        {
                            htmlLblText.Text = "An error occured. (" + ex.GetType().ToString() + ")\r\n" + rem.Note;
                        }
                    }
                }).Start();
            }
            catch (Exception ex)
            {
                BLIO.WriteError(ex, "TransformAPITextToValue() failed with " + ex.GetType().ToString());
                //return reminderText;
            }
        }