Skip to content

Commit 5902cff

Browse files
authored
Better handle location access error (#117)
1 parent 86ab2ff commit 5902cff

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

data/Application.css

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@
1818
*/
1919

2020
window {
21+
transition: all 600ms ease-in-out;
22+
}
23+
24+
window spinner {
25+
margin: 2em;
26+
}
27+
28+
.weather {
2129
padding: 2em;
2230
font-weight: 500;
23-
transition: all 600ms ease-in-out;
2431
}
2532

26-
.title-3 {
33+
.weather .title-3 {
2734
font-size: 1.5em;
2835
}
2936

30-
.title-1 {
37+
.weather .title-1 {
3138
font-size: 3em;
3239
font-weight: 800;
3340
}

io.github.danirabbit.nimbus.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ modules:
3939
buildsystem: meson
4040
config-opts:
4141
- '-Denable-gtk-doc=false'
42+
- '-Denable-installed-tests=false'
4243
- '-Dsoup2=false'
4344
sources:
4445
- type: archive

src/MainWindow.vala

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
public class MainWindow : Gtk.ApplicationWindow {
77
private Gtk.Stack stack;
8-
private GWeather.Location location;
8+
private Gtk.Spinner spinner;
9+
private Gtk.Grid grid;
10+
private Granite.Placeholder placeholder;
11+
private GWeather.Location? location = null;
912
private GWeather.Info weather_info;
1013

1114
construct {
12-
get_location.begin ();
13-
1415
weather_info = new GWeather.Info (location) {
1516
contact_info = "[email protected]"
1617
};
@@ -36,30 +37,34 @@ public class MainWindow : Gtk.ApplicationWindow {
3637
valign = Gtk.Align.START
3738
};
3839

39-
var grid = new Gtk.Grid () {
40+
grid = new Gtk.Grid () {
4041
column_spacing = 12
4142
};
4243
grid.attach (weather_icon, 0, 0, 1, 2);
4344
grid.attach (temp_label, 1, 0, 1, 2);
4445
grid.attach (weather_label, 2, 0);
4546
grid.attach (location_label, 2, 1);
47+
grid.add_css_class ("weather");
4648

47-
var spinner = new Gtk.Spinner () {
49+
spinner = new Gtk.Spinner () {
4850
halign = Gtk.Align.CENTER,
4951
vexpand = true,
5052
spinning = true
5153
};
5254

53-
var alert_label = new Gtk.Label (_("Unable to Get Location"));
55+
placeholder = new Granite.Placeholder (_("Unable to Get Location")) {
56+
icon = new ThemedIcon ("location-disabled-symbolic"),
57+
description = _("Make sure location access is turned on in <a href='settings://privacy/location'>System SettingsSecurity &amp; Privacy</a>")
58+
};
5459

5560
stack = new Gtk.Stack () {
5661
transition_type = Gtk.StackTransitionType.CROSSFADE,
5762
valign = Gtk.Align.CENTER,
58-
vhomogeneous = true
63+
vhomogeneous = false
5964
};
6065
stack.add_child (spinner);
61-
stack.add_named (grid, "weather");
62-
stack.add_named (alert_label, "alert");
66+
stack.add_child (grid);
67+
stack.add_child (placeholder);
6368

6469
var window_handle = new Gtk.WindowHandle () {
6570
child = stack
@@ -71,8 +76,18 @@ public class MainWindow : Gtk.ApplicationWindow {
7176
titlebar = new Gtk.Grid () { visible = false };
7277
title = _("Nimbus");
7378

79+
get_location ();
80+
7481
notify["is-active"].connect (() => {
75-
weather_info.update ();
82+
if (stack.visible_child == spinner || !is_active) {
83+
return;
84+
}
85+
86+
if (location == null) {
87+
get_location ();
88+
} else {
89+
weather_info.update ();
90+
}
7691
});
7792

7893
weather_info.updated.connect (() => {
@@ -107,29 +122,40 @@ public class MainWindow : Gtk.ApplicationWindow {
107122
});
108123
}
109124

110-
public async void get_location () {
111-
try {
112-
var simple = yield new GClue.Simple (Application.get_default ().application_id, GClue.AccuracyLevel.CITY, null);
125+
private void get_location () {
126+
stack.visible_child = spinner;
127+
128+
get_gclue_simple.begin ((obj, res) => {
129+
var simple = get_gclue_simple.end (res);
130+
if (simple != null) {
131+
simple.notify["location"].connect (() => {
132+
on_location_updated (simple.location.latitude, simple.location.longitude);
133+
});
113134

114-
simple.notify["location"].connect (() => {
115135
on_location_updated (simple.location.latitude, simple.location.longitude);
116-
});
136+
} else {
137+
stack.visible_child = placeholder;
138+
}
139+
});
140+
}
117141

118-
on_location_updated (simple.location.latitude, simple.location.longitude);
142+
private async GClue.Simple? get_gclue_simple () {
143+
try {
144+
var simple = yield new GClue.Simple (Application.get_default ().application_id, GClue.AccuracyLevel.CITY, null);
145+
return simple;
119146
} catch (Error e) {
120147
warning ("Failed to connect to GeoClue2 service: %s", e.message);
121-
stack.visible_child_name = "alert";
122-
return;
148+
return null;
123149
}
124150
}
125151

126-
public void on_location_updated (double latitude, double longitude) {
152+
private void on_location_updated (double latitude, double longitude) {
127153
location = GWeather.Location.get_world ();
128154
location = location.find_nearest_city (latitude, longitude);
129155
if (location != null) {
130156
weather_info.location = location;
131157
weather_info.update ();
132-
stack.visible_child_name = "weather";
158+
stack.visible_child = grid;
133159
}
134160
}
135161
}

0 commit comments

Comments
 (0)