Skip to content

Commit

Permalink
library shows current item
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronson Mathews committed Jul 30, 2020
1 parent 88b1bbc commit 601dc0a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
9 changes: 3 additions & 6 deletions src/chaptermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ChapterModel::ChapterModel(QObject *parent)
: QAbstractListModel(parent)
{
// connect library
connect(Player::instance(), &Player::currentIndexChanged, this, &ChapterModel::chapterIndexChanged);
connect(Player::instance(), &Player::currentIndexChanged, this, &ChapterModel::refresh);
}


Expand All @@ -17,11 +17,6 @@ void ChapterModel::refresh() {
endResetModel();
}

void ChapterModel::chapterIndexChanged(int xIndex)
{
refresh();
}


int ChapterModel::rowCount(const QModelIndex & /*parent*/) const
{
Expand Down Expand Up @@ -52,6 +47,8 @@ QVariant ChapterModel::data(const QModelIndex &index, int role) const
return QVariant(c.path);
case DurationRole:
return QVariant(Util::getDisplayTime(c.duration));
case CurrentItemRole:
return QVariant(index.row() == Player::instance()->chapterIndex());
}
return QVariant();
}
5 changes: 2 additions & 3 deletions src/chaptermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ChapterModel : public QAbstractListModel
TitleRole,
FileNameRole,
DurationRole,
CurrentItemRole,
};

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Expand All @@ -33,12 +34,10 @@ class ChapterModel : public QAbstractListModel
names[TitleRole] = "title";
names[FileNameRole] = "fileName";
names[DurationRole] = "duration";
names[CurrentItemRole] = "isCurrentItem";
return names;
}

public slots:
void chapterIndexChanged(int xIndex);


};

Expand Down
24 changes: 14 additions & 10 deletions src/librarymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include "librarymodel.h"
#include "database.h"
#include "util.h"
#include "player.h"


LibraryModel::LibraryModel(QObject *parent)
: QAbstractListModel(parent)
{
connect(Player::instance(), &Player::playlistChanged, this, &LibraryModel::refresh);
}


Expand All @@ -30,18 +32,20 @@ QVariant LibraryModel::data(const QModelIndex &index, int role) const
const Book &book = Database::instance()->getLibraryItems().at(index.row());

switch (role) {
case LibraryIndexRole:
return QVariant(index.row());
case TitleRole:
return QVariant(book.title);
case ChaptersRole:
return QVariant(book.chapters.count());
case DurationRole:
return QVariant(Util::getDisplayTime(book.duration));
case ArtistRole:
return QVariant(book.artist);
case LibraryIndexRole:
return QVariant(index.row());
case TitleRole:
return QVariant(book.title);
case ChaptersRole:
return QVariant(book.chapters.count());
case DurationRole:
return QVariant(Util::getDisplayTime(book.duration));
case ArtistRole:
return QVariant(book.artist);
case PathRole:
return QVariant(book.path);
case CurrentItemRole:
return Player::instance()->currentItem() == book.path;
}
return QVariant();
}
2 changes: 2 additions & 0 deletions src/librarymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class LibraryModel : public QAbstractListModel
DurationRole,
ArtistRole,
PathRole,
CurrentItemRole,
};

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Expand All @@ -37,6 +38,7 @@ class LibraryModel : public QAbstractListModel
names[DurationRole] = "duration";
names[ArtistRole] = "artist";
names[PathRole] = "path";
names[CurrentItemRole] = "isCurrentItem";
return names;
}

Expand Down
23 changes: 16 additions & 7 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Player::Player(QMediaPlayer *parent)
connect(this, &QMediaPlayer::volumeChanged, this, &Player::volumeChanged);
connect(this, &QMediaPlayer::playbackRateChanged, this, &Player::speedChanged);

//connect(this, &QMediaPlayer::seekableChanged, this, &Player::seekableChanged); // broken on android
connect(this, &QMediaPlayer::seekableChanged, this, &Player::updateSeekable); // broken on android
//connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferChanged); // broken

mTimer = new QTimer(this);
Expand Down Expand Up @@ -147,6 +147,7 @@ void Player::endSleepTimer()

void Player::exitHandler()
{
Database::instance()->writeBook(*mCurrentBook);
}


Expand Down Expand Up @@ -215,20 +216,20 @@ QString Player::titleText() const

void Player::positionChanged(qint64 xPosition)
{

if (mSetPosition > xPosition) {
if (mSetPosition != -1) {
// dont change progress if media is changing
if (!QMediaPlayer::isSeekable())
return;

setPosition(mSetPosition);
QMediaPlayer::setPosition(mSetPosition);
setMuted(false);
qDebug() << "unmute";
mSetPosition = -1;
mChangingBook = false;
}

mProgress = mCurrentBook->getStartProgressChapter(chapterIndex()) + xPosition;
mCurrentBook->progress = mProgress;
Database::instance()->writeBook(*mCurrentBook);
emit progressChanged();
}

Expand All @@ -248,6 +249,7 @@ void Player::setProgress(qint64 xPosition) {
// set chapter and position
QMediaPlayer::setMuted(true);
mSetPosition = chapter_position;
qDebug() << "muted" << mSetPosition;
setChapterIndex(chapter_index);
}

Expand Down Expand Up @@ -327,13 +329,17 @@ void Player::setSleepTimerEnabled(bool xEnabled)

void Player::updateMediaStatus(QMediaPlayer::MediaStatus xStatus)
{
if (xStatus == QMediaPlayer::NoMedia && chapterIndex() == -1 && mRepeatMode == Repeat::LIBRARY) {
if (xStatus == QMediaPlayer::NoMedia && chapterIndex() == -1 && mRepeatMode == Repeat::LIBRARY && !mChangingBook) {
QString next_book = Database::instance()->getNextLibraryItem(mCurrentBook->path)->path;
setCurrentItem(next_book);
qDebug() << "load next book from lib" << next_book;
}
}

void Player::updateSeekable(bool xSeekable)
{
qDebug() << "seekable" << xSeekable;
}


void Player::setRepeatMode(Player::Repeat xMode)
{
Expand Down Expand Up @@ -365,12 +371,15 @@ void Player::setRepeatMode(Player::Repeat xMode)

void Player::setCurrentItem(QString &xIndex)
{
qDebug() << "setItem" << xIndex;

if (xIndex.isEmpty())
return;

if (mCurrentBook != nullptr && mCurrentBook->path == xIndex)
return;

mChangingBook = true;

// save book progress
if (mCurrentBook != nullptr)
Expand Down
2 changes: 2 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public slots:
void setSleepTime(int xTime);
void setSleepTimerEnabled(bool xEnabled);
void updateMediaStatus(QMediaPlayer::MediaStatus xStatus);
void updateSeekable(bool xSeekable);

signals:
void progressChanged();
Expand All @@ -103,6 +104,7 @@ public slots:
qint64 mProgress = 0;
int mSleepTime = 3600000; // 1hr
bool mSleepTimeEnabled = false;
bool mChangingBook = false;
QTimer * mTimer = nullptr;
explicit Player(QMediaPlayer *parent = nullptr);
};
Expand Down
2 changes: 1 addition & 1 deletion src/qml/LibraryPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Page {

Rectangle {
id: background
color: ListView.isCurrentItem? Style.library.color_highlight : Style.library.color_background // TODO: model.isCurrentItem for active item
color: model.isCurrentItem? Style.library.color_highlight : Style.library.color_background
radius: Style.library.radius_background
implicitHeight: library_title.height + library_artist.height + Style.library.margin * 3
implicitWidth: library_list.width
Expand Down

0 comments on commit 601dc0a

Please sign in to comment.