58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Optional
|
||
|
|
from .models import TrackerConfig, TrackedTask
|
||
|
|
from .utils import get_platform, ensure_dir
|
||
|
|
from .ui.overlay import OverlayWindow
|
||
|
|
|
||
|
|
|
||
|
|
class Application:
|
||
|
|
def __init__(self):
|
||
|
|
self.config_path = Path("config.json")
|
||
|
|
self.tasks_dir = Path("tasks")
|
||
|
|
self.config: TrackerConfig = self._load_or_create_config()
|
||
|
|
self.current_task: Optional[TrackedTask] = None
|
||
|
|
self.platform = get_platform()
|
||
|
|
|
||
|
|
# Загружаем текущую задачу, если указан ID
|
||
|
|
if self.config.current_task_id:
|
||
|
|
self.current_task = self._load_task(self.config.current_task_id)
|
||
|
|
|
||
|
|
# Создаём директорию для задач, если нужно
|
||
|
|
ensure_dir(self.tasks_dir)
|
||
|
|
|
||
|
|
def _load_or_create_config(self) -> TrackerConfig:
|
||
|
|
if self.config_path.exists():
|
||
|
|
with open(self.config_path, "r", encoding="utf-8") as f:
|
||
|
|
data = json.load(f)
|
||
|
|
return TrackerConfig.model_validate(data)
|
||
|
|
else:
|
||
|
|
config = TrackerConfig()
|
||
|
|
self._save_config(config)
|
||
|
|
return config
|
||
|
|
|
||
|
|
def _save_config(self, config: TrackerConfig) -> None:
|
||
|
|
with open(self.config_path, "w", encoding="utf-8") as f:
|
||
|
|
f.write(config.model_dump_json(indent=2))
|
||
|
|
|
||
|
|
def _load_task(self, task_id: str) -> Optional[TrackedTask]:
|
||
|
|
task_file = self.tasks_dir / f"{task_id}.json"
|
||
|
|
if task_file.exists():
|
||
|
|
try:
|
||
|
|
with open(task_file, "r", encoding="utf-8") as f:
|
||
|
|
data = json.load(f)
|
||
|
|
return TrackedTask.model_validate(data)
|
||
|
|
except Exception:
|
||
|
|
return None
|
||
|
|
return None
|
||
|
|
|
||
|
|
def save_task(self, task: TrackedTask) -> None:
|
||
|
|
task_file = self.tasks_dir / f"{task.task_id}.json"
|
||
|
|
with open(task_file, "w", encoding="utf-8") as f:
|
||
|
|
f.write(task.model_dump_json(indent=2))
|
||
|
|
|
||
|
|
def run(self):
|
||
|
|
# Пока просто запускаем UI
|
||
|
|
# Позже сюда добавим трекинг в фоне
|
||
|
|
overlay = OverlayWindow()
|
||
|
|
overlay.run()
|