/* logic/reader.c — 阅读方向状态(默认 RTL;M4/M6 的阅读器直接复用)。 * * M5 评审 P1 修复:方向设置要「下次启动生效」。最小持久化(不新增任何方向状态): * - mr_reader_set_direction 成功切换后 best-effort 写 meow_config("reader.dir","rtl"/"ltr"); * 写失败只打 WARN,不阻断切换(本次运行仍生效); * - mr_reader_get_direction 首次调用惰性读一次配置恢复;无配置/值非法 → 默认 RTL; * 之后走纯内存快路径(不再读配置)。 * 依赖面:只 include 配置/日志两个模块头(不含 UI);宿主测试用 * test/settings_reader_stub.c 顶替 meow_config_* 与 meow_log。 */ #include "reader.h" #include "include/meow_ui_config.h" #include "include/meow_ui_log.h" #include #define MR_DIR_CFG_KEY "reader.dir" #define MR_DIR_CFG_LTR "ltr" #define MR_DIR_CFG_RTL "rtl" static int g_direction = MR_DIR_RTL; static int g_restored; /* 0=尚未从配置恢复(首次 get/set 时读一次) */ static void reader_restore_once(void) { char buf[8]; if (g_restored) return; g_restored = 1; /* 无配置也只读一次:之后纯内存 */ if (meow_config_get(MR_DIR_CFG_KEY, buf, sizeof buf) != MEOW_CFG_OK) return; if (strcmp(buf, MR_DIR_CFG_LTR) == 0) g_direction = MR_DIR_LTR; else if (strcmp(buf, MR_DIR_CFG_RTL) == 0) g_direction = MR_DIR_RTL; /* 其它值:忽略,保持默认 RTL */ } int mr_reader_get_direction(void) { reader_restore_once(); return g_direction; } void mr_reader_set_direction(int dir) { if (dir != MR_DIR_RTL && dir != MR_DIR_LTR) return; reader_restore_once(); /* 先恢复:避免「没 get 过就 set」把配置里的旧方向当默认值 */ if (g_direction == dir) return; /* 无变化不写盘 */ g_direction = dir; if (meow_config_set(MR_DIR_CFG_KEY, dir == MR_DIR_LTR ? MR_DIR_CFG_LTR : MR_DIR_CFG_RTL) != MEOW_CFG_OK) meow_log(MEOW_LOG_WARN, "reader.dir: save failed (best-effort, kept in memory)"); } #ifdef MR_READER_TEST_HOOKS void mr_reader_test_reset(void) { g_direction = MR_DIR_RTL; g_restored = 0; } #endif