Added a playback history object to the core.

This commit is contained in:
Arjun Naik 2014-07-28 22:48:26 +02:00 committed by Stein Magnus Jodal
parent c629e105d7
commit ed87ab8dd1
5 changed files with 78 additions and 0 deletions

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
# flake8: noqa # flake8: noqa
from .actor import Core from .actor import Core
from .history import TrackHistory
from .library import LibraryController from .library import LibraryController
from .listener import CoreListener from .listener import CoreListener
from .playback import PlaybackController, PlaybackState from .playback import PlaybackController, PlaybackState

View File

@ -7,6 +7,7 @@ import pykka
from mopidy import audio, backend, mixer from mopidy import audio, backend, mixer
from mopidy.audio import PlaybackState from mopidy.audio import PlaybackState
from mopidy.core.history import TrackHistory
from mopidy.core.library import LibraryController from mopidy.core.library import LibraryController
from mopidy.core.listener import CoreListener from mopidy.core.listener import CoreListener
from mopidy.core.playback import PlaybackController from mopidy.core.playback import PlaybackController
@ -23,6 +24,10 @@ class Core(
"""The library controller. An instance of """The library controller. An instance of
:class:`mopidy.core.LibraryController`.""" :class:`mopidy.core.LibraryController`."""
history = None
"""The playback history. An instance of
:class:`mopidy.core.TrackHistory`"""
playback = None playback = None
"""The playback controller. An instance of """The playback controller. An instance of
:class:`mopidy.core.PlaybackController`.""" :class:`mopidy.core.PlaybackController`."""
@ -42,6 +47,8 @@ class Core(
self.library = LibraryController(backends=self.backends, core=self) self.library = LibraryController(backends=self.backends, core=self)
self.history = TrackHistory()
self.playback = PlaybackController( self.playback = PlaybackController(
mixer=mixer, backends=self.backends, core=self) mixer=mixer, backends=self.backends, core=self)

42
mopidy/core/history.py Normal file
View File

@ -0,0 +1,42 @@
from __future__ import unicode_literals
import logging
from mopidy.models import Track
logger = logging.getLogger(__name__)
class TrackHistory():
track_list = []
def add_track(self, track):
"""
:param track: track to change to
:type track: :class:`mopidy.models.Track`
"""
if type(track) is not Track:
logger.warning('Cannot add non-Track type object to TrackHistory')
return
# Reorder the track history if the track is already present.
if track in self.track_list:
self.track_list.remove(track)
self.track_list.insert(0, track)
def get_history_size(self):
"""
Returns the number of tracks in the history.
:returns: The number of tracks in the history.
:rtype :int
"""
return len(self.track_list)
def get_history(self):
"""
Returns the history.
:returns: The history as a list of `mopidy.models.Track`
:rtype: L{`mopidy.models.Track`}
"""
return self.track_list

View File

@ -248,6 +248,7 @@ class PlaybackController(object):
if success: if success:
self.core.tracklist.mark_playing(tl_track) self.core.tracklist.mark_playing(tl_track)
# TODO: replace with stream-changed # TODO: replace with stream-changed
self.core.history.add_track(tl_track.track)
self._trigger_track_playback_started() self._trigger_track_playback_started()
else: else:
self.core.tracklist.mark_unplayable(tl_track) self.core.tracklist.mark_unplayable(tl_track)

View File

@ -0,0 +1,27 @@
import unittest
from mopidy.core import TrackHistory
from mopidy.models import Track
class PlaybackHistoryTest(unittest.TestCase):
def setUp(self):
self.tracks = [
Track(uri='dummy1:a', name='foo'),
Track(uri='dummy2:a', name='foo'),
Track(uri='dummy3:a', name='bar')
]
self.history = TrackHistory()
def test_add_track(self):
self.history.add_track(self.tracks[0])
self.assertEqual(self.history.get_history_size(), 1)
def test_track_order(self):
self.history.add_track(self.tracks[0])
self.history.add_track(self.tracks[1])
self.history.add_track(self.tracks[2])
self.history.add_track(self.tracks[0])
self.assertEqual(self.history.get_history_size(), 3)
self.assertEqual(self.history.get_history()[0], self.tracks[0])