Implement "Alarm set" popup. 27/114427/2
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Tue, 14 Feb 2017 11:55:20 +0000 (13:55 +0200)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Tue, 14 Feb 2017 11:55:20 +0000 (13:55 +0200)
Change-Id: Ib70dbf79fa689d7a893d2e5f67092b3a93bb0075
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
alarm-app/src/Input/InputView.cpp
alarm-app/src/List/AlarmItem.cpp
lib-common/inc/Common/AlarmSetPopup.h [new file with mode: 0644]
lib-common/inc/Common/Format.h
lib-common/src/Common/AlarmSetPopup.cpp [new file with mode: 0644]
lib-common/src/Common/Format.cpp

index 752036f3a3bbc6e1882202871c0ecb2c215063cf..cce5378ed26e5faebba602fcd3174852476b724c 100644 (file)
@@ -18,6 +18,7 @@
 #include "Input/SetRepeatView.h"
 #include "Input/SetTimeView.h"
 #include "Common/AlarmDeletedPopup.h"
+#include "Common/AlarmSetPopup.h"
 #include "Common/Model/AlarmConsumer.h"
 
 #include "Ui/PageIndex.h"
@@ -110,6 +111,7 @@ void InputView::saveAlarm()
        tm time = m_TimeView->getTimePicker()->getTime();
        m_Alarm.setTime(time.tm_hour, time.tm_min);
        m_Alarm.setRepeat(m_RepeatView->getDaySelector()->getSelectedDays());
+       m_Alarm.setEnabled(true);
 
        elm_object_disabled_set(m_Button, EINA_TRUE);
        AlarmConsumer::getInstance().saveAlarm(m_Alarm, [this](bool isSuccess) {
@@ -119,6 +121,9 @@ void InputView::saveAlarm()
                        return;
                }
 
+               auto popup = new Common::AlarmSetPopup(m_Alarm);
+               popup->create(getEvasObject());
+               popup->show();
                getPage()->close();
        });
 }
index 3b4482ad324d38b6041c7bf2bfbfc6b1eb4fc3e4..e9edd58e1800562866413d41c1314737878ea990 100644 (file)
@@ -17,6 +17,7 @@
 #include "List/AlarmItem.h"
 #include "Input/InputView.h"
 
+#include "Common/AlarmSetPopup.h"
 #include "Common/Format.h"
 #include "Common/Model/Alarm.h"
 #include "Common/Model/AlarmConsumer.h"
@@ -120,5 +121,11 @@ void AlarmItem::onSelected()
 void AlarmItem::onAlarmEnabled(Evas_Object *check, void *eventInfo)
 {
        m_Alarm.setEnabled(elm_check_state_get(check));
-       AlarmConsumer::getInstance().updateAlarm(m_Alarm, nullptr);
+       AlarmConsumer::getInstance().updateAlarm(m_Alarm, [this](bool isSuccess) {
+               if (isSuccess && m_Alarm.isEnabled()) {
+                       auto popup = new Common::AlarmSetPopup(m_Alarm);
+                       popup->create(getParent()->getEvasObject());
+                       popup->show();
+               }
+       });
 }
diff --git a/lib-common/inc/Common/AlarmSetPopup.h b/lib-common/inc/Common/AlarmSetPopup.h
new file mode 100644 (file)
index 0000000..ffe3443
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef COMMON_ALARM_SET_POPUP_H
+#define COMMON_ALARM_SET_POPUP_H
+
+#include "Ui/Toast.h"
+
+namespace Common
+{
+       namespace Model
+       {
+               class Alarm;
+       }
+
+       /**
+        * @brief Toast popup notifying when alarm will go off after being set.
+        */
+       class EXPORT_API AlarmSetPopup : public Ui::Toast
+       {
+       public:
+               /**
+                * @brief Create toast popup.
+                * @param[in]   alarm   Alarm being set
+                */
+               explicit AlarmSetPopup(Model::Alarm &alarm);
+
+       private:
+               virtual void onCreated() override;
+
+               Model::Alarm &m_Alarm;
+       };
+}
+
+#endif /* COMMON_ALARM_SET_POPUP_H */
index b36d6811721e6ce6bca9c688633ca26e73377db1..8bd5bc0ec06257aa00843c62220560627aaea949 100644 (file)
@@ -55,6 +55,13 @@ namespace Common
         * @return Formatted weekday letters with highlighted repeat.
         */
        EXPORT_API const char *formatRepeat(int repeat);
+
+       /**
+        * @brief Create "Alarm set for ... from now" message.
+        * @param[in]   date    Alarm date
+        * @return Formatted localized message.
+        */
+       EXPORT_API const char *formatAlarmSetMessage(const tm &date);
 }
 
 #endif /* COMMON_FORMAT_H */
diff --git a/lib-common/src/Common/AlarmSetPopup.cpp b/lib-common/src/Common/AlarmSetPopup.cpp
new file mode 100644 (file)
index 0000000..350524c
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Common/AlarmSetPopup.h"
+#include "Common/Format.h"
+#include "Common/Model/Alarm.h"
+
+using namespace Common;
+
+AlarmSetPopup::AlarmSetPopup(Model::Alarm &alarm)
+       : m_Alarm(alarm)
+{
+}
+
+void AlarmSetPopup::onCreated()
+{
+       elm_object_style_set(getEvasObject(), "toast/circle");
+       setText(Common::formatAlarmSetMessage(m_Alarm.getDate()));
+}
index ea725336a2da173c86b0d646f6894ccd7eca68af..7246ed4fabc037585c5956de3c46e656e870ae60 100644 (file)
 #include "Common/Format.h"
 
 #include <app_i18n.h>
+#include <array>
 #include <string>
 #include <system_settings.h>
 
-#define DAY_COUNT 7
+#define DAY_COUNT   7
+#define HOUR_COUNT  24
+#define MIN_COUNT   60
+#define SEC_COUNT   60
+
 #define TIME_BUFFER_SIZE 64
 #define DATE_BUFFER_SIZE 32
 #define REPEAT_BUFFER_SIZE 256
+#define MESSAGE_BUFFER_SIZE 128
 
 namespace
 {
@@ -36,6 +42,28 @@ namespace
                "WDS_ALM_BUTTON_F_M_FRIDAY_ABB",
                "WDS_ALM_BUTTON_S_M_SATURDAY_ABB"
        };
+
+       const struct {
+               const char *singular;
+               const char *plural;
+       } durations[] = {
+               { "1_DAY_", "DAYS_" },
+               { "1_HR_",  "HRS_" },
+               { "1_MIN_", "MINS_" }
+       };
+
+       std::array<int, 3> getDateDiff(const tm &date)
+       {
+               time_t now = time(nullptr);
+               now -= now % SEC_COUNT;
+
+               int diff = (mktime((tm *) &date) - now) / SEC_COUNT;
+               int mins = diff % MIN_COUNT;
+               int hours = (diff / MIN_COUNT) % HOUR_COUNT;
+               int days = diff / MIN_COUNT / HOUR_COUNT;
+
+               return { { days, hours, mins } };
+       }
 }
 
 Utils::Range<const char **> Common::getWeekdayLetters()
@@ -96,3 +124,48 @@ const char *Common::formatRepeat(int repeat)
        }
        return buffer.c_str();
 }
+
+/*
+ * The function selects a translatable string such as:
+ *      WDS_ALM_TPOP_ALARM_SET_FOR_1_HR_PD_MINS_FROM_NOW_ABB
+ *      WDS_ALM_TPOP_ALARM_SET_FOR_P1SD_HRS_P2SD_MINS_FROM_NOW_ABB
+ * by formatting it based on date difference.
+ *
+ * Translatable strings use "PD" to represent "%d" when plural form (>1)
+ * of days, hours or minutes is used.
+ *
+ * If there are several plurals in the string - positional specifiers are used
+ * instead of "%d" such as "%1$d", "%2$d" and "%3$d" which are represented by
+ * "P1SD", "P2SD" and "P3SD" respectively.
+ */
+const char *Common::formatAlarmSetMessage(const tm &date)
+{
+       auto diffs = getDateDiff(date);
+       int args[] = { 0, 0, 0 };
+       bool isMultiPlurals = (int(diffs[0] > 1) + int(diffs[1] > 1) + int(diffs[2] > 1)) > 1;
+
+       std::string format("WDS_ALM_TPOP_ALARM_SET_FOR_");
+       for (size_t i = 0, j = 0; i < diffs.size(); ++i) {
+               auto &duration = durations[i];
+               if (diffs[i] > 0) {
+                       if (diffs[i] > 1) {
+                               args[j++] = diffs[i];
+                               if (isMultiPlurals) {
+                                       format += 'P';
+                                       format += j + '0';
+                                       format += "SD_";
+                               } else {
+                                       format += "PD_";
+                               }
+                               format += duration.plural;
+                       } else {
+                               format += duration.singular;
+                       }
+               }
+       }
+       format += "FROM_NOW_ABB";
+
+       static char buffer[MESSAGE_BUFFER_SIZE];
+       snprintf(buffer, sizeof(buffer), _(format.c_str()), args[0], args[1], args[2]);
+       return buffer;
+}