~aleteoryx/muditaos

ref: 14918dc4f968a6339aff07ff78ca2a56ccc2037a muditaos/module-apps/application-alarm-clock/widgets/AlarmOptionsItem.cpp -rw-r--r-- 11.6 KiB
14918dc4 — jimmorrisson [EGD-4925] Change new filesystem handling implementation in module-gui. (#1193) 5 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmOptionsItem.hpp"
#include "AlarmClockStyle.hpp"
#include <InputEvent.hpp>
#include <Style.hpp>
#include <Utils.hpp>
#include <module-services/service-audio/service-audio/AudioServiceAPI.hpp>
#include <purefs/filesystem_paths.hpp>

namespace gui
{

    AlarmOptionsItem::AlarmOptionsItem(app::Application *app,
                                       AlarmOptionItemName itemName,
                                       std::function<void(const UTF8 &)> bottomBarTemporaryMode,
                                       std::function<void()> bottomBarRestoreFromTemporaryMode)
        : itemName(itemName), bottomBarTemporaryMode(std::move(bottomBarTemporaryMode)),
          bottomBarRestoreFromTemporaryMode(std::move(bottomBarRestoreFromTemporaryMode))
    {
        application = app;
        assert(app != nullptr);

        setMinimumSize(style::window::default_body_width, style::alarmClock::window::item::options::height);

        setEdges(RectangleEdge::Bottom);
        setPenWidth(style::window::default_border_rect_no_focus);
        setMargins(gui::Margins(style::margins::small, style::margins::huge / 2, 0, style::margins::huge / 2));

        vBox = new gui::VBox(this, 0, 0, 0, 0);
        vBox->setEdges(gui::RectangleEdge::None);
        vBox->activeItem = false;

        descriptionLabel = new gui::Label(vBox, 0, 0, 0, 0);
        descriptionLabel->setMinimumSize(style::window::default_body_width,
                                         style::alarmClock::window::item::options::label_h);
        descriptionLabel->setEdges(gui::RectangleEdge::None);
        descriptionLabel->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        descriptionLabel->setFont(style::window::font::small);
        descriptionLabel->activeItem = false;

        hBox = new gui::HBox(vBox, 0, 0, 0, 0);
        hBox->setMinimumSize(style::window::default_body_width,
                             style::alarmClock::window::item::options::height -
                                 style::alarmClock::window::item::options::label_h);
        hBox->setEdges(gui::RectangleEdge::None);
        hBox->activeItem = false;

        leftArrow = new gui::Image(hBox, 0, 0, 0, 0);
        leftArrow->setMinimumSize(style::alarmClock::window::item::options::arrow_w_h,
                                  style::alarmClock::window::item::options::arrow_w_h);
        leftArrow->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        leftArrow->activeItem = false;
        leftArrow->set("arrow_left");
        leftArrow->setVisible(false);

        optionLabel = new gui::Label(hBox, 0, 0, 0, 0);
        optionLabel->setMinimumSize(
            style::window::default_body_width - 2 * style::alarmClock::window::item::options::arrow_w_h,
            style::alarmClock::window::item::options::height - style::alarmClock::window::item::options::label_h);
        optionLabel->setMargins(gui::Margins(style::alarmClock::window::item::options::arrow_w_h / 2, 0, 0, 0));
        optionLabel->setEdges(gui::RectangleEdge::None);
        optionLabel->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        optionLabel->setFont(style::window::font::medium);
        optionLabel->activeItem = false;

        rightArrow = new gui::Image(hBox, 0, 0, 0, 0);
        rightArrow->setMinimumSize(style::alarmClock::window::item::options::arrow_w_h,
                                   style::alarmClock::window::item::options::arrow_w_h);
        rightArrow->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
        rightArrow->activeItem = false;
        rightArrow->set("arrow_right");
        rightArrow->setVisible(false);

        prepareOptionsNames();
        applyCallbacks();
    }

    void AlarmOptionsItem::prepareOptionsNames()
    {
        songsList = getMusicFilesList();
        optionsNames.clear();
        switch (itemName) {
        case AlarmOptionItemName::Sound:
            descriptionLabel->setText(utils::localize.get("app_alarm_clock_sound"));
            break;
        case AlarmOptionItemName::Snooze:
            descriptionLabel->setText(utils::localize.get("app_alarm_clock_snooze"));
            break;
        case AlarmOptionItemName::Repeat:
            descriptionLabel->setText(utils::localize.get("app_alarm_clock_repeat"));
            break;
        }
        if (itemName == AlarmOptionItemName::Sound) {
            for (const auto &musicFile : songsList) {
                optionsNames.push_back(musicFile.title);
            }
        }
        else if (itemName == AlarmOptionItemName::Snooze) {
            optionsNames.push_back(utils::localize.get("app_alarm_clock_snooze_5_min"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_snooze_10_min"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_snooze_15_min"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_snooze_30_min"));
        }
        else if (itemName == AlarmOptionItemName::Repeat) {
            optionsNames.push_back(utils::localize.get("app_alarm_clock_repeat_never"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_repeat_everyday"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_repeat_week_days"));
            optionsNames.push_back(utils::localize.get("app_alarm_clock_repeat_custom"));
        }
    }

    void AlarmOptionsItem::applyCallbacks()
    {
        focusChangedCallback = [&](Item &item) {
            if (item.focus) {
                optionLabel->setFont(style::window::font::mediumbold);
                optionLabel->setMargins(gui::Margins(0, 0, 0, 0));
                leftArrow->setVisible(true);
                rightArrow->setVisible(true);
                hBox->resizeItems();
                if (itemName == AlarmOptionItemName::Sound) {
                    bottomBarTemporaryMode(utils::localize.get("app_alarm_clock_play_pause"));
                }
                if (itemName == AlarmOptionItemName::Repeat && actualVectorIndex == optionsNames.size() - 1) {
                    bottomBarTemporaryMode(utils::localize.get("app_alarm_clock_edit"));
                }
            }
            else {
                optionLabel->setFont(style::window::font::medium);
                optionLabel->setMargins(gui::Margins(style::alarmClock::window::item::options::arrow_w_h / 2, 0, 0, 0));
                leftArrow->setVisible(false);
                rightArrow->setVisible(false);
                hBox->resizeItems();
                bottomBarRestoreFromTemporaryMode();
            }
            return true;
        };

        inputCallback = [&](gui::Item &item, const gui::InputEvent &event) {
            if (event.state != gui::InputEvent::State::keyReleasedShort) {
                return false;
            }

            if (event.is(gui::KeyCode::KEY_LEFT)) {
                actualVectorIndex--;
                if (actualVectorIndex >= optionsNames.size()) {
                    actualVectorIndex = optionsNames.size() - 1;
                }
                optionLabel->setText(optionsNames[actualVectorIndex]);
                return true;
            }

            if (event.is(gui::KeyCode::KEY_RIGHT)) {
                actualVectorIndex++;
                if (actualVectorIndex >= optionsNames.size()) {
                    actualVectorIndex = 0;
                }
                optionLabel->setText(optionsNames[actualVectorIndex]);
                return true;
            }

            if (event.is(gui::KeyCode::KEY_LF) && itemName == AlarmOptionItemName::Sound) {
                if (musicStatus == MusicStatus::Stop) {
                    musicStatus = MusicStatus::Play;
                    AudioServiceAPI::PlaybackStart(
                        application, audio::PlaybackType::Multimedia, songsList[actualVectorIndex].filePath);
                }
                else if (musicStatus == MusicStatus::Play) {
                    musicStatus = MusicStatus::Stop;
                    AudioServiceAPI::StopAll(application);
                }
            }
            return false;
        };

        onSaveCallback = [&](std::shared_ptr<AlarmsRecord> alarm) {
            switch (itemName) {
            case AlarmOptionItemName::Sound: {
                alarm->path = songsList[actualVectorIndex].filePath;
                break;
            }
            case AlarmOptionItemName::Snooze: {
                alarm->snooze = static_cast<uint32_t>(snoozeOptions[actualVectorIndex]);
                break;
            }
            case AlarmOptionItemName::Repeat: {
                alarm->repeat = actualVectorIndex;
                break;
            }
            }
        };

        onLoadCallback = [&](std::shared_ptr<AlarmsRecord> alarm) {
            switch (itemName) {
            case AlarmOptionItemName::Sound: {
                auto it = std::find_if(songsList.begin(), songsList.end(), [alarm](const audio::Tags &tag) {
                    return tag.filePath == alarm->path.c_str();
                });
                if (it == songsList.end()) {
                    LOG_DEBUG("No such song in the list");
                    actualVectorIndex = 0;
                }
                else {
                    actualVectorIndex = std::distance(songsList.begin(), it);
                }
                break;
            }
            case AlarmOptionItemName::Snooze: {
                auto it =
                    std::find(snoozeOptions.begin(), snoozeOptions.end(), static_cast<AlarmSnooze>(alarm->snooze));
                if (it == snoozeOptions.end()) {
                    actualVectorIndex = 0;
                }
                else {
                    actualVectorIndex = std::distance(snoozeOptions.begin(), it);
                }
                break;
            }
            case AlarmOptionItemName::Repeat: {
                if (alarm->repeat < optionsNames.size() - 1) {
                    actualVectorIndex = alarm->repeat;
                }
                else {
                    actualVectorIndex = optionsNames.size() - 1;
                }
                break;
            }
            }
            optionLabel->setText(optionsNames[actualVectorIndex]);
        };
    }

    bool AlarmOptionsItem::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim)
    {
        vBox->setPosition(0, 0);
        vBox->setSize(newDim.w, newDim.h);
        return true;
    }

    std::vector<audio::Tags> AlarmOptionsItem::getMusicFilesList()
    {
        const auto musicFolder = (purefs::dir::getUserDiskPath() / "music").string();
        std::vector<audio::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       = AudioServiceAPI::GetFileTags(application, filePath);
                if (fileTags) {
                    musicFiles.push_back(*fileTags);
                    LOG_DEBUG("file: %s found", ent.path().filename().c_str());
                }
                else {
                    LOG_ERROR("Not an audio file %s", ent.path().filename().c_str());
                }
            }
        }
        LOG_INFO("Total number of music files found: %u", static_cast<unsigned int>(musicFiles.size()));
        return musicFiles;
    }

} /* namespace gui */