fix(favorite):修复被收藏应用在应用组中时,仍可以添加该应用到收藏区域的问题

This commit is contained in:
qiqi49 2024-09-11 15:41:39 +08:00 committed by youdiansaodongxi
parent e6b89b52af
commit 31050086de
4 changed files with 29 additions and 13 deletions

View File

@ -219,9 +219,15 @@ void AppFavoritesModel::clearFavorites()
FavoritesConfig::instance().clear();
}
bool AppFavoritesModel::getApp(const QString &appid, DataEntity &app)
bool AppFavoritesModel::isAppIncluded(const QString &appid)
{
return m_sourceModel->databaseInterface()->getApp(appid, app);
QModelIndex sourceIndex = m_sourceModel->index(m_sourceModel->indexOfApp(appid), 0, {});
if (!sourceIndex.isValid()) {
return false;
}
QPersistentModelIndex index(sourceIndex);
return m_favoritesApps.contains(index);
}
void AppFavoritesModel::onAppRemoved(const QModelIndex &parent, int first, int last)

View File

@ -42,7 +42,7 @@ public:
void addAppToFavorites(const QString &id, const int &index);
void removeAppFromFavorites(const QString &id);
void changeFileState(const QString &url, const bool &favorite);
bool getApp(const QString &appid, DataEntity &app);
bool isAppIncluded(const QString &appid);
private:
explicit AppFavoritesModel(QObject *parent = nullptr);

View File

@ -22,6 +22,7 @@
#include <QDir>
#include <QFile>
#include "favorites-config.h"
#include "favorite-folder-helper.h"
#define FOLDER_FILE_PATH ".config/ukui-menu/"
#define FOLDER_FILE_NAME "favorite.json"
@ -138,19 +139,24 @@ void FavoritesConfig::initConfig()
m_favoritesList.clear();
QJsonArray array = jsonDocument.array();
QJsonArray newArray;
bool needSync = false;
for (int i = 0; i < array.size(); i++) {
if (array.at(i).isString()) {
m_favoritesList.append(array.at(i).toString());
newArray.append(array.at(i));
QString id = array.at(i).toString();
// 配置文件出现错误, 某一个应用 同时出现在了 应用组的内部和外部
if (id.startsWith(APP_ID_SCHEME) && FavoriteFolderHelper::instance()->containApp(id.mid(APP_ID_SCHEME.length()))) {
needSync = true;
continue;
}
m_favoritesList.append(id);
} else {
needSync = true;
}
}
file.open(QFile::WriteOnly);
jsonDocument.setArray(newArray);
file.write(jsonDocument.toJson());
file.flush();
file.close();
if (needSync) {
sync();
}
}
} // UkuiMenu

View File

@ -80,6 +80,11 @@ void FavoritesModel::openMenu(const int &row)
void FavoritesModel::addAppToFavorites(const QString &id, const int &index)
{
if (AppFavoritesModel::instance().isAppIncluded(id) || FavoriteFolderHelper::instance()->containApp(id)) {
qWarning() << "This application is already included in the favorite apps!";
return;
}
int appOrder;
if (index == -1) {
appOrder = FavoritesConfig::instance().configSize();
@ -108,8 +113,7 @@ void FavoritesModel::addAppsToNewFolder(const QString &idFrom, const QString &id
return;
}
DataEntity app;
if (AppFavoritesModel::instance().getApp(idFrom, app) && AppFavoritesModel::instance().getApp(idTo, app)) {
if (AppFavoritesModel::instance().isAppIncluded(idFrom) && AppFavoritesModel::instance().isAppIncluded(idTo)) {
FavoriteFolderHelper::instance()->addAppsToNewFolder(idFrom, idTo, "");
}
}