improve group update

This commit is contained in:
arm64v8a
2022-10-28 18:38:30 +09:00
parent 3943f75bc5
commit 68d7ce31c5
6 changed files with 20 additions and 36 deletions

View File

@@ -55,6 +55,7 @@ namespace NekoRay::sub {
auto j = DecodeB64IfValid(link.fragment().toUtf8(), QByteArray::Base64UrlEncoding); auto j = DecodeB64IfValid(link.fragment().toUtf8(), QByteArray::Base64UrlEncoding);
if (j.isEmpty()) return; if (j.isEmpty()) return;
ent->bean->FromJsonBytes(j.toUtf8()); ent->bean->FromJsonBytes(j.toUtf8());
showLog("nekoray format: " + ent->bean->DisplayTypeAndName());
} }
// SOCKS // SOCKS
@@ -290,16 +291,8 @@ namespace NekoRay::sub {
#endif #endif
} }
// 不要刷新,下载导入完会自己刷新 // 在新的 thread 运行
void GroupUpdater::AsyncUpdate(const QString &str, int _sub_gid, void GroupUpdater::AsyncUpdate(const QString &str, int _sub_gid, const std::function<void()> &finish) {
QObject *caller, const std::function<void()> &callback) {
if (caller != nullptr && callback != nullptr) {
connectOnce(this, &GroupUpdater::AsyncUpdateCallback, caller,
[=](QObject *receiver) {
if (receiver == caller) callback();
});
}
auto content = str.trimmed(); auto content = str.trimmed();
bool asURL = false; bool asURL = false;
@@ -316,7 +309,8 @@ namespace NekoRay::sub {
runOnNewThread([=] { runOnNewThread([=] {
Update(str, _sub_gid, asURL); Update(str, _sub_gid, asURL);
emit AsyncUpdateCallback(caller); emit asyncUpdateCallback(_sub_gid);
if (finish != nullptr) finish();
}); });
} }

View File

@@ -18,14 +18,13 @@ namespace NekoRay::sub {
Q_OBJECT Q_OBJECT
public: public:
void AsyncUpdate(const QString &str, int _sub_gid = -1, void AsyncUpdate(const QString &str, int _sub_gid = -1, const std::function<void()> &finish = nullptr);
QObject *caller = nullptr, const std::function<void()> &callback = nullptr);
void Update(const QString &_str, int _sub_gid = -1, bool _not_sub_as_url = false); void Update(const QString &_str, int _sub_gid = -1, bool _not_sub_as_url = false);
signals: signals:
void AsyncUpdateCallback(QObject *caller); void asyncUpdateCallback(int gid);
}; };
extern GroupUpdater *groupUpdater; extern GroupUpdater *groupUpdater;

View File

@@ -58,13 +58,13 @@ void DialogManageGroups::on_update_all_clicked() {
for (const auto &gid: NekoRay::profileManager->_groups) { for (const auto &gid: NekoRay::profileManager->_groups) {
auto group = NekoRay::profileManager->GetGroup(gid); auto group = NekoRay::profileManager->GetGroup(gid);
if (group == nullptr || group->url.isEmpty()) continue; 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; return;
} }
} }
} }
void DialogManageGroups::_update_one_group(int _order) { void UI_update_one_group(int _order) {
// calculate next group // calculate next group
int nextOrder = _order; int nextOrder = _order;
QSharedPointer<NekoRay::Group> nextGroup; QSharedPointer<NekoRay::Group> nextGroup;
@@ -81,17 +81,8 @@ void DialogManageGroups::_update_one_group(int _order) {
auto group = NekoRay::profileManager->GetGroup(NekoRay::profileManager->_groups[_order]); auto group = NekoRay::profileManager->GetGroup(NekoRay::profileManager->_groups[_order]);
if (group == nullptr) return; if (group == nullptr) return;
NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, this, [=] { // v2.2: listener is moved to GroupItem, no refresh here.
// refresh ui NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, [=] {
for (int i = 0; i < ui->listWidget->count(); i++) { if (nextGroup != nullptr) UI_update_one_group(nextOrder);
auto w = ui->listWidget->itemWidget(ui->listWidget->item(i));
if (w == nullptr) return;
auto item = dynamic_cast<GroupItem *>(w);
if (item->ent->id == group->id) {
item->refresh_data();
}
}
//
if (nextGroup != nullptr) _update_one_group(nextOrder);
}); });
} }

View File

@@ -22,11 +22,13 @@ public:
private: private:
Ui::DialogManageGroups *ui; Ui::DialogManageGroups *ui;
void _update_one_group(int _order);
private slots: private slots:
void on_add_clicked(); void on_add_clicked();
void on_update_all_clicked(); void on_update_all_clicked();
}; };
// 更新所有订阅 关闭分组窗口时 更新动作继续执行
void UI_update_one_group(int _order);

View File

@@ -1169,9 +1169,7 @@ void MainWindow::on_menu_update_subscripton_triggered() {
if (group->url.isEmpty()) return; if (group->url.isEmpty()) return;
if (mw_sub_updating) return; if (mw_sub_updating) return;
mw_sub_updating = true; mw_sub_updating = true;
NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, this, [=] { NekoRay::sub::groupUpdater->AsyncUpdate(group->url, group->id, [&] { mw_sub_updating = false; });
mw_sub_updating = false;
});
} }
void MainWindow::on_menu_remove_unavailable_triggered() { void MainWindow::on_menu_remove_unavailable_triggered() {

View File

@@ -51,6 +51,8 @@ GroupItem::GroupItem(QWidget *parent, const QSharedPointer<NekoRay::Group> &ent,
if (ent == nullptr) return; if (ent == nullptr) return;
connect(this, &GroupItem::edit_clicked, this, &GroupItem::on_edit_clicked); 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(); refresh_data();
} }
@@ -96,9 +98,7 @@ void GroupItem::refresh_data() {
} }
void GroupItem::on_update_sub_clicked() { void GroupItem::on_update_sub_clicked() {
NekoRay::sub::groupUpdater->AsyncUpdate(ent->url, ent->id, this, [=] { NekoRay::sub::groupUpdater->AsyncUpdate(ent->url, ent->id);
refresh_data();
});
} }
void GroupItem::on_edit_clicked() { void GroupItem::on_edit_clicked() {