~aleteoryx/muditaos

1c551cc4e48b96a0555319a1d2ad03f34729489c — Mateusz Grzegorzek 4 years ago 441b9b4
[BH-831] Show popup on Alarm activation - part III

Get alarm time from service-time
M products/BellHybrid/apps/application-bell-alarm/ApplicationBellAlarm.cpp => products/BellHybrid/apps/application-bell-alarm/ApplicationBellAlarm.cpp +1 -1
@@ 43,6 43,6 @@ namespace app
        if (dynamic_cast<sys::ResponseMessage *>(retMsg.get())->retCode == sys::ReturnCodes::Success) {
            return retMsg;
        }
        return std::make_shared<sys::ResponseMessage>();
        return handleAsyncResponse(resp);
    }
} // namespace app

M products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp => products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp +1 -1
@@ 67,6 67,6 @@ namespace app
            alarm->registerAudioStream(msg->token);
        }

        return sys::msgHandled();
        return handleAsyncResponse(resp);
    }
} // namespace app

M products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp => products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp +1 -1
@@ 134,7 134,7 @@ namespace app
            }
            return sys::msgHandled();
        }
        return std::make_shared<sys::ResponseMessage>();
        return handleAsyncResponse(resp);
    }

    void ApplicationBellSettings::setBrightness(bsp::eink_frontlight::BrightnessPercentage value)

M products/BellHybrid/apps/common/include/common/popups/AlarmActivatedWindow.hpp => products/BellHybrid/apps/common/include/common/popups/AlarmActivatedWindow.hpp +5 -1
@@ 5,13 5,17 @@

#include <apps-common/popups/presenter/PowerOffPresenter.hpp>
#include <apps-common/windows/Dialog.hpp>
#include <AsyncTask.hpp>

namespace gui
{
    class AlarmActivatedWindow : public Dialog
    class AlarmActivatedWindow : public Dialog, public app::AsyncCallbackReceiver
    {
      public:
        explicit AlarmActivatedWindow(app::ApplicationCommon *app);
        void onBeforeShow(ShowMode mode, SwitchData *data) override;

      private:
        bool onAlarmResponseMessage(sys::ResponseMessage *response, ShowMode mode);
    };
} /* namespace gui */

M products/BellHybrid/apps/common/src/popups/AlarmActivatedWindow.cpp => products/BellHybrid/apps/common/src/popups/AlarmActivatedWindow.cpp +37 -9
@@ 1,30 1,58 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md


#include <ApplicationCommon.hpp>
#include <apps-common/messages/DialogMetadataMessage.hpp>
#include <common/TimeUtils.hpp>
#include <common/popups/AlarmActivatedWindow.hpp>
#include <popups/Popups.hpp>
#include <service-time/AlarmMessage.hpp>
#include <service-time/Constants.hpp>

namespace gui
{
    AlarmActivatedWindow::AlarmActivatedWindow(app::ApplicationCommon *app)
        : Dialog(app, popup::window::alarm_activated_window)
        : Dialog(app, popup::window::alarm_activated_window), app::AsyncCallbackReceiver{app}
    {}

    void AlarmActivatedWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    void AlarmActivatedWindow::onBeforeShow(ShowMode mode, [[maybe_unused]] SwitchData *data)
    {
        const auto alarmTime = utils::time::getCurrentTime(); // TODO: Get alarm time from db
        auto msg             = std::make_unique<DialogMetadataMessage>(
            gui::DialogMetadata{.title = "",
                                .icon  = "bell_alarm_activated",
                                .text  = utils::time::getBottomDescription(
                                    utils::time::calculateTimeDifference(alarmTime, utils::time::getCurrentTime()))});
        auto task = app::AsyncRequest::createFromMessage(
            std::make_unique<alarms::AlarmGetNextSingleEventsRequestMessage>(), service::name::service_time);

        auto onResponseCallback = [this, mode](auto response) { return onAlarmResponseMessage(response, mode); };
        task->execute(this->application, this, onResponseCallback);

        DialogMetadata metaData;
        metaData.icon = "bell_alarm_activated";
        auto msg      = std::make_unique<DialogMetadataMessage>(std::move(metaData));
        Dialog::onBeforeShow(mode, msg.get());
    }

    bool AlarmActivatedWindow::onAlarmResponseMessage(sys::ResponseMessage *response, ShowMode mode)
    {
        auto result = dynamic_cast<alarms::AlarmGetNextSingleEventsResponseMessage *>(response);
        if (result == nullptr || result->retCode != sys::ReturnCodes::Success) {
            LOG_WARN("Get next single event request failed!");
            return false;
        }
        const auto &singleEvents = result->singleEvents;
        if (singleEvents.empty()) {
            LOG_WARN("There is no event!");
            return false;
        }
        const auto &startDate = singleEvents.front().startDate;
        LOG_DEBUG("Alarm time: %s", TimePointToString(startDate).c_str());
        const auto alarmTime = std::chrono::system_clock::to_time_t(startDate);
        DialogMetadata metaData;
        metaData.icon = "bell_alarm_activated";
        metaData.text = utils::time::getBottomDescription(
            utils::time::calculateTimeDifference(alarmTime, utils::time::getCurrentTime()));
        auto msg = std::make_unique<DialogMetadataMessage>(std::move(metaData));
        Dialog::onBeforeShow(mode, msg.get());
        statusBar->setVisible(false);
        header->setTitleVisibility(false);
        bottomBar->setActive(BottomBar::Side::RIGHT, false);
        return true;
    }
} /* namespace gui */