[gnome-music] view: don't hit tracker every time the user scrolls



commit 3dc9dcc4bdaeea7c208ea1a632f5770d6e0b603b
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Aug 14 22:24:47 2013 +0200

    view: don't hit tracker every time the user scrolls
    
    Cache the number of items in the fully loaded view the first time
    it's needed, and use that value for all future scrolling.
    
    Note: if we ever support watching the library for changes, we need
    to invalidate the cache.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706025

 gnomemusic/view.py    |    5 ++++-
 gnomemusic/widgets.py |   11 +++++++----
 2 files changed, 11 insertions(+), 5 deletions(-)
---
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index a42609f..fc1c82a 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -72,6 +72,7 @@ class ViewContainer(Stack):
         else:
             self._grid.add(box)
 
+        self._cached_count = -1
         self._loadMore = Widgets.LoadMoreButton(self._get_remaining_item_count)
         box.pack_end(self._loadMore.widget, False, False, 0)
         self._loadMore.widget.connect('clicked', self._populate)
@@ -102,7 +103,9 @@ class ViewContainer(Stack):
                           self._on_view_selection_changed)
 
     def _get_remaining_item_count(self):
-        return Widgets.get_remaining_item_count(self.countQuery, self._offset)
+        if self._cached_count < 0:
+            self._cached_count = Widgets.get_count(self.countQuery)
+        return self._cached_count - self._offset
 
     def _on_header_bar_toggled(self, button):
         if button.get_active():
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 71fa00a..34646ac 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -17,13 +17,13 @@ else:
 ERROR_ICON_NAME = 'dialog-error-symbolic'
 
 
-def get_remaining_item_count(countQuery, _offset):
+def get_count(countQuery):
     count = -1
     if countQuery:
         cursor = tracker.query(countQuery, None)
         if cursor and cursor.next(None):
             count = cursor.get_integer(0)
-    return count - _offset
+    return count
 
 
 class LoadMoreButton:
@@ -399,6 +399,7 @@ class AllArtistsAlbums(ArtistAlbums):
         ArtistAlbums.__init__(self, _("All Artists"), [], player)
         self._offset = 0
         self.countQuery = Query.ALBUMS_COUNT
+        self._cached_count = -1
         self._load_more = LoadMoreButton(self._get_remaining_item_count)
         self.pack_end(self._load_more.widget, False, False, 0)
         self._load_more.widget.connect('clicked', self._populate)
@@ -406,7 +407,9 @@ class AllArtistsAlbums(ArtistAlbums):
         self._populate()
 
     def _get_remaining_item_count(self):
-        return get_remaining_item_count(self.countQuery, self._offset)
+        if self._cached_count < 0:
+            self._cached_count = get_count(self.countQuery)
+        return self._cached_count - self._offset
 
     def _connect_view(self):
         self._adjustmentValueId =\
@@ -441,7 +444,7 @@ class AllArtistsAlbums(ArtistAlbums):
 
         # special case this values which happen at construction
         if (((value != 0) or (upper != 1) or (page_size != 1))
-                and get_remaining_item_count(self.countQuery, self._offset) > 0):
+                and self._get_remaining_item_count() > 0):
             end = not (value < (upper - page_size - revealAreaHeight))
         self._load_more.set_block(not end)
 


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