Commit 8532e902 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

IMedia: Add a markAsPlayed function

Which allows application to signal that a media was played in order to
increase its playcount and position in history without providing a
playback position.
Fix #434
parent b43c3f84
Pipeline #219300 passed with stage
in 7 minutes and 44 seconds
......@@ -225,6 +225,17 @@ public:
*/
virtual bool setPlayCount( uint32_t playCount ) = 0;
virtual time_t lastPlayedDate() const = 0;
/**
* @brief markAsPlayed Will mark the media as played and will bump it in the
* history
* @return true in case of success, false otherwise
*
* This is intended as an alternative to setLastPosition/setLastTime in case
* where the user isn't interested in saving the progression in database, but
* still cares about the media appearing in the history and using its play
* count.
*/
virtual bool markAsPlayed() = 0;
virtual ShowEpisodePtr showEpisode() const = 0;
virtual const std::vector<FilePtr>& files() const = 0;
/**
......
......@@ -440,6 +440,21 @@ IMedia::ProgressResult Media::setLastTime( int64_t lastTime )
return setLastPositionAndTime( positionType, position, lastTime );
}
bool Media::markAsPlayed()
{
auto lastPlayedDate = time( nullptr );
const std::string req = "UPDATE " + Table::Name +
" SET last_played_date = ?, play_count = play_count + 1"
" WHERE id_media = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, lastPlayedDate,
m_id ) == false )
return false;
m_lastPlayedDate = lastPlayedDate;
++m_playCount;
return true;
}
bool Media::setPlayCount( uint32_t playCount )
{
static const std::string req = "UPDATE " + Media::Table::Name + " SET "
......
......@@ -191,6 +191,7 @@ class Media : public IMedia,
virtual ProgressResult setLastTime( int64_t lastTime ) override;
virtual bool setPlayCount( uint32_t playCount ) override;
virtual time_t lastPlayedDate() const override;
virtual bool markAsPlayed() override;
virtual bool removeFromHistory() override;
virtual bool isFavorite() const override;
virtual bool setFavorite( bool favorite ) override;
......
......@@ -1451,6 +1451,49 @@ static void ExternalMrlDifferentPorts( Tests* T )
ASSERT_NE( m2->id(), m->id() );
}
static void MarkAsPlayed( Tests* T )
{
auto m = T->ml->addMedia( "file:///path/to/movie.mkv", IMedia::Type::Video );
ASSERT_NON_NULL( m );
ASSERT_EQ( 0u, m->lastPlayedDate() );
ASSERT_EQ( 0u, m->playCount() );
ASSERT_EQ( -1, m->lastTime() );
ASSERT_TRUE( m->lastPosition() < .0f );
auto history = T->ml->history()->all();
ASSERT_EQ( 0u, history.size() );
auto res = m->markAsPlayed();
ASSERT_TRUE( res );
ASSERT_EQ( 1u, m->playCount() );
ASSERT_NE( 0u, m->lastPlayedDate() );
ASSERT_EQ( -1, m->lastTime() );
ASSERT_TRUE( m->lastPosition() < .0f );
m = T->ml->media( m->id() );
ASSERT_EQ( 1u, m->playCount() );
ASSERT_NE( 0u, m->lastPlayedDate() );
ASSERT_EQ( -1, m->lastTime() );
ASSERT_TRUE( m->lastPosition() < .0f );
history = T->ml->history()->all();
ASSERT_EQ( 1u, history.size() );
res = m->markAsPlayed();
ASSERT_TRUE( res );
ASSERT_EQ( 2u, m->playCount() );
ASSERT_NE( 0u, m->lastPlayedDate() );
ASSERT_EQ( -1, m->lastTime() );
ASSERT_TRUE( m->lastPosition() < .0f );
m = T->ml->media( m->id() );
ASSERT_EQ( 2u, m->playCount() );
ASSERT_NE( 0u, m->lastPlayedDate() );
ASSERT_EQ( -1, m->lastTime() );
ASSERT_TRUE( m->lastPosition() < .0f );
}
int main( int ac, char** av )
{
INIT_TESTS( Media );
......@@ -1512,6 +1555,7 @@ int main( int ac, char** av )
ADD_TEST( FlushUserProvidedThumbnails );
ADD_TEST( Lyrics );
ADD_TEST( ExternalMrlDifferentPorts );
ADD_TEST( MarkAsPlayed );
END_TESTS
}
......@@ -642,6 +642,7 @@ media_tests = [
'FlushUserProvidedThumbnails',
'Lyrics',
'ExternalMrlDifferentPorts',
'MarkAsPlayed',
]
foreach t : media_tests
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment