diff --git a/src/DirectoryNode.cpp b/src/DirectoryNode.cpp index c0f3b76..bce5ddd 100644 --- a/src/DirectoryNode.cpp +++ b/src/DirectoryNode.cpp @@ -116,31 +116,72 @@ DirectoryNode *DirectoryNode::findFirstDirectoryWithMP3s() return nullptr; } -void DirectoryNode::advanceToNextMP3() +void DirectoryNode::advanceToFirstMP3InThisNode() { + if (mp3Files.size()>0) { + currentPlaying = &mp3Files[0]; + } +} + +DirectoryNode* DirectoryNode::advanceToNextMP3(const String* currentGlobal) { - if (currentPlaying != nullptr) + bool useFirst = false; + Serial.println(currentGlobal->c_str()); + if (currentGlobal != nullptr) { for (size_t i = 0; i < mp3Files.size(); i++) { - if (*currentPlaying == mp3Files[i]) + if (*currentGlobal == mp3Files[i]) { // Found the current playing MP3 file if (i < mp3Files.size() - 1) { // Advance to the next MP3 file in the same directory currentPlaying = &mp3Files[i + 1]; - return; + return this; + } + useFirst = true; + // Reached the end of the MP3 files in the directory + break; + } + } + } + + + // We are either not playing, or we've exhausted all the MP3 files in this directory. + // Therefore, we need to recursively look in our subdirectories. + for (auto subdir : subdirectories) + { + Serial.println("searching next node"); + + if (useFirst && subdir->mp3Files.size()>0) { + subdir->currentPlaying = &subdir->mp3Files[0]; + return subdir; + } + + // Have each subdirectory advance its song + for (size_t i = 0; i < subdir->mp3Files.size(); i++) + { + if (*currentGlobal == subdir->mp3Files[i]) + { + // Found the current playing MP3 file + if (i < subdir->mp3Files.size() - 1) + { + // Advance to the next MP3 file in the same directory + subdir->currentPlaying = &subdir->mp3Files[i + 1]; + return subdir; + } else { + useFirst = true; } // Reached the end of the MP3 files in the directory break; } } } - // If not playing or reached the end, set the first MP3 file as the current playing - if (!mp3Files.empty()) - { - currentPlaying = &mp3Files[0]; - } + + // If we get here, there were no MP3 files or subdirectories left to check + currentPlaying = nullptr; + Serial.println("no more nodes found"); + return this; } String DirectoryNode::getDirectoryStructureHTML() const diff --git a/src/DirectoryNode.h b/src/DirectoryNode.h index 20dacdd..5eaadbe 100644 --- a/src/DirectoryNode.h +++ b/src/DirectoryNode.h @@ -26,7 +26,8 @@ public: void addMP3File(const String& mp3File); void buildDirectoryTree(const char* currentPath); void printDirectoryTree(int level = 0) const; - void advanceToNextMP3(); + DirectoryNode* advanceToNextMP3(const String* currentGlobal); + void advanceToFirstMP3InThisNode(); String getDirectoryStructureHTML() const; void appendIndentation(String& html, int level) const; DirectoryNode* findFirstDirectoryWithMP3s(); diff --git a/src/WebContent.h b/src/WebContent.h index 4b2d7b5..debf04e 100644 --- a/src/WebContent.h +++ b/src/WebContent.h @@ -5,11 +5,10 @@ const char index_html[] PROGMEM = R"rawliteral(