Merge branch 'persistent-ui-btn-state' into develop

This commit is contained in:
emeric
2024-09-13 18:46:01 +02:00
25 changed files with 490 additions and 122 deletions
+17 -6
View File
@@ -67,14 +67,25 @@ namespace lms::core::stringUtils
template<typename T>
[[nodiscard]] std::optional<T> readAs(std::string_view str)
{
T res;
if constexpr (std::is_enum_v<T>)
{
using UnderlyingType = std::underlying_type_t<T>;
std::optional<UnderlyingType> underlyingValue{ readAs<UnderlyingType>(str) };
if (!underlyingValue)
return std::nullopt;
std::istringstream iss{ std::string{ str } };
iss >> res;
if (iss.fail())
return std::nullopt;
return static_cast<T>(*underlyingValue);
}
else
{
T res;
std::istringstream iss{ std::string{ str } };
iss >> res;
if (iss.fail())
return std::nullopt;
return res;
return res;
}
}
template<>
+1
View File
@@ -24,6 +24,7 @@ add_library(lmsdatabase SHARED
impl/Track.cpp
impl/TrackBookmark.cpp
impl/Types.cpp
impl/UIState.cpp
impl/User.cpp
impl/Utils.cpp
)
+19 -1
View File
@@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 66 };
static constexpr Version LMS_DATABASE_VERSION{ 67 };
}
VersionInfo::VersionInfo()
@@ -744,6 +744,23 @@ SELECT
session.getDboSession()->execute("UPDATE scan_settings SET scan_version = scan_version + 1");
}
void migrateFromV66(Session& session)
{
// New way of handling UI settings
session.getDboSession()->execute(R"(CREATE TABLE IF NOT EXISTS "ui_state" (
"id" integer primary key autoincrement,
"version" integer not null,
"item" text not null,
"value" text not null,
"user_id" bigint,
constraint "fk_ui_state_user" foreign key ("user_id") references "user" ("id") on delete cascade deferrable initially deferred
))");
session.getDboSession()->execute("ALTER TABLE user DROP COLUMN repeat_all");
session.getDboSession()->execute("ALTER TABLE user DROP COLUMN radio");
session.getDboSession()->execute("ALTER TABLE user DROP COLUMN cur_playing_track_pos");
}
bool doDbMigration(Session& session)
{
static const std::string outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -786,6 +803,7 @@ SELECT
{ 63, migrateFromV63 },
{ 64, migrateFromV64 },
{ 65, migrateFromV65 },
{ 66, migrateFromV66 },
};
bool migrationPerformed{};
+2
View File
@@ -44,6 +44,7 @@
#include "database/TrackFeatures.hpp"
#include "database/TrackList.hpp"
#include "database/TransactionChecker.hpp"
#include "database/UIState.hpp"
#include "database/User.hpp"
#include "EnumSetTraits.hpp"
@@ -117,6 +118,7 @@ namespace lms::db
_session.mapClass<TrackFeatures>("track_features");
_session.mapClass<TrackList>("tracklist");
_session.mapClass<TrackListEntry>("tracklist_entry");
_session.mapClass<UIState>("ui_state");
_session.mapClass<User>("user");
}
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database/UIState.hpp"
#include "core/ILogger.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "IdTypeTraits.hpp"
#include "StringViewTraits.hpp"
#include "Utils.hpp"
namespace lms::db
{
UIState::UIState(std::string_view item, ObjectPtr<User> user)
: _item{ item }
, _user{ getDboPtr(user) }
{
}
UIState::pointer UIState::create(Session& session, std::string_view item, ObjectPtr<User> user)
{
return session.getDboSession()->add(std::unique_ptr<UIState>{ new UIState{ item, user } });
}
std::size_t UIState::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM ui_state"));
}
UIState::pointer UIState::find(Session& session, UIStateId settingId)
{
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<UIState>>("SELECT ui_s from ui_state ui_s").where("ui_s.id = ?").bind(settingId) };
return utils::fetchQuerySingleResult(query);
}
UIState::pointer UIState::find(Session& session, std::string_view item, UserId userId)
{
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<UIState>>("SELECT ui_s from ui_state ui_s").where("ui_s.item = ?").bind(item).where("ui_s.user_id = ?").bind(userId) };
return utils::fetchQuerySingleResult(query);
}
} // namespace lms::db
+1
View File
@@ -24,6 +24,7 @@
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/UIState.hpp"
#include "IdTypeTraits.hpp"
#include "StringViewTraits.hpp"
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Wt/Dbo/Dbo.h>
#include "core/String.hpp"
#include "database/Object.hpp"
#include "database/Types.hpp"
#include "database/UIStateId.hpp"
#include "database/UserId.hpp"
namespace lms::db
{
class User;
class Session;
class UIState final : public Object<UIState, UIStateId>
{
public:
UIState() = default;
static std::size_t getCount(Session& session);
static pointer find(Session& session, UIStateId settingId);
static pointer find(Session& session, std::string_view item, UserId userId);
// Getters
std::string_view getValue() const { return _value; };
// Setters
void setValue(std::string_view value) { _value = value; };
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _item, "item");
Wt::Dbo::field(a, _value, "value");
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
UIState(std::string_view item, ObjectPtr<User> user);
static pointer create(Session& session, std::string_view item, ObjectPtr<User> user);
std::string _item;
std::string _value;
Wt::Dbo::ptr<User> _user;
};
} // namespace lms::db
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "database/IdType.hpp"
LMS_DECLARE_IDTYPE(UIStateId)
+3 -16
View File
@@ -35,6 +35,7 @@ namespace lms::db
{
class AuthToken;
class Session;
class UIState;
class User final : public Object<User, UserId>
{
@@ -104,9 +105,6 @@ namespace lms::db
void setSubsonicEnableTranscodingByDefault(bool value) { _subsonicEnableTranscodingByDefault = value; }
void setSubsonicDefaultTranscodintOutputFormat(TranscodingOutputFormat encoding) { _subsonicDefaultTranscodingOutputFormat = encoding; }
void setSubsonicDefaultTranscodingOutputBitrate(Bitrate bitrate);
void setCurPlayingTrackPos(std::size_t pos) { _curPlayingTrackPos = pos; }
void setRadio(bool val) { _radio = val; }
void setRepeatAll(bool val) { _repeatAll = val; }
void setUITheme(UITheme uiTheme) { _uiTheme = uiTheme; }
void clearAuthTokens();
void setSubsonicArtistListMode(SubsonicArtistListMode mode) { _subsonicArtistListMode = mode; }
@@ -121,9 +119,6 @@ namespace lms::db
bool getSubsonicEnableTranscodingByDefault() const { return _subsonicEnableTranscodingByDefault; }
TranscodingOutputFormat getSubsonicDefaultTranscodingOutputFormat() const { return _subsonicDefaultTranscodingOutputFormat; }
Bitrate getSubsonicDefaultTranscodingOutputBitrate() const { return _subsonicDefaultTranscodingOutputBitrate; }
std::size_t getCurPlayingTrackPos() const { return _curPlayingTrackPos; }
bool isRepeatAllSet() const { return _repeatAll; }
bool isRadioSet() const { return _radio; }
UITheme getUITheme() const { return _uiTheme; }
SubsonicArtistListMode getSubsonicArtistListMode() const { return _subsonicArtistListMode; }
FeedbackBackend getFeedbackBackend() const { return _feedbackBackend; }
@@ -147,12 +142,8 @@ namespace lms::db
Wt::Dbo::field(a, _scrobblingBackend, "scrobbling_backend");
Wt::Dbo::field(a, _listenbrainzToken, "listenbrainz_token");
// UI player settings
Wt::Dbo::field(a, _curPlayingTrackPos, "cur_playing_track_pos");
Wt::Dbo::field(a, _repeatAll, "repeat_all");
Wt::Dbo::field(a, _radio, "radio");
Wt::Dbo::hasMany(a, _authTokens, Wt::Dbo::ManyToOne, "user");
Wt::Dbo::hasMany(a, _uiStates, Wt::Dbo::ManyToOne, "user");
}
private:
@@ -178,12 +169,8 @@ namespace lms::db
TranscodingOutputFormat _subsonicDefaultTranscodingOutputFormat{ defaultSubsonicTranscodingOutputFormat };
int _subsonicDefaultTranscodingOutputBitrate{ defaultSubsonicTranscodingOutputBitrate };
// User's dynamic data (UI)
int _curPlayingTrackPos{}; // Current track position in queue
bool _repeatAll{};
bool _radio{};
Wt::Dbo::collection<Wt::Dbo::ptr<AuthToken>> _authTokens;
Wt::Dbo::collection<Wt::Dbo::ptr<UIState>> _uiStates;
};
} // namespace lms::db
+3
View File
@@ -29,6 +29,8 @@
#include "database/StarredArtist.hpp"
#include "database/StarredRelease.hpp"
#include "database/StarredTrack.hpp"
#include "database/UIState.hpp"
#include "database/User.hpp"
namespace lms::db::tests
{
@@ -349,6 +351,7 @@ VALUES
EXPECT_FALSE(StarredTrack::find(session, StarredTrackId{}));
EXPECT_FALSE(Track::find(session, TrackId{}));
EXPECT_FALSE(TrackList::find(session, TrackListId{}));
EXPECT_FALSE(UIState::find(session, UIStateId{}));
EXPECT_FALSE(User::find(session, UserId{}));
}
}