From ba5c86f0ba3fefee3a3c1bc9d30fe448e72c4bff Mon Sep 17 00:00:00 2001 From: scawful Date: Sat, 9 Nov 2024 20:38:00 -0500 Subject: [PATCH] Refactor recent files management: consolidate RecentFilesManager into project.h and remove redundant recent_files.h --- src/app/core/project.h | 56 ++++++++++++++++++++++++- src/app/editor/code/memory_editor.h | 1 - src/app/editor/editor_manager.cc | 2 +- src/app/editor/editor_manager.h | 1 - src/app/editor/utils/recent_files.h | 64 ----------------------------- 5 files changed, 55 insertions(+), 69 deletions(-) delete mode 100644 src/app/editor/utils/recent_files.h diff --git a/src/app/core/project.h b/src/app/core/project.h index c6b691fe..cc60d178 100644 --- a/src/app/core/project.h +++ b/src/app/core/project.h @@ -1,16 +1,20 @@ #ifndef YAZE_APP_CORE_PROJECT_H #define YAZE_APP_CORE_PROJECT_H +#include #include #include +#include #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& GetRecentFiles() const { + return recent_files_; + } + + private: + std::string filename_; + std::vector recent_files_; +}; + } // namespace app } // namespace yaze diff --git a/src/app/editor/code/memory_editor.h b/src/app/editor/code/memory_editor.h index d35c1bc4..e97262bf 100644 --- a/src/app/editor/code/memory_editor.h +++ b/src/app/editor/code/memory_editor.h @@ -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" diff --git a/src/app/editor/editor_manager.cc b/src/app/editor/editor_manager.cc index aea1a1d4..1a440e81 100644 --- a/src/app/editor/editor_manager.cc +++ b/src/app/editor/editor_manager.cc @@ -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" diff --git a/src/app/editor/editor_manager.h b/src/app/editor/editor_manager.h index ff4f1769..a542f178 100644 --- a/src/app/editor/editor_manager.h +++ b/src/app/editor/editor_manager.h @@ -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" diff --git a/src/app/editor/utils/recent_files.h b/src/app/editor/utils/recent_files.h deleted file mode 100644 index 2b0c7f6c..00000000 --- a/src/app/editor/utils/recent_files.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef YAZE_APP_EDITOR_UTILS_RECENT_FILES_H -#define YAZE_APP_EDITOR_UTILS_RECENT_FILES_H - -#include -#include -#include -#include - -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& GetRecentFiles() const { - return recentFiles_; - } - - private: - std::string filename_; - std::vector recentFiles_; -}; - -} // namespace editor -} // namespace app -} // namespace yaze - -#endif // YAZE_APP_EDITOR_UTILS_RECENT_FILES_H \ No newline at end of file