Commit 46c7040e authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Media: Do not delay public field modification writes to DB

Since the public interface doesn't expose a save() method, those changes
would never be changed
parent 05b177e6
......@@ -61,13 +61,13 @@ class IMedia
virtual int64_t duration() const = 0;
virtual ShowEpisodePtr showEpisode() const = 0;
virtual int playCount() const = 0;
virtual void increasePlayCount() = 0;
virtual bool increasePlayCount() = 0;
virtual const std::vector<FilePtr>& files() const = 0;
///
/// \brief progress Returns the progress, in the [0;1] range
///
virtual float progress() const = 0;
virtual void setProgress( float progress ) = 0;
virtual bool setProgress( float progress ) = 0;
///
/// \brief rating The media rating, or -1 if unset.
/// It is up to the application to determine the values it wishes to use.
......@@ -75,9 +75,9 @@ class IMedia
/// will be accepted.
///
virtual int rating() const = 0;
virtual void setRating( int rating ) = 0;
virtual bool setRating( int rating ) = 0;
virtual bool isFavorite() const = 0;
virtual void setFavorite( bool favorite ) = 0;
virtual bool setFavorite( bool favorite ) = 0;
virtual bool addLabel( LabelPtr label ) = 0;
virtual bool removeLabel( LabelPtr label ) = 0;
virtual MoviePtr movie() const = 0;
......
......@@ -159,11 +159,16 @@ int Media::playCount() const
return m_playCount;
}
void Media::increasePlayCount()
bool Media::increasePlayCount()
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"play_count = ?, last_played_date = ? WHERE id_media = ?";
auto lastPlayedDate = time( nullptr );
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_playCount + 1, lastPlayedDate, m_id ) == false )
return false;
m_playCount++;
m_lastPlayedDate = time( nullptr );
m_changed = true;
m_lastPlayedDate = lastPlayedDate;
return true;
}
float Media::progress() const
......@@ -171,12 +176,15 @@ float Media::progress() const
return m_progress;
}
void Media::setProgress( float progress )
bool Media::setProgress( float progress )
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET progress = ? WHERE id_media = ?";
if ( progress == m_progress || progress < 0 || progress > 1.0 )
return;
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, progress, m_id ) == false )
return false;
m_progress = progress;
m_changed = true;
return true;
}
int Media::rating() const
......@@ -184,12 +192,15 @@ int Media::rating() const
return m_rating;
}
void Media::setRating( int rating )
bool Media::setRating( int rating )
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET rating = ? WHERE id_media = ?";
if ( m_rating == rating )
return;
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, rating, m_id ) == false )
return false;
m_rating = rating;
m_changed = true;
return true;
}
bool Media::isFavorite() const
......@@ -197,12 +208,15 @@ bool Media::isFavorite() const
return m_isFavorite;
}
void Media::setFavorite( bool favorite )
bool Media::setFavorite( bool favorite )
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET is_favorite = ? WHERE id_media = ?";
if ( m_isFavorite == favorite )
return;
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, favorite, m_id ) == false )
return false;
m_isFavorite = favorite;
m_changed = true;
return true;
}
const std::vector<FilePtr>& Media::files() const
......@@ -284,13 +298,12 @@ void Media::setThumbnail(const std::string& thumbnail )
bool Media::save()
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"type = ?, subtype = ?, duration = ?, play_count = ?, last_played_date = ?, progress = ?, rating = ?,"
"thumbnail = ?, title = ?, is_favorite = ? WHERE id_media = ?";
"type = ?, subtype = ?, duration = ?, progress = ?,"
"thumbnail = ?, title = ? WHERE id_media = ?";
if ( m_changed == false )
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_type, m_subType, m_duration, m_playCount,
m_lastPlayedDate, m_progress, m_rating, m_thumbnail, m_title,
m_isFavorite, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_type, m_subType, m_duration,
m_progress, m_thumbnail, m_title, m_id ) == false )
{
return false;
}
......
......@@ -79,13 +79,13 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
virtual bool removeLabel( LabelPtr label ) override;
virtual std::vector<LabelPtr> labels() override;
virtual int playCount() const override;
virtual void increasePlayCount() override;
virtual bool increasePlayCount() override;
virtual float progress() const override;
virtual void setProgress( float progress ) override;
virtual bool setProgress( float progress ) override;
virtual int rating() const override;
virtual void setRating( int rating ) override;
virtual bool setRating( int rating ) override;
virtual bool isFavorite() const override;
virtual void setFavorite( bool favorite ) override;
virtual bool setFavorite( bool favorite ) override;
virtual const std::vector<FilePtr>& files() const override;
virtual MoviePtr movie() const override;
void setMovie( MoviePtr movie );
......
......@@ -106,7 +106,6 @@ TEST_F( Medias, PlayCount )
ASSERT_EQ( 0, f->playCount() );
f->increasePlayCount();
ASSERT_EQ( 1, f->playCount() );
f->save();
Reload();
......@@ -123,7 +122,6 @@ TEST_F( Medias, Progress )
ASSERT_EQ( .0f, f->progress() );
f->setProgress( 0.666f );
ASSERT_EQ( .666f, f->progress() );
f->save();
Reload();
......@@ -136,7 +134,6 @@ TEST_F( Medias, Rating )
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( -1, f->rating() );
f->setRating( 12345 );
f->save();
ASSERT_EQ( 12345, f->rating() );
Reload();
......@@ -268,7 +265,6 @@ TEST_F( Medias, Favorite )
ASSERT_FALSE( m->isFavorite() );
m->setFavorite( true );
m->save();
ASSERT_TRUE( m->isFavorite() );
Reload();
......
Markdown is supported
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