A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: back navigation from BMPViewer (#1597)

## Summary

This fixes navigating back from the BMP Viewer to the FileBrowser which
was broken when moving to the new ActivityManager

This is fixed by making FileBrowserActivity able to take a full file
path on enter and splitting the basePath and fileName from it and
navigating to the correct place.

fixes:
https://github.com/crosspoint-reader/crosspoint-reader/issues/1553

duplicates:
https://github.com/crosspoint-reader/crosspoint-reader/pull/910 to some
extend but mine has the file cursor at the correct file instead of the
first one in the folder

## Additional Context

* Add any other information that might be helpful for the reviewer
(e.g., performance implications, potential risks,
specific areas to focus on).

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**PARTIALLY**_

---------

Co-authored-by: Jan Ivanov <jan.ivanov@sirma.com>

authored by

Jan Ivanov
Jan Ivanov
and committed by
GitHub
8d6b35b8 83cd96bc

+43 -13
+8
lib/FsHelpers/FsHelpers.cpp
··· 78 78 79 79 bool hasMarkdownExtension(std::string_view fileName) { return checkFileExtension(fileName, ".md"); } 80 80 81 + std::string extractFolderPath(const std::string& filePath) { 82 + const auto lastSlash = filePath.find_last_of('/'); 83 + if (lastSlash == std::string::npos || lastSlash == 0) { 84 + return "/"; 85 + } 86 + return filePath.substr(0, lastSlash); 87 + } 88 + 81 89 } // namespace FsHelpers
+2
lib/FsHelpers/FsHelpers.h
··· 55 55 // Check for .md extension (case-insensitive) 56 56 bool hasMarkdownExtension(std::string_view fileName); 57 57 58 + std::string extractFolderPath(const std::string& filePath); 59 + 58 60 } // namespace FsHelpers
+29 -2
src/activities/home/FileBrowserActivity.cpp
··· 108 108 void FileBrowserActivity::onEnter() { 109 109 Activity::onEnter(); 110 110 111 - loadFiles(); 112 111 selectorIndex = 0; 113 112 113 + auto root = Storage.open(basepath.c_str()); 114 + if (!root) { 115 + basepath = "/"; 116 + loadFiles(); 117 + } else if (!root.isDirectory()) { 118 + root.close(); 119 + lockLongPressBack = mappedInput.isPressed(MappedInputManager::Button::Back); 120 + 121 + const std::string oldPath = basepath; 122 + basepath = FsHelpers::extractFolderPath(basepath); 123 + loadFiles(); 124 + 125 + const auto pos = oldPath.find_last_of('/'); 126 + const std::string fileName = oldPath.substr(pos + 1); 127 + selectorIndex = findEntry(fileName); 128 + } else { 129 + root.close(); 130 + loadFiles(); 131 + } 132 + 114 133 requestUpdate(); 115 134 } 116 135 ··· 129 148 130 149 void FileBrowserActivity::loop() { 131 150 // Long press BACK (1s+) goes to root folder 151 + // but Long press BACK (1s+) from ReaderActivity sends us here with the MappedInput already set. 152 + // So ignore it the first time. 132 153 if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= GO_HOME_MS && 133 - basepath != "/") { 154 + basepath != "/" && !lockLongPressBack) { 134 155 basepath = "/"; 135 156 loadFiles(); 136 157 selectorIndex = 0; 158 + requestUpdate(); 159 + return; 160 + } 161 + 162 + if (lockLongPressBack && mappedInput.wasReleased(MappedInputManager::Button::Back)) { 163 + lockLongPressBack = false; 137 164 return; 138 165 } 139 166
+2
src/activities/home/FileBrowserActivity.h
··· 17 17 18 18 size_t selectorIndex = 0; 19 19 20 + bool lockLongPressBack = false; 21 + 20 22 // Files state 21 23 std::string basepath = "/"; 22 24 std::vector<std::string> files;
+1 -9
src/activities/reader/ReaderActivity.cpp
··· 13 13 #include "activities/util/BmpViewerActivity.h" 14 14 #include "activities/util/FullScreenMessageActivity.h" 15 15 16 - std::string ReaderActivity::extractFolderPath(const std::string& filePath) { 17 - const auto lastSlash = filePath.find_last_of('/'); 18 - if (lastSlash == std::string::npos || lastSlash == 0) { 19 - return "/"; 20 - } 21 - return filePath.substr(0, lastSlash); 22 - } 23 - 24 16 bool ReaderActivity::isXtcFile(const std::string& path) { return FsHelpers::hasXtcExtension(path); } 25 17 26 18 bool ReaderActivity::isTxtFile(const std::string& path) { ··· 77 69 78 70 void ReaderActivity::goToLibrary(const std::string& fromBookPath) { 79 71 // If coming from a book, start in that book's folder; otherwise start from root 80 - auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath); 72 + auto initialPath = fromBookPath.empty() ? "/" : FsHelpers::extractFolderPath(fromBookPath); 81 73 activityManager.goToFileBrowser(std::move(initialPath)); 82 74 } 83 75
-1
src/activities/reader/ReaderActivity.h
··· 18 18 static bool isTxtFile(const std::string& path); 19 19 static bool isBmpFile(const std::string& path); 20 20 21 - static std::string extractFolderPath(const std::string& filePath); 22 21 void goToLibrary(const std::string& fromBookPath = ""); 23 22 void onGoToEpubReader(std::unique_ptr<Epub> epub); 24 23 void onGoToXtcReader(std::unique_ptr<Xtc> xtc);
+1 -1
src/activities/util/BmpViewerActivity.cpp
··· 94 94 Activity::loop(); 95 95 96 96 if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { 97 - onGoHome(); 97 + activityManager.goToFileBrowser(filePath); 98 98 return; 99 99 } 100 100 }