Skip to content

Commit 1201fc9

Browse files
committed
Optimize wallpaper update on startup when Windows location is enabled
1 parent 0cb9c9e commit 1201fc9

File tree

5 files changed

+48
-62
lines changed

5 files changed

+48
-62
lines changed

src/AppContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public AppContext(string[] args) : base(new HiddenForm())
3232

3333
Task.Run(() =>
3434
{
35-
LocationManager.Initialize();
36-
scheduler.RunAndUpdateLocation();
35+
scheduler.RunAndUpdateLocation(false, LocationManager.Initialize);
3736
MainForm.Invoke(() => LaunchSequence.NextStep());
3837
UpdateChecker.Initialize();
3938
});

src/EventScheduler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ public bool Run(bool forceImageUpdate = false, DisplayEvent overrideEvent = null
133133
return true;
134134
}
135135

136-
public async void RunAndUpdateLocation(bool forceImageUpdate = false)
136+
public async void RunAndUpdateLocation(bool forceImageUpdate = false, Action locationReady = null)
137137
{
138138
bool result = Run(forceImageUpdate);
139+
locationReady?.Invoke();
139140

140141
if (result && JsonConfig.settings.locationMode == 1 && await UwpLocation.UpdateGeoposition())
141142
{

src/LocationIQ.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/LocationManager.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
using RestSharp;
56
using System;
7+
using System.Collections.Generic;
8+
using System.Globalization;
9+
using System.Threading.Tasks;
610
using System.Windows.Forms;
711

812
namespace WinDynamicDesktop
@@ -42,5 +46,45 @@ private static void OnLocationDialogClosed(object sender, FormClosedEventArgs e)
4246
LaunchSequence.NextStep();
4347
}
4448
}
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+
}
4589
}
4690
}

src/ScheduleDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private async void okButton_Click(object sender, EventArgs e)
166166
{
167167
if (radioButton1.Checked)
168168
{
169-
await LocationIQService.GetLocationData(locationBox.Text, this);
169+
await LocationManager.FetchLocationData(locationBox.Text, this);
170170
}
171171
else if (radioButton2.Checked)
172172
{

0 commit comments

Comments
 (0)