perf(app-database-service):去掉收藏换位置功能,只记录是否收藏。

This commit is contained in:
JunjieBai 2024-03-06 10:37:25 +08:00 committed by iaom
parent 3925e34749
commit b7cbc5ae6e
3 changed files with 17 additions and 122 deletions

View File

@ -108,4 +108,8 @@ bool ApplicationInfo::tranWinIdToDesktopFilePath(const QVariant &winId, QString
return d->m_infoTable->tranWinIdToDesktopFilePath(winId, desktopFilePath); return d->m_infoTable->tranWinIdToDesktopFilePath(winId, desktopFilePath);
} }
void ApplicationInfo::removeAppFromFavorites(const QString &desktopFilePath) {
return d->m_infoTable->setAppFavoritesState(desktopFilePath, 0);
}
ApplicationInfo::~ApplicationInfo() = default; ApplicationInfo::~ApplicationInfo() = default;

View File

@ -73,25 +73,33 @@ public:
/** /**
* @brief AppInfoTable::setAppToFavorites * @brief AppInfoTable::setAppToFavorites
* set the app to favorites apps(default is at 1) * set the app to favorites apps
* @param desktopFilePath: the desktop file path of app * @param desktopFilePath: the desktop file path of app
*/ */
void setAppToFavorites(const QString &desktopFilePath); void setAppToFavorites(const QString &desktopFilePath);
/** /**
* @brief AppInfoTable::removeAppFromFavorites
* remove the app from favorites apps
* @param desktopFilePath: the desktop file path of app
*/
void removeAppFromFavorites(const QString &desktopFilePath);
/**
* this function has been deprecated, use setAppToFavorites and removeAppFromFavorites instead
* @brief AppInfoTable::setFavoritesTo * @brief AppInfoTable::setFavoritesTo
* set the favorites state of the app to num, you can also use to change the position of the app which is one of the Favorites Apps * set the favorites state of the app to num, you can also use to change the position of the app which is one of the Favorites Apps
* @param desktopFilePath: the desktop file path of app * @param desktopFilePath: the desktop file path of app
* @param num: the favorites app's position(from 1). If num is 0, it will remove the app from the favorites apps * @param num: the favorites app's position(from 1). If num is 0, it will remove the app from the favorites apps
*/ */
void setFavoritesOfApp(const QString &desktopFilePath, size_t num); [[deprecated]] void setFavoritesOfApp(const QString &desktopFilePath, size_t num);
/** /**
* @brief AppInfoTable::setAppToTop * @brief AppInfoTable::setAppToTop
* set the app to top apps(default is at 1) * set the app to top apps(default is at 1)
* @param desktopFilePath: desktop file path of app * @param desktopFilePath: desktop file path of app
*/ */
void setAppToTop(const QString &desktopFilePath); [[deprecated]] void setAppToTop(const QString &desktopFilePath);
/** /**
* @brief AppInfoTable::setAppTopTo * @brief AppInfoTable::setAppTopTo
@ -99,7 +107,7 @@ public:
* @param desktopFilePath: the desktop file path of app * @param desktopFilePath: the desktop file path of app
* @param num: the top app's position(from 1). If num is 0, it will remove the app from the top apps * @param num: the top app's position(from 1). If num is 0, it will remove the app from the top apps
*/ */
void setTopOfApp(const QString &desktopFilePath, size_t num); [[deprecated]] void setTopOfApp(const QString &desktopFilePath, size_t num);
void setAppLaunchedState(const QString &desktopFilePath, bool launched = true); void setAppLaunchedState(const QString &desktopFilePath, bool launched = true);
@ -110,7 +118,7 @@ public:
* @param desktopFilePath: the desktop file path of the process corresponding to pid * @param desktopFilePath: the desktop file path of the process corresponding to pid
* @return bool:true if success,else false * @return bool:true if success,else false
*/ */
bool tranPidToDesktopFp(int pid, QString &desktopFilePath);//obsolete [[deprecated]] bool tranPidToDesktopFp(int pid, QString &desktopFilePath);//obsolete
bool tranPidToDesktopFp(uint pid, QString &desktopFilePath); bool tranPidToDesktopFp(uint pid, QString &desktopFilePath);
/** /**

View File

@ -790,123 +790,6 @@ bool AppDBManager::handleFavoritesStateUpdate(const QString &desktopFilePath, co
QString cmd; QString cmd;
ApplicationInfoMap infos; ApplicationInfoMap infos;
//获取应用在数据库中的favorites标志位
uint previousPos = 0;
bool getPrevious(false);
cmd = "SELECT FAVORITES FROM APPINFO WHERE DESKTOP_FILE_PATH =:desktopFilePath";
query.prepare(cmd);
query.bindValue(":desktopFilePath", desktopFilePath);
if (query.exec()) {
if (query.next()) {
previousPos = query.value("FAVORITES").toUInt();
getPrevious = true;
} else {
qWarning() << query.lastQuery() << query.lastError();
}
} else {
qWarning() << query.lastQuery() << query.lastError();
}
//收藏位未改变
if (getPrevious && previousPos == num) {
res = false;
qWarning() << "favorites state has no changes, I quit!";
return res;
}
//查询目前favorites最大值
uint maxFavorite = 0;
query.prepare("SELECT max(FAVORITES) as max FROM APPINFO");
if (query.exec()) {
if (query.next()) {
maxFavorite = query.value("max").toUInt();
if (maxFavorite + 1 < num) {
qWarning() << QString("Max favorites pos is %0.To be moved to a invalid pos, I quit!!").arg(query.value("max").toInt());
res = false;
}
}
} else {
qWarning() << query.lastError() << query.lastQuery();
res = false;
}
if (!res) {
return res;
}
//取消收藏时,将对应应用前移
if (!num) {
if (previousPos < 1) {
res = false;
}
if (res) {
if (!this->startTransaction()) {
res = false;
return res;
}
cmd = QString("UPDATE APPINFO SET MODIFYED_TIME='%0', FAVORITES=FAVORITES-1 WHERE FAVORITES > %1;")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(previousPos);
if (query.exec(cmd)) {
cmd = QString("SELECT DESKTOP_FILE_PATH,FAVORITES FROM APPINFO WHERE FAVORITES >= %0;").arg(previousPos);
if (query.exec(cmd)) {
qDebug() << "Prepare to cancel the favorite state of" << desktopFilePath;
while (query.next()) {
infos[query.value("DESKTOP_FILE_PATH").toString()].insert(ApplicationProperty::Favorites, query.value("FAVORITES"));
}
} else {
qWarning() << query.lastQuery() << query.lastError();
}
} else {
res = false;
qWarning() << query.lastQuery() << query.lastError();
m_database.rollback();
}
}
} else {
//直接设置时(要设置的应用未被收藏查询要设置的favorites标志位是否被占用占用则将该应用及其之后的应用的favorites标志位后移
if (getPrevious && !previousPos) {
cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO WHERE FAVORITES = %1").arg(num);
if (!query.exec(cmd)) {
qWarning() << "Fail to exec:" << cmd << "because:" << query.lastError();
res = false;
} else {
if (!this->startTransaction()) {
return false;
}
if (query.next()) {
//默认收藏后顶到第一个,其余后移,若想直接收藏到对应位置则将该位置后的收藏应用后移
query.prepare(QString("UPDATE APPINFO SET MODIFYED_TIME='%0', FAVORITES=FAVORITES+1 WHERE FAVORITES >= %1;")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num));
if (!query.exec()) {
res = false;
qWarning() << query.lastError() << query.lastQuery();
m_database.rollback();
} else {
cmd = QString("SELECT DESKTOP_FILE_PATH, FAVORITES FROM APPINFO WHERE FAVORITES >= %1").arg(num);
if (!query.exec(cmd)) {
qWarning() << query.lastError() << query.lastQuery();
res = false;
} else {
while (query.next()) {
infos[query.value("DESKTOP_FILE_PATH").toString()].insert(ApplicationProperty::Favorites, query.value("FAVORITES"));
}
}
}
}
}
} else {
//触发修改位置逻辑
res = maxFavorite < num? false : this->handleChangeFavoritesPos(desktopFilePath, num, previousPos, infos);
qDebug() << "change favorites pos:" << res;
}
}
//移动位置执行不成功则不去更新对应应用的favorites标志位
if (!res) {
return res;
}
//更新favorites状态 //更新favorites状态
cmd = QString("UPDATE APPINFO SET MODIFYED_TIME='%0', FAVORITES=%1 WHERE DESKTOP_FILE_PATH=:desktopFilePath") cmd = QString("UPDATE APPINFO SET MODIFYED_TIME='%0', FAVORITES=%1 WHERE DESKTOP_FILE_PATH=:desktopFilePath")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))