Cleanups
This commit is contained in:
@@ -101,9 +101,7 @@ class Game(Gtk.Box):
|
||||
self.app.quit()
|
||||
|
||||
def toggle_hidden(self):
|
||||
data = json.loads(
|
||||
(self.win.games_dir / f"{self.game_id}.json").read_text("utf-8")
|
||||
)
|
||||
data = json.load((self.win.games_dir / f"{self.game_id}.json").open())
|
||||
|
||||
data["hidden"] = not data["hidden"]
|
||||
|
||||
|
||||
@@ -63,10 +63,9 @@ def heroic_importer(win):
|
||||
if not win.schema.get_boolean("heroic-import-epic"):
|
||||
pass
|
||||
elif (heroic_dir / "store_cache" / "legendary_library.json").exists():
|
||||
data = (heroic_dir / "store_cache" / "legendary_library.json").read_text(
|
||||
"utf-8"
|
||||
library = json.load(
|
||||
(heroic_dir / "store_cache" / "legendary_library.json").open()
|
||||
)
|
||||
library = json.loads(data)
|
||||
|
||||
try:
|
||||
for game in library["library"]:
|
||||
@@ -117,8 +116,7 @@ def heroic_importer(win):
|
||||
if not win.schema.get_boolean("heroic-import-gog"):
|
||||
pass
|
||||
elif (heroic_dir / "gog_store" / "installed.json").exists():
|
||||
data = (heroic_dir / "gog_store" / "installed.json").read_text("utf-8")
|
||||
installed = json.loads(data)
|
||||
installed = json.load((heroic_dir / "gog_store" / "installed.json").open())
|
||||
|
||||
importer.total_queue += len(installed["installed"])
|
||||
importer.queue += len(installed["installed"])
|
||||
@@ -137,8 +135,7 @@ def heroic_importer(win):
|
||||
continue
|
||||
|
||||
# Get game title and developer from library.json as they are not present in installed.json
|
||||
data = (heroic_dir / "gog_store" / "library.json").read_text("utf-8")
|
||||
library = json.loads(data)
|
||||
library = json.load((heroic_dir / "gog_store" / "library.json").open())
|
||||
for game in library["games"]:
|
||||
if game["app_name"] == app_name:
|
||||
values["developer"] = game["developer"]
|
||||
@@ -165,8 +162,7 @@ def heroic_importer(win):
|
||||
if not win.schema.get_boolean("heroic-import-sideload"):
|
||||
pass
|
||||
elif (heroic_dir / "sideload_apps" / "library.json").exists():
|
||||
data = (heroic_dir / "sideload_apps" / "library.json").read_text("utf-8")
|
||||
library = json.loads(data)
|
||||
library = json.load((heroic_dir / "sideload_apps" / "library.json").open())
|
||||
|
||||
importer.total_queue += len(library["games"])
|
||||
importer.queue += len(library["games"])
|
||||
|
||||
@@ -326,7 +326,7 @@ def create_details_window(win, game_id=None):
|
||||
path = win.games_dir / f"{game_id}.json"
|
||||
|
||||
if path.exists():
|
||||
data = json.loads(path.read_text("utf-8"))
|
||||
data = json.load(path.open())
|
||||
data.update(values)
|
||||
save_game(win, data)
|
||||
else:
|
||||
|
||||
@@ -26,13 +26,14 @@ def get_games(win, game_ids=None):
|
||||
if not win.games_dir.exists():
|
||||
return {}
|
||||
|
||||
if game_ids:
|
||||
game_files = [win.games_dir / f"{game_id}.json" for game_id in game_ids]
|
||||
else:
|
||||
game_files = win.games_dir.iterdir()
|
||||
game_files = (
|
||||
[win.games_dir / f"{game_id}.json" for game_id in game_ids]
|
||||
if game_ids
|
||||
else win.games_dir.iterdir()
|
||||
)
|
||||
|
||||
for game in game_files:
|
||||
data = json.loads(game.read_text("utf-8"))
|
||||
for open_file in game_files:
|
||||
data = json.load(open_file.open())
|
||||
games[data["game_id"]] = data
|
||||
|
||||
return games
|
||||
|
||||
@@ -79,6 +79,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
||||
active_game_id = None
|
||||
scaled_pixbuf = None
|
||||
details_view_game_cover = None
|
||||
sort_state = "a-z"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -126,6 +127,9 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
||||
self.library.set_filter_func(self.search_filter)
|
||||
self.hidden_library.set_filter_func(self.hidden_search_filter)
|
||||
|
||||
self.library.set_sort_func(self.sort_func)
|
||||
self.hidden_library.set_sort_func(self.sort_func)
|
||||
|
||||
self.update_games(get_games(self))
|
||||
|
||||
# Connect signals
|
||||
@@ -358,44 +362,24 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
||||
0.2 + (sum(luminances) / len(luminances) + min(luminances)) / 2
|
||||
)
|
||||
|
||||
def a_z_sort(self, child1, child2):
|
||||
name1 = child1.get_first_child().name.lower()
|
||||
name2 = child2.get_first_child().name.lower()
|
||||
if name1 > name2:
|
||||
return 1
|
||||
if name1 < name2:
|
||||
return -1
|
||||
if child1.get_first_child().game_id > child2.get_first_child().game_id:
|
||||
return 1
|
||||
return -1
|
||||
def sort_func(self, child1, child2):
|
||||
games = (child1.get_first_child(), child2.get_first_child())
|
||||
var, order = "name", True
|
||||
|
||||
def z_a_sort(self, child1, child2):
|
||||
name1 = child1.get_first_child().name.lower()
|
||||
name2 = child2.get_first_child().name.lower()
|
||||
if name1 > name2:
|
||||
return -1
|
||||
return 1 if name1 < name2 else self.a_z_sort(child1, child2)
|
||||
if self.sort_state in ("newest", "oldest"):
|
||||
var, order = "added", self.sort_state == "newest"
|
||||
elif self.sort_state == "last_played":
|
||||
var = "last_played"
|
||||
elif self.sort_state == "a-z":
|
||||
order = False
|
||||
|
||||
def newest_sort(self, child1, child2):
|
||||
time1 = self.games[child1.get_first_child().game_id].added
|
||||
time2 = self.games[child2.get_first_child().game_id].added
|
||||
if time1 > time2:
|
||||
return -1
|
||||
return 1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||
def get_value(index):
|
||||
return str(getattr(games[index], var)).lower()
|
||||
|
||||
def oldest_sort(self, child1, child2):
|
||||
time1 = self.games[child1.get_first_child().game_id].added
|
||||
time2 = self.games[child2.get_first_child().game_id].added
|
||||
if time1 > time2:
|
||||
return 1
|
||||
return -1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||
if var != "name" and get_value(0) == get_value(1):
|
||||
var, order = "name", True
|
||||
|
||||
def last_played_sort(self, child1, child2):
|
||||
time1 = self.games[child1.get_first_child().game_id].last_played
|
||||
time2 = self.games[child2.get_first_child().game_id].last_played
|
||||
if time1 > time2:
|
||||
return -1
|
||||
return 1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||
return ((get_value(0) > get_value(1)) ^ order) * 2 - 1
|
||||
|
||||
def on_go_back_action(self, _widget, _unused, _x=None, _y=None):
|
||||
if self.stack.get_visible_child() == self.hidden_library_view:
|
||||
@@ -427,28 +411,12 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
||||
|
||||
def on_sort_action(self, action, state):
|
||||
action.set_state(state)
|
||||
state = str(state).strip("'")
|
||||
|
||||
if state == "a-z":
|
||||
sort_func = self.a_z_sort
|
||||
|
||||
elif state == "z-a":
|
||||
sort_func = self.z_a_sort
|
||||
|
||||
elif state == "newest":
|
||||
sort_func = self.newest_sort
|
||||
|
||||
elif state == "oldest":
|
||||
sort_func = self.oldest_sort
|
||||
|
||||
else:
|
||||
sort_func = self.last_played_sort
|
||||
self.sort_state = str(state).strip("'")
|
||||
self.library.invalidate_sort()
|
||||
|
||||
Gio.Settings(schema_id="hu.kramo.Cartridge.State").set_string(
|
||||
"sort-mode", state
|
||||
"sort-mode", self.sort_state
|
||||
)
|
||||
self.library.set_sort_func(sort_func)
|
||||
self.hidden_library.set_sort_func(sort_func)
|
||||
|
||||
def on_toggle_search_action(self, _widget, _unused):
|
||||
if self.stack.get_visible_child() == self.library_view:
|
||||
|
||||
Reference in New Issue
Block a user