|
2 | 2 | // License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | // file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
4 | 4 |
|
| 5 | +using RestSharp; |
5 | 6 | using System;
|
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Globalization; |
| 9 | +using System.Threading.Tasks; |
6 | 10 | using System.Windows.Forms;
|
7 | 11 |
|
8 | 12 | namespace WinDynamicDesktop
|
@@ -42,5 +46,45 @@ private static void OnLocationDialogClosed(object sender, FormClosedEventArgs e)
|
42 | 46 | LaunchSequence.NextStep();
|
43 | 47 | }
|
44 | 48 | }
|
| 49 | + |
| 50 | + public static async Task FetchLocationData(string locationStr, ScheduleDialog dialog) |
| 51 | + { |
| 52 | + var client = new RestClient("https://us1.locationiq.com"); |
| 53 | + |
| 54 | + var request = new RestRequest("v1/search.php"); |
| 55 | + request.AddParameter("key", Environment.GetEnvironmentVariable("LOCATIONIQ_API_KEY")); |
| 56 | + request.AddParameter("q", locationStr); |
| 57 | + request.AddParameter("format", "json"); |
| 58 | + request.AddParameter("limit", "1"); |
| 59 | + |
| 60 | + var response = await client.ExecuteAsync<List<LocationIQData>>(request); |
| 61 | + |
| 62 | + if (response.IsSuccessful) |
| 63 | + { |
| 64 | + JsonConfig.settings.location = locationStr; |
| 65 | + HandleLocationSuccess(response.Data[0], dialog); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + MessageDialog.ShowWarning(_("The location you entered was invalid, or you are not connected to " + |
| 70 | + "the Internet. Check your Internet connection and try a different location. You can use a " + |
| 71 | + "complete address or just the name of your city/region."), _("Error")); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static void HandleLocationSuccess(LocationIQData data, ScheduleDialog dialog) |
| 76 | + { |
| 77 | + JsonConfig.settings.latitude = double.Parse(data.lat, CultureInfo.InvariantCulture); |
| 78 | + JsonConfig.settings.longitude = double.Parse(data.lon, CultureInfo.InvariantCulture); |
| 79 | + SolarData solarData = SunriseSunsetService.GetSolarData(DateTime.Today); |
| 80 | + |
| 81 | + DialogResult result = MessageDialog.ShowQuestion(string.Format(_("Is this location correct?\n\n{0}\n{1}"), |
| 82 | + data.display_name, SunriseSunsetService.GetSunriseSunsetString(solarData)), _("Question")); |
| 83 | + |
| 84 | + if (result == DialogResult.Yes) |
| 85 | + { |
| 86 | + dialog.Invoke(new Action(() => dialog.HandleScheduleChange())); |
| 87 | + } |
| 88 | + } |
45 | 89 | }
|
46 | 90 | }
|
0 commit comments