[gnome-music/wip/mschraal/core: 79/118] albumwidget2: Make duration work again



commit ed64d73605152950bee80f55ef2056a8b1b46a4b
Author: Marinus Schraal <mschraal gnome org>
Date:   Thu Jun 20 17:50:14 2019 +0200

    albumwidget2: Make duration work again

 gnomemusic/corealbum.py            | 16 ++++++++++++++++
 gnomemusic/coremodel.py            | 24 +++++++++++++++++++++---
 gnomemusic/widgets/albumwidget2.py | 11 ++++-------
 3 files changed, 41 insertions(+), 10 deletions(-)
---
diff --git a/gnomemusic/corealbum.py b/gnomemusic/corealbum.py
index 1cce9273..5081a451 100644
--- a/gnomemusic/corealbum.py
+++ b/gnomemusic/corealbum.py
@@ -13,6 +13,7 @@ class CoreAlbum(GObject.GObject):
 
     artist = GObject.Property(type=str)
     composer = GObject.Property(type=str, default=None)
+    duration = GObject.Property(type=int, default=0)
     media = GObject.Property(type=Grl.Media)
     selected = GObject.Property(type=bool, default=False)
     title = GObject.Property(type=str)
@@ -39,5 +40,20 @@ class CoreAlbum(GObject.GObject):
     def model(self):
         if self._model is None:
             self._model = self._coremodel.get_album_model(self.props.media)
+            self._model.connect("items-changed", self._on_list_items_changed)
+
+        self._on_list_items_changed(self._model, None, None, None)
 
         return self._model
+
+    def _on_list_items_changed(self, model, pos, removed, added):
+        for coredisc in model:
+            coredisc.connect("notify::duration", self._on_duration_changed)
+
+    def _on_duration_changed(self, coredisc, duration):
+        duration = 0
+
+        for coredisc in self.props.model:
+            duration += coredisc.props.duration
+
+        self.props.duration = duration
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 6d097a4d..0799a1b8 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -27,9 +27,29 @@ from gnomemusic.widgets.songwidget import SongWidget
 
 class CoreDisc(GObject.GObject):
 
+    duration = GObject.Property(type=int, default=None)
     media = GObject.Property(type=Grl.Media, default=None)
     model = GObject.Property(type=Gio.ListModel, default=None)
 
+    def __init__(self, media, model):
+        super().__init__()
+
+        self.props.model = model
+        self.update(media)
+
+        self.props.model.connect("items-changed", self._on_list_items_changed)
+
+    def update(self, media):
+        self.props.media = media
+
+    def _on_list_items_changed(self, model, pos, removed, added):
+        duration = 0
+
+        for coresong in model:
+            duration += coresong.props.duration
+
+        self.props.duration = duration
+
 
 class CoreArtistAlbum(GObject.GObject):
 
@@ -103,9 +123,7 @@ class CoreModel(GObject.GObject):
             model_sort.set_sort_func(
                 self._wrap_list_store_sort_func(_disc_sort))
 
-            coredisc = CoreDisc()
-            coredisc.props.media = disc
-            coredisc.props.model = model_sort
+            coredisc = CoreDisc(disc, model_sort)
 
             disc_model.append(coredisc)
 
diff --git a/gnomemusic/widgets/albumwidget2.py b/gnomemusic/widgets/albumwidget2.py
index 2341619f..094eb9a4 100644
--- a/gnomemusic/widgets/albumwidget2.py
+++ b/gnomemusic/widgets/albumwidget2.py
@@ -82,6 +82,8 @@ class AlbumWidget2(Gtk.EventBox):
         self._album_model = corealbum.props.model
         self._listbox.bind_model(self._album_model, self._create_widget)
 
+        corealbum.connect("notify::duration", self._on_duration_changed)
+
     def _create_widget(self, disc):
         disc_box = self._create_disc_box(
             disc.media.get_album_disc_number(), disc.model)
@@ -114,13 +116,8 @@ class AlbumWidget2(Gtk.EventBox):
         self._composer_label.props.visible = show
         self._composer_info_label.props.visible = show
 
-    @log
-    def _set_duration_label(self):
-        total_duration = 0
-        for song in self._model[0]:
-            total_duration += song.props.duration
-
-        mins = (total_duration // 60) + 1
+    def _on_duration_changed(self, coredisc, duration):
+        mins = (coredisc.props.duration // 60) + 1
         self._running_info_label.props.label = ngettext(
             "{} minute", "{} minutes", mins).format(mins)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]