Refactor recent files management: consolidate RecentFilesManager into project.h and remove redundant recent_files.h

This commit is contained in:
scawful
2024-11-09 20:38:00 -05:00
parent 4a9df65ef5
commit ba5c86f0ba
5 changed files with 55 additions and 69 deletions

View File

@@ -1,16 +1,20 @@
#ifndef YAZE_APP_CORE_PROJECT_H
#define YAZE_APP_CORE_PROJECT_H
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "app/core/common.h"
#include "app/core/constants.h"
#include "app/core/utils/file_util.h"
namespace yaze {
namespace app {
const std::string kRecentFilesFilename = "recent_files.txt";
constexpr char kEndOfProjectFile[] = "EndOfProjectFile";
/**
@@ -31,13 +35,13 @@ struct Project : public core::ExperimentFlags {
* @return An absl::Status indicating the success or failure of the project
* creation.
*/
absl::Status Create(const std::string &project_name) {
absl::Status Create(const std::string& project_name) {
name = project_name;
project_opened_ = true;
return absl::OkStatus();
}
absl::Status Open(const std::string &project_path);
absl::Status Open(const std::string& project_path);
absl::Status Save();
absl::Status CheckForEmptyFields() {
@@ -61,6 +65,54 @@ struct Project : public core::ExperimentFlags {
std::string keybindings_file = "";
};
class RecentFilesManager {
public:
RecentFilesManager() : RecentFilesManager(kRecentFilesFilename) {}
RecentFilesManager(const std::string& filename) : filename_(filename) {}
void AddFile(const std::string& file_path) {
// Add a file to the list, avoiding duplicates
auto it = std::find(recent_files_.begin(), recent_files_.end(), file_path);
if (it == recent_files_.end()) {
recent_files_.push_back(file_path);
}
}
void Save() {
std::ofstream file(filename_);
if (!file.is_open()) {
return; // Handle the error appropriately
}
for (const auto& file_path : recent_files_) {
file << file_path << std::endl;
}
}
void Load() {
std::ifstream file(filename_);
if (!file.is_open()) {
return;
}
recent_files_.clear();
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
recent_files_.push_back(line);
}
}
}
const std::vector<std::string>& GetRecentFiles() const {
return recent_files_;
}
private:
std::string filename_;
std::vector<std::string> recent_files_;
};
} // namespace app
} // namespace yaze

View File

@@ -16,7 +16,6 @@
#include "app/editor/sprite/sprite_editor.h"
#include "app/editor/utils/editor.h"
#include "app/editor/utils/gfx_context.h"
#include "app/editor/utils/recent_files.h"
#include "app/emu/emulator.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"

View File

@@ -4,6 +4,7 @@
#include "absl/strings/match.h"
#include "app/core/constants.h"
#include "app/core/platform/file_dialog.h"
#include "app/core/project.h"
#include "app/editor/code/assembly_editor.h"
#include "app/editor/dungeon/dungeon_editor.h"
#include "app/editor/graphics/graphics_editor.h"
@@ -13,7 +14,6 @@
#include "app/editor/overworld/overworld_editor.h"
#include "app/editor/sprite/sprite_editor.h"
#include "app/editor/utils/flags.h"
#include "app/editor/utils/recent_files.h"
#include "app/emu/emulator.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"

View File

@@ -4,7 +4,6 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "absl/status/status.h"
#include "app/core/message.h"
#include "app/core/project.h"
#include "app/editor/code/assembly_editor.h"
#include "app/editor/code/memory_editor.h"

View File

@@ -1,64 +0,0 @@
#ifndef YAZE_APP_EDITOR_UTILS_RECENT_FILES_H
#define YAZE_APP_EDITOR_UTILS_RECENT_FILES_H
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
namespace yaze {
namespace app {
namespace editor {
class RecentFilesManager {
public:
RecentFilesManager(const std::string& filename) : filename_(filename) {}
void AddFile(const std::string& filePath) {
// Add a file to the list, avoiding duplicates
auto it = std::find(recentFiles_.begin(), recentFiles_.end(), filePath);
if (it == recentFiles_.end()) {
recentFiles_.push_back(filePath);
}
}
void Save() {
std::ofstream file(filename_);
if (!file.is_open()) {
return; // Handle the error appropriately
}
for (const auto& filePath : recentFiles_) {
file << filePath << std::endl;
}
}
void Load() {
std::ifstream file(filename_);
if (!file.is_open()) {
return; // Handle the error appropriately
}
recentFiles_.clear();
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
recentFiles_.push_back(line);
}
}
}
const std::vector<std::string>& GetRecentFiles() const {
return recentFiles_;
}
private:
std::string filename_;
std::vector<std::string> recentFiles_;
};
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_UTILS_RECENT_FILES_H