77using namespace Pinetime ::Applications::Screens;
88
99namespace {
10- static constexpr char defaultLabelText[] = " Find my phone" ;
11- static constexpr char alertSentLabelText[] = " Alert sent" ;
12- static constexpr char noConnectionLabelText[] = " No connection" ;
10+ static constexpr char alertSentLabelText[] = " Alerting" ;
11+
1312 static constexpr char noneLabelText[] = " Stop" ;
1413 static constexpr char highLabelText[] = " Ring" ;
15- static constexpr int restoreLabelTimeoutSec = 2 ;
16- static constexpr TickType_t restoreLabelTimeoutTicks = restoreLabelTimeoutSec * configTICK_RATE_HZ;
1714
1815 void btnImmediateAlertEventHandler (lv_obj_t * obj, lv_event_t event) {
1916 auto * screen = static_cast <FindMyPhone*>(obj->user_data );
2017 screen->OnImmediateAlertEvent (obj, event);
2118 }
22-
23- void RestoreLabelTaskCallback (lv_task_t * task) {
24- auto * screen = static_cast <FindMyPhone*>(task->user_data );
25- screen->RestoreLabelText ();
26- screen->StopRestoreLabelTask ();
27- }
2819}
2920
30- FindMyPhone::FindMyPhone (Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient) : immediateAlertClient {immediateAlertClient} {
31- lastLevel = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
21+ const FindMyPhone::LabelState FindMyPhone::stoppedLabelState {
22+ .text = " Alert stopped" ,
23+ .color = LV_COLOR_WHITE,
24+ };
25+
26+ const FindMyPhone::LabelState FindMyPhone::noConnectionLabelState {
27+ .text = " No connection" ,
28+ .color = LV_COLOR_WHITE,
29+ };
30+
31+ const FindMyPhone::LabelState FindMyPhone::noServiceLabelState {
32+ .text = " No service" ,
33+ .color = LV_COLOR_WHITE,
34+ };
35+
36+ const FindMyPhone::LabelState FindMyPhone::defaultLabelState {
37+ .text = " Ready" ,
38+ .color = LV_COLOR_WHITE,
39+ };
3240
41+ const FindMyPhone::LabelState FindMyPhone::alertingLabelState {
42+ .text = " Alerting" ,
43+ .color = LV_COLOR_RED,
44+ };
45+
46+ FindMyPhone::FindMyPhone (Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient) : immediateAlertClient {immediateAlertClient} {
3347 container = lv_cont_create (lv_scr_act (), nullptr );
3448
3549 lv_obj_set_size (container, LV_HOR_RES, LV_VER_RES);
@@ -40,10 +54,6 @@ FindMyPhone::FindMyPhone(Pinetime::Controllers::ImmediateAlertClient& immediateA
4054
4155 lblTitle = lv_label_create (lv_scr_act (), nullptr );
4256
43- lv_label_set_text_static (lblTitle, defaultLabelText);
44- lv_obj_set_style_local_text_color (lblTitle, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
45- lv_obj_align (lblTitle, nullptr , LV_ALIGN_CENTER, 0 , -40 );
46-
4757 btnNone = lv_btn_create (container, nullptr );
4858 btnNone->user_data = this ;
4959 lv_obj_set_event_cb (btnNone, btnImmediateAlertEventHandler);
@@ -61,59 +71,68 @@ FindMyPhone::FindMyPhone(Pinetime::Controllers::ImmediateAlertClient& immediateA
6171 lblHigh = lv_label_create (btnHigh, nullptr );
6272 lv_label_set_text_static (lblHigh, highLabelText);
6373 lv_obj_set_style_local_bg_color (btnHigh, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
74+ refreshTask = lv_task_create (RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this );
6475}
6576
6677FindMyPhone::~FindMyPhone () {
78+ lv_task_del (refreshTask);
6779 lv_obj_clean (lv_scr_act ());
6880}
6981
7082void FindMyPhone::OnImmediateAlertEvent (lv_obj_t * obj, lv_event_t event) {
7183 if (event == LV_EVENT_CLICKED) {
7284 if (obj == btnNone) {
73- lastLevel = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
85+ lastUserInitiatedLevel = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
7486 } else if (obj == btnHigh) {
75- lastLevel = Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert;
87+ lastUserInitiatedLevel = Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert;
88+ } else {
89+ // Unknown button?
90+ ASSERT (false );
91+ return ;
7692 }
77- UpdateImmediateAlerts ( );
93+ immediateAlertClient. SendImmediateAlert (*lastUserInitiatedLevel );
7894 }
7995}
8096
81- void FindMyPhone::UpdateImmediateAlerts () {
82- switch (lastLevel) {
83- case Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert:
84- lv_obj_set_style_local_text_color (lblTitle, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
97+ const FindMyPhone::LabelState& FindMyPhone::GetLabelState () const {
98+ const auto service_state = immediateAlertClient.GetState ();
99+ switch (service_state) {
100+ case Pinetime::Controllers::ImmediateAlertClient::State::NoConnection:
101+ return noConnectionLabelState;
102+ case Pinetime::Controllers::ImmediateAlertClient::State::NoIAS:
103+ return noServiceLabelState;
104+ case Pinetime::Controllers::ImmediateAlertClient::State::Connected:
85105 break ;
106+ }
107+ // Conntected state handling.
108+ if (!lastUserInitiatedLevel.has_value ()) {
109+ return defaultLabelState;
110+ }
111+ switch (*lastUserInitiatedLevel) {
112+ case Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert:
113+ return stoppedLabelState;
86114 case Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert:
87- lv_obj_set_style_local_text_color (lblTitle, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
88- break ;
115+ return alertingLabelState;
89116 case Pinetime::Controllers::ImmediateAlertClient::Levels::MildAlert:
90- // Not supported.
117+ // Not supported
118+ default :
91119 ASSERT (false );
92- break ;
120+ return alertingLabelState ;
93121 }
94- if (immediateAlertClient.SendImmediateAlert (lastLevel)) {
95- lv_label_set_text_static (lblTitle, alertSentLabelText);
96- } else {
97- lv_label_set_text_static (lblTitle, noConnectionLabelText);
98- }
99- ScheduleRestoreLabelTask ();
100122}
101123
102- void FindMyPhone::ScheduleRestoreLabelTask () {
103- if (taskRestoreLabelText) {
104- return ;
105- }
106- taskRestoreLabelText = lv_task_create (RestoreLabelTaskCallback, restoreLabelTimeoutTicks, LV_TASK_PRIO_MID, this );
107- }
108-
109- void FindMyPhone::StopRestoreLabelTask () {
110- if (taskRestoreLabelText) {
111- lv_task_del (taskRestoreLabelText);
112- taskRestoreLabelText = nullptr ;
124+ void FindMyPhone::Refresh () {
125+ const auto service_state = immediateAlertClient.GetState ();
126+ if (service_state == Pinetime::Controllers::ImmediateAlertClient::State::Connected) {
127+ lv_obj_clear_state (btnNone, LV_STATE_DISABLED);
128+ lv_obj_clear_state (btnHigh, LV_STATE_DISABLED);
129+ } else {
130+ lv_obj_add_state (btnNone, LV_STATE_DISABLED);
131+ lv_obj_add_state (btnHigh, LV_STATE_DISABLED);
132+ lastUserInitiatedLevel = std::nullopt ;
113133 }
114- }
115-
116- void FindMyPhone::RestoreLabelText () {
117- lv_label_set_text_static (lblTitle, defaultLabelText);
118- lv_obj_set_style_local_text_color (lblTitle, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
134+ const auto & label_state = GetLabelState ();
135+ lv_obj_set_style_local_text_color (lblTitle, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, label_state.color );
136+ lv_label_set_text_static (lblTitle, label_state.text );
137+ lv_obj_align (lblTitle, nullptr , LV_ALIGN_CENTER, 0 , -40 );
119138}
0 commit comments