From 68d7ce31c538d5181e4767a20836171d09a1adad Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Fri, 28 Oct 2022 18:38:30 +0900 Subject: [PATCH] improve group update --- sub/GroupUpdater.cpp | 16 +++++----------- sub/GroupUpdater.hpp | 5 ++--- ui/dialog_manage_groups.cpp | 19 +++++-------------- ui/dialog_manage_groups.h | 6 ++++-- ui/mainwindow.cpp | 4 +--- ui/widget/GroupItem.cpp | 6 +++--- 6 files changed, 20 insertions(+), 36 deletions(-) diff --git a/sub/GroupUpdater.cpp b/sub/GroupUpdater.cpp index ae67137..6b9c054 100644 --- a/sub/GroupUpdater.cpp +++ b/sub/GroupUpdater.cpp @@ -55,6 +55,7 @@ namespace NekoRay::sub { auto j = DecodeB64IfValid(link.fragment().toUtf8(), QByteArray::Base64UrlEncoding); if (j.isEmpty()) return; ent->bean->FromJsonBytes(j.toUtf8()); + showLog("nekoray format: " + ent->bean->DisplayTypeAndName()); } // SOCKS @@ -290,16 +291,8 @@ namespace NekoRay::sub { #endif } - // 不要刷新,下载导入完会自己刷新 - void GroupUpdater::AsyncUpdate(const QString &str, int _sub_gid, - QObject *caller, const std::function &callback) { - if (caller != nullptr && callback != nullptr) { - connectOnce(this, &GroupUpdater::AsyncUpdateCallback, caller, - [=](QObject *receiver) { - if (receiver == caller) callback(); - }); - } - + // 在新的 thread 运行 + void GroupUpdater::AsyncUpdate(const QString &str, int _sub_gid, const std::function &finish) { auto content = str.trimmed(); bool asURL = false; @@ -316,7 +309,8 @@ namespace NekoRay::sub { runOnNewThread([=] { Update(str, _sub_gid, asURL); - emit AsyncUpdateCallback(caller); + emit asyncUpdateCallback(_sub_gid); + if (finish != nullptr) finish(); }); } diff --git a/sub/GroupUpdater.hpp b/sub/GroupUpdater.hpp index 2fb9105..2df0c2f 100644 --- a/sub/GroupUpdater.hpp +++ b/sub/GroupUpdater.hpp @@ -18,14 +18,13 @@ namespace NekoRay::sub { Q_OBJECT public: - void AsyncUpdate(const QString &str, int _sub_gid = -1, - QObject *caller = nullptr, const std::function &callback = nullptr); + void AsyncUpdate(const QString &str, int _sub_gid = -1, const std::function &finish = nullptr); void Update(const QString &_str, int _sub_gid = -1, bool _not_sub_as_url = false); signals: - void AsyncUpdateCallback(QObject *caller); + void asyncUpdateCallback(int gid); }; extern GroupUpdater *groupUpdater; diff --git a/ui/dialog_manage_groups.cpp b/ui/dialog_manage_groups.cpp index d424be6..70ad30d 100644 --- a/ui/dialog_manage_groups.cpp +++ b/ui/dialog_manage_groups.cpp @@ -58,13 +58,13 @@ void DialogManageGroups::on_update_all_clicked() { for (const auto &gid: NekoRay::profileManager->_groups) { auto group = NekoRay::profileManager->GetGroup(gid); if (group == nullptr || group->url.isEmpty()) continue; - _update_one_group(NekoRay::profileManager->_groups.indexOf(gid)); + UI_update_one_group(NekoRay::profileManager->_groups.indexOf(gid)); return; } } } -void DialogManageGroups::_update_one_group(int _order) { +void UI_update_one_group(int _order) { // calculate next group int nextOrder = _order; QSharedPointer nextGroup; @@ -81,17 +81,8 @@ void DialogManageGroups::_update_one_group(int _order) { auto group = NekoRay::profileManager->GetGroup(NekoRay::profileManager->_groups[_order]); if (group == nullptr) return; - NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, this, [=] { - // refresh ui - for (int i = 0; i < ui->listWidget->count(); i++) { - auto w = ui->listWidget->itemWidget(ui->listWidget->item(i)); - if (w == nullptr) return; - auto item = dynamic_cast(w); - if (item->ent->id == group->id) { - item->refresh_data(); - } - } - // - if (nextGroup != nullptr) _update_one_group(nextOrder); + // v2.2: listener is moved to GroupItem, no refresh here. + NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, [=] { + if (nextGroup != nullptr) UI_update_one_group(nextOrder); }); } diff --git a/ui/dialog_manage_groups.h b/ui/dialog_manage_groups.h index 01baedb..e30ca81 100644 --- a/ui/dialog_manage_groups.h +++ b/ui/dialog_manage_groups.h @@ -22,11 +22,13 @@ public: private: Ui::DialogManageGroups *ui; - void _update_one_group(int _order); - private slots: void on_add_clicked(); void on_update_all_clicked(); }; + +// 更新所有订阅 关闭分组窗口时 更新动作继续执行 + +void UI_update_one_group(int _order); diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index c959d24..ba05d8b 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -1169,9 +1169,7 @@ void MainWindow::on_menu_update_subscripton_triggered() { if (group->url.isEmpty()) return; if (mw_sub_updating) return; mw_sub_updating = true; - NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, this, [=] { - mw_sub_updating = false; - }); + NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, [&] { mw_sub_updating = false; }); } void MainWindow::on_menu_remove_unavailable_triggered() { diff --git a/ui/widget/GroupItem.cpp b/ui/widget/GroupItem.cpp index d02191d..e7670b2 100644 --- a/ui/widget/GroupItem.cpp +++ b/ui/widget/GroupItem.cpp @@ -51,6 +51,8 @@ GroupItem::GroupItem(QWidget *parent, const QSharedPointer &ent, if (ent == nullptr) return; connect(this, &GroupItem::edit_clicked, this, &GroupItem::on_edit_clicked); + connect(NekoRay::sub::groupUpdater, &NekoRay::sub::GroupUpdater::asyncUpdateCallback, + this, [=](int gid) { if (gid == this->ent->id) refresh_data(); }); refresh_data(); } @@ -96,9 +98,7 @@ void GroupItem::refresh_data() { } void GroupItem::on_update_sub_clicked() { - NekoRay::sub::groupUpdater->AsyncUpdate(ent->url, ent->id, this, [=] { - refresh_data(); - }); + NekoRay::sub::groupUpdater->AsyncUpdate(ent->url, ent->id); } void GroupItem::on_edit_clicked() {