Skip to content
Repos

DevPlaceApp

C++ 2.6 KB64 lines Opens in site

Directory Raw Download

#pragma once
// Spec: shell.md section 5 (AppSettingsStore). DIFF: UserDefaults -> one JSON file settings.json in the SDL pref
// path, written atomically (temp + rename), size capped (SettingsKeys::kMaxFileBytes). Defaults: appearance dark,
// show* true, recentEmojis [] (max 30), drafts "". A file that is too large or not a JSON object is ignored loudly
// (rejectedLoads() counter + log) and defaults are used; a key of the wrong type falls back to its default.
// Every setter bumps version() and notifies subscribers, then persists.
#include <string>
#include <vector>

#include "core/Observable.hpp"
#include "model/AppearanceMode.hpp"

namespace dp {

class SettingsStore : public Observable {
 public:
  static SettingsStore& instance();  // pref path
  explicit SettingsStore(std::string directory);

  AppearanceMode appearance() const { return appearance_; }
  bool showFeedAttachments() const { return showFeedAttachments_; }
  bool showFeedComments() const { return showFeedComments_; }
  bool showReportButtons() const { return showReportButtons_; }
  const std::vector<std::string>& recentEmojis() const { return recentEmojis_; }
  const std::string& draftPostTitle() const { return draftPostTitle_; }
  const std::string& draftPostMessage() const { return draftPostMessage_; }
  const std::string& draftReplyMessage() const { return draftReplyMessage_; }

  void setAppearance(AppearanceMode value);
  void setShowFeedAttachments(bool value);
  void setShowFeedComments(bool value);
  void setShowReportButtons(bool value);
  void setDraftPostTitle(const std::string& value);
  void setDraftPostMessage(const std::string& value);
  void setDraftReplyMessage(const std::string& value);
  // Removes an existing occurrence, inserts at index 0, keeps at most 30.
  void recordEmojiPick(const std::string& emoji);

  // Switches the backing directory (tests, profiles) and reloads from it. Empty = memory only.
  void rebind(std::string directory);
  void reload();
  const std::string& filePath() const { return path_; }
  unsigned rejectedLoads() const { return rejectedLoads_; }
  unsigned failedSaves() const { return failedSaves_; }

 private:
  void changed();
  void save();
  static std::string capped(const std::string& value, std::size_t maxBytes);

  std::string path_;
  AppearanceMode appearance_ = AppearanceMode::Dark;
  bool showFeedAttachments_ = true;
  bool showFeedComments_ = true;
  bool showReportButtons_ = true;
  std::vector<std::string> recentEmojis_;
  std::string draftPostTitle_;
  std::string draftPostMessage_;
  std::string draftReplyMessage_;
  unsigned rejectedLoads_ = 0;
  unsigned failedSaves_ = 0;
};

}  // namespace dp
39.1ms