~aleteoryx/muditaos

ref: 8109062f8dc3db5d7151bd73e88f8f0398e14e50 muditaos/module-apps/apps-common/models/SongsRepository.cpp -rw-r--r-- 10.6 KiB
8109062f — rrandomsky [MOS-723] Fix for french translation for SIM cards 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SongsRepository.hpp"

#include <algorithm>
#include <log/log.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <service-audio/AudioServiceName.hpp>
#include <time/ScopedTime.hpp>
#include <service-audio/AudioMessage.hpp>
#include <module-db/queries/multimedia_files/QueryMultimediaFilesGetLimited.hpp>
#include <module-db/queries/multimedia_files/QueryMultimediaFilesGet.hpp>

#include <filesystem>

namespace constants
{
    inline constexpr auto cacheMaxSize{30};
    inline constexpr auto cacheThreshold{10};
} // namespace constants

namespace app::music
{
    ServiceAudioTagsFetcher::ServiceAudioTagsFetcher(ApplicationCommon *application) : application(application)
    {}

    std::optional<tags::fetcher::Tags> ServiceAudioTagsFetcher::getFileTags(const std::string &filePath) const
    {
        return tags::fetcher::fetchTags(filePath);
    }

    SongsRepository::SongsRepository(ApplicationCommon *application,
                                     std::unique_ptr<AbstractTagsFetcher> tagsFetcher,
                                     std::string pathPrefix)
        : app::AsyncCallbackReceiver{application}, application{application},
          tagsFetcher(std::move(tagsFetcher)), pathPrefix{pathPrefix}
    {}

    void SongsRepository::initCache()
    {
        auto query = std::make_unique<db::multimedia_files::query::GetLimited>(0, constants::cacheMaxSize);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::MultimediaFiles);
        musicFilesModelCache.recordsOffset = 0;

        task->setCallback([this](auto response) {
            auto result = dynamic_cast<db::multimedia_files::query::GetLimitedResult *>(response);

            if (result == nullptr) {
                return false;
            }

            return initCacheCallback(result->getResult(), result->getCount());
        });
        task->execute(application, this);
    }

    void SongsRepository::getMusicFilesList(std::uint32_t offset,
                                            std::uint32_t limit,
                                            const OnGetMusicFilesListCallback &callback)
    {
        auto query = std::make_unique<db::multimedia_files::query::GetLimitedByPath>(pathPrefix, offset, limit);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::MultimediaFiles);

        task->setCallback([this, callback, offset](auto response) {
            auto result = dynamic_cast<db::multimedia_files::query::GetLimitedResult *>(response);
            musicFilesViewCache.records.clear();

            if (result == nullptr) {
                return false;
            }

            for (auto &record : result->getResult()) {
                musicFilesViewCache.records.push_back(record);
            }
            musicFilesViewCache.recordsOffset = offset;
            musicFilesViewCache.recordsCount  = result->getCount();

            if (callback) {
                callback(musicFilesViewCache.records, musicFilesViewCache.recordsCount);
            }
            return true;
        });
        task->execute(application, this);
    }

    std::size_t SongsRepository::getCachedFileIndex(const std::string &filePath) const
    {
        auto it = std::find_if(musicFilesModelCache.records.begin(),
                               musicFilesModelCache.records.end(),
                               [filePath](const auto &musicFile) { return musicFile.fileInfo.path == filePath; });

        if (it != musicFilesModelCache.records.end()) {
            return std::distance(musicFilesModelCache.records.begin(), it);
        }

        return std::numeric_limits<size_t>::max();
    }

    std::string SongsRepository::getNextFilePath(const std::string &filePath) const
    {
        const auto currentIndex = getCachedFileIndex(filePath);

        if (currentIndex == std::numeric_limits<size_t>::max() ||
            currentIndex >= musicFilesModelCache.records.size() - 1) {
            return "";
        }
        return musicFilesModelCache.records[currentIndex + 1].fileInfo.path;
    }

    std::string SongsRepository::getPreviousFilePath(const std::string &filePath) const
    {
        const auto currentIndex = getCachedFileIndex(filePath);

        if (currentIndex == std::numeric_limits<size_t>::max() || currentIndex == 0 ||
            currentIndex > musicFilesModelCache.records.size()) {
            return "";
        }
        return musicFilesModelCache.records[currentIndex - 1].fileInfo.path;
    }

    std::uint32_t SongsRepository::calculateOffset()
    {
        if (musicFilesViewCache.recordsCount < constants::cacheMaxSize) {
            return 0;
        }
        else if (musicFilesViewCache.recordsOffset < constants::cacheThreshold) {
            return 0;
        }
        else if (musicFilesViewCache.recordsOffset >
                 musicFilesViewCache.recordsCount - (constants::cacheMaxSize - constants::cacheThreshold)) {
            return musicFilesViewCache.recordsCount - constants::cacheMaxSize;
        }
        else {
            return musicFilesViewCache.recordsOffset - constants::cacheThreshold;
        }
    }

    void SongsRepository::updateRepository(const std::string &filePath)
    {
        const auto currentIndex = getCachedFileIndex(filePath);

        if (currentIndex == std::numeric_limits<size_t>::max()) {
            updateMusicFilesList(
                calculateOffset(),
                constants::cacheMaxSize,
                [this](const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                       unsigned int repoRecordsCount,
                       std::uint32_t offset) { return newDataCallback(records, repoRecordsCount, offset); });
        }
        else if (currentIndex > musicFilesModelCache.records.size() - constants::cacheThreshold) {
            updateMusicFilesList(
                musicFilesModelCache.recordsOffset + musicFilesModelCache.records.size() + 1,
                constants::cacheThreshold,
                [this](const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                       unsigned int repoRecordsCount,
                       std::uint32_t offset) { return newBackDataCallback(records, repoRecordsCount, offset); });
        }
        else if (currentIndex < constants::cacheThreshold) {
            updateMusicFilesList(
                musicFilesModelCache.recordsOffset > constants::cacheThreshold
                    ? musicFilesModelCache.recordsOffset - constants::cacheThreshold
                    : 0,
                musicFilesModelCache.recordsOffset > constants::cacheThreshold ? constants::cacheThreshold
                                                                               : musicFilesModelCache.recordsOffset,
                [this](const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                       unsigned int repoRecordsCount,
                       std::uint32_t offset) { return newFrontDataCallback(records, repoRecordsCount, offset); });
        }
    }

    void SongsRepository::updateMusicFilesList(std::uint32_t offset,
                                               std::uint32_t limit,
                                               const OnUpdateMusicFilesListCallback &callback)
    {
        auto query = std::make_unique<db::multimedia_files::query::GetLimited>(offset, limit);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::MultimediaFiles);

        task->setCallback([offset, callback](auto response) {
            auto result = dynamic_cast<db::multimedia_files::query::GetLimitedResult *>(response);

            if (result == nullptr) {
                return false;
            }

            return callback(result->getResult(), result->getCount(), offset);
        });
        task->execute(application, this);
    }

    bool SongsRepository::initCacheCallback(const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                                            unsigned int repoRecordsCount)
    {
        musicFilesModelCache.records.clear();
        for (auto &record : records) {
            musicFilesModelCache.records.push_back(record);
        }
        musicFilesModelCache.recordsOffset = 0;
        musicFilesModelCache.recordsCount  = repoRecordsCount;

        return true;
    }

    bool SongsRepository::newDataCallback(const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                                          unsigned int repoRecordsCount,
                                          std::uint32_t offset)
    {
        musicFilesModelCache.records.clear();
        for (auto &record : records) {
            musicFilesModelCache.records.push_back(record);
        }
        musicFilesModelCache.recordsOffset = offset;
        musicFilesModelCache.recordsCount  = repoRecordsCount;

        return true;
    }

    bool SongsRepository::newBackDataCallback(const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                                              unsigned int repoRecordsCount,
                                              std::uint32_t offset)
    {
        for (auto &record : records) {
            musicFilesModelCache.records.push_back(record);
            musicFilesModelCache.records.erase(musicFilesModelCache.records.begin(),
                                               musicFilesModelCache.records.begin() + 1);
            musicFilesModelCache.recordsOffset++;
        }
        musicFilesModelCache.recordsCount = repoRecordsCount;

        return true;
    }

    bool SongsRepository::newFrontDataCallback(const std::vector<db::multimedia_files::MultimediaFilesRecord> &records,
                                               unsigned int repoRecordsCount,
                                               std::uint32_t offset)
    {
        auto iterator = 0;
        for (auto &record : records) {
            musicFilesModelCache.records.insert(musicFilesModelCache.records.begin() + iterator, record);
            musicFilesModelCache.records.pop_back();
            iterator++;
        }
        musicFilesModelCache.recordsOffset = offset;
        musicFilesModelCache.recordsCount  = repoRecordsCount;

        return true;
    }

    std::optional<db::multimedia_files::MultimediaFilesRecord> SongsRepository::getRecord(
        const std::string &filePath) const
    {
        const auto index = getCachedFileIndex(filePath);
        if (index == std::numeric_limits<size_t>::max()) {
            return std::nullopt;
        }
        return musicFilesModelCache.records[index];
    }

} // namespace app::music