Commit 6abbc02d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

IMedia: Return the setLastPosition/Time result as an enum

This way the application knows if the progress was reset or not, and can
save a later fetch
Fix #369
parent ea08f4bf
......@@ -106,6 +106,28 @@ public:
};
static constexpr size_t NbMeta = 17;
/**
* @brief The ProgressResult enum describes the result for setLastPosition
* and setLastTime operations
*/
enum class ProgressResult : uint8_t
{
/// An error occured and the progress wasn't changed
Error,
/// The provided position/time was interpreted as the beginning of the
/// media and has been reset to -1. This nedia playback is now not
/// considered started.
Begin,
/// The provided position/time was not interpreted as a special position
/// and was updated as provided in the database. The playback will be
/// considered in progress
AsIs,
/// The provided position/time was interpreted as the end of the media.
/// The playback will not be considered in progress anymore and the
/// play count has been incremented.
End,
};
virtual ~IMedia() = default;
virtual int64_t id() const = 0;
......@@ -150,6 +172,8 @@ public:
* @brief setLastPosition updates the last playback position
*
* @param lastPosition The current playback position expressed by a number in the range [0;1]
* @return a ProgressResult value indicating how the value was intepreted and
* if the operation succeeded
*
* The media library will interpret the value to determine if the playback
* is completed and the media should be marked as watched (therefor increasing
......@@ -166,7 +190,6 @@ public:
* database but the playcount will not be updated, nor will the position be
* clamped in the [0;1] range.
*
* This returns true in case of success, false otherwise.
* Calling lastPosition() or playCount() afterward will fetch the curated values.
* This will also bump the media last played date, causing it to appear at
* the top of the history.
......@@ -174,7 +197,7 @@ public:
* If the duration is unknown, lastTime will be set to -1 when this function
* is called.
*/
virtual bool setLastPosition( float lastPosition ) = 0;
virtual IMedia::ProgressResult setLastPosition( float lastPosition ) = 0;
/**
* @brief lastTime Returns the last playback time as provided by the application
*
......@@ -186,14 +209,15 @@ public:
/**
* @brief setLastTime Sets the last playback time.
* @param lastTime A time in millisecond
* @return true in case of success, false otherwise.
* @return a ProgressResult value indicating how the value was intepreted and
* if the operation succeeded
*
* This is similar to setLastPosition but works with a time in
* milliseconds rather than a percentage.
* If the duration is unknown, calling this function will reset the lastProgress
* to -1.
*/
virtual bool setLastTime( int64_t lastTime ) = 0;
virtual IMedia::ProgressResult setLastTime( int64_t lastTime ) = 0;
/**
* @brief setPlayCount Set a specific value to this media's play count
*
......
......@@ -309,8 +309,9 @@ Media::PositionTypes Media::computePositionType( float position ) const
return PositionTypes::Any;
}
bool Media::setLastPositionAndTime( PositionTypes positionType, float lastPos,
int64_t lastTime )
IMedia::ProgressResult
Media::setLastPositionAndTime( PositionTypes positionType, float lastPos,
int64_t lastTime )
{
auto lastPlayedDate = time( nullptr );
float curatedPosition;
......@@ -357,7 +358,7 @@ bool Media::setLastPositionAndTime( PositionTypes positionType, float lastPos,
}
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, curatedPosition,
curatedTime, lastPlayedDate, m_id ) == false )
return false;
return ProgressResult::Error;
if ( incrementPlaycount == true )
m_playCount++;
m_lastPlayedDate = lastPlayedDate;
......@@ -365,10 +366,18 @@ bool Media::setLastPositionAndTime( PositionTypes positionType, float lastPos,
m_lastTime = curatedTime;
m_ml->getCb()->onHistoryChanged( isStream() ? HistoryType::Media :
HistoryType::Network );
return true;
switch ( positionType )
{
case PositionTypes::Begin:
return ProgressResult::Begin;
case PositionTypes::End:
return ProgressResult::End;
default:
return ProgressResult::AsIs;
}
}
bool Media::setLastPosition( float lastPosition )
IMedia::ProgressResult Media::setLastPosition( float lastPosition )
{
int64_t lastTime;
PositionTypes positionType;
......@@ -385,7 +394,7 @@ bool Media::setLastPosition( float lastPosition )
return setLastPositionAndTime( positionType, lastPosition, lastTime );
}
bool Media::setLastTime( int64_t lastTime )
IMedia::ProgressResult Media::setLastTime( int64_t lastTime )
{
float position;
PositionTypes positionType;
......
......@@ -171,8 +171,8 @@ class Media : public IMedia,
virtual bool removeLabel( LabelPtr label ) override;
virtual Query<ILabel> labels() const override;
virtual uint32_t playCount() const override;
virtual bool setLastPosition( float lastPosition ) override;
virtual bool setLastTime( int64_t lastTime ) override;
virtual ProgressResult setLastPosition( float lastPosition ) override;
virtual ProgressResult setLastTime( int64_t lastTime ) override;
virtual bool setPlayCount( uint32_t playCount ) override;
virtual time_t lastPlayedDate() const override;
virtual bool removeFromHistory() override;
......@@ -353,8 +353,8 @@ private:
int64_t duration );
std::vector<std::shared_ptr<Media>> fetchMatchingUngrouped();
PositionTypes computePositionType( float position ) const;
bool setLastPositionAndTime( PositionTypes positionType, float lastPos,
int64_t lastTime );
ProgressResult setLastPositionAndTime( PositionTypes positionType,
float lastPos, int64_t lastTime );
/*
* Marked as private so that it can only be used through IMedia interface
......
......@@ -124,7 +124,7 @@ static void SetProgress( Tests* T )
*/
auto expectedPosition = 0.5f;
auto res = m1->setLastPosition( expectedPosition );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( expectedPosition, m1->lastPosition() );
ASSERT_EQ( m1->duration() / 2, m1->lastTime() );
ASSERT_EQ( 0u, m1->playCount() );
......@@ -136,7 +136,7 @@ static void SetProgress( Tests* T )
/* Then update to a progress to 3%. This should reset it to -1 */
res = m1->setLastTime( 54000 ); //3% of 30 minutes in milliseconds
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::Begin, res );
ASSERT_EQ( -1, m1->lastTime() );
ASSERT_EQ( -1.f, m1->lastPosition() );
ASSERT_EQ( 0u, m1->playCount() );
......@@ -147,7 +147,7 @@ static void SetProgress( Tests* T )
/* Then again at 4% and ensure the progress is still -1 */
res = m1->setLastPosition( 0.04 );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::Begin, res );
ASSERT_EQ( -1, m1->lastTime() );
ASSERT_EQ( -1.f, m1->lastPosition() );
m1 = T->ml->media( m1->id() );
......@@ -159,7 +159,7 @@ static void SetProgress( Tests* T )
* progress reset
*/
res = m1->setLastTime( 1782000 );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::End, res );
ASSERT_EQ( -1, m1->lastTime() );
ASSERT_EQ( -1.f, m1->lastPosition() );
ASSERT_EQ( 1u, m1->playCount() );
......@@ -171,7 +171,7 @@ static void SetProgress( Tests* T )
/* Now do the same with a longer media to ensure the "margin" are updated */
expectedPosition = 0.5f;
res = m2->setLastPosition( expectedPosition );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( expectedPosition, m2->lastPosition() );
ASSERT_EQ( m2->duration() / 2, m2->lastTime() );
ASSERT_EQ( 0u, m2->playCount() );
......@@ -182,7 +182,7 @@ static void SetProgress( Tests* T )
/* This media should only ignore the first & last percent */
res = m2->setLastPosition( 0.009f );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::Begin, res );
ASSERT_EQ( -1.f, m2->lastPosition() );
ASSERT_EQ( -1, m2->lastTime() );
ASSERT_EQ( 0u, m2->playCount() );
......@@ -194,7 +194,7 @@ static void SetProgress( Tests* T )
/* So check 0.01 is not ignored */
expectedPosition = 0.01f;
res = m2->setLastPosition( expectedPosition );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( expectedPosition, m2->lastPosition() );
/* Don't fail the tests because of a rounding error */
ASSERT_TRUE( m2->lastTime() >= 180000 - 1 &&
......@@ -212,7 +212,7 @@ static void SetProgress( Tests* T )
*/
expectedPosition = 0.98;
res = m2->setLastPosition( expectedPosition );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( expectedPosition, m2->lastPosition() );
ASSERT_TRUE( m2->lastTime() >= 17640000 - 1 &&
m2->lastTime() <= 17640000 );
......@@ -229,7 +229,7 @@ static void SetLastPositionNoDuration( Tests* T )
auto m = T->ml->addMedia( "media.mkv", IMedia::Type::Video );
/* Check that we accept any value when no duration is provided */
auto res = m->setLastPosition( 123.456f );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( 123.456f, m->lastPosition() );
/* And ensure we didn't infer the last_time value since we can't compute it */
ASSERT_EQ( -1, m->lastTime() );
......@@ -242,7 +242,7 @@ static void SetLastPositionNoDuration( Tests* T )
* position back
*/
res = m->setLastTime( 123456 );
ASSERT_TRUE( res );
ASSERT_EQ( IMedia::ProgressResult::AsIs, res );
ASSERT_EQ( 123456, m->lastTime() );
ASSERT_EQ( -1.f, m->lastPosition() );
m = T->ml->media( m->id() );
......
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