Compare commits

...
Author SHA1 Message Date
Jonah Brüchert c1341fad05 Also use optimized search models for mobile search history 2023-08-07 20:22:04 +02:00
Théophile Gilgien 0b1a8105d4 Small cleanup 2023-08-07 15:58:30 +02:00
Theophile Gilgien 5b1f3c7120 remove filterText
It isn't needed anymore, in the two places where it was still used, it
can be replaced by Library.searches.filter.
2023-08-07 14:41:00 +02:00
Jonah Brüchert 9c11b75658 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 02:00:16 +02:00
Jonah Brüchert 3bce344e0c Improve performance of search popup with new local search model 2023-08-07 01:59:34 +02:00
Jonah Brüchert 592ad4ec68 Small library cleanup 2023-08-07 01:59:05 +02:00
Bart Ribbers 2894ab83e2 Allow building with KF6 2023-08-06 21:32:23 +02:00
6 changed files with 123 additions and 58 deletions

No files matched your search

+12 -5
View File
@@ -14,13 +14,20 @@ project(audiotube VERSION ${RELEASE_SERVICE_VERSION})
include(FeatureSummary)
set(QT5_MIN_VERSION 5.15)
set(KF5_MIN_VERSION 5.81)
if (QT_MAJOR_VERSION STREQUAL "6")
set(QT_MIN_VERSION "6.4.0")
set(KF_MIN_VERSION "5.240.0")
set(KF_MAJOR_VERSION "6")
else()
set(QT_MIN_VERSION "5.15")
set(KF_MIN_VERSION "5.81.0")
set(KF_MAJOR_VERSION "5")
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE)
find_package(ECM ${KF_MIN_VERSION} REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
@@ -39,8 +46,8 @@ ecm_setup_version(${PROJECT_VERSION}
kde_enable_exceptions()
find_package(Qt${QT_MAJOR_VERSION} ${QT5_MIN_VERSION} REQUIRED COMPONENTS Core Gui Qml QuickControls2 Svg Sql Widgets Concurrent DBus)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 I18n CoreAddons Crash WindowSystem)
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} REQUIRED COMPONENTS Core Gui Qml QuickControls2 Svg Sql Widgets Concurrent DBus)
find_package(KF${KF_MAJOR_VERSION} ${KF_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 I18n CoreAddons Crash WindowSystem)
find_package(pybind11 REQUIRED)
find_package(Ytdlp REQUIRED RUNTIME)
find_package(YTMusicAPI 1.0.2 REQUIRED RUNTIME)
+4 -9
View File
@@ -38,11 +38,10 @@ Kirigami.ScrollablePage {
Repeater {
id: recentsRepeater
Layout.fillWidth: true
model: SortFilterModel {
filterRole: PlaybackHistoryModel.Title
filterRegularExpression: searchLoader.filterExpression
sourceModel: Library.playbackHistory
model: LocalSearchModel {
searchQuery: searchLoader.text
}
delegate: Kirigami.DelegateRecycler {
Layout.alignment: Qt.AlignTop
sourceComponent: searchAlbum
@@ -61,10 +60,7 @@ Kirigami.ScrollablePage {
Repeater {
Layout.fillHeight: true
Layout.fillWidth: true
model: SortFilterModel {
sourceModel: Library.searches
filterRegularExpression: searchLoader.filterExpression
}
model: Library.searches
delegate: Kirigami.AbstractListItem {
implicitHeight: Kirigami.Units.gridUnit * 2
@@ -98,5 +94,4 @@ Kirigami.ScrollablePage {
}
}
}
}
+7 -16
View File
@@ -13,7 +13,6 @@ import org.kde.ytmusic 1.0
Item {
function forceFocus(){searchField.forceActiveFocus()}
function accepted(){searchField.accepted()}
property alias filterExpression: searchField.filterExpression
property alias text: searchField.text
id: root
@@ -25,21 +24,17 @@ Item {
readOnly: true
}
Kirigami.SearchField {
property var filterExpression: new RegExp(`.*${filterText}.*`, "i")
property string filterText: ""
id: searchField
autoAccept: false
width: root.width
selectByMouse: true
onFocusChanged: {
filterText = text
if (wideScreen && focus)
popup.open()
}
onTextEdited: {
filterText = text
Library.searches.filter = text
if(completionList.count === 0) {
//no matching search -> message should display
Library.temporarySearch = ""
@@ -202,7 +197,7 @@ Item {
from: searchField.height
duration: 200
to: completionList
? (searchField.filterText && recentsRepeater.count > 0 //can't use recentsRepeater.visible directly, because it always returns false at this stage
? (Library.searches.filter && recentsRepeater.count > 0 //can't use recentsRepeater.visible directly, because it always returns false at this stage
? (Math.min(fieldContainer.height+ (completionList.count) * (completionList.delegateHeight + completionList.delegatePadding) + Kirigami.Separator.implicitHeight + recentsRepeater.implicitHeight, Kirigami.Units.gridUnit * 20))+2*(popup.shadowSize+popup.expansion)
: (completionList.count === 0
?(Math.min(fieldContainer.height+ noMatchingSearchLabel.height + 2*noMatchingSearchLabel.Layout.margins, Kirigami.Units.gridUnit * 20))+2*(popup.shadowSize+popup.expansion)
@@ -314,13 +309,12 @@ Item {
Layout.fillWidth: true
visible: searchField.filterText && recentsRepeater.count > 0 //if changed, adjust playOpenHeight
visible: Library.searches.filter && recentsRepeater.count > 0 //if changed, adjust playOpenHeight
model: SortFilterModel {
filterRole: PlaybackHistoryModel.Title
filterRegularExpression: searchField.filterExpression
sourceModel: Library.playbackHistory
model: LocalSearchModel {
searchQuery: Library.searches.filter
}
delegate: searchAlbum
}
Kirigami.Separator {
@@ -355,10 +349,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();
});
});
}
+28 -4
View File
@@ -84,21 +84,42 @@ 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;
};
///
/// Provides a list of recently played songs matching a search query.
///
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 +127,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 +153,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 +168,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");