From 8fda6dc7c26323568a4ba244aa31268c36b0239b Mon Sep 17 00:00:00 2001 From: Jamie Gravendeel Date: Sat, 27 Dec 2025 17:51:02 +0100 Subject: [PATCH] collections: Use a set for game ids --- cartridges/collections.py | 8 ++++---- cartridges/ui/collections.py | 9 ++++----- cartridges/ui/window.py | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cartridges/collections.py b/cartridges/collections.py index b8e0f59..57293b9 100644 --- a/cartridges/collections.py +++ b/cartridges/collections.py @@ -36,7 +36,7 @@ class Collection(Gio.SimpleActionGroup): def __init__(self, **kwargs: Any): super().__init__(**kwargs) - self.game_ids = self.game_ids or [] + self.game_ids = self.game_ids or set() self.bind_property( "icon", self, @@ -81,12 +81,12 @@ def _get_collections() -> Generator[Collection]: yield Collection( name=data["name"], icon=data["icon"], - game_ids=[ + game_ids={ ident for ident in data["game-ids"] if not ident.startswith(imported.ID) or ident in manually_added_game_ids - ], + }, ) except (KeyError, TypeError): continue @@ -111,7 +111,7 @@ def save(): { "name": GLib.Variant.new_string(collection.name), "icon": GLib.Variant.new_string(collection.icon), - "game-ids": GLib.Variant.new_strv(collection.game_ids), + "game-ids": GLib.Variant.new_strv(tuple(collection.game_ids)), "removed": GLib.Variant.new_boolean(collection.removed), } for collection in cast(Iterable[Collection], model) diff --git a/cartridges/ui/collections.py b/cartridges/ui/collections.py index cc85387..498dd3f 100644 --- a/cartridges/ui/collections.py +++ b/cartridges/ui/collections.py @@ -114,12 +114,11 @@ class CollectionsBox(Adw.Bin): for button in cast(Iterable[CollectionButton], self.box): game_ids = button.collection.game_ids old_game_ids = game_ids.copy() - in_collection = self.game.game_id in game_ids - if button.props.active and not in_collection: - game_ids.append(self.game.game_id) - elif not button.props.active and in_collection: - game_ids.remove(self.game.game_id) + if button.props.active: + game_ids.add(self.game.game_id) + else: + game_ids.discard(self.game.game_id) if game_ids != old_game_ids: filter_changed = True diff --git a/cartridges/ui/window.py b/cartridges/ui/window.py index f797ccf..b7c72da 100644 --- a/cartridges/ui/window.py +++ b/cartridges/ui/window.py @@ -267,7 +267,7 @@ class Window(Adw.ApplicationWindow): def _add_collection(self, game_id: str | None = None): collection = Collection() if game_id: - collection.game_ids.append(game_id) + collection.game_ids.add(game_id) details = CollectionDetails(collection=collection) details.present(self)