on xenial help sign in
Reload Infographics userdata when the day changes (LP: #1378814)
7ed77a1Tomás Tormo
qml/Greeter/Infographics.qml+25−3
@@ -25,6 +25,8 @@ Item {
25
25
26 property int animDuration: 10
26 property int animDuration: 10
27
27
28+ property int currentWeekDay
29+
28 QtObject {
30 QtObject {
29 id: d
31 id: d
30 objectName: "infographicPrivate"
32 objectName: "infographicPrivate"
@@ -56,6 +58,24 @@ Item {
56 onDataDisappeared: startShowAnimation() // show "no data" label
58 onDataDisappeared: startShowAnimation() // show "no data" label
57 }
59 }
58
60
61+ LiveTimer {
62+ frequency: LiveTimer.Hour
63+ onTrigger: handleTimerTrigger()
64+ }
65+
66+ function handleTimerTrigger(){
67+ var today = new Date().getDay()
68+ if(infographic.currentWeekDay !== today){
69+ infographic.currentWeekDay = today
70+ reloadUserData();
71+ }
72+ }
73+
74+ function reloadUserData(){
75+ d.useDotAnimation = false
76+ infographic.model.nextDataSource()
77+ }
78+
59 function startShowAnimation() {
79 function startShowAnimation() {
60 dotHideAnimTimer.stop()
80 dotHideAnimTimer.stop()
61 notification.hideAnim.stop()
81 notification.hideAnim.stop()
@@ -81,7 +101,10 @@ Item {
81
101
82 visible: model.username !== ""
102 visible: model.username !== ""
83
103
84− Component.onCompleted: startShowAnimation()
104+ Component.onCompleted: {
105+ currentWeekDay = new Date().getDay()
106+ startShowAnimation()
107+ }
85
108
86 Item {
109 Item {
87 id: dataCircle
110 id: dataCircle
@@ -405,8 +428,7 @@ Item {
405
428
406 onDoubleClicked: {
429 onDoubleClicked: {
407 if (!d.animating) {
430 if (!d.animating) {
408− d.useDotAnimation = false
431+ reloadUserData()
409− infographic.model.nextDataSource()
410 }
432 }
411 }
433 }
412 }
434 }
tests/mocks/libusermetrics/UserMetrics.cpp+33−13
@@ -88,12 +88,16 @@ public:
88 void setUsername(const QString &username);
88 void setUsername(const QString &username);
89
89
90protected:
90protected:
91+ void findAndSetUsername(const QString &username);
92+
91 void nextFakeData();
93 void nextFakeData();
92
94
93 void generateFakeData();
95 void generateFakeData();
94
96
95 void loadFakeData();
97 void loadFakeData();
96
98
99+ void loadUserMetricsData();
100+
97 void finishSetFakeData();
101 void finishSetFakeData();
98
102
99private:
103private:
@@ -131,7 +135,7 @@ private:
131}
135}
132
136
133UserMetricsDataPrivate::UserMetricsDataPrivate(UserMetricsData *parent) :
137UserMetricsDataPrivate::UserMetricsDataPrivate(UserMetricsData *parent) :
134− q_ptr(parent), m_firstColor(this), m_secondColor(this)
138+ q_ptr(parent), m_label(""), m_firstColor(this), m_secondColor(this)
135{
139{
136 m_length = calculateLength();
140 m_length = calculateLength();
137}
141}
@@ -229,12 +233,8 @@ UserMetricsPrivate::~UserMetricsPrivate()
229{
233{
230}
234}
231
235
232−void UserMetricsPrivate::setUsername(const QString &username)
236+void UserMetricsPrivate::findAndSetUsername(const QString &username)
233{
237{
234− if (m_username == username && m_newData) {
235− return;
236− }
237−
238 m_username = username;
238 m_username = username;
239
239
240 m_dataIndex = m_fakeData.constFind(m_username);
240 m_dataIndex = m_fakeData.constFind(m_username);
@@ -243,6 +243,16 @@ void UserMetricsPrivate::setUsername(const QString &username)
243 m_dataIndex = m_fakeData.constFind("");
243 m_dataIndex = m_fakeData.constFind("");
244 }
244 }
245
245
246+ m_newData = *m_dataIndex;
247+}
248+
249+void UserMetricsPrivate::setUsername(const QString &username)
250+{
251+ if (m_username == username && m_newData) {
252+ return;
253+ }
254+
255+ findAndSetUsername(username);
246 loadFakeData();
256 loadFakeData();
247
257
248 q_ptr->usernameChanged(m_username);
258 q_ptr->usernameChanged(m_username);
@@ -332,7 +342,6 @@ void UserMetricsPrivate::generateFakeData()
332
342
333void UserMetricsPrivate::loadFakeData()
343void UserMetricsPrivate::loadFakeData()
334{
344{
335− m_newData = *m_dataIndex;
336
345
337 bool oldLabelEmpty = m_label.isEmpty();
346 bool oldLabelEmpty = m_label.isEmpty();
338 bool newLabelEmpty = m_newData->label().isEmpty();
347 bool newLabelEmpty = m_newData->label().isEmpty();
@@ -351,24 +360,28 @@ void UserMetricsPrivate::loadFakeData()
351 // we emit no signal if the data has stayed empty
360 // we emit no signal if the data has stayed empty
352}
361}
353
362
354−void UserMetricsPrivate::finishSetFakeData()
363+void UserMetricsPrivate::loadUserMetricsData()
355{
364{
356− bool oldLabelEmpty = m_label.isEmpty();
357− bool newLabelEmpty = m_newData->label().isEmpty();
358−
359 m_label = m_newData->label();
365 m_label = m_newData->label();
360 m_firstColor = m_newData->firstColor();
366 m_firstColor = m_newData->firstColor();
361 m_firstMonth.setVariantList(m_newData->firstMonth());
367 m_firstMonth.setVariantList(m_newData->firstMonth());
362 m_secondColor = m_newData->secondColor();
368 m_secondColor = m_newData->secondColor();
363 m_secondMonth.setVariantList(m_newData->secondMonth());
369 m_secondMonth.setVariantList(m_newData->secondMonth());
370+ m_currentDay = m_newData->length();
371+}
364
372
373+void UserMetricsPrivate::finishSetFakeData()
374+{
375+ bool oldLabelEmpty = m_label.isEmpty();
376+ bool newLabelEmpty = m_newData->label().isEmpty();
365 bool currentDayChanged = m_currentDay != m_newData->length();
377 bool currentDayChanged = m_currentDay != m_newData->length();
366− m_currentDay = m_newData->length();
378+
379+ loadUserMetricsData();
367
380
368 q_ptr->labelChanged(m_label);
381 q_ptr->labelChanged(m_label);
369 if (currentDayChanged)
382 if (currentDayChanged)
370 {
383 {
371− q_ptr->currentDayChanged(m_currentDay);
384+ q_ptr->currentDayChanged(m_currentDay);
372 }
385 }
373
386
374 if (oldLabelEmpty && !newLabelEmpty)
387 if (oldLabelEmpty && !newLabelEmpty)
@@ -396,6 +409,7 @@ void UserMetricsPrivate::nextFakeData()
396 }
409 }
397 }
410 }
398
411
412+ m_newData = *m_dataIndex;
399 loadFakeData();
413 loadFakeData();
400}
414}
401
415
@@ -464,6 +478,12 @@ void UserMetrics::readyForDataChangeSlot()
464 d_ptr->finishSetFakeData();
478 d_ptr->finishSetFakeData();
465}
479}
466
480
481+void UserMetrics::reset()
482+{
483+ d_ptr->findAndSetUsername("");
484+ d_ptr->loadUserMetricsData();
485+}
486+
467/**
487/**
468 * Factory methods
488 * Factory methods
469 */
489 */
tests/mocks/libusermetrics/UserMetrics.h+2
@@ -61,6 +61,8 @@ public:
61
61
62 QAbstractItemModel *secondMonth() const;
62 QAbstractItemModel *secondMonth() const;
63
63
64+ Q_INVOKABLE void reset();
65+
64Q_SIGNALS:
66Q_SIGNALS:
65 void labelChanged(const QString &label);
67 void labelChanged(const QString &label);
66
68
tests/qmltests/Greeter/tst_Infographics.qml+77−13
@@ -38,15 +38,62 @@ Item {
38 width: units.gu(120)
38 width: units.gu(120)
39 height: units.gu(120)
39 height: units.gu(120)
40
40
41+ Loader {
42+ id: loader
43+
44+ active: false
45+ anchors.fill: parent
46+
47+ property bool componentDestroyed: true
48+ sourceComponent: Component {
49+ Infographics {
50+ id: infographic
51+ width: loader.width
52+ height: loader.height
53+ model: infographicModel
54+
55+ Component.onDestruction: {
56+ loader.componentDestroyed = true
57+ }
58+ }
59+ }
60+ }
61+
41 UT.UnityTestCase {
62 UT.UnityTestCase {
42 name: "Infographics"
63 name: "Infographics"
43 when: windowShown
64 when: windowShown
44
65
45− property var dataCircle: findChild(infographic, "dataCircle")
66+ property var dataCircle
67+ property var dots
68+ property var label
69+ property var presentCircles
70+ property var pastCircles
71+ property var infographic
72+
73+ function reloadInfographic() {
74+ loader.active = false;
75+ tryCompare(loader, "status", Loader.Null);
76+ tryCompare(loader, "item", null);
77+ tryCompare(loader, "componentDestroyed", true);
78+ loader.active = true;
79+ tryCompare(loader, "status", Loader.Ready);
80+ loader.componentDestroyed = false
81+ infographic = loader.item
82+ }
83+
84+ function reloadModel() {
85+ infographicModel.reset()
86+ }
87+
88+ function init() {
89+ reloadModel()
90+ reloadInfographic()
46− property var dots: findChild(infographic, "dots")
91+ dataCircle = findChild(infographic, "dataCircle")
92+ dots = findChild(infographic, "dots")
47− property var label: findChild(infographic, "label")
93+ label = findChild(infographic, "label")
48− property var presentCircles: findChild(infographic, "presentCircles")
94+ presentCircles = findChild(infographic, "presentCircles")
49− property var pastCircles: findChild(infographic, "pastCircles")
95+ pastCircles = findChild(infographic, "pastCircles")
96+ }
50
97
51 function test_dot_states_data() {
98 function test_dot_states_data() {
52 return [
99 return [
@@ -74,9 +121,10 @@ Item {
74
121
75 function test_set_username_data() {
122 function test_set_username_data() {
76 return [
123 return [
77− { username: "has-password", label: "<b>19</b> minutes talk time", visible: true },
124+ { username: "has-password", label: "<b>19</b> minutes talk time", visible: true },
78− { username: "two-factor", label: "", visible: true },
125+ { username: "two-factor", label: "", visible: true },
79− { username: "", label: "", visible: false },
126+ { username: "", label: "", visible: false },
127+
80 ]
128 ]
81 }
129 }
82
130
@@ -85,13 +133,29 @@ Item {
85 tryCompare(label, "text", data.label)
133 tryCompare(label, "text", data.label)
86 compare(infographic.visible, data.visible);
134 compare(infographic.visible, data.visible);
87 }
135 }
88− }
89
136
90− Infographics {
137+ function test_update_userdata_when_new_day_data()
91− id: infographic
138+ {
139+ return [
140+ { tag: "Same day", currentDayOffset: 0, label: "<b>19</b> minutes talk time" },
141+ { tag: "Different day", currentDayOffset: 1, label: "<b>33</b> messages today" },
142+ ]
143+ }
92
144
93− anchors.fill: parent
145+ function test_update_userdata_when_new_day(data)
146+ {
147+ //Given
148+ var today = new Date().getDay()
94− model: infographicModel
149+ infographicModel.username = "single"
150+ infographic.currentWeekDay = (today - data.currentDayOffset)
151+
152+ //When
153+ infographic.handleTimerTrigger()
154+
155+ //Then
156+ tryCompare(infographic, "currentWeekDay", today)
157+ tryCompare(label, "text", data.label)
158+ }
95 }
159 }
96
160
97 Dot { id: dot }
161 Dot { id: dot }