🎨 Improved internal manager storage

- Stored pipeline managers in a set
- Renamed store method to enable_manager_in_pipeline
- Simplified a bit the ugly code™ in details_window
This commit is contained in:
GeoffreyCoulaud
2023-06-16 15:38:05 +02:00
parent e694341a31
commit beba0ff1e2
3 changed files with 19 additions and 27 deletions

View File

@@ -205,35 +205,26 @@ class DetailsWindow(Adw.Window):
self.game.save()
self.game.update()
# TODO: this is fucked up
# TODO: this is fucked up (less than before)
# Get a cover from SGDB if none is present
if not self.game_cover.get_pixbuf():
self.game.set_loading(1)
sgdb_manager: SGDBManager = shared.store.managers[SGDBManager][0]
sgdb_manager: SGDBManager = shared.store.managers[SGDBManager]
sgdb_manager.reset_cancellable()
pipeline = Pipeline(self.game, {}, (sgdb_manager,))
pipeline.connect("advanced", self.update_cover_callback)
pipeline.advance()
sgdb_manager.process_game(self.game, {}, self.update_cover_callback)
self.game_cover.pictures.remove(self.cover)
self.close()
self.win.show_details_view(self.game)
def update_cover_callback(self, pipeline: Pipeline):
# Check that managers are done
if not pipeline.is_done:
return
def update_cover_callback(self, manager: SGDBManager):
# Set the game as not loading
self.game.set_loading(-1)
self.game.update()
# Handle errors that occured
errors = []
for manager in pipeline.done:
errors.extend(manager.collect_errors())
for error in errors:
for error in manager.collect_errors():
# On auth error, inform the user
if isinstance(error, SGDBAuthError):
create_dialog(

View File

@@ -92,8 +92,7 @@ class CartridgesApplication(Adw.Application):
shared.store.add_manager(SteamAPIManager())
shared.store.add_manager(OnlineCoverManager())
shared.store.add_manager(SGDBManager())
shared.store.manager_to_pipeline(FileManager)
shared.store.enable_manager_in_pipelines(FileManager)
# Create actions
self.create_actions(

View File

@@ -10,20 +10,26 @@ class Store:
"""Class in charge of handling games being added to the app."""
managers: dict[type[Manager], Manager]
pipeline_managers: set[Manager]
pipelines: dict[str, Pipeline]
games: dict[str, Game]
def __init__(self) -> None:
self.managers = {}
self.games = {}
self.pipeline_managers = set()
self.pipelines = {}
self.games = {}
def add_manager(self, manager: Manager, in_pipeline=True):
"""Add a manager that will run when games are added"""
self.managers[type(manager)] = [manager, in_pipeline]
"""Add a manager to the store"""
manager_type = type(manager)
self.managers[manager_type] = manager
if in_pipeline:
self.enable_manager_in_pipelines(manager_type)
def manager_to_pipeline(self, manager_type: type[Manager]):
self.managers[manager_type][1] = True
def enable_manager_in_pipelines(self, manager_type: type[Manager]):
"""Make a manager run in new pipelines"""
self.pipeline_managers.add(self.managers[manager_type])
def cleanup_game(self, game: Game) -> None:
"""Remove a game's files"""
@@ -65,16 +71,12 @@ class Store:
return None
# Connect signals
for manager, _in_pipeline in self.managers.values():
for manager in self.managers.values():
for signal in manager.signals:
game.connect(signal, manager.execute_resilient_manager_logic)
# Run the pipeline for the game
pipeline = Pipeline(
game,
additional_data,
(manager[0] for manager in self.managers.values() if manager[1]),
)
pipeline = Pipeline(game, additional_data, self.pipeline_managers)
self.games[game.game_id] = game
self.pipelines[game.game_id] = pipeline
pipeline.advance()