修改文件夹数据接口

This commit is contained in:
hewenfei 2023-03-22 18:11:36 +08:00
parent 0f0170801b
commit 71a3e79930
3 changed files with 91 additions and 122 deletions

View File

@ -40,6 +40,7 @@ AppFolderHelper *AppFolderHelper::instance()
AppFolderHelper::AppFolderHelper()
{
qRegisterMetaType<Folder>("Folder");
if (!QFile::exists(s_folderConfigFile)) {
QDir dir;
QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH);
@ -66,95 +67,104 @@ AppFolderHelper::~AppFolderHelper()
void AppFolderHelper::insertFolder(const Folder &folder)
{
QMutexLocker locker(&m_mutex);
m_folders.append(folder);
m_folders.insert(folder.id, folder);
}
void AppFolderHelper::addAppToFolder(const QString& appId, const QString& folderName)
void AppFolderHelper::addAppToFolder(const QString &appId, const int &folderId)
{
if (appId.isEmpty() || folderName.isEmpty()) {
if (appId.isEmpty()) {
return;
}
{
QMutexLocker locker(&m_mutex);
Folder *folder = findFolder(folderName);
if (folder) {
if (!folder->apps.contains(appId)) {
folder->apps.append(appId);
}
if (!m_folders.contains(folderId)) {
return;
}
Folder &folder = m_folders[folderId];
if (folder.apps.contains(appId)) {
return;
}
// 新文件夹放最后
Folder folder;
folder.name = folderName;
folder.apps.append(appId);
if (m_folders.isEmpty()) {
folder.index = 1;
} else {
folder.index = m_folders.last().index + 1;
}
Q_EMIT folderDataChanged();
}
void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &folderName)
{
if (appId.isEmpty()) {
return;
}
// TODO: max越界处理
int max = m_folders.isEmpty() ? -1 : m_folders.lastKey();
QString name = folderName;
if (name.isEmpty()) {
name = tr("New Folder %1").arg(m_folders.size() + 1);
}
Folder folder;
folder.id = ++max;
folder.name = name;
folder.apps.append(appId);
insertFolder(folder);
Q_EMIT folderDataChanged();
}
void AppFolderHelper::removeAppFromFolder(const QString &appId, const QString &folderName)
void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folderId)
{
if (appId.isEmpty() || folderName.isEmpty()) {
if (appId.isEmpty()) {
return;
}
{
QMutexLocker locker(&m_mutex);
Folder *folder = findFolder(folderName);
if (!folder) {
if (!m_folders.contains(folderId)) {
return;
}
if (folder->apps.contains(appId)) {
folder->apps.removeOne(appId);
Folder &folder = m_folders[folderId];
if (!folder.apps.contains(appId)) {
return;
}
folder.apps.removeOne(appId);
}
bool AppFolderHelper::deleteFolder(const QString &folderName)
Q_EMIT folderDataChanged();
}
bool AppFolderHelper::deleteFolder(const int& folderId)
{
if (folderName.isEmpty()) {
QMutexLocker locker(&m_mutex);
if (!m_folders.contains(folderId)) {
return false;
}
QMutexLocker locker(&m_mutex);
QVector<Folder>::iterator iterator = m_folders.begin();
for (; iterator != m_folders.end(); ++iterator) {
if (folderName == iterator->name) {
m_folders.erase(iterator);
if (m_folders.remove(folderId)) {
return true;
}
}
return false;
}
QVector<Folder> AppFolderHelper::folderData()
QList<Folder> AppFolderHelper::folderData()
{
QMutexLocker locker(&m_mutex);
return m_folders;
return m_folders.values();
}
bool AppFolderHelper::searchFolder(const QString& folderName, Folder &folder)
bool AppFolderHelper::searchFolder(const int &folderId, Folder &folder)
{
if (folderName.isEmpty()) {
return false;
}
QMutexLocker locker(&m_mutex);
Folder *tmp = findFolder(folderName);
if (!tmp) {
if (!m_folders.contains(folderId)) {
return false;
}
folder.name = tmp->name;
folder.index = tmp->index;
folder.apps = tmp->apps;
const Folder &tmp = m_folders[folderId];
folder = tmp;
return true;
}
@ -166,51 +176,14 @@ bool AppFolderHelper::searchFolderByAppName(const QString &appId, Folder &folder
}
QMutexLocker locker(&m_mutex);
Folder *tmp = findFolderByAppName(appId);
if (!tmp) {
return false;
}
folder.name = tmp->name;
folder.index = tmp->index;
folder.apps = tmp->apps;
for (const auto &tmp: m_folders) {
if (tmp.apps.contains(appId)) {
folder = tmp;
return true;
}
Folder *AppFolderHelper::findFolder(const QString &folderName)
{
if (m_folders.empty()) {
return nullptr;
}
QVector<Folder>::iterator iterator = m_folders.begin();
for (; iterator != m_folders.end(); ++iterator) {
if (folderName == iterator->name) {
// 如果定义了 QT_STRICT_ITERATORS 宏那么迭代器为class iterator; 否则迭代器为typedef T* iterator;
// kylin上的qt库没有定义QT_STRICT_ITERATORS宏需要注意该处可能出现的编译错误参见QVector源码。
// return &*iterator;
return iterator;
}
}
return nullptr;
}
Folder *AppFolderHelper::findFolderByAppName(const QString &appId)
{
if (m_folders.empty()) {
return nullptr;
}
QVector<Folder>::iterator iterator = m_folders.begin();
for (; iterator != m_folders.end(); ++iterator) {
if (iterator->apps.contains(appId)) {
return iterator;
}
}
return nullptr;
return false;
}
void AppFolderHelper::forceSync()
@ -243,15 +216,12 @@ void AppFolderHelper::readData()
// 遍历json数据节点
QJsonArray jsonArray = jsonDocument.array();
QJsonArray::iterator iterator = jsonArray.begin();
while (iterator != jsonArray.end()) {
QJsonObject object = (*iterator).toObject();
if (object.contains("name") && object.contains("index") && object.contains("apps")) {
for (const auto &value : jsonArray) {
QJsonObject object = value.toObject();
if (object.contains("name") && object.contains("id") && object.contains("apps")) {
Folder folder;
folder.name = object.value(QLatin1String("name")).toString();
folder.index = object.value(QLatin1String("index")).toInt();
folder.id = object.value(QLatin1String("id")).toInt();
QJsonArray apps = object.value(QLatin1String("apps")).toArray();
for (const auto &app : apps) {
@ -262,8 +232,6 @@ void AppFolderHelper::readData()
insertFolder(folder);
}
}
++iterator;
}
}
@ -274,7 +242,6 @@ void AppFolderHelper::saveData()
return;
}
// 读取json数据
QJsonDocument jsonDocument;
QJsonArray folderArray;
@ -289,7 +256,7 @@ void AppFolderHelper::saveData()
}
object.insert("name", folder.name);
object.insert("index", folder.index);
object.insert("id", folder.id);
object.insert("apps", apps);
folderArray.append(object);

View File

@ -22,27 +22,30 @@
#include <QObject>
#include <QMutex>
#include <QMutexLocker>
#include <QVector>
#include <QMap>
#include <QString>
#include <QStringList>
namespace UkuiMenu {
class AppFolderHelper;
class Folder
{
Q_GADGET
Q_PROPERTY(int index READ getIndex)
Q_PROPERTY(int id READ getId)
Q_PROPERTY(QString name READ getName)
Q_PROPERTY(QStringList apps READ getApps)
friend class AppFolderHelper;
public:
int getIndex() const { return index; }
int getId() const { return id; }
QString getName() const { return name; }
QStringList getApps() const { return apps; }
public:
int index;
QString name;
QStringList apps;
private:
int id; // 文件夹唯一Id文件夹排序值
QString name; // 名称
QStringList apps; // 应用列表
};
class AppFolderHelper : public QObject
@ -59,13 +62,14 @@ public:
// TODO 修改文件夹信息
// xxxx
bool searchFolder(const QString& folderName, Folder &folder);
bool searchFolder(const int& folderId, Folder &folder);
bool searchFolderByAppName(const QString& appId, Folder &folder);
QVector<Folder> folderData();
QList<Folder> folderData();
void addAppToFolder(const QString& appId, const QString& folderName);
void removeAppFromFolder(const QString& appId, const QString& folderName);
bool deleteFolder(const QString& folderName);
void addAppToFolder(const QString& appId, const int& folderId);
void addAppToNewFolder(const QString& appId, const QString& folderName);
void removeAppFromFolder(const QString& appId, const int& folderId);
bool deleteFolder(const int& folderId);
void forceSync();
@ -77,13 +81,11 @@ private:
void readData();
void saveData();
void insertFolder(const Folder& folder);
Folder *findFolder(const QString& folderName);
Folder *findFolderByAppName(const QString& appId);
// TODO 配置文件监听
private:
QMutex m_mutex;
QVector<Folder> m_folders;
QMap<int, Folder> m_folders;
static QString s_folderConfigFile;
};

View File

@ -108,22 +108,22 @@ void AllAppDataProvider::reloadFolderData()
m_folderApps.clear();
m_folderData.clear();
QVector<Folder> folders = AppFolderHelper::instance()->folderData();
QList<Folder> folders = AppFolderHelper::instance()->folderData();
if (folders.isEmpty()) {
return;
}
std::sort(folders.begin(), folders.end(), [](const Folder &a, const Folder &b) {
return a.index < b.index;
return a.getId() < b.getId();
});
DataEntity folderItem;
for (const auto &folder : folders) {
folderItem.setType(DataType::Folder);
folderItem.setIcon("image://appicon/text-plain");
folderItem.setName(folder.name);
folderItem.setName(folder.getName());
m_folderApps.unite(folder.apps.toSet());
m_folderApps.unite(folder.getApps().toSet());
m_folderData.append(folderItem);
}
}