[gnome-weather] ForecastsBox: improve filtering and grouping
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-weather] ForecastsBox: improve filtering and grouping
- Date: Fri, 13 Dec 2013 17:51:09 +0000 (UTC)
commit 60ac8615afe53ac23677b984bc2d581a6737aeab
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Fri Dec 13 18:46:12 2013 +0100
ForecastsBox: improve filtering and grouping
Instead of filtering dates that are separated by at least 12 hours,
separate two dates if they would give different "Day Part" strings.
Also, slightly tweak the mapping between hours and day parts,
and filter out the night part before early morning. This way, we
make sure the night never comes before morning (which makes sense
in some locales, but not in others, and maybe confusing in general)
src/forecast.js | 22 +++++++++++++++-------
src/strings.js | 22 +++++++++++++++-------
2 files changed, 30 insertions(+), 14 deletions(-)
---
diff --git a/src/forecast.js b/src/forecast.js
index 0365323..7168f69 100644
--- a/src/forecast.js
+++ b/src/forecast.js
@@ -51,8 +51,10 @@ const ForecastBox = new Lang.Class({
});
let subday = this._hasSubdayResolution(dates);
+ this._today = GLib.DateTime.new_now_local();
+ this._tomorrow = this._today.add_days(1);
- let current;
+ let current, currentPart;
let n = 0;
// limit to 5 infos max
for (let i = 0; i < dates.length && n < 5; i++) {
@@ -62,7 +64,15 @@ const ForecastBox = new Lang.Class({
// at least 12 hours
let [ok, date] = info.get_value_update();
let datetime = GLib.DateTime.new_from_unix_local(date);
- if (current && datetime.difference(current) < TWELVE_HOURS)
+ let part = Strings.getDatetimePart(datetime);
+
+ // Filter out "uninteresting" times (ie, during the night)
+ if (part == -1)
+ continue;
+
+ // Filter two datetime that would give the same "Day Part" string
+ if (current && part == currentPart &&
+ Util.arrayEqual(current.get_ymd(), datetime.get_ymd()))
continue;
let text = '<b>' + this._getDate(datetime, subday) + '</b>';
@@ -82,6 +92,7 @@ const ForecastBox = new Lang.Class({
this._grid.attach(temperature, n, 2, 1, 1);
current = datetime;
+ currentPart = part;
n++;
}
},
@@ -101,16 +112,13 @@ const ForecastBox = new Lang.Class({
},
_getDate: function(datetime, subday) {
- let now = GLib.DateTime.new_now_local();
- let tomorrow = now.add_days(1);
-
- if (Util.arrayEqual(now.get_ymd(),
+ if (Util.arrayEqual(this._today.get_ymd(),
datetime.get_ymd())) {
if (subday)
return Strings.formatToday(datetime);
else
return _("Today");
- } else if (Util.arrayEqual(tomorrow.get_ymd(),
+ } else if (Util.arrayEqual(this._tomorrow.get_ymd(),
datetime.get_ymd())) {
if (subday)
return Strings.formatTomorrow(datetime);
diff --git a/src/strings.js b/src/strings.js
index 81e9a44..6ec2324 100644
--- a/src/strings.js
+++ b/src/strings.js
@@ -31,32 +31,40 @@ const DAY_PARTS = [
const TODAY_PARTS = [N_("Tonight"), N_("This morning"), N_("This afternoon"), N_("This evening")];
const TOMORROW_PARTS = [N_("Tomorrow night"), N_("Tomorrow morning"), N_("Tomorrow afternoon"), N_("Tomorrow
evening")];
-function _getDatetimePart(datetime) {
+function getDatetimePart(datetime) {
let h = datetime.get_hour();
- if (h < 6 || h >= 21)
- return 0;
+ // 0-5: late night -> filtered out
+ // 5-12: morning
+ // 12-18: afternoon
+ // 18-21: evening
+ // 21-24: night
+
+ if (h < 5)
+ return -1;
else if (h < 12)
return 1;
else if (h < 18)
return 2;
- else
+ else if (h < 21)
return 3;
+ else
+ return 0;
}
function formatToday(datetime) {
- let part = _getDatetimePart(datetime);
+ let part = getDatetimePart(datetime);
return Gettext.gettext(TODAY_PARTS[part]);
}
function formatTomorrow(datetime) {
- let part = _getDatetimePart(datetime);
+ let part = getDatetimePart(datetime);
return Gettext.gettext(TOMORROW_PARTS[part]);
}
function formatDayPart(datetime) {
let day = datetime.get_day_of_week() - 1;
- let part = _getDatetimePart(datetime);
+ let part = getDatetimePart(datetime);
return Gettext.gettext(DAY_PARTS[day][part]);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]