~aleteoryx/muditaos

7d3949ced2c5486fc0c23ed99a4ca6c742a3f8ca — mkamonMdt 4 years ago 57874ca
[BH-893] Add BGSounds progress window

The commit provides an implementation to MVP of
BackgroundsSounds ProgressWindow. The implementation
is restricted to basic UI and timer functionality.
22 files changed, 517 insertions(+), 154 deletions(-)

M image/assets/lang/English.json
M module-apps/application-meditation/widgets/MeditationTimer.cpp
M module-apps/application-meditation/widgets/MeditationTimer.hpp
M module-apps/apps-common/CMakeLists.txt
R module-apps/apps-common/widgets/{ProgressTimerImpl => ProgressTimer}.cpp
M module-apps/apps-common/widgets/ProgressTimer.hpp
D module-apps/apps-common/widgets/ProgressTimerImpl.hpp
A module-apps/apps-common/widgets/TimerWithCallbacks.hpp
M products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp
M products/BellHybrid/apps/application-bell-background-sounds/CMakeLists.txt
M products/BellHybrid/apps/application-bell-background-sounds/data/BGSoundsStyle.hpp
M products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp
A products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.cpp
A products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.hpp
M products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsMainWindow.cpp
A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.cpp
A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.hpp
A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.cpp
A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.hpp
M products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp
M products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp
M products/BellHybrid/apps/application-bell-powernap/windows/PowerNapProgressWindow.cpp
M image/assets/lang/English.json => image/assets/lang/English.json +1 -0
@@ 76,6 76,7 @@
  "common_minute_lower": "minute",
  "common_minutes_lower": "minutes",
  "common_minute_short": "min",
  "common_paused": "Paused",
  "locale_12hour_min": "%I:%M %p",
  "locale_12hour_min_short": "%I:%M",
  "locale_24hour_min": "%H:%M",

M module-apps/application-meditation/widgets/MeditationTimer.cpp => module-apps/application-meditation/widgets/MeditationTimer.cpp +3 -3
@@ 7,7 7,7 @@
#include <GuiTimer.hpp>
#include <purefs/filesystem_paths.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <apps-common/widgets/ProgressTimerImpl.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>

#include <gsl/assert>



@@ 55,7 55,7 @@ namespace gui
        text->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        text->setEditMode(EditMode::Browse);

        timer = std::make_unique<app::ProgressTimerImpl>(application, this, meditationTimerName, timerTick);
        timer = std::make_unique<app::ProgressTimer>(application, *this, meditationTimerName, timerTick);
        timer->attach(progressBar);
        timer->attach(text);
        auto intervalCallback = [app = application] {


@@ 78,7 78,7 @@ namespace gui
        text->setVisible(isVisible);
    }

    app::ProgressTimer &MeditationTimer::getTimer() noexcept
    app::TimerWithCallbacks &MeditationTimer::getTimer() noexcept
    {
        Expects(timer != nullptr);
        return *timer;

M module-apps/application-meditation/widgets/MeditationTimer.hpp => module-apps/application-meditation/widgets/MeditationTimer.hpp +7 -2
@@ 8,7 8,12 @@
#include <gui/widgets/BoxLayout.hpp>
#include <gui/widgets/ProgressBar.hpp>
#include <gui/widgets/Text.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <apps-common/widgets/TimerWithCallbacks.hpp>

namespace app
{
    class ProgressTimer;
}

namespace gui
{


@@ 26,7 31,7 @@ namespace gui
        [[nodiscard]] auto onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) -> bool override;

        void setCounterVisible(bool isVisible) noexcept;
        [[nodiscard]] app::ProgressTimer &getTimer() noexcept;
        [[nodiscard]] app::TimerWithCallbacks &getTimer() noexcept;
        [[nodiscard]] gui::Progress &getProgress() noexcept;

      private:

M module-apps/apps-common/CMakeLists.txt => module-apps/apps-common/CMakeLists.txt +1 -1
@@ 47,7 47,7 @@ target_sources(apps-common
        widgets/TimeSetFmtSpinner.cpp
        widgets/TimeWidget.cpp
        widgets/WidgetsUtils.cpp
        widgets/ProgressTimerImpl.cpp
        widgets/ProgressTimer.cpp
        windows/AppWindow.cpp
        windows/BrightnessWindow.cpp
        windows/Dialog.cpp

R module-apps/apps-common/widgets/ProgressTimerImpl.cpp => module-apps/apps-common/widgets/ProgressTimer.cpp +25 -24
@@ 1,10 1,11 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ProgressTimerImpl.hpp"
#include "ProgressTimer.hpp"
#include <Text.hpp>
#include <ProgressBar.hpp>
#include <ApplicationCommon.hpp>
#include <apps-common/GuiTimer.hpp>
#include <time/time_conversion.hpp>
#include <gsl/assert>



@@ 15,29 16,29 @@ namespace
namespace app
{

    ProgressTimerImpl::ProgressTimerImpl(app::ApplicationCommon *app,
                                         gui::Item *parent,
                                         std::string timerName,
                                         std::chrono::milliseconds baseTick,
                                         ProgressCountdownMode countdownMode)
    ProgressTimer::ProgressTimer(app::ApplicationCommon *app,
                                 gui::Item &parent,
                                 std::string timerName,
                                 std::chrono::milliseconds baseTick,
                                 ProgressCountdownMode countdownMode)
        : app{app}, parent{parent}, name{std::move(timerName)}, baseTickInterval{baseTick}, countdownMode{countdownMode}
    {}

    void ProgressTimerImpl::resetProgress()
    void ProgressTimer::resetProgress()
    {
        if (progress != nullptr) {
            progress->setValue(0);
        }
    }

    void ProgressTimerImpl::update()
    void ProgressTimer::update()
    {
        updateText();
        updateProgress();
        app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
    }

    void ProgressTimerImpl::updateText()
    void ProgressTimer::updateText()
    {
        using utils::time::Duration;
        if (text == nullptr) {


@@ 53,7 54,7 @@ namespace app
        text->setText(std::move(timerText));
    }

    void ProgressTimerImpl::updateProgress()
    void ProgressTimer::updateProgress()
    {
        if (progress != nullptr) {
            const auto percentage  = static_cast<float>(elapsed.count()) / duration.count();


@@ 62,7 63,7 @@ namespace app
        }
    }

    void ProgressTimerImpl::reset(std::chrono::seconds _duration, std::chrono::seconds _interval)
    void ProgressTimer::reset(std::chrono::seconds _duration, std::chrono::seconds _interval)
    {
        Expects(_duration != std::chrono::seconds::zero());



@@ 75,21 76,21 @@ namespace app
        resetProgress();
    }

    void ProgressTimerImpl::start()
    void ProgressTimer::start()
    {
        startTimer();
        isRunning = true;
    }

    void ProgressTimerImpl::startTimer()
    void ProgressTimer::startTimer()
    {
        Expects(app != nullptr);
        parent->timerCallback = [this](gui::Item &it, sys::Timer &task) { return onTimerTimeout(it, task); };
        timerTask             = app::GuiTimerFactory::createPeriodicTimer(app, parent, name, baseTickInterval);
        parent.timerCallback = [this](gui::Item &it, sys::Timer &task) { return onTimerTimeout(task); };
        timerTask            = app::GuiTimerFactory::createPeriodicTimer(app, &parent, name, baseTickInterval);
        timerTask.start();
    }

    auto ProgressTimerImpl::onTimerTimeout(gui::Item &self, sys::Timer &task) -> bool
    auto ProgressTimer::onTimerTimeout(sys::Timer &task) -> bool
    {
        if (isStopped() || isFinished()) {
            task.stop();


@@ 107,43 108,43 @@ namespace app
        return true;
    }

    auto ProgressTimerImpl::isFinished() const noexcept -> bool
    auto ProgressTimer::isFinished() const noexcept -> bool
    {
        return duration <= elapsed;
    }

    auto ProgressTimerImpl::isStopped() const noexcept -> bool
    auto ProgressTimer::isStopped() const noexcept -> bool
    {
        return !isRunning;
    }

    auto ProgressTimerImpl::intervalReached() const noexcept -> bool
    auto ProgressTimer::intervalReached() const noexcept -> bool
    {
        return hasInterval && (elapsed.count() % interval.count()) == 0;
    }

    void ProgressTimerImpl::stop()
    void ProgressTimer::stop()
    {
        isRunning = false;
    }

    void ProgressTimerImpl::registerOnFinishedCallback(std::function<void()> cb)
    void ProgressTimer::registerOnFinishedCallback(std::function<void()> cb)
    {
        onFinishedCallback = std::move(cb);
    }

    void ProgressTimerImpl::registerOnIntervalCallback(std::function<void()> cb)
    void ProgressTimer::registerOnIntervalCallback(std::function<void()> cb)
    {
        onIntervalCallback = std::move(cb);
    }

    void ProgressTimerImpl::attach(gui::Progress *_progress)
    void ProgressTimer::attach(gui::Progress *_progress)
    {
        Expects(_progress != nullptr);
        progress = _progress;
    }

    void ProgressTimerImpl::attach(gui::Text *_text)
    void ProgressTimer::attach(gui::Text *_text)
    {
        Expects(_text != nullptr);
        text = _text;

M module-apps/apps-common/widgets/ProgressTimer.hpp => module-apps/apps-common/widgets/ProgressTimer.hpp +59 -22
@@ 1,29 1,33 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "TimerWithCallbacks.hpp"
#include <Timers/TimerHandle.hpp>
#include <atomic>
#include <chrono>
#include <functional>

#include <string>
namespace
{
    constexpr auto baseTickDefault = std::chrono::milliseconds{1000};
} // namespace
namespace gui
{
    class Item;
    class Text;
    class Progress;
} // namespace gui

namespace app
{
    class ProgressTimerUIConfigurator
    {
    class ApplicationCommon;

      public:
        virtual ~ProgressTimerUIConfigurator()       = default;
        virtual void attach(gui::Progress *progress) = 0;
        virtual void attach(gui::Text *clock)        = 0;
    enum class ProgressCountdownMode
    {
        Decreasing,
        Increasing
    };

    /** ProgressTimer provides an interface that connect Timer's features to UI representation.
    /** ProgressTimer connects Timer's features with UI representation.
     * The Timer's features consists of:
     * 1) counting time down,
     * 2) ability to perform action (via onIntervalCallback) periodically on reaching an interval


@@ 32,19 36,52 @@ namespace app
     * 1) ability to present time left on attached Text
     * 2) ability to present timer's progress on attached class realising Progress interface.
     */
    class ProgressTimer : public ProgressTimerUIConfigurator
    class ProgressTimer : public app::TimerWithCallbacks
    {
        app::ApplicationCommon *app = nullptr;
        gui::Item &parent;
        gui::Text *text         = nullptr;
        gui::Progress *progress = nullptr;
        const std::string name;

        std::atomic_bool isRunning{false};
        std::chrono::seconds duration{std::chrono::seconds::zero()};
        std::chrono::seconds elapsed{std::chrono::seconds::zero()};
        std::chrono::seconds interval{std::chrono::seconds::zero()};
        std::chrono::milliseconds baseTickInterval{std::chrono::milliseconds::zero()};
        bool hasInterval = false;

        sys::TimerHandle timerTask;

        std::function<void()> onFinishedCallback = nullptr;
        std::function<void()> onIntervalCallback = nullptr;
        ProgressCountdownMode countdownMode;

        void startTimer();
        void update();
        void updateText();
        void updateProgress();
        void resetProgress();
        [[nodiscard]] auto onTimerTimeout(sys::Timer &timerTask) -> bool;
        [[nodiscard]] auto isFinished() const noexcept -> bool;
        [[nodiscard]] auto intervalReached() const noexcept -> bool;

      public:
        [[nodiscard]] virtual auto isStopped() const noexcept -> bool                    = 0;
        virtual void reset(std::chrono::seconds duration,
                           std::chrono::seconds interval = std::chrono::seconds::zero()) = 0;
        virtual void start()                                                             = 0;
        virtual void stop()                                                              = 0;
        virtual void registerOnFinishedCallback(std::function<void()> cb)                = 0;
        virtual void registerOnIntervalCallback(std::function<void()> cb)                = 0;

        virtual void attach(gui::Progress *progress) = 0;
        virtual void attach(gui::Text *text)         = 0;
        ProgressTimer(app::ApplicationCommon *app,
                      gui::Item &parent,
                      std::string timerName,
                      std::chrono::milliseconds baseTick,
                      ProgressCountdownMode countdownMode = ProgressCountdownMode::Decreasing);
        void reset(std::chrono::seconds _duration,
                   std::chrono::seconds _interval = std::chrono::seconds::zero()) override;
        void start() override;
        void stop() override;
        void registerOnFinishedCallback(std::function<void()> cb) override;
        void registerOnIntervalCallback(std::function<void()> cb) override;
        [[nodiscard]] auto isStopped() const noexcept -> bool override;

        void attach(gui::Progress *_progress);
        void attach(gui::Text *_text);
    };

} // namespace app

D module-apps/apps-common/widgets/ProgressTimerImpl.hpp => module-apps/apps-common/widgets/ProgressTimerImpl.hpp +0 -78
@@ 1,78 0,0 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once

#include "ProgressTimer.hpp"
#include <apps-common/GuiTimer.hpp>
#include <chrono>
#include <atomic>
namespace
{

    constexpr auto baseTickDefault = std::chrono::milliseconds{1000};
} // namespace

namespace gui
{
    class Item;
    class Text;
    class Progress;
} // namespace gui

namespace app
{
    enum class ProgressCountdownMode
    {
        Decreasing,
        Increasing
    };

    class ProgressTimerImpl : public ProgressTimer
    {
        app::ApplicationCommon *app = nullptr;
        gui::Item *parent       = nullptr;
        gui::Text *text         = nullptr;
        gui::Progress *progress = nullptr;
        const std::string name;

        std::atomic_bool isRunning{false};
        std::chrono::seconds duration{std::chrono::seconds::zero()};
        std::chrono::seconds elapsed{std::chrono::seconds::zero()};
        std::chrono::seconds interval{std::chrono::seconds::zero()};
        std::chrono::milliseconds baseTickInterval{std::chrono::milliseconds::zero()};
        bool hasInterval = false;

        sys::TimerHandle timerTask;

        std::function<void()> onFinishedCallback = nullptr;
        std::function<void()> onIntervalCallback = nullptr;
        ProgressCountdownMode countdownMode;

        void startTimer();
        void update();
        void updateText();
        void updateProgress();
        void resetProgress();
        [[nodiscard]] auto onTimerTimeout(gui::Item &self, sys::Timer &timerTask) -> bool;
        [[nodiscard]] auto isFinished() const noexcept -> bool;
        [[nodiscard]] auto intervalReached() const noexcept -> bool;

      public:
        ProgressTimerImpl(app::ApplicationCommon *app,
                          gui::Item *parent,
                          std::string timerName,
                          std::chrono::milliseconds baseTick,
                          ProgressCountdownMode countdownMode = ProgressCountdownMode::Decreasing);
        void reset(std::chrono::seconds _duration,
                   std::chrono::seconds _interval = std::chrono::seconds::zero()) override;
        void start() override;
        void stop() override;
        void registerOnFinishedCallback(std::function<void()> cb) override;
        void registerOnIntervalCallback(std::function<void()> cb) override;
        [[nodiscard]] auto isStopped() const noexcept -> bool override;

        void attach(gui::Progress *_progress) override;
        void attach(gui::Text *_text) override;
    };

} // namespace app

A module-apps/apps-common/widgets/TimerWithCallbacks.hpp => module-apps/apps-common/widgets/TimerWithCallbacks.hpp +25 -0
@@ 0,0 1,25 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <chrono>
#include <functional>

namespace app
{

    class TimerWithCallbacks
    {
      public:
        virtual ~TimerWithCallbacks()                                                    = default;
        [[nodiscard]] virtual auto isStopped() const noexcept -> bool                    = 0;
        virtual void reset(std::chrono::seconds duration,
                           std::chrono::seconds interval = std::chrono::seconds::zero()) = 0;
        virtual void start()                                                             = 0;
        virtual void stop()                                                              = 0;
        virtual void registerOnFinishedCallback(std::function<void()> cb)                = 0;
        virtual void registerOnIntervalCallback(std::function<void()> cb)                = 0;
    };

} // namespace app

M products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp => products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp +11 -0
@@ 4,7 4,10 @@
#include "ApplicationBellBackgroundSounds.hpp"
#include "presenter/BGSoundsMainWindowPresenter.hpp"
#include "presenter/BGSoundsTimerSelectPresenter.hpp"
#include "presenter/BGSoundsProgressPresenter.hpp"
#include "windows/BGSoundsMainWindow.hpp"
#include "windows/BGSoundsPausedWindow.hpp"
#include "windows/BGSoundsProgressWindow.hpp"
#include "windows/BGSoundsTimerSelectWindow.hpp"

namespace app


@@ 38,6 41,14 @@ namespace app
                auto presenter = std::make_unique<bgSounds::BGSoundsTimerSelectPresenter>(settings.get());
                return std::make_unique<gui::BGSoundsTimerSelectWindow>(app, std::move(presenter));
            });
        windowsFactory.attach(
            gui::window::name::bgSoundsProgress, [this](ApplicationCommon *app, const std::string &name) {
                auto presenter = std::make_unique<bgSounds::BGSoundsProgressPresenter>(app, settings.get());
                return std::make_unique<gui::BGSoundsProgressWindow>(app, std::move(presenter));
            });
        windowsFactory.attach(gui::window::name::bgSoundsPaused, [](ApplicationCommon *app, const std::string &name) {
            return std::make_unique<gui::BGSoundsPausedWindow>(app);
        });
    }

    sys::MessagePointer ApplicationBellBackgroundSounds::DataReceivedHandler(sys::DataMessage *msgl,

M products/BellHybrid/apps/application-bell-background-sounds/CMakeLists.txt => products/BellHybrid/apps/application-bell-background-sounds/CMakeLists.txt +6 -0
@@ 13,15 13,21 @@ target_include_directories(application-bell-background-sounds
target_sources(application-bell-background-sounds
    PRIVATE
        ApplicationBellBackgroundSounds.cpp
        presenter/BGSoundsProgressPresenter.cpp
        presenter/BGSoundsTimerSelectPresenter.cpp
        windows/BGSoundsMainWindow.cpp
        windows/BGSoundsPausedWindow.cpp
        windows/BGSoundsProgressWindow.cpp
        windows/BGSoundsTimerSelectWindow.cpp

        data/BGSoundsCommon.hpp
        data/BGSoundsStyle.hpp
        presenter/BGSoundsMainWindowPresenter.hpp
        presenter/BGSoundsProgressPresenter.hpp
        presenter/BGSoundsTimerSelectPresenter.hpp
        windows/BGSoundsMainWindow.hpp
        windows/BGSoundsPausedWindow.hpp
        windows/BGSoundsProgressWindow.hpp
        windows/BGSoundsTimerSelectWindow.hpp

    PUBLIC

M products/BellHybrid/apps/application-bell-background-sounds/data/BGSoundsStyle.hpp => products/BellHybrid/apps/application-bell-background-sounds/data/BGSoundsStyle.hpp +10 -0
@@ 9,4 9,14 @@ namespace gui::bgSoundsStyle
{
    inline constexpr auto descriptionFont = style::window::font::largelight;
    inline constexpr auto timerValueFont  = style::window::font::supersizemelight;
    namespace progress
    {
        inline constexpr auto bottomDescTopMargin = 20U;
        inline constexpr auto boxesCount          = 16;
    } // namespace progress

    namespace pause
    {
        inline constexpr auto textH = 200U;
    }
} // namespace gui::bgSoundsStyle

M products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp => products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp +3 -1
@@ 7,8 7,10 @@

namespace gui::window::name
{
    inline constexpr auto bgSoundsPaused      = "BGSoundsPausedWindow";
    inline constexpr auto bgSoundsProgress    = "BGSoundsProgressWindow";
    inline constexpr auto bgSoundsTimerSelect = "BGSoundsTimerSelectWindow";
}
} // namespace gui::window::name

namespace app
{

A products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.cpp => products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.cpp +59 -0
@@ 0,0 1,59 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BGSoundsProgressPresenter.hpp"
#include "data/BGSoundsCommon.hpp"
#include <ApplicationBellBackgroundSounds.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <service-db/Settings.hpp>
#include <Timers/TimerFactory.hpp>
#include <Utils.hpp>

#include <gsl/assert>

namespace app::bgSounds
{
    BGSoundsProgressPresenter::BGSoundsProgressPresenter(app::ApplicationCommon *app, settings::Settings *settings)
        : settings{settings}
    {}

    void BGSoundsProgressPresenter::setTimer(std::unique_ptr<app::TimerWithCallbacks> &&_timer)
    {
        timer = std::move(_timer);
        timer->registerOnFinishedCallback([this]() { onFinished(); });
    }

    void BGSoundsProgressPresenter::activate()
    {
        Expects(timer != nullptr);
        const auto value = settings->getValue(timerValueDBRecordName, settings::SettingsScope::AppLocal);
        runTimer         = utils::is_number(value);
        if (runTimer) {
            timer->reset(std::chrono::minutes{utils::getNumericValue<int>(value)});
            timer->start();
        }
    }
    void BGSoundsProgressPresenter::stop()
    {
        timer->stop();
        onFinished();
    }
    void BGSoundsProgressPresenter::onFinished()
    {
        getView()->onFinished();
    }
    void BGSoundsProgressPresenter::pause()
    {
        if (not timer->isStopped()) {
            timer->stop();
            getView()->onPaused();
        }
    }
    void BGSoundsProgressPresenter::resume()
    {
        if (runTimer && timer->isStopped()) {
            timer->start();
        }
    }

} // namespace app::bgSounds

A products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.hpp => products/BellHybrid/apps/application-bell-background-sounds/presenter/BGSoundsProgressPresenter.hpp +65 -0
@@ 0,0 1,65 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <apps-common/BasePresenter.hpp>
#include <apps-common/widgets/TimerWithCallbacks.hpp>
#include <memory>
namespace app
{
    class ApplicationCommon;
} // namespace app
namespace gui
{
    class Item;
} // namespace gui
namespace settings
{
    class Settings;
}

namespace app::bgSounds
{
    class BGSoundsProgressContract
    {
      public:
        class View
        {
          public:
            ~View()                   = default;
            virtual void onFinished() = 0;
            virtual void onPaused()   = 0;
        };

        class Presenter : public BasePresenter<BGSoundsProgressContract::View>
        {
          public:
            virtual void activate()                                                 = 0;
            virtual void stop()                                                     = 0;
            virtual void pause()                                                    = 0;
            virtual void resume()                                                   = 0;
            virtual void setTimer(std::unique_ptr<app::TimerWithCallbacks> &&timer) = 0;
        };
    };

    class AlarmController;

    class BGSoundsProgressPresenter : public BGSoundsProgressContract::Presenter
    {
        settings::Settings *settings = nullptr;
        std::unique_ptr<app::TimerWithCallbacks> timer;
        bool runTimer = false;

        void activate() override;
        void stop() override;
        void pause() override;
        void resume() override;
        void setTimer(std::unique_ptr<app::TimerWithCallbacks> &&_timer) override;

        void onFinished();

      public:
        BGSoundsProgressPresenter(app::ApplicationCommon *app, settings::Settings *settings);
    };
} // namespace app::bgSounds

M products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsMainWindow.cpp => products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsMainWindow.cpp +2 -0
@@ 3,7 3,9 @@

#include "BGSoundsMainWindow.hpp"
#include <application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp>
#include <ApplicationBellBackgroundSounds.hpp>
#include <module-gui/gui/input/InputEvent.hpp>

namespace gui
{
    BGSoundsMainWindow::BGSoundsMainWindow(

A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.cpp => products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.cpp +45 -0
@@ 0,0 1,45 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BGSoundsPausedWindow.hpp"
#include "data/BGSoundsStyle.hpp"
#include <ApplicationBellBackgroundSounds.hpp>

#include <BoxLayout.hpp>
#include <Image.hpp>
#include <Text.hpp>
#include <i18n/i18n.hpp>
#include <module-gui/gui/input/InputEvent.hpp>

namespace gui
{
    BGSoundsPausedWindow::BGSoundsPausedWindow(app::ApplicationCommon *app)
        : AppWindow(app, gui::window::name::bgSoundsPaused)
    {
        buildInterface();
    }

    void BGSoundsPausedWindow::buildInterface()
    {
        AppWindow::buildInterface();

        auto body = new gui::VBox(this, 0, 0, style::window_width, style::window_height);
        body->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));

        new gui::Image(body, "pause");

        auto text = new gui::Text(body, 0, 0, body->getWidth(), bgSoundsStyle::pause::textH);
        text->setText(utils::translate("common_paused"));
        text->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        text->setVisible(true);
    }

    auto BGSoundsPausedWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (inputEvent.isShortRelease(KeyCode::KEY_ENTER)) {
            application->returnToPreviousWindow();
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }
} // namespace gui

A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.hpp => products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsPausedWindow.hpp +19 -0
@@ 0,0 1,19 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <AppWindow.hpp>

namespace gui
{
    class BGSoundsPausedWindow : public AppWindow
    {
        void buildInterface() override;
        auto onInput(const InputEvent &inputEvent) -> bool override;

      public:
        explicit BGSoundsPausedWindow(app::ApplicationCommon *app);
    };

} // namespace gui

A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.cpp => products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.cpp +124 -0
@@ 0,0 1,124 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BGSoundsProgressWindow.hpp"
#include <ApplicationBellBackgroundSounds.hpp>
#include "data/BGSoundsStyle.hpp"
#include <apps-common/widgets/BellBaseLayout.hpp>
#include <apps-common/widgets/BarGraph.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <apps-common/GuiTimer.hpp>
#include <Text.hpp>

namespace
{
    inline constexpr auto recordNamePlaceHolder = "Meditative Surprises";
    inline constexpr auto bgSoundsTimerName     = "BGSoundsProgressTimer";
    inline constexpr std::chrono::seconds timerTick{1};

    void decorateProgressItem(gui::Rect *item, gui::Alignment::Vertical alignment)
    {
        item->setEdges(gui::RectangleEdge::None);
        item->activeItem = false;
        item->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, alignment));
    }
    void createTitle(gui::VBox *parent)
    {
        auto title = new gui::Text(parent, 0, 0, parent->getWidth(), parent->getHeight() / 2);
        title->setFont(gui::bgSoundsStyle::descriptionFont);
        title->setText(recordNamePlaceHolder);
        decorateProgressItem(title, gui::Alignment::Vertical::Top);
    }
    gui::HBarGraph *createProgress(gui::VBox *parent)
    {
        auto progressBox = new gui::HBox(parent, 0, 0, parent->getWidth(), parent->getHeight() / 2);
        decorateProgressItem(progressBox, gui::Alignment::Vertical::Bottom);
        auto progressBar =
            new gui::HBarGraph(progressBox, 0, 0, gui::bgSoundsStyle::progress::boxesCount, gui::BarGraphStyle::Heavy);
        decorateProgressItem(progressBar, gui::Alignment::Vertical::Center);
        return progressBar;
    }
    gui::Text *createTimer(gui::Item *parent)
    {
        using namespace style;
        using namespace gui::bgSoundsStyle;
        auto timer = new gui::Text(
            parent, 0, 0, bell_base_layout::w, bell_base_layout::outer_layouts_h - progress::bottomDescTopMargin);
        timer->setFont(descriptionFont);
        timer->setMargins(gui::Margins(0, progress::bottomDescTopMargin, 0, 0));
        decorateProgressItem(timer, gui::Alignment::Vertical::Top);
        return timer;
    }
} // namespace

namespace gui
{
    BGSoundsProgressWindow::BGSoundsProgressWindow(
        app::ApplicationCommon *app, std::shared_ptr<app::bgSounds::BGSoundsProgressContract::Presenter> presenter)
        : AppWindow(app, gui::window::name::bgSoundsProgress), presenter{std::move(presenter)}
    {
        this->presenter->attach(this);
        buildInterface();
    }

    void BGSoundsProgressWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (mode == ShowMode::GUI_SHOW_RETURN) {
            presenter->resume();
            return;
        }
        presenter->activate();
    }

    void BGSoundsProgressWindow::buildInterface()
    {
        AppWindow::buildInterface();
        buildLayout();
        configureTimer();
    }
    void BGSoundsProgressWindow::buildLayout()
    {
        auto body = new gui::BellBaseLayout(this, 0, 0, style::bell_base_layout::w, style::bell_base_layout::h, false);
        auto vBox =
            new VBox(body->getCenterBox(), 0, 0, style::bell_base_layout::w, style::bell_base_layout::center_layout_h);
        decorateProgressItem(vBox, gui::Alignment::Vertical::Top);
        createTitle(vBox);
        progressBar = createProgress(vBox);
        timerText   = createTimer(body->lastBox);

        dimensionChangedCallback = [&](Item &, const BoundingBox &newDim) -> bool {
            body->setArea({0, 0, newDim.w, newDim.h});
            return true;
        };
    }
    void BGSoundsProgressWindow::configureTimer()
    {
        auto timer = std::make_unique<app::ProgressTimer>(application, *this, bgSoundsTimerName, timerTick);
        timer->attach(progressBar);
        timer->attach(timerText);
        presenter->setTimer(std::move(timer));
    }

    auto BGSoundsProgressWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (inputEvent.isShortRelease()) {
            if (inputEvent.is(KeyCode::KEY_RF)) {
                presenter->stop();
                return true;
            }
            else if (inputEvent.is(KeyCode::KEY_ENTER)) {
                presenter->pause();
                return true;
            }
        }
        return AppWindow::onInput(inputEvent);
    }
    void BGSoundsProgressWindow::onFinished()
    {
        application->returnToPreviousWindow();
    }
    void BGSoundsProgressWindow::onPaused()
    {
        application->switchWindow(gui::window::name::bgSoundsPaused);
    }
} // namespace gui

A products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.hpp => products/BellHybrid/apps/application-bell-background-sounds/windows/BGSoundsProgressWindow.hpp +33 -0
@@ 0,0 1,33 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "presenter/BGSoundsProgressPresenter.hpp"
#include <AppWindow.hpp>

namespace gui
{
    class HBarGraph;
    class Text;
    class BGSoundsProgressWindow : public AppWindow, public app::bgSounds::BGSoundsProgressContract::View
    {
        std::shared_ptr<app::bgSounds::BGSoundsProgressContract::Presenter> presenter;
        gui::HBarGraph *progressBar = nullptr;
        gui::Text *timerText        = nullptr;

        void buildInterface() override;
        void onBeforeShow(ShowMode mode, SwitchData *data) override;
        auto onInput(const InputEvent &inputEvent) -> bool override;

        void buildLayout();
        void configureTimer();

        void onFinished() override;
        void onPaused() override;

      public:
        BGSoundsProgressWindow(app::ApplicationCommon *app,
                               std::shared_ptr<app::bgSounds::BGSoundsProgressContract::Presenter> presenter);
    };
} // namespace gui

M products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp => products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp +5 -11
@@ 6,7 6,7 @@
#include "data/PowerNapCommon.hpp"
#include "widgets/PowerNapAlarm.hpp"

#include <apps-common/widgets/ProgressTimerImpl.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <service-db/Settings.hpp>
#include <Timers/TimerFactory.hpp>
#include <Utils.hpp>


@@ 15,9 15,7 @@

namespace
{
    inline constexpr auto powernapTimerName = "PowerNapTimer";
    inline constexpr auto powernapAlarmTimerName = "PowerNapAlarmTimer";
    inline constexpr std::chrono::seconds timerTick{1};
    inline constexpr std::chrono::minutes powernapAlarmTimeout{3};
} // namespace
namespace app::powernap


@@ 28,14 26,15 @@ namespace app::powernap
        : app{app}, settings{settings}, alarm{alarm},
          napAlarmTimer{sys::TimerFactory::createSingleShotTimer(
              app, powernapAlarmTimerName, powernapAlarmTimeout, [this](sys::Timer &) { onNapAlarmFinished(); })}

    {}

    void PowerNapProgressPresenter::initTimer(gui::Item *parent)
    void PowerNapProgressPresenter::setTimer(std::unique_ptr<app::TimerWithCallbacks> &&_timer)
    {
        timer = std::make_unique<app::ProgressTimerImpl>(
            app, parent, powernapTimerName, timerTick, app::ProgressCountdownMode::Increasing);
        timer = std::move(_timer);
        timer->registerOnFinishedCallback([this]() { onNapFinished(); });
    }

    void PowerNapProgressPresenter::activate()
    {
        Expects(timer != nullptr);


@@ 49,11 48,6 @@ namespace app::powernap
        napAlarmTimer.stop();
        onNapAlarmFinished();
    }
    app::ProgressTimerUIConfigurator &PowerNapProgressPresenter::getUIConfigurator() noexcept
    {
        Expects(timer != nullptr);
        return *timer;
    }
    void PowerNapProgressPresenter::onNapFinished()
    {
        alarm.start();

M products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp => products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp +4 -8
@@ 4,7 4,7 @@
#pragma once

#include <apps-common/BasePresenter.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <apps-common/widgets/TimerWithCallbacks.hpp>
#include <Timers/TimerHandle.hpp>
#include <memory>
namespace app


@@ 36,27 36,23 @@ namespace app::powernap
        class Presenter : public BasePresenter<PowerNapProgressContract::View>
        {
          public:
            virtual void initTimer(gui::Item *parent)                              = 0;
            virtual app::ProgressTimerUIConfigurator &getUIConfigurator() noexcept = 0;
            virtual void activate()                                                = 0;
            virtual void endNap()                                                  = 0;
            virtual void setTimer(std::unique_ptr<app::TimerWithCallbacks> &&timer) = 0;
        };
    };

    class AlarmController;

    class PowerNapProgressPresenter : public PowerNapProgressContract::Presenter
    {
        app::ApplicationCommon *app  = nullptr;
        settings::Settings *settings = nullptr;
        PowerNapAlarm &alarm;
        std::unique_ptr<app::ProgressTimer> timer;
        std::unique_ptr<app::TimerWithCallbacks> timer;
        sys::TimerHandle napAlarmTimer;

        void initTimer(gui::Item *parent) override;
        app::ProgressTimerUIConfigurator &getUIConfigurator() noexcept override;
        void activate() override;
        void endNap() override;
        void setTimer(std::unique_ptr<app::TimerWithCallbacks> &&_timer) override;

        void onNapFinished();
        void onNapAlarmFinished();

M products/BellHybrid/apps/application-bell-powernap/windows/PowerNapProgressWindow.cpp => products/BellHybrid/apps/application-bell-powernap/windows/PowerNapProgressWindow.cpp +10 -4
@@ 7,6 7,7 @@
#include "data/PowerNapSwitchData.hpp"
#include <apps-common/widgets/BellBaseLayout.hpp>
#include <apps-common/widgets/BarGraph.hpp>
#include <apps-common/widgets/ProgressTimer.hpp>
#include <apps-common/GuiTimer.hpp>
#include <Text.hpp>



@@ 45,6 46,9 @@ namespace
        decorateProgressItem(timer, gui::Alignment::Vertical::Top);
        return timer;
    }

    inline constexpr auto powernapTimerName = "PowerNapTimer";
    inline constexpr std::chrono::seconds timerTick{1};
} // namespace

namespace gui


@@ 53,8 57,8 @@ namespace gui
        app::ApplicationCommon *app, std::shared_ptr<app::powernap::PowerNapProgressContract::Presenter> presenter)
        : AppWindow(app, gui::window::name::powernapProgress), presenter{std::move(presenter)}
    {
        buildInterface();
        this->presenter->attach(this);
        buildInterface();
    }

    void PowerNapProgressWindow::onBeforeShow(ShowMode mode, SwitchData *data)


@@ 85,9 89,11 @@ namespace gui
    }
    void PowerNapProgressWindow::configureTimer()
    {
        presenter->initTimer(this);
        presenter->getUIConfigurator().attach(progressBar);
        presenter->getUIConfigurator().attach(timerText);
        auto timer = std::make_unique<app::ProgressTimer>(
            application, *this, powernapTimerName, timerTick, app::ProgressCountdownMode::Increasing);
        timer->attach(progressBar);
        timer->attach(timerText);
        presenter->setTimer(std::move(timer));
    }

    auto PowerNapProgressWindow::onInput(const InputEvent &inputEvent) -> bool