~aleteoryx/muditaos

ref: 6b3fdbc99f17803845fa355ddf29a3a94720a746 muditaos/module-apps/application-settings-new/models/SoundsModel.cpp -rw-r--r-- 4.1 KiB
6b3fdbc9 — tomaszkrosnowski [EGD-6613] Audio assets tags are not displayed 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "SoundsModel.hpp"

#include <ListView.hpp>
#include <purefs/filesystem_paths.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <time/time_conversion.hpp>
#include <Style.hpp>
#include <filesystem>

unsigned int SoundsModel::requestRecordsCount()
{
    return internalData.size();
}

unsigned int SoundsModel::getMinimalItemHeight() const
{
    return style::listview::scroll::min_space;
}

void SoundsModel::requestRecords(const uint32_t offset, const uint32_t limit)
{
    setupModel(offset, limit);
    list->onProviderDataUpdate();
}

gui::ListItem *SoundsModel::getItem(gui::Order order)
{
    return getRecord(order);
}

void SoundsModel::createData(app::Application *app, audio_settings::AbstractAudioSettingsModel *model)
{
    assert(model);

    // configure according to type
    std::filesystem::path folder = getSoundPath(model);

    // iterate through selected folder and collect all sounds names
    std::vector<std::filesystem::path> sounds;
    if (std::filesystem::is_directory(folder)) {
        LOG_INFO("Scanning sound folder: %s", folder.c_str());
        for (const auto &entry : std::filesystem::directory_iterator(folder)) {
            if (std::filesystem::is_directory(entry)) {
                continue;
            }

            const auto &filePath = entry.path();
            if (filePath.extension() == ".mp3")
                sounds.push_back(filePath);
        }
        LOG_INFO("Found %d sounds in folder %s", static_cast<int>(sounds.size()), folder.c_str());
    }
    else {
        LOG_ERROR("Cannot find directory: %s", folder.c_str());
    }

    applyItems(sounds, app, model);
}

void SoundsModel::clearData()
{
    list->clear();
    list->rebuildList();
}

std::filesystem::path SoundsModel::getSoundPath(audio_settings::AbstractAudioSettingsModel *model)
{
    assert(model);
    switch (model->getPlaybackType()) {
    case audio::PlaybackType::CallRingtone:
        return purefs::dir::getCurrentOSPath() / "assets/audio/ringtone";

    case audio::PlaybackType::TextMessageRingtone:
        return purefs::dir::getCurrentOSPath() / "assets/audio/sms";

    case audio::PlaybackType::Notifications:
        return purefs::dir::getCurrentOSPath() / "assets/audio/alarm";

    default:
        return purefs::dir::getCurrentOSPath() / "assets/audio";
    }
}

void SoundsModel::applyItems(const std::vector<std::filesystem::path> &sounds,
                             app::Application *app,
                             audio_settings::AbstractAudioSettingsModel *model)
{
    std::string selectedSound = purefs::dir::getCurrentOSPath() / model->getSound();
    for (const auto &sound : sounds) {
        bool isSelected = false;
        if (sound == selectedSound) {
            isSelected = true;
        }

        std::string itemTitle;
        auto fileTags = AudioServiceAPI::GetFileTags(app, sound);
        if (fileTags) {
            itemTitle = fileTags->title;
        }

        if (itemTitle.empty()) {
            itemTitle = sound.filename();
        }

        auto item = new gui::SettingsSoundItem(itemTitle, isSelected);

        switch (model->getPlaybackType()) {

        case audio::PlaybackType::CallRingtone:
        case audio::PlaybackType::TextMessageRingtone:
        case audio::PlaybackType::Notifications:
            item->activatedCallback = [=](gui::Item &) {
                auto fileRelativePath = sound.lexically_relative(purefs::dir::getCurrentOSPath());
                LOG_INFO("Setting sound to %s", fileRelativePath.c_str());
                model->setSound(fileRelativePath);
                app->returnToPreviousWindow();
                return true;
            };
            break;

        default:
            item->activatedCallback = [=](gui::Item &) {
                app->returnToPreviousWindow();
                return true;
            };
            break;
        }

        internalData.push_back(item);
    }

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

    list->rebuildList();
}