~aleteoryx/muditaos

ref: 13af1f4cde458a9124bd315732be1e689a311bf0 muditaos/module-apps/application-music-player/models/SongsModel.cpp -rw-r--r-- 4.8 KiB
13af1f4c — Lucjan Bryndza [EGD-7334] Add initial startup indexer 4 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
// 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_player
{

    SongsModel::SongsModel(std::shared_ptr<AbstractSongsRepository> songsRepository)
        : songsRepository{std::move(songsRepository)}
    {}

    auto SongsModel::requestRecordsCount() -> unsigned int
    {
        return internalData.size();
    }

    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)
    {
        setupModel(offset, limit);
        list->onProviderDataUpdate();
    }

    auto SongsModel::getItem(gui::Order order) -> gui::ListItem *
    {
        return getRecord(order);
    }

    void SongsModel::createData(OnShortReleaseCallback shortReleaseCallback,
                                OnLongPressCallback longPressCallback,
                                OnSetBottomBarTemporaryCallback bottomBarTemporaryMode,
                                OnRestoreBottomBarTemporaryCallback bottomBarRestoreFromTemporaryMode)
    {
        songsRepository->scanMusicFilesList();
        auto songsList = songsRepository->getMusicFilesList();
        for (const auto &song : songsList) {
            auto item = new gui::SongItem(song.artist,
                                          song.title,
                                          utils::time::Duration(song.total_duration_s).str(),
                                          bottomBarTemporaryMode,
                                          bottomBarRestoreFromTemporaryMode);

            item->activatedCallback = [=](gui::Item &) {
                shortReleaseCallback(song.filePath);
                return true;
            };

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

            internalData.push_back(item);
        }

        for (auto &item : internalData) {
            item->deleteByList = false;
        }
    }

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

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

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

    size_t SongsModel::getCurrentIndex() const
    {
        auto index = songsRepository->getFileIndex(songContext.filePath);
        return index == std::numeric_limits<size_t>::max() ? 0 : index;
    }

    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)
    {
        using namespace gui;
        clearCurrentItemState();

        songContext = context;

        updateCurrentItemState();
    }

    void SongsModel::clearCurrentSongContext()
    {
        clearCurrentItemState();
        songContext.clear();
    }

    void SongsModel::clearCurrentItemState()
    {
        using namespace gui;
        const auto songIndex = getCurrentIndex();
        if (songIndex < internalData.size()) {
            internalData[songIndex]->setState(SongItem::ItemState::None);
        }
    }

    void SongsModel::updateCurrentItemState()
    {
        using namespace gui;
        const auto songIndex = getCurrentIndex();
        if (songIndex >= internalData.size()) {
            return;
        }

        if (songContext.isPlaying()) {
            internalData[songIndex]->setState(SongItem::ItemState::Playing);
        }
        else if (songContext.isPaused()) {
            internalData[songIndex]->setState(SongItem::ItemState::Paused);
        }
        else {
            internalData[songIndex]->setState(SongItem::ItemState::None);
        }
    }

    void SongsModel::clearData()
    {
        list->reset();
        eraseInternalData();
    }
} // namespace app::music_player