2021-02-15 21:11:13 +01:00
|
|
|
#include "objects.hpp"
|
|
|
|
#include "globals.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fancy.hpp>
|
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
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
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock favLock(Globals::gFavoritesMutex);
|
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
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01: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)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock favLock(Globals::gFavoritesMutex);
|
2021-02-15 21:11:13 +01:00
|
|
|
if (tabs.size() > index)
|
|
|
|
{
|
|
|
|
auto &tab = tabs.at(index);
|
|
|
|
for (auto &sound : tab.sounds)
|
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01: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
|
|
|
|
{
|
|
|
|
Fancy::fancy.logTime().warning() << "Tried to remove non existant tab" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Data::setTabs(const std::vector<Tab> &newTabs)
|
|
|
|
{
|
|
|
|
tabs = newTabs;
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock favLock(Globals::gFavoritesMutex);
|
2021-02-15 21:11:13 +01:00
|
|
|
|
|
|
|
Globals::gSounds.clear();
|
2021-03-11 17:24:46 +01:00
|
|
|
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
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existant tab " << id << std::endl;
|
|
|
|
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
|
|
|
{
|
|
|
|
std::shared_lock lock(Globals::gSoundsMutex);
|
|
|
|
|
|
|
|
if (Globals::gSounds.find(id) != Globals::gSounds.end())
|
|
|
|
{
|
|
|
|
return Globals::gSounds.at(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existant 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);
|
|
|
|
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock favLock(Globals::gFavoritesMutex);
|
2021-02-19 14:08:16 +01:00
|
|
|
for (const auto &sound : realTab.sounds)
|
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01: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)
|
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fancy::fancy.logTime().warning() << "Tried to access non existant Tab " << id << std::endl;
|
|
|
|
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;
|
|
|
|
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock favLock(Globals::gFavoritesMutex);
|
2021-02-20 02:40:44 +01:00
|
|
|
Globals::gSounds.clear();
|
2021-03-11 17:24:46 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-03-11 17:24:46 +01:00
|
|
|
Globals::gFavorites.insert({sound.id, sound});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 17:24:46 +01:00
|
|
|
std::optional<Sound> Data::markFavorite(const std::uint32_t &id, bool favourite)
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
|
|
|
auto sound = getSound(id);
|
|
|
|
if (!sound)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:36:08 +01:00
|
|
|
sound->get().isFavorite = favourite;
|
2021-03-09 20:38:09 +01:00
|
|
|
if (favourite)
|
|
|
|
{
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock lock(Globals::gFavoritesMutex);
|
|
|
|
Globals::gFavorites.insert({id, sound->get()});
|
2021-03-09 20:38:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-11 17:24:46 +01:00
|
|
|
std::unique_lock lock(Globals::gFavoritesMutex);
|
|
|
|
if (Globals::gFavorites.find(id) != Globals::gFavorites.end())
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-03-11 17:24:46 +01:00
|
|
|
Globals::gFavorites.erase(id);
|
2021-02-20 02:40:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-09 20:38:09 +01:00
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
2021-03-11 17:24:46 +01:00
|
|
|
std::vector<Sound> Data::getFavorites()
|
2021-03-09 20:38:09 +01:00
|
|
|
{
|
2021-03-11 17:24:46 +01:00
|
|
|
std::shared_lock lock(Globals::gFavoritesMutex);
|
2021-03-09 20:38:09 +01:00
|
|
|
|
|
|
|
std::vector<Sound> rtn;
|
2021-03-11 17:24:46 +01:00
|
|
|
rtn.reserve(Globals::gFavorites.size());
|
2021-03-09 20:38:09 +01:00
|
|
|
|
2021-03-11 17:24:46 +01:00
|
|
|
for (const auto &sound : Globals::gFavorites)
|
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
|