Soundux/src/core/objects/data.cpp

198 lines
5.3 KiB
C++
Raw Normal View History

#include "data.hpp"
#include <core/global/globals.hpp>
#include <fancy.hpp>
namespace Soundux::Objects
{
Tab Data::addTab(Tab tab)
{
tab.id = tabs.size();
tabs.emplace_back(tab);
for (auto &sound : tabs.back().sounds)
{
2021-05-21 20:23:24 +02:00
Globals::gSounds->insert({sound.id, sound});
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
}
}
return tabs.back();
}
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);
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
}
}
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;
}
}
else
{
2021-03-12 00:34:22 +01:00
Fancy::fancy.logTime().warning() << "Tried to remove non existent tab" << std::endl;
}
}
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-19 14:08:16 +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});
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
}
}
}
}
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;
}
std::optional<std::reference_wrapper<Sound>> Data::getSound(const std::uint32_t &id)
{
2021-05-21 20:23:24 +02:00
auto scopedSounds = Globals::gSounds.scoped();
if (scopedSounds->find(id) != scopedSounds->end())
{
2021-05-21 20:23:24 +02:00
return scopedSounds->at(id);
}
2021-03-12 00:34:22 +01:00
Fancy::fancy.logTime().warning() << "Tried to access non existent sound " << id << std::endl;
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);
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});
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;
}
void Data::set(const Data &other)
{
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-03-04 10:36:10 +01:00
for (std::size_t i = 0; tabs.size() > i; i++)
{
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});
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
}
}
}
}
void Data::markFavorite(const std::uint32_t &id, bool favourite)
2021-03-09 20:38:09 +01:00
{
auto sound = getSound(id);
if (sound)
2021-03-09 20:38:09 +01: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()});
}
else
{
2021-05-21 20:23:24 +02:00
auto scopedFavorites = Globals::gFavorites.scoped();
if (scopedFavorites->find(id) != scopedFavorites->end())
{
2021-05-21 20:23:24 +02:00
scopedFavorites->erase(id);
}
}
}
}
std::vector<std::uint32_t> Data::getFavoriteIds()
{
2021-05-21 20:23:24 +02:00
auto scopedFavorites = Globals::gFavorites.scoped();
std::vector<std::uint32_t> rtn;
2021-05-21 20:23:24 +02:00
rtn.reserve(scopedFavorites->size());
2021-05-21 20:23:24 +02:00
for (const auto &sound : *scopedFavorites)
{
rtn.emplace_back(sound.first);
}
return rtn;
2021-03-09 20:38:09 +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;
}
} // namespace Soundux::Objects