Mercurial > minori
diff src/gui/pages/seasons.cc @ 325:78929794e7d8
pages/seasons: run seasons search in a separate thread
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 13 Jun 2024 00:36:41 -0400 |
parents | 2115488eb302 |
children | 10096c5489e3 |
line wrap: on
line diff
--- a/src/gui/pages/seasons.cc Wed Jun 12 23:03:22 2024 -0400 +++ b/src/gui/pages/seasons.cc Thu Jun 13 00:36:41 2024 -0400 @@ -16,17 +16,53 @@ #include <QToolButton> #include <QVBoxLayout> +SeasonsPageSearchThread::SeasonsPageSearchThread(QObject* parent) : QThread(parent) { +} + +void SeasonsPageSearchThread::AddToQueue(Anime::SeriesSeason season, Date::Year year) { + queue_mutex_.lock(); + queue_.push({season, year}); + queue_mutex_.unlock(); +} + +void SeasonsPageSearchThread::run() { + queue_mutex_.lock(); + + while (!queue_.empty() && !isInterruptionRequested()) { + Season season = queue_.front(); + + /* unlock the mutex for a long blocking operation, so items + * can be added without worry */ + queue_mutex_.unlock(); + + if (Services::GetSeason(season.season, season.year)) + emit ReceivedSeason(season.season, season.year); + + queue_mutex_.lock(); + + queue_.pop(); + } + + queue_mutex_.unlock(); +} + +static SeasonsPageSearchThread search_thread_; + +/* ------------------------------------------------------------------------------------- */ + static constexpr Date::Year GetClosestDecade(Date::Year year) { return year - (year % 10); } -void SeasonsPage::SetSeason(Anime::SeriesSeason season, Date::Year year) { +void SeasonsPage::Refresh() { + setUpdatesEnabled(false); + if (!buttons || !season_button) return; buttons->clear(); - for (const auto& id : Anime::Season::GetAllAnimeForSeason(season, year)) { + for (const auto& id : Anime::Season::GetAllAnimeForSeason(season_, year_)) { QListWidgetItem* item = new QListWidgetItem; AnimeButton* button = new AnimeButton(this); button->SetAnime(Anime::db.items[id]); @@ -35,7 +71,16 @@ buttons->setItemWidget(item, button); } - season_button->setText(Strings::ToQString(Translate::ToLocalString(season)) + " " + QString::number(year)); + season_button->setText(Strings::ToQString(Translate::ToLocalString(season_)) + " " + QString::number(year_)); + + setUpdatesEnabled(true); +} + +void SeasonsPage::SetSeason(Anime::SeriesSeason season, Date::Year year) { + season_ = season; + year_ = year; + + Refresh(); } SeasonsPage::SeasonsPage(QWidget* parent) : QFrame(parent) { @@ -53,7 +98,6 @@ toolbar->setMovable(false); { - /* todo: clean this up... this sucks... */ static constexpr Date::Year last_year = 1960; auto create_year_menu = [this](QWidget* parent, QMenu* parent_menu, Date::Year year){ @@ -62,7 +106,7 @@ QMenu* menu = new QMenu(year_s, parent); for (const auto& season : Anime::SeriesSeasons) { QAction* action = menu->addAction(Strings::ToQString(Translate::ToLocalString(season)) + " " + year_s); - connect(action, &QAction::triggered, this, [this, season, year]{ + connect(action, &QAction::triggered, this, [this, season, year] { SetSeason(season, year); }); } @@ -100,8 +144,9 @@ { toolbar->addAction(QIcon(":/icons/16x16/arrow-circle-315.png"), tr("Refresh data"), [this]{ - Services::GetSeason(Anime::SeriesSeason::Summer, 2011U); - SetSeason(Anime::SeriesSeason::Summer, 2011U); + search_thread_.AddToQueue(season_, year_); + if (!search_thread_.isRunning()) + search_thread_.start(); }); } @@ -190,6 +235,11 @@ full_layout->setContentsMargins(0, 0, 0, 0); full_layout->setSpacing(0); + connect(&search_thread_, &SeasonsPageSearchThread::ReceivedSeason, this, [this](Anime::SeriesSeason season, Date::Year year) { + if (season == season_ && year == year_) + Refresh(); + }); + /* Do NOT move this up in this function, buttons HAS to be initialized */ SetSeason(Anime::SeriesSeason::Summer, 2011U); }