Skip to content

Commit

Permalink
uhh... things, ig?
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Dec 11, 2023
1 parent 1be8d73 commit 0904765
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 80 deletions.
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 = {}
17 changes: 7 additions & 10 deletions cozy/model/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ 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

@property
def storage_locations(self):
Expand All @@ -78,14 +77,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}")

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
36 changes: 13 additions & 23 deletions cozy/view_model/book_detail_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ def remaining_text(self) -> Optional[str]:
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]:
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 +112,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 +124,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 +164,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 +198,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
50 changes: 23 additions & 27 deletions cozy/view_model/library_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ def selected_filter(self, value):
self._notify("selected_filter")

@property
def is_any_book_in_progress(self):
return any(book.position > 0 for book in self.books)
def is_any_book_in_progress(self) -> bool:
for book in self.books:
if book.position > 0:
return True
return False

@property
def authors(self):
Expand Down Expand Up @@ -135,24 +138,20 @@ def remove_book(self, book: Book):

def display_book_filter(self, book_element: BookElement):
book = book_element.book
author = book.author
reader = book.reader

hide_offline_books = self._application_settings.hide_offline
book_is_online = self._fs_monitor.get_book_online(book)

if hide_offline_books and not book_is_online and not book.downloaded:
return False

if self.library_view_mode == LibraryViewMode.CURRENT:
return True if book.last_played > 0 else False

if self.selected_filter == _("All"):
return True
elif hide_offline_books and not book_is_online and not book.downloaded:
return False
elif self.library_view_mode == LibraryViewMode.CURRENT:
return book.last_played > 0
elif self.library_view_mode == LibraryViewMode.AUTHOR:
return True if self.selected_filter in author else False
return self.selected_filter in book.author
elif self.library_view_mode == LibraryViewMode.READER:
return True if self.selected_filter in reader else False
return self.selected_filter in book.reader

def display_book_sort(self, book_element1, book_element2):
if self._library_view_mode == LibraryViewMode.CURRENT:
Expand All @@ -164,7 +163,10 @@ def open_library(self):
self._notify("library_view_mode")

def book_files_exist(self, book: Book) -> bool:
return any(os.path.exists(chapter.file) for chapter in book.chapters)
for chapter in book.chapters:
if os.path.exists(chapter.file):
return True
return False

def _on_fs_monitor_event(self, event, _):
if event == "storage-online":
Expand All @@ -175,10 +177,6 @@ def _on_fs_monitor_event(self, event, _):
self._notify("authors")
self._notify("readers")
self._notify("books-filter")
elif event == "external-storage-added":
pass
elif event == "external-storage-removed":
pass

def _on_application_setting_changed(self, event, _):
if event == "hide-offline":
Expand All @@ -200,17 +198,14 @@ def _on_importer_event(self, event, message):
self._notify("books")
self._notify("books-filter")
self._notify("library_view_mode")
if event == "import-failed":
elif event == "import-failed":
ImportFailedDialog(message).show()

def _on_player_event(self, event, message):
if event == "play":
book = message

if book:
self._notify("current_book_in_playback")
self._notify("playing")
self._notify("books-filter")
if event == "play" and message:
self._notify("current_book_in_playback")
self._notify("playing")
self._notify("books-filter")
elif event == "pause":
self._notify("playing")
elif event == "chapter-changed":
Expand All @@ -219,7 +214,7 @@ def _on_player_event(self, event, message):
elif event == "stop":
self._notify("playing")
self._notify("current_book_in_playback")
elif event == "position" or event == "book-finished":
elif event in {"position", "book-finished"}:
self._notify("book-progress")

def _on_settings_event(self, event: str, message):
Expand Down Expand Up @@ -252,11 +247,12 @@ def delete_book_files(self, book: Book):
for chapter in book.chapters:
try:
os.remove(chapter.file)
log.info("Deleted file: {}".format(chapter.file))
except Exception as e:
log.error("Failed to delete file: {}".format(chapter.file))
log.debug(e)
reporter.warning("library_view_model", "Failed to delete a file.")
else:
log.info("Deleted file: {}".format(chapter.file))

def play_book(self, book: Book):
self._player.play_pause_book(book)
2 changes: 1 addition & 1 deletion cozy/view_model/playback_control_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def open_book_detail(self):
self.emit_event(OpenView.BOOK, self.book)

def _on_player_event(self, event, message):
if event == "play" or event == "pause":
if event in {"play", "pause"}:
if self.book:
self._notify("playing")
elif event == "position":
Expand Down
4 changes: 2 additions & 2 deletions cozy/view_model/playback_speed_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def __init__(self):
def playback_speed(self) -> float:
if self._book:
return self._book.playback_speed
else:
return 1.0

return 1.0

@playback_speed.setter
def playback_speed(self, new_value: float):
Expand Down
16 changes: 4 additions & 12 deletions cozy/view_model/sleep_timer_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ def destroy(self):
self._stop_timer()

def _start_timer(self):
if self._sleep_timer:
return

if self.remaining_seconds < 1:
return

if not self._player.playing:
if self._sleep_timer or self.remaining_seconds < 1 or not self._player.playing:
return

log.info("Start Timer")
Expand Down Expand Up @@ -114,12 +108,10 @@ def _on_timer_tick(self):
self._notify_main_thread("remaining_seconds")

def _get_fadeout(self) -> int:
fadeout = 0

if self._app_settings.sleep_timer_fadeout:
fadeout = self._app_settings.sleep_timer_fadeout_duration
return self._app_settings.sleep_timer_fadeout_duration

return fadeout
return 0

def _stop_playback(self):
fadeout = self._get_fadeout()
Expand Down Expand Up @@ -150,7 +142,7 @@ def _on_player_changed(self, event, _):
self._notify("stop_after_chapter")
elif event == "play":
self._start_timer()
elif event == "pause" or event == "stop":
elif event in {"pause", "stop"}:
self._stop_timer()
elif event == "fadeout-finished" and self._wait_for_fadeout_end:
self._wait_for_fadeout_end = False
Expand Down

0 comments on commit 0904765

Please sign in to comment.