collections: Use a set for game ids

This commit is contained in:
Jamie Gravendeel
2025-12-27 17:51:02 +01:00
parent b592b95302
commit 8fda6dc7c2
3 changed files with 9 additions and 10 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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)