59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
from core.models.session.SessionState import SessionState
|
|
from core.controllers.ProfileController import ProfileController
|
|
from core.infrastructure.global_state import ProfileStateRepository
|
|
from core.observers.Notifications import Notifications, EventType
|
|
|
|
|
|
class SessionStateController:
|
|
@staticmethod
|
|
def get(id: int):
|
|
return SessionState.find_by_id(id)
|
|
|
|
@staticmethod
|
|
def get_or_new(id: int):
|
|
session_state = SessionStateController.get(id)
|
|
|
|
if session_state is None:
|
|
# Update the Singleton Repo
|
|
repo = ProfileStateRepository()
|
|
repo.sync_session_added(id)
|
|
|
|
# Create and save the session
|
|
session_state = SessionState(id)
|
|
session_state.save()
|
|
return session_state
|
|
|
|
return session_state
|
|
|
|
@staticmethod
|
|
def exists(id: int):
|
|
return SessionState.exists(id)
|
|
|
|
@staticmethod
|
|
def all():
|
|
return SessionState.all()
|
|
|
|
@staticmethod
|
|
def update_or_create(session_state):
|
|
# save to model:
|
|
session_state.save()
|
|
|
|
# get profile id:
|
|
profile_id = session_state.id
|
|
|
|
# Ensure repo knows this session exists
|
|
ProfileStateRepository().sync_session_added(profile_id)
|
|
|
|
# notify UI:
|
|
Notifications.emit(EventType.SESSION_PROFILE_ENABLE, profile_id)
|
|
|
|
@staticmethod
|
|
def remove(profile_id: int):
|
|
# save to model:
|
|
SessionState.dissolve(profile_id)
|
|
|
|
# cache:
|
|
ProfileStateRepository().sync_session_removed(profile_id)
|
|
|
|
# notify UI:
|
|
Notifications.emit(EventType.SESSION_PROFILE_DISABLE, profile_id)
|