Extract Shade struct and create ShadeRepository
This commit is contained in:
66
lib/shade_repository/ShadeRepository.cpp
Normal file
66
lib/shade_repository/ShadeRepository.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "ShadeRepository.h"
|
||||
#include "Shade.h"
|
||||
|
||||
void ShadeRepository::upsert(Shade shade)
|
||||
{
|
||||
for (Shade &existing_shade : shades)
|
||||
{
|
||||
if (existing_shade.key == shade.key)
|
||||
{
|
||||
existing_shade.friendlyName = shade.friendlyName;
|
||||
existing_shade.ID = shade.ID;
|
||||
for (auto &callback : shadeChangedCallbacks) {
|
||||
callback(shade);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Didn't find it in the repository, add it
|
||||
shades.push_back(shade);
|
||||
for (auto &callback : shadeAddedCallbacks) {
|
||||
callback(shade);
|
||||
}
|
||||
}
|
||||
|
||||
Shade* ShadeRepository::findById(const uint16_t id)
|
||||
{
|
||||
for (Shade &shade : shades)
|
||||
{
|
||||
if (shade.ID == id)
|
||||
{
|
||||
return &shade;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Shade* ShadeRepository::findByKey(const std::string &key)
|
||||
{
|
||||
for (Shade &shade : shades)
|
||||
{
|
||||
if (shade.key == key)
|
||||
{
|
||||
return &shade;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<Shade>::iterator ShadeRepository::begin()
|
||||
{
|
||||
return shades.begin();
|
||||
}
|
||||
|
||||
std::vector<Shade>::iterator ShadeRepository::end()
|
||||
{
|
||||
return shades.end();
|
||||
}
|
||||
|
||||
void ShadeRepository::addShadeAddedCallback(std::function<void(Shade&)> callback) {
|
||||
shadeAddedCallbacks.push_back(callback);
|
||||
}
|
||||
|
||||
void ShadeRepository::addShadeChangedCallback(std::function<void(Shade&)> callback) {
|
||||
shadeChangedCallbacks.push_back(callback);
|
||||
}
|
||||
Reference in New Issue
Block a user