[gnome-music/wip/jfelder/clean-albumsview: 4/4] widgets: Use utils functions



commit 9f23ce130c1e8e94d97eeeacc29f8a354e46c53a
Author: Jean Felder <jfelder src gnome org>
Date:   Thu Mar 29 14:44:04 2018 +0200

    widgets: Use utils functions
    
    Create a new utils function get_media_year to get the creation year of
    a media.

 gnomemusic/utils.py                     | 15 +++++++++++++++
 gnomemusic/views/albumsview.py          |  9 +++------
 gnomemusic/views/searchview.py          |  2 +-
 gnomemusic/widgets/albumwidget.py       | 23 +++++++++--------------
 gnomemusic/widgets/artistalbumwidget.py |  5 ++---
 5 files changed, 30 insertions(+), 24 deletions(-)
---
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index 603685e9..fb4e6487 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -87,6 +87,21 @@ def get_media_title(item):
             or _("Untitled"))
 
 
+def get_media_year(item):
+    """Returns the year when the media was created.
+
+    :param item: A Grilo Media object
+    :return: The creation year or None if not defined
+    :rtype: string
+    """
+    date = item.get_creation_date()
+
+    if not date:
+        return None
+
+    return str(date.get_year())
+
+
 def seconds_to_string(duration):
     """Convert a time in seconds to a mm:ss string
 
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index f54c5801..b107e438 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -106,15 +106,12 @@ class AlbumsView(BaseView):
             child.check.set_active(not child.check.get_active())
             return
 
-        title = utils.get_media_title(item)
-        artist = utils.get_artist_name(item)
-
         self._album_widget.update(
-            artist, title, item, self._header_bar, self._selection_toolbar)
+            item, self._header_bar, self._selection_toolbar)
 
         self._header_bar.set_state(ToolbarState.CHILD_VIEW)
-        self._header_bar.header_bar.set_title(title)
-        self._header_bar.header_bar.sub_title = artist
+        self._header_bar.header_bar.set_title(utils.get_album_title(item))
+        self._header_bar.header_bar.sub_title = utils.get_artist_name(item)
         self.set_visible_child(self._album_widget)
 
     @log
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 3f65347a..7eeca778 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -118,7 +118,7 @@ class SearchView(BaseView):
             item = self.model[_iter][5]
 
             self._album_widget.update(
-                artist, title, item, self._header_bar, self._selection_toolbar)
+                item, self._header_bar, self._selection_toolbar)
             self._header_bar.set_state(ToolbarState.SEARCH_VIEW)
 
             self._header_bar.header_bar.set_title(title)
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index e831e562..78daac14 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -120,11 +120,9 @@ class AlbumWidget(Gtk.EventBox):
         )
 
     @log
-    def update(self, artist, album, item, header_bar, selection_toolbar):
+    def update(self, item, header_bar, selection_toolbar):
         """Update the album widget.
 
-        :param str artist: The artist name
-        :param str album: The album name
         :param item: The grilo media item
         :param header_bar: The header bar object
         :param selection_toolbar: The selection toolbar object
@@ -137,7 +135,6 @@ class AlbumWidget(Gtk.EventBox):
 
         self.selection_toolbar = selection_toolbar
         self._header_bar = header_bar
-        self._album = album
         self._duration = 0
         art = ArtImage(Art.Size.LARGE, item)
         art.image = self._builder.get_object('cover')
@@ -148,17 +145,15 @@ class AlbumWidget(Gtk.EventBox):
         header_bar._cancel_button.connect(
             'clicked', self._on_header_cancel_button_clicked)
 
-        # FIXME: use utils
-        escaped_artist = GLib.markup_escape_text(artist)
-        escaped_album = GLib.markup_escape_text(album)
-        self._builder.get_object('artist_label').set_markup(escaped_artist)
-        self._builder.get_object('title_label').set_markup(escaped_album)
+        self._album = utils.get_album_title(item)
+        self._builder.get_object('artist_label').set_text(
+            utils.get_artist_name(item))
+        self._builder.get_object('title_label').set_text(self._album)
 
-        if (item.get_creation_date()):
-            self._builder.get_object('released_label_info').set_text(
-                str(item.get_creation_date().get_year()))
-        else:
-            self._builder.get_object('released_label_info').set_text('----')
+        year = utils.get_media_year(item)
+        if not year:
+            year = '----'
+        self._builder.get_object('released_label_info').set_text(year)
 
         self._set_composer_label(item)
 
diff --git a/gnomemusic/widgets/artistalbumwidget.py b/gnomemusic/widgets/artistalbumwidget.py
index c0b471e7..95e16648 100644
--- a/gnomemusic/widgets/artistalbumwidget.py
+++ b/gnomemusic/widgets/artistalbumwidget.py
@@ -78,9 +78,8 @@ class ArtistAlbumWidget(Gtk.Box):
             self._selection_mode_allowed)
 
         ui.get_object('title').set_label(self._album_title)
-        creation_date = self._media.get_creation_date()
-        if creation_date:
-            year = creation_date.get_year()
+        year = utils.get_media_year(self._media)
+        if year:
             ui.get_object('year').set_markup(
                 '<span color=\'grey\'>{}</span>'.format(year))
 


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