FileTreeComp does not show newly created files

gui

#1

(tested on iOS)
Why do I get two different results when I create a new XML file in the xmlFolder (cf. textEditorReturnKeyPressed) :

  • if I set the directoryList directory to xmlFolder, I don’t see the created files
  • if I set the directoryList directory to xmlFolder.getParentDirectory(), I can see the files by browsing and unfolding the “XMLdocs” folder.

Initialization :

    directoryList.setDirectory (xmlFolder , true, true);
    File path = File::getSpecialLocation(File::currentApplicationFile);
    String sPath = path.getFullPathName();
    sPath += "/XMLdocs";
    xmlFolder = File(sPath);
    xmlFolder.createDirectory();
    directoryList.setDirectory (xmlFolder , true, true);

//////////////

void textEditorReturnKeyPressed (TextEditor& te) override
{
    hideKeyboard();
    saveAsXML();
    fileTreeComp.refresh();
}


void saveAsXML()
{
    XmlElement *xmlAppState = arbre.createXml();
    String sPath = xmlFolder.getFullPathName();
    userEnteredFileName = textGUI->textEditor.getText();
    sPath += "/";
    sPath += userEnteredFileName;
    sPath += ".xml";
    File fileToSave = File(sPath);
    xmlAppState->writeToFile(fileToSave, "");
}

#2

Surely it’s the DirectoryContentsList you need to refresh, not the file tree comp?

And I just feel the need to say that code like this:

void saveAsXML()
{
    XmlElement *xmlAppState = arbre.createXml();
    String sPath = xmlFolder.getFullPathName();
    userEnteredFileName = textGUI->textEditor.getText();
    sPath += "/";
    sPath += userEnteredFileName;
    sPath += ".xml";
    File fileToSave = File(sPath);
    xmlAppState->writeToFile(fileToSave, "");
}

…makes me sad.

I’d be much happier if all our users posted code that was more like this:

bool saveAsXML()
{
    auto name = File::createLegalFileName (textGUI->textEditor.getText());

    ScopedPointer<XmlElement> xmlAppState = arbre.createXml();
    jassert (xmlAppState != nullptr); // if your code makes an assumption like this, you should explicitly check for it

    return xmlAppState->writeToFile (xmlFolder.getChildFile (name)
                                              .withFileExtension ("xml"), {});
}

:slight_smile:


#3

Thanks Julian, that was correct.
I’m happy somehow to make you sad, but not sad to the point where you would not answer! Sorry, but I’m afraid I’ll post more ‘bad practices’ code like this in the future. Surely not all users will.