Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements in code #825

Merged
merged 11 commits into from
Jan 19, 2024
10 changes: 6 additions & 4 deletions cozy/architecture/observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ def remove_bind(self, prop: str, callback: Callable):
log.info("Prop not found in observers. Skipping remove bind...")

def _notify(self, prop: str):
if prop not in self._observers:
return

try:
if prop in self._observers:
for callback in self._observers[prop]:
callback()
for callback in self._observers[prop]:
callback()
except Exception as e:
log.error(e)
reporter.exception("observable", e)

def _notify_main_thread(self, prop: str):
GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self._notify, (prop))
GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self._notify, prop)

def _destroy_observers(self):
self._observers = {}
28 changes: 12 additions & 16 deletions cozy/model/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import List, Optional

import peewee

Expand All @@ -16,8 +15,8 @@


class Settings:
_storages: List[Storage] = []
_db = cache = inject.attr(SqliteDatabase)
_storages: list[Storage] = []
_db = inject.attr(SqliteDatabase)

def __init__(self):
self._db_object: SettingsModel = SettingsModel.get()
Expand All @@ -27,7 +26,7 @@ def first_start(self) -> bool:
return self._db_object.first_start

@property
def last_played_book(self) -> Optional[Book]:
def last_played_book(self) -> Book | None:
try:
return self._db_object.last_played_book
except peewee.DoesNotExist:
Expand All @@ -48,21 +47,20 @@ def last_played_book(self, new_value):
self._db_object.save(only=self._db_object.dirty_fields)

@property
def default_location(self):
return next(location
for location
in self.storage_locations
if location.default)
def default_location(self) -> bool:
for location in self.storage_locations:
if location.default:
return True
rdbende marked this conversation as resolved.
Show resolved Hide resolved

@property
def storage_locations(self):
def storage_locations(self) -> list[Storage]:
if not self._storages:
self._load_all_storage_locations()

return self._storages

@property
def external_storage_locations(self):
def external_storage_locations(self) -> list[Storage]:
if not self._storages:
self._load_all_storage_locations()

Expand All @@ -78,14 +76,12 @@ def _load_all_storage_locations(self):
try:
self._storages.append(Storage(self._db, storage_db_obj.id))
except InvalidPath:
log.error("Invalid path found in database, skipping: {}".format(storage_db_obj.path))
log.error(f"Invalid path found in database, skipping: {storage_db_obj.path}")
rdbende marked this conversation as resolved.
Show resolved Hide resolved
rdbende marked this conversation as resolved.
Show resolved Hide resolved

self._ensure_default_storage_present()

def _ensure_default_storage_present(self):
default_storage_present = any(storage.default
for storage
in self._storages)
default_storage_present = any(storage.default for storage in self._storages)

if not default_storage_present and len(self._storages) > 0:
if not default_storage_present and self._storages:
self._storages[0].default = True
31 changes: 0 additions & 31 deletions cozy/model/single_file_chapter.py

This file was deleted.

7 changes: 3 additions & 4 deletions cozy/ui/library_view.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import functools
from typing import Optional

from gi.repository import Gtk, Adw
from gi.repository.Gtk import Builder
from gi.repository import Adw, Gtk

from cozy.ext import inject
from cozy.ui.widgets.book_element import BookElement
from cozy.ui.delete_book_view import DeleteBookView
from cozy.ui.widgets.filter_list_box import FilterListBox
from cozy.view_model.library_view_model import LibraryViewModel, LibraryViewMode, LibraryPage
from cozy.view_model.library_view_model import LibraryViewModel, LibraryViewMode

READER_PAGE = "reader"
AUTHOR_PAGE = "author"
Expand All @@ -23,7 +22,7 @@
class LibraryView:
_view_model: LibraryViewModel = inject.attr(LibraryViewModel)

def __init__(self, builder: Builder):
def __init__(self, builder: Gtk.Builder):
self._builder = builder
self._connected_book_element: Optional[BookElement] = None

Expand Down
52 changes: 20 additions & 32 deletions cozy/view_model/book_detail_view_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from cozy import tools
from cozy.application_settings import ApplicationSettings
from cozy.architecture.event_sender import EventSender
Expand Down Expand Up @@ -29,7 +27,7 @@ def __init__(self):

self._play = False
self._current_chapter = None
self._book: Book = None
self._book: Book | None = None
self._lock_ui: bool = False

self._player.add_listener(self._on_player_event)
Expand All @@ -45,14 +43,14 @@ def playing(self) -> bool:
return self._player.playing

@property
def current_chapter(self) -> Optional[Chapter]:
def current_chapter(self) -> Chapter | None:
if not self.book:
return None

return self.book.current_chapter

@property
def book(self) -> Optional[Book]:
def book(self) -> Book | None:
return self._book

@book.setter
Expand All @@ -77,33 +75,32 @@ def book(self, value: Book):
self._notify("book")

@property
def last_played_text(self) -> Optional[str]:
def last_played_text(self) -> str | None:
if not self._book:
return None

return tools.past_date_to_human_readable(self._book.last_played)

@property
def total_text(self) -> Optional[str]:
def total_text(self) -> str | None:
if not self._book:
return None

return tools.seconds_to_human_readable(self._book.duration / self._book.playback_speed)

@property
def remaining_text(self) -> Optional[str]:
def remaining_text(self) -> str | None:
if not self._book:
return None

remaining = self._book.duration / self._book.playback_speed - self._book.progress / self._book.playback_speed
return tools.seconds_to_human_readable(remaining)
remaining = self._book.duration - self._book.progress
return tools.seconds_to_human_readable(remaining / self._book.playback_speed)

@property
def progress_percent(self) -> Optional[float]:
def progress_percent(self) -> float | None:
if not self._book:
return None

if self._book.duration < 1:
elif self._book.duration < 1:
return 1.0

return self._book.progress / self._book.duration
Expand All @@ -113,9 +110,7 @@ def disk_count(self) -> int:
if not self._book:
return 0

return len({chapter.disk
for chapter
in self._book.chapters})
return len({chapter.disk for chapter in self._book.chapters})

@property
def is_book_available(self) -> bool:
Expand All @@ -127,10 +122,10 @@ def is_book_available(self) -> bool:
@property
def is_book_external(self) -> bool:
first_chapter_path = self._book.chapters[0].file
return any(storage.path
in first_chapter_path
for storage
in self._settings.external_storage_locations)
for storage in self._settings.external_storage_locations:
if storage.path in first_chapter_path:
return True
return False

@property
def lock_ui(self) -> bool:
Expand Down Expand Up @@ -167,19 +162,17 @@ def _on_player_event(self, event, message):
if not self.book:
return

if event == "play" or event == "pause":
if event in {"play", "pause"}:
self._notify("playing")
elif event == "position" or event == "book-finished":
elif event in {"position", "book-finished"}:
self._notify("progress_percent")
self._notify("remaining_text")

def _on_fs_monitor_event(self, event, _):
if not self._book:
return

if event == "storage-online":
self._notify("is_book_available")
elif event == "storage-offline":
if event in {"storage-online", "storage-offline"}:
self._notify("is_book_available")

def _on_book_current_chapter_changed(self):
Expand All @@ -203,15 +196,10 @@ def _on_playback_speed_changed(self):
self._notify("total_text")

def _on_offline_cache_event(self, event, message):
try:
if message.id != self._book.id:
return
except Exception as e:
if not self._book or self._book.id != message.id:
return

if event == "book-offline-removed":
self._notify("downloaded")
elif event == "book-offline":
if event in {"book-offline-removed", "book-offline"}:
self._notify("downloaded")

def _on_app_setting_changed(self, event, _):
Expand Down
2 changes: 1 addition & 1 deletion cozy/view_model/headerbar_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self):

@property
def lock_ui(self) -> bool:
return self._view == View.NO_MEDIA or self._view == View.EMPTY_STATE or self._view == View.PREPARING_LIBRARY
return self._view in {View.NO_MEDIA, View.EMPTY_STATE, View.PREPARING_LIBRARY}

@property
def state(self) -> HeaderBarState:
Expand Down
Loading