""" 자동 점심시간 적용 컨트롤러. 조건: auto_lunch=true, 출근 후 4시간 이상, 점심 미적용, 오늘 미적용. 주말/공휴일은 스킵. 1Hz hot path에서 호출되므로 캐시 사용. """ from __future__ import annotations from datetime import datetime from core.settings_keys import AUTO_LUNCH class AutoLunchManager: """update_display() 1Hz tick에서 호출.""" def __init__(self, window): self.window = window self.db = window.db self._enabled_cache = None # None=미로딩, True/False=캐시값 self._non_working_cache = None self._non_working_date = None def invalidate(self) -> None: """설정 변경 시 캐시 무효화 (reload_settings에서 호출).""" self._enabled_cache = None def maybe_apply(self, now: datetime) -> None: w = self.window if w.auto_lunch_applied_today or w.lunch_break_enabled: return if self._enabled_cache is None: self._enabled_cache = ( self.db.get_setting(AUTO_LUNCH, 'false').lower() == 'true' ) if not self._enabled_cache: return today = now.date() if self._non_working_date != today: self._non_working_cache = w.time_calc.is_non_working_day(now, self.db) self._non_working_date = today if self._non_working_cache: return elapsed = now - w.clock_in_time if elapsed.total_seconds() < 4 * 3600: return w.auto_lunch_applied_today = True w.lunch_break_enabled = True w.lunch_button.setChecked(True) w.update_lunch_status() if w.is_clocked_in: self.db.update_lunch_break(today.isoformat(), True)