~aleteoryx/muditaos

ref: 1fff0a5115f78af1d5bccad88e366dd1d9663e36 muditaos/module-apps/application-alarm-clock/widgets/AlarmMusicOptionsItem.cpp -rw-r--r-- 5.2 KiB
1fff0a51 — Lukasz Mastalerz [BH-1768] Snooze mode with deep pressed knob 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmMusicOptionsItem.hpp"

#include <service-audio/AudioServiceAPI.hpp>
#include <purefs/filesystem_paths.hpp>

namespace gui
{
    AlarmMusicOptionsItem::AlarmMusicOptionsItem(app::ApplicationCommon *app,
                                                 const std::string &description,
                                                 std::shared_ptr<SoundsPlayer> player,
                                                 std::function<void(const UTF8 &text)> navBarTemporaryMode,
                                                 std::function<void()> navBarRestoreFromTemporaryMode)
        : AlarmOptionsItem(description), navBarTemporaryMode(std::move(navBarTemporaryMode)),
          navBarRestoreFromTemporaryMode(std::move(navBarRestoreFromTemporaryMode))
    {
        assert(app != nullptr);

        alarmSoundList = getMusicFilesList();
        std::vector<UTF8> printOptions;
        for (const auto &musicFile : getMusicFilesList()) {
            printOptions.push_back(musicFile.title);
        }
        optionSpinner->setData({printOptions});

        inputCallback = [=](gui::Item &item, const gui::InputEvent &event) {
            if (event.isShortRelease(gui::KeyCode::KEY_LF)) {
                if (!player->previouslyPlayed(getFilePath(optionSpinner->getCurrentValue())) ||
                    player->isInState(SoundsPlayer::State::Stopped)) {
                    player->play(getFilePath(optionSpinner->getCurrentValue()),
                                 [=]() { this->navBarTemporaryMode(utils::translate(style::strings::common::play)); });
                    this->navBarTemporaryMode(utils::translate(style::strings::common::pause));
                }
                else if (player->isInState(SoundsPlayer::State::Paused)) {
                    player->resume();
                    this->navBarTemporaryMode(utils::translate(style::strings::common::pause));
                }
                else {
                    player->pause();
                    this->navBarTemporaryMode(utils::translate(style::strings::common::play));
                }
            }

            // stop preview playback when we go back
            if (player->isInState(SoundsPlayer::State::Playing) && event.isShortRelease(gui::KeyCode::KEY_RF)) {
                player->stop();
            }

            const auto actionHandled = optionSpinner->onInput(event);
            if (actionHandled && player->isInState(SoundsPlayer::State::Playing)) {
                player->play(getFilePath(optionSpinner->getCurrentValue()),
                             [=]() { this->navBarTemporaryMode(utils::translate(style::strings::common::play)); });
            }
            return actionHandled;
        };

        focusChangedCallback = [=](Item &item) {
            setFocusItem(focus ? optionSpinner : nullptr);

            if (focus) {
                this->navBarTemporaryMode(utils::translate(style::strings::common::play));
            }
            else {
                this->navBarRestoreFromTemporaryMode();
            }

            // stop preview playback when we loose focus
            if (!player->isInState(SoundsPlayer::State::Stopped)) {
                player->stop();
            }

            return true;
        };

        onSaveCallback = [=](std::shared_ptr<AlarmEventRecord> alarm) {
            // stop preview playback if it is played
            if (!player->isInState(SoundsPlayer::State::Stopped)) {
                player->stop();
            }
            alarm->musicTone = getFilePath(optionSpinner->getCurrentValue());
        };

        onLoadCallback = [&](std::shared_ptr<AlarmEventRecord> alarm) {
            optionSpinner->setCurrentValue(getTitle(alarm->musicTone));
        };
    }

    std::vector<tags::fetcher::Tags> AlarmMusicOptionsItem::getMusicFilesList()
    {
        const auto musicFolder = (purefs::dir::getAssetsDirPath() / "audio/alarm").string();
        std::vector<tags::fetcher::Tags> musicFiles;
        LOG_INFO("Scanning music folder: %s", musicFolder.c_str());
        for (const auto &ent : std::filesystem::directory_iterator(musicFolder)) {
            if (!ent.is_directory()) {
                const auto filePath = std::string(musicFolder) + "/" + ent.path().filename().c_str();
                auto fileTags       = tags::fetcher::fetchTags(filePath);
                musicFiles.push_back(fileTags);
                LOG_DEBUG("file: %s found", ent.path().filename().c_str());
            }
        }
        LOG_INFO("Total number of music files found: %u", static_cast<unsigned int>(musicFiles.size()));
        return musicFiles;
    }

    std::string AlarmMusicOptionsItem::getTitle(const std::string &filePath)
    {
        for (const auto &musicFile : alarmSoundList) {
            if (musicFile.filePath == filePath) {
                return musicFile.title;
            }
        }
        return std::string();
    }

    std::string AlarmMusicOptionsItem::getFilePath(const std::string &title)
    {
        for (const auto &musicFile : alarmSoundList) {
            if (musicFile.title == title) {
                return musicFile.filePath;
            }
        }
        return std::string();
    }
} /* namespace gui */