2021-05-22 16:41:36 +02:00
|
|
|
#include "data.hpp"
|
|
|
|
#include <core/global/globals.hpp>
|
2021-02-15 21:11:13 +01:00
|
|
|
#include <fancy.hpp>
|
|
|
|
|
|
|
|
namespace Soundux::Objects
|
|
|
|
{
|
2021-02-16 15:40:26 +01:00
|
|
|
Tab Data::addTab(Tab tab)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
|
|
|
tab.id = tabs.size();
|
2021-02-23 23:19:15 +01:00
|
|
|
tabs.emplace_back(tab);
|
2021-02-15 21:11:13 +01:00
|
|
|
|
2021-02-16 00:50:27 +01:00
|
|
|
for (auto &sound : tabs.back().sounds)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->insert({sound.id, sound});
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->insert({sound.id, sound});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
2021-02-16 15:40:26 +01:00
|
|
|
|
|
|
|
return tabs.back();
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
void Data::removeTabById(const std::uint32_t &index)
|
|
|
|
{
|
|
|
|
if (tabs.size() > index)
|
|
|
|
{
|
|
|
|
auto &tab = tabs.at(index);
|
|
|
|
for (auto &sound : tab.sounds)
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->erase(sound.id);
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->erase(sound.id);
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tabs.erase(tabs.begin() + index);
|
2021-02-19 14:08:16 +01:00
|
|
|
|
2021-03-04 10:36:10 +01:00
|
|
|
for (std::size_t i = 0; tabs.size() > i; i++)
|
2021-02-19 14:08:16 +01:00
|
|
|
{
|
|
|
|
tabs.at(i).id = i;
|
|
|
|
}
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-12 00:34:22 +01:00
|
|
|
Fancy::fancy.logTime().warning() << "Tried to remove non existent tab" << std::endl;
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void Data::setTabs(const std::vector<Tab> &newTabs)
|
|
|
|
{
|
|
|
|
tabs = newTabs;
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->clear();
|
|
|
|
Globals::gFavorites->clear();
|
2021-03-04 10:36:10 +01:00
|
|
|
for (std::size_t i = 0; tabs.size() > i; i++)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-02-19 14:08:16 +01:00
|
|
|
auto &tab = tabs.at(i);
|
|
|
|
tab.id = i;
|
|
|
|
|
2021-02-16 00:50:27 +01:00
|
|
|
for (auto &sound : tab.sounds)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->insert({sound.id, sound});
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->insert({sound.id, sound});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<Tab> Data::getTabs() const
|
|
|
|
{
|
|
|
|
return tabs;
|
|
|
|
}
|
2021-02-16 23:21:23 +01:00
|
|
|
std::optional<Tab> Data::getTab(const std::uint32_t &id) const
|
|
|
|
{
|
|
|
|
if (tabs.size() > id)
|
|
|
|
{
|
|
|
|
return tabs.at(id);
|
|
|
|
}
|
|
|
|
|
2021-03-12 00:34:22 +01:00
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existent tab " << id << std::endl;
|
2021-02-16 23:21:23 +01:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-02-16 00:50:27 +01:00
|
|
|
std::optional<std::reference_wrapper<Sound>> Data::getSound(const std::uint32_t &id)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
auto scopedSounds = Globals::gSounds.scoped();
|
|
|
|
if (scopedSounds->find(id) != scopedSounds->end())
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
return scopedSounds->at(id);
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 00:34:22 +01:00
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existent sound " << id << std::endl;
|
2021-02-15 21:11:13 +01:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-02-19 14:08:16 +01:00
|
|
|
std::optional<Tab> Data::setTab(const std::uint32_t &id, const Tab &tab)
|
|
|
|
{
|
|
|
|
if (tabs.size() > id)
|
|
|
|
{
|
|
|
|
auto &realTab = tabs.at(id);
|
|
|
|
|
|
|
|
for (const auto &sound : realTab.sounds)
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->erase(sound.id);
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->erase(sound.id);
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-02-19 14:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
realTab = tab;
|
|
|
|
|
|
|
|
for (auto &sound : realTab.sounds)
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->insert({sound.id, sound});
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->insert({sound.id, sound});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-02-19 14:08:16 +01:00
|
|
|
}
|
|
|
|
return realTab;
|
|
|
|
}
|
|
|
|
|
2021-03-12 00:34:22 +01:00
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existent Tab " << id << std::endl;
|
2021-02-19 14:08:16 +01:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-03-05 23:14:43 +01:00
|
|
|
void Data::set(const Data &other)
|
2021-02-20 02:40:44 +01:00
|
|
|
{
|
|
|
|
tabs = other.tabs;
|
|
|
|
width = other.width;
|
|
|
|
height = other.height;
|
|
|
|
soundIdCounter = other.soundIdCounter;
|
|
|
|
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->clear();
|
|
|
|
Globals::gFavorites->clear();
|
2021-02-20 02:40:44 +01:00
|
|
|
|
2021-03-04 10:36:10 +01:00
|
|
|
for (std::size_t i = 0; tabs.size() > i; i++)
|
2021-02-20 02:40:44 +01:00
|
|
|
{
|
|
|
|
auto &tab = tabs.at(i);
|
|
|
|
tab.id = i;
|
|
|
|
|
|
|
|
for (auto &sound : tab.sounds)
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gSounds->insert({sound.id, sound});
|
2021-03-10 08:36:08 +01:00
|
|
|
if (sound.isFavorite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->insert({sound.id, sound});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-25 19:53:10 +02:00
|
|
|
void Data::markFavorite(const std::uint32_t &id, bool favourite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
|
|
|
auto sound = getSound(id);
|
2021-04-03 16:37:48 +02:00
|
|
|
if (sound)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-04-03 16:37:48 +02:00
|
|
|
sound->get().isFavorite = favourite;
|
|
|
|
if (favourite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
Globals::gFavorites->insert({id, sound->get()});
|
2021-04-03 16:37:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
auto scopedFavorites = Globals::gFavorites.scoped();
|
|
|
|
if (scopedFavorites->find(id) != scopedFavorites->end())
|
2021-04-03 16:37:48 +02:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
scopedFavorites->erase(id);
|
2021-04-03 16:37:48 +02:00
|
|
|
}
|
2021-02-20 02:40:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-25 19:53:10 +02:00
|
|
|
}
|
|
|
|
std::vector<std::uint32_t> Data::getFavoriteIds()
|
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
auto scopedFavorites = Globals::gFavorites.scoped();
|
2021-04-25 19:53:10 +02:00
|
|
|
|
|
|
|
std::vector<std::uint32_t> rtn;
|
2021-05-21 20:23:24 +02:00
|
|
|
rtn.reserve(scopedFavorites->size());
|
2021-04-25 19:53:10 +02:00
|
|
|
|
2021-05-21 20:23:24 +02:00
|
|
|
for (const auto &sound : *scopedFavorites)
|
2021-04-25 19:53:10 +02:00
|
|
|
{
|
|
|
|
rtn.emplace_back(sound.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn;
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
2021-03-11 17:24:46 +01:00
|
|
|
std::vector<Sound> Data::getFavorites()
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-05-21 20:23:24 +02:00
|
|
|
auto scopedFavorites = Globals::gFavorites.scoped();
|
2021-03-09 20:38:09 +01:00
|
|
|
|
|
|
|
std::vector<Sound> rtn;
|
2021-05-21 20:23:24 +02:00
|
|
|
rtn.reserve(scopedFavorites->size());
|
2021-03-09 20:38:09 +01:00
|
|
|
|
2021-05-21 20:23:24 +02:00
|
|
|
for (const auto &sound : *scopedFavorites)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
|
|
|
rtn.emplace_back(sound.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn;
|
2021-02-20 02:40:44 +01:00
|
|
|
}
|
2021-02-15 21:11:13 +01:00
|
|
|
} // namespace Soundux::Objects
|