Refactored AchievementProgress UpdateData to Re-Sort

When AchievementProgress::UpdateData(false) is called, it will now empty itself and reinsert all existing boxes, re-sorted into their current buckets, and call UpdateProgress on them all.
This commit is contained in:
LillyJadeKatrin 2024-06-19 22:54:24 -04:00
parent 75c2360aea
commit 9b9e6c4582
2 changed files with 43 additions and 21 deletions

View File

@ -62,6 +62,9 @@ AchievementBox::AchievementBox(QWidget* parent, rc_client_achievement_t* achieve
void AchievementBox::UpdateData()
{
std::lock_guard lg{AchievementManager::GetInstance().GetLock()};
// rc_client guarantees m_achievement will be valid as long as the game is loaded
if (!AchievementManager::GetInstance().IsGameLoaded())
return;
const auto& badge = AchievementManager::GetInstance().GetAchievementBadge(
m_achievement->id, m_achievement->state != RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED);
@ -92,6 +95,11 @@ void AchievementBox::UpdateData()
void AchievementBox::UpdateProgress()
{
std::lock_guard lg{AchievementManager::GetInstance().GetLock()};
// rc_client guarantees m_achievement will be valid as long as the game is loaded
if (!AchievementManager::GetInstance().IsGameLoaded())
return;
if (m_achievement->measured_percent > 0.000)
{
m_progress_bar->setRange(0, 100);

View File

@ -42,13 +42,27 @@ void AchievementProgressWidget::UpdateData(bool clean_all)
{
m_achievement_boxes.clear();
ClearLayoutRecursively(m_common_layout);
}
else
{
while (auto* item = m_common_layout->takeAt(0))
{
auto* widget = item->widget();
m_common_layout->removeWidget(widget);
if (std::strcmp(widget->metaObject()->className(), "QLabel") == 0)
{
delete widget;
delete item;
}
}
}
auto& instance = AchievementManager::GetInstance();
if (!instance.IsGameLoaded())
return;
auto* client = instance.GetClient();
auto* achievement_list = rc_client_create_achievement_list(
client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL,
auto* achievement_list =
rc_client_create_achievement_list(client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL,
RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_PROGRESS);
for (u32 ix = 0; ix < achievement_list->num_buckets; ix++)
{
@ -56,19 +70,19 @@ void AchievementProgressWidget::UpdateData(bool clean_all)
for (u32 jx = 0; jx < achievement_list->buckets[ix].num_achievements; jx++)
{
auto* achievement = achievement_list->buckets[ix].achievements[jx];
auto box_itr = m_achievement_boxes.find(achievement->id);
if (box_itr == m_achievement_boxes.end())
{
m_achievement_boxes[achievement->id] = std::make_shared<AchievementBox>(this, achievement);
}
else
{
box_itr->second->UpdateProgress();
}
m_common_layout->addWidget(m_achievement_boxes[achievement->id].get());
}
}
rc_client_destroy_achievement_list(achievement_list);
}
else
{
for (auto box : m_achievement_boxes)
{
box.second->UpdateData();
}
}
}
void AchievementProgressWidget::UpdateData(