Ken Van Hoeylandt 6c67845645
Cleanup and improvements (#194)
- Lots of changes for migrating C code to C++
- Improved `Lockable` in several ways like adding `withLock()` (+ tests)
- Improved `Semaphore` a bit for improved readability, and also added some tests
- Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC
- Simplification of WiFi connection
- Updated funding options
- (and more)
2025-01-28 17:39:58 +01:00

73 lines
1.4 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <dirent.h>
#include "Mutex.h"
namespace tt::app::files {
class State {
public:
enum PendingAction {
ActionNone,
ActionDelete,
ActionRename
};
private:
Mutex mutex = Mutex(Mutex::Type::Recursive);
std::vector<dirent> dir_entries;
std::string current_path;
std::string selected_child_entry;
PendingAction action = ActionNone;
public:
State();
void freeEntries() {
dir_entries.clear();
}
~State() {
freeEntries();
}
bool setEntriesForChildPath(const std::string& child_path);
bool setEntriesForPath(const std::string& path);
template <std::invocable<const std::vector<dirent> &> Func>
void withEntries(Func&& onEntries) const {
mutex.withLock([&]() {
std::invoke(std::forward<Func>(onEntries), dir_entries);
});
}
bool getDirent(uint32_t index, dirent& dirent);
void setSelectedChildEntry(const std::string& newFile) {
selected_child_entry = newFile;
action = ActionNone;
}
std::string getSelectedChildEntry() const { return selected_child_entry; }
std::string getCurrentPath() const { return current_path; }
std::string getSelectedChildPath() const;
PendingAction getPendingAction() const {
return action;
}
void setPendingAction(PendingAction newAction) {
action = newAction;
}
};
}