Exemplo n.º 1
0
        private async void DoSearch()
        {
            SetInfo(null, false);
            ClearImages();
            string text = txtText.Text;

            if (text.Length < MINCHARS)
            {
                m_Message = Strings.Item("SAW_Edit_OS_TooShort").Replace("%0", MINCHARS.ToString());
                return;
            }

            m_Message = Strings.Item("SAW_Edit_OS_Contacting");
            pnlImages.Invalidate();

            try
            {
                // sample URL: https://www.opensymbols.org/api/v1/symbols/search?q=house
                string searchResult = await GetStringAsync(@"https://www.opensymbols.org/api/v1/symbols/search?q=" + Uri.EscapeUriString(text));

                pnlImages.Invalidate();

                var json = JArray.Parse(searchResult);
                if (json.Count == 0)
                {
                    m_Message = Strings.Item("SAW_Edit_OS_NoneFound");
                }
                else
                {
                    if (json.Count > MAXIMAGES)
                    {
                        SetInfo(Strings.Item("SAW_Edit_OS_TooMany").Replace("%0", json.Count.ToString()).Replace("%1", MAXIMAGES.ToString()), true);
                    }
                    int failures = 0;
                    foreach (JObject imageJson in json.Take(MAXIMAGES))
                    {
                        try
                        {
                            DisplayedImage image = await GetImage(imageJson);

                            if (image != null)
                            {
                                if (m_Message != null)                                 // remove message, if any, once first image is displayed
                                {
                                    m_Message = null;
                                    pnlImages.Invalidate();
                                }
                                ShowPicture(image);
                            }
                        }
                        catch (Exception ex)
                        {
                            Utilities.LogSubError("Image " + (imageJson["symbol_key"]?.ToString() ?? "?") + " failed: " + ex.Message);
                            failures += 1;
                        }
                    }
                    if (failures > 0)
                    {
                        SetInfo(Strings.Item("SAW_Edit_OS_SomeFailed").Replace("%0", failures.ToString()), true);
                    }
                }
            }
            catch (Exception ex)
            {
                m_Message = Strings.Item("SAW_Edit_OS_Failed") + ex.Message;
                Utilities.LogSubError(ex);
            }
            pnlImages.Invalidate();
        }