Compare commits

...
Author SHA1 Message Date
Jonah Brüchert f4b2f06484 Small library cleanup 2023-08-07 01:54:55 +02:00
Jonah Brüchert 89dda4c28a Filter search history in the database
Allows to easily limit the default number of history entries.
Previously, limiting the number of sql results would not have worked,
since the filter was being applied on top of them.
2023-08-07 01:45:54 +02:00
Jonah Brüchert e82fb652ee Improve performance of search popup with new local search model 2023-08-07 01:32:07 +02:00
4 changed files with 102 additions and 36 deletions

No files matched your search

+5 -8
View File
@@ -40,6 +40,7 @@ Item {
onTextEdited: {
filterText = text
Library.searches.filter = text
if(completionList.count === 0) {
//no matching search -> message should display
Library.temporarySearch = ""
@@ -316,11 +317,10 @@ Item {
visible: searchField.filterText && recentsRepeater.count > 0 //if changed, adjust playOpenHeight
model: SortFilterModel {
filterRole: PlaybackHistoryModel.Title
filterRegularExpression: searchField.filterExpression
sourceModel: Library.playbackHistory
model: LocalSearchModel {
searchQuery: searchField.text
}
delegate: searchAlbum
}
Kirigami.Separator {
@@ -355,10 +355,7 @@ Item {
property int empiricDelegateHeight: recentsRepeater.visible ? (mainScrollView.contentHeight - recentsRepeater.height) / count : (mainScrollView.contentHeight + mainScrollViewLayout.spacing) / (count)
id: completionList
model: SortFilterModel {
sourceModel: Library.searches
filterRegularExpression: searchField.filterExpression
}
model: Library.searches
function isSelectedDelegateVisible() {
if(!recentsRepeater.visible) {
+71 -24
View File
@@ -27,25 +27,10 @@ Library::Library(QObject *parent)
}()))
{
m_database->runMigrations(":/migrations/");
m_searches = new SearchHistoryModel(m_database->getResults<SingleValue<QString>>("select distinct (search_query) from searches order by search_id desc"), this);
connect(this, &Library::changeFavourites, [this]() {
auto future = m_database->getResults<Song>("select * from favourites natural join songs order by favourites.rowid desc");
m_favourites= new FavouritesModel(std::move(future), this);
Q_EMIT favouritesChanged();
});
Q_EMIT changeFavourites();
m_searches = new SearchHistoryModel(this);
connect(this, &Library::changePlaybackHistory, [this]() {
//playbackHistory
auto future = m_database->getResults<PlayedSong>("select * from played_songs natural join songs");
m_playbackHistory = new PlaybackHistoryModel(std::move(future), this);
//mostPlayed
auto future2 = m_database->getResults<PlayedSong>("select * from played_songs natural join songs order by plays desc limit 10");
m_mostPlayed = new PlaybackHistoryModel(std::move(future2), this);
Q_EMIT playbackHistoryChanged();
});
Q_EMIT changePlaybackHistory();
refreshFavourites();
refreshPlaybackHistory();
}
Library::~Library() = default;
@@ -64,13 +49,15 @@ FavouritesModel *Library::favourites()
void Library::addFavourite(const QString &videoId, const QString &title, const QString &artist, const QString &album)
{
QCoro::connect(addSong(videoId, title, artist, album), this, [=, this] {
QCoro::connect(m_database->execute("insert or ignore into favourites (video_id) values (?)", videoId), this, &Library::changeFavourites);
QCoro::connect(m_database->execute("insert or ignore into favourites (video_id) values (?)", videoId),
this, &Library::refreshFavourites);
});
}
void Library::removeFavourite(const QString &videoId)
{
QCoro::connect(m_database->execute("delete from favourites where video_id = ?", videoId), this, &Library::changeFavourites);
QCoro::connect(m_database->execute("delete from favourites where video_id = ?", videoId),
this, &Library::refreshFavourites);
}
FavouriteWatcher *Library::favouriteWatcher(const QString &videoId)
@@ -114,17 +101,41 @@ PlaybackHistoryModel *Library::playbackHistory()
return m_playbackHistory;
}
void Library::refreshPlaybackHistory()
{
// playbackHistory
auto future = m_database->getResults<PlayedSong>(
"select * from played_songs natural join songs");
m_playbackHistory = new PlaybackHistoryModel(std::move(future), this);
// mostPlayed
auto future2 = m_database->getResults<PlayedSong>(
"select * from played_songs natural join songs order by plays desc limit 10");
m_mostPlayed = new PlaybackHistoryModel(std::move(future2), this);
Q_EMIT playbackHistoryChanged();
}
void Library::refreshFavourites()
{
auto future = m_database->getResults<Song>(
"select * from favourites natural join songs order by favourites.rowid desc");
m_favourites = new FavouritesModel(std::move(future), this);
Q_EMIT favouritesChanged();
}
void Library::addPlaybackHistoryItem(const QString &videoId, const QString &title, const QString &artist, const QString &album)
{
QCoro::connect(addSong(videoId, title, artist, album), this, [=, this] {
QCoro::connect(m_database->execute("insert or ignore into played_songs (video_id, plays) values (?, ?)", videoId, 0), this, [=, this] {
QCoro::connect(m_database->execute("update played_songs set plays = plays + 1 where video_id = ? ", videoId), this, &Library::changePlaybackHistory);
QCoro::connect(m_database->execute("update played_songs set plays = plays + 1 where video_id = ? ", videoId),
this, &Library::refreshPlaybackHistory);
});
});
}
void Library::removePlaybackHistoryItem(const QString &videoId)
{
QCoro::connect(m_database->execute("delete from played_songs where video_id = ?", videoId), this, &Library::changePlaybackHistory);
QCoro::connect(m_database->execute("delete from played_songs where video_id = ?", videoId),
this, &Library::refreshPlaybackHistory);
}
WasPlayedWatcher *Library::wasPlayedWatcher(const QString& videoId)
@@ -164,6 +175,11 @@ PlaybackHistoryModel::PlaybackHistoryModel(QFuture<std::vector<PlayedSong>> &&so
});
}
PlaybackHistoryModel::PlaybackHistoryModel(QObject *parent)
: QAbstractListModel(parent)
{
}
QHash<int, QByteArray> PlaybackHistoryModel::roleNames() const {
return {
{Roles::VideoId, "videoId"},
@@ -272,9 +288,25 @@ bool FavouriteWatcher::isFavourite() const {
return m_isFavourite;
}
SearchHistoryModel::SearchHistoryModel(QFuture<std::vector<SingleValue<QString>>> &&historyFuture, QObject *parent)
: QAbstractListModel(parent)
SearchHistoryModel::SearchHistoryModel(Library *library)
: QAbstractListModel(library)
{
auto historyFuture = library->database()
.getResults<SingleValue<QString>>("select distinct (search_query) from searches order by search_id desc limit 20");
connect(this, &SearchHistoryModel::filterChanged, this, [library, this]() {
auto future = library->database()
.getResults<SingleValue<QString>>("select distinct (search_query) from searches "
"where search_query like '%" % m_filter % "%'"
"order by search_id desc limit 20");
QCoro::connect(std::move(future), this, [this](auto history) {
beginResetModel();
m_history = history;
endResetModel();
});
});
QCoro::connect(std::move(historyFuture), this, [this](const auto history) {
beginResetModel();
m_history = history;
@@ -391,3 +423,18 @@ void WasPlayedWatcher::update(std::optional<SingleValue<bool> > result)
Q_EMIT wasPlayedChanged();
}
}
LocalSearchModel::LocalSearchModel(QObject *parent) : PlaybackHistoryModel(parent)
{
connect(this, &LocalSearchModel::searchQueryChanged, this, [this]() {
auto resultFuture = Library::instance().database()
.getResults<PlayedSong>("select * from played_songs natural join songs "
"where title like '%" % m_searchQuery % "%' "
"order by plays desc limit 10");
QCoro::connect(std::move(resultFuture), this, [this](auto results) {
beginResetModel();
m_playedSongs = results;
endResetModel();
});
});
}
+25 -4
View File
@@ -84,21 +84,39 @@ public:
Q_ENUM(Roles);
PlaybackHistoryModel(QFuture<std::vector<PlayedSong>> &&songs, QObject *parent = nullptr);
PlaybackHistoryModel(QObject *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
std::vector<PlayedSong> getPlayedSong() const;
private:
protected:
std::vector<PlayedSong> m_playedSongs;
};
class LocalSearchModel : public PlaybackHistoryModel {
Q_OBJECT
Q_PROPERTY(QString searchQuery MEMBER m_searchQuery NOTIFY searchQueryChanged)
Q_SIGNAL void searchQueryChanged();
public:
LocalSearchModel(QObject *parent = nullptr);
QString m_searchQuery;
};
class Library;
class SearchHistoryModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QString filter MEMBER m_filter NOTIFY filterChanged)
public:
SearchHistoryModel(QFuture<std::vector<SingleValue<QString>>> &&historyFuture, QObject *parent = nullptr);
SearchHistoryModel(Library *library);
int rowCount(const QModelIndex &parent) const override;
void removeSearch(const QString &search);
QVariant data(const QModelIndex &index, int role) const override;
@@ -106,10 +124,13 @@ public:
const QString & temporarySearch() const;
void setTemporarySearch(QString const& text);
Q_SIGNAL void filterChanged();
private:
size_t getRow(QString const &search) const;
std::vector<SingleValue<QString>> m_history;
QString m_temporarySearch;
QString m_filter;
};
class Library : public QObject
@@ -129,7 +150,7 @@ public:
FavouritesModel *favourites();
Q_SIGNAL void favouritesChanged();
Q_SIGNAL void changeFavourites();
void refreshFavourites();
Q_INVOKABLE void addFavourite(const QString &videoId, const QString &title, const QString &artist, const QString &album);
Q_INVOKABLE void removeFavourite(const QString &videoId);
Q_INVOKABLE FavouriteWatcher *favouriteWatcher(const QString &videoId);
@@ -144,7 +165,7 @@ public:
PlaybackHistoryModel *playbackHistory();
Q_SIGNAL void playbackHistoryChanged();
Q_SIGNAL void changePlaybackHistory();
void refreshPlaybackHistory();
Q_INVOKABLE void addPlaybackHistoryItem(const QString &videoId, const QString &title, const QString &artist, const QString &album);
Q_INVOKABLE void removePlaybackHistoryItem(const QString &videoId);
Q_INVOKABLE WasPlayedWatcher *wasPlayedWatcher(const QString &videoId);
+1
View File
@@ -91,6 +91,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qmlRegisterUncreatableType<PlaybackHistoryModel>(URI, 1, 0, "PlaybackHistoryModel","");
qmlRegisterType<QSortFilterProxyModel>(URI, 1, 0, "SortFilterModel");
qmlRegisterType<Blur>(URI, 1, 0, "Blur");
qmlRegisterType<LocalSearchModel>(URI, 1, 0, "LocalSearchModel");
qmlRegisterType<LocalPlaylistModel>(URI, 1, 0, "LocalPlaylistModel");
qmlRegisterType<LocalPlaylistsModel>(URI, 1, 0, "LocalPlaylistsModel");
qmlRegisterType<PlaylistImporter>(URI, 1, 0, "PlaylistImporter");