Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件夹手动排序功能的实现思路 #94

Closed
altairwei opened this issue Jun 30, 2021 · 1 comment
Closed

文件夹手动排序功能的实现思路 #94

altairwei opened this issue Jun 30, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@altairwei
Copy link
Contributor

首先 WizQTClient 的实现方式可以参考一下:

void WizCategoryView::updatePersonalFolderLocation(WizDatabase& db, \
                                                    const QString& strOldLocation, const QString& strNewLocation)
{
    WizCategoryViewAllFoldersItem* pItem = dynamic_cast<WizCategoryViewAllFoldersItem* >(findAllFolderItem());
    if (!pItem)
        return;

    // the last item of folder root should be trash
    WizCategoryViewTrashItem* trashItem = findTrash(db.kbGUID());
    if (trashItem && pItem->indexOfChild(trashItem) != pItem->childCount() - 1)
    {
        pItem->takeChild(pItem->indexOfChild(trashItem));
        pItem->insertChild(pItem->childCount(), trashItem);
    }

    if (strOldLocation != strNewLocation)
    {
        db.updateLocation(strOldLocation, strNewLocation);
        CWizStdStringArray childLocations;
        db.getAllChildLocations(strNewLocation, childLocations);
        for (CString childLocation : childLocations)
        {
            findFolder(childLocation, true, true);
        }
    }

    // 文件夹移动后触发folder loacation changed,需要更新顺序
    QString str = getAllFoldersPosition();
    db.setFoldersPos(str, -1);
    db.setFoldersPosModified();

    emit categoryItemPositionChanged(db.kbGUID());
}

QString WizCategoryView::getAllFoldersPosition(WizCategoryViewFolderItem* pItem, int& nStartPos)
{
    if (!pItem)
        return QString();

    QString str ="\"" + pItem->location() + "\": " + QString::number(nStartPos);
    nStartPos ++;

    for (int i = 0; i < pItem->childCount(); i++)
    {
        WizCategoryViewFolderItem* childItem = dynamic_cast<WizCategoryViewFolderItem* >(pItem->child(i));
        Q_ASSERT(childItem);
        str += ", \n";
        str += getAllFoldersPosition(childItem, nStartPos);
    }

    return str;
}

void WizDatabase::setFoldersPos(const QString& foldersPos, qint64 nVersion)
{
    setLocalValueVersion("folders_pos", nVersion);
    setMeta("SYNC_INFO", "FOLDERS_POS", foldersPos);

    bool bPositionChanged = false;

    CString str(foldersPos);
    str.trim();
    str.trim('{');
    str.trim('}');
    str.replace("\n", "");
    str.replace("\r", "");
    if (str.isEmpty())
        return;

    CWizStdStringArray arrPos;
    ::WizSplitTextToArray(str, ',', arrPos);

    QSettings* setting = WizGlobal::settings();
    setting->beginGroup("FolderPosition");
    setting->remove("");
    setting->endGroup();

    CWizStdStringArray::const_iterator it;
    for (it= arrPos.begin(); it != arrPos.end(); it++) {
        CString strLine = *it;
        CString strLocation;
        CString strPos;
        if (!::WizStringSimpleSplit(strLine, ':', strLocation, strPos))
            continue;
        strLocation.trim();
        strLocation.trim('\"');

        int nPos = wiz_ttoi(strPos);
        if (0 == nPos)
            continue;

        int nPosOld = setting->value("FolderPosition/" + strLocation).toInt();
        if (nPosOld != nPos) {
            setting->setValue("FolderPosition/" + strLocation, nPos);
            bPositionChanged = true;
        }
    }

    setting->sync();

    if (bPositionChanged) {
        Q_EMIT folderPositionChanged();
    }
}

void WizDatabase::setFoldersPosModified()
{
    setLocalValueVersion("folders_pos", -1);
    setLocalValueVersion("folders", -1);
}

void WizDatabase::setLocalValueVersion(const QString& strKey,
                                        qint64 nServerVersion)
{
    setMetaInt64(WIZ_META_SYNCINFO_SECTION, "KEY_" + strKey + "_VERSION", nServerVersion);
}

然后 API 文档中写了:

# 文件夹排序
put /ks/category/sort/:kbGuid
body: {

  '/My Notes/': 0,
  '/New Folder/': 1,
}
@TankNee
Copy link
Owner

TankNee commented Jun 30, 2021

👌

@TankNee TankNee added the enhancement New feature or request label Jun 30, 2021
@TankNee TankNee closed this as completed in 64de804 Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants