~aleteoryx/muditaos

ref: 1340e6949c0762585b0de844863c91c859fb19b1 muditaos/module-apps/application-music-player/models/SongsModel.cpp -rw-r--r-- 5.9 KiB
1340e694 — Bartosz Revert "[MOS-578] Fix incorrect logic with SMS notifications" 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SongsModel.hpp"
#include "Style.hpp"
#include "application-music-player/widgets/SongItem.hpp"

#include <ListView.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <time/time_conversion.hpp>

namespace app::music
{

    SongsListItemProvider::SongsListItemProvider(ApplicationCommon *app) : DatabaseModel(app)
    {}

    SongsModelInterface::SongsModelInterface(ApplicationCommon *app) : SongsListItemProvider(app)
    {}

    SongsModel::SongsModel(app::ApplicationCommon *app, std::shared_ptr<AbstractSongsRepository> songsRepository)
        : SongsModelInterface(app), songsRepository{std::move(songsRepository)}
    {}

    auto SongsModel::requestRecordsCount() -> unsigned int
    {
        return recordsCount;
    }

    auto SongsModel::getMinimalItemSpaceRequired() const -> unsigned int
    {
        return musicPlayerStyle::songItem::h + style::margins::small * 2;
    }

    void SongsModel::requestRecords(const uint32_t offset, const uint32_t limit)
    {
        songsRepository->getMusicFilesList(
            offset,
            limit,
            [this](const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                   unsigned int repoRecordsCount) { return onMusicListRetrieved(records, repoRecordsCount); });
    }

    auto SongsModel::getItem(gui::Order order) -> gui::ListItem *
    {
        std::shared_ptr<db::multimedia_files::MultimediaFilesRecord> song = getRecord(order);
        if (!song) {
            return nullptr;
        }

        auto item = new gui::SongItem(song->tags.album.artist,
                                      song->tags.title,
                                      utils::time::Duration(song->audioProperties.songLength).str(),
                                      navBarTemporaryMode,
                                      navBarRestoreFromTemporaryMode);

        if (songContext.filePath == song->fileInfo.path) {
            item->setState(songContext.isPlaying() ? gui::SongItem::ItemState::Playing
                                                   : (songContext.isPaused() ? gui::SongItem::ItemState::Paused
                                                                             : gui::SongItem::ItemState::None));
        }
        else {
            item->setState(gui::SongItem::ItemState::None);
        }

        item->activatedCallback = [this, song](gui::Item &) {
            if (shortReleaseCallback != nullptr) {
                activatedRecord = *song;
                shortReleaseCallback(song->fileInfo.path);
                return true;
            }
            return false;
        };

        item->inputCallback = [=](gui::Item &, const gui::InputEvent &event) {
            if (event.isLongRelease(gui::KeyCode::KEY_ENTER)) {
                if (longPressCallback != nullptr) {
                    longPressCallback();
                    return true;
                }
            }
            return false;
        };

        return item;
    }

    void SongsModel::createData(OnShortReleaseCallback shortReleaseCallback,
                                OnLongPressCallback longPressCallback,
                                OnSetNavBarTemporaryCallback navBarTemporaryMode,
                                OnRestoreNavBarTemporaryCallback navBarRestoreFromTemporaryMode)
    {
        this->shortReleaseCallback           = shortReleaseCallback;
        this->longPressCallback              = longPressCallback;
        this->navBarTemporaryMode            = navBarTemporaryMode;
        this->navBarRestoreFromTemporaryMode = navBarRestoreFromTemporaryMode;

        songsRepository->initCache();
    }

    bool SongsModel::isSongPlaying() const noexcept
    {
        return songContext.currentSongState == SongState::Playing;
    }

    void SongsModel::setCurrentSongState(SongState songState) noexcept
    {
        songContext.currentSongState = songState;
    }

    std::optional<audio::Token> SongsModel::getCurrentFileToken() const noexcept
    {
        return songContext.currentFileToken;
    }

    std::optional<db::multimedia_files::MultimediaFilesRecord> SongsModel::getActivatedRecord() const noexcept
    {
        return activatedRecord;
    }

    std::string SongsModel::getNextFilePath(const std::string &filePath) const
    {
        return songsRepository->getNextFilePath(filePath);
    }

    std::string SongsModel::getPreviousFilePath(const std::string &filePath) const
    {
        return songsRepository->getPreviousFilePath(filePath);
    }

    SongContext SongsModel::getCurrentSongContext() const noexcept
    {
        return songContext;
    }

    void SongsModel::setCurrentSongContext(SongContext context)
    {
        songContext     = context;
        activatedRecord = songsRepository->getRecord(context.filePath);
    }

    void SongsModel::clearCurrentSongContext()
    {
        songContext.clear();
        activatedRecord = std::nullopt;
    }

    void SongsModel::clearData()
    {
        if (list != nullptr) {
            list->reset();
        }
    }

    [[nodiscard]] bool SongsModel::updateRecords(std::vector<db::multimedia_files::MultimediaFilesRecord> records)
    {
        DatabaseModel::updateRecords(std::move(records));
        list->onProviderDataUpdate();
        return true;
    }

    bool SongsModel::onMusicListRetrieved(const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                                          unsigned int repoRecordsCount)
    {
        if (list != nullptr && recordsCount != repoRecordsCount) {
            recordsCount = repoRecordsCount;
            list->reSendLastRebuildRequest();
            return false;
        }
        return updateRecords(records);
    }

    void SongsModel::updateRepository(const std::string &filePath)
    {
        songsRepository->updateRepository(filePath);
    }

} // namespace app::music