Skip to content

Commit

Permalink
updated the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Eism committed Dec 3, 2024
1 parent f842b50 commit 2e67805
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 63 deletions.
5 changes: 5 additions & 0 deletions src/framework/global/serialization/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ void JsonArray::resize(size_t i)
array_mut(m_data).resize(i);
}

bool JsonArray::empty() const
{
return array_const(m_data).size() == 0;
}

JsonValue JsonArray::at(size_t i) const
{
std::shared_ptr<JsonData> d = std::make_shared<JsonData>();
Expand Down
2 changes: 2 additions & 0 deletions src/framework/global/serialization/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class JsonArray
size_t size() const;
void resize(size_t i);

bool empty() const;

JsonValue at(size_t i) const;

JsonArray& set(size_t i, bool v);
Expand Down
3 changes: 3 additions & 0 deletions src/musesounds/imusesoundsconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
#pragma once

#include "types/string.h"

#include "modularity/imoduleinterface.h"

#include "network/networktypes.h"
Expand All @@ -36,5 +38,6 @@ class IMuseSoundsConfiguration : MODULE_EXPORT_INTERFACE
virtual muse::network::RequestHeaders headers() const = 0;

virtual QUrl soundsUrl() const = 0;
virtual QUrl soundPageUrl(const muse::String& soundCode) const = 0;
};
}
9 changes: 8 additions & 1 deletion src/musesounds/internal/musesoundsconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using namespace muse::network;
static const std::string module_name("musesounds");
static const Settings::Key GET_SOUNDS_TESTING_MODE_KEY(module_name, "musesounds/getSoundsTestingMode");

static const muse::String OPEN_SOUND_URL("https://www.musehub.com/muse-sounds/");

void MuseSoundsConfiguration::init()
{
settings()->setDefaultValue(GET_SOUNDS_TESTING_MODE_KEY, Val(false));
Expand All @@ -38,7 +40,7 @@ void MuseSoundsConfiguration::init()
RequestHeaders MuseSoundsConfiguration::headers() const
{
RequestHeaders headers;
headers.rawHeaders["Accept"] = "application/json";
headers.rawHeaders["Content-Type"] = "application/json";
return headers;
}

Expand All @@ -48,6 +50,11 @@ QUrl MuseSoundsConfiguration::soundsUrl() const
: QUrl("https://cosmos-customer-webservice-dev.azurewebsites.net/graphql");
}

QUrl MuseSoundsConfiguration::soundPageUrl(const muse::String& soundCode) const
{
return QUrl(OPEN_SOUND_URL + soundCode);
}

bool MuseSoundsConfiguration::isTestingMode() const
{
return settings()->value(GET_SOUNDS_TESTING_MODE_KEY).toBool();
Expand Down
1 change: 1 addition & 0 deletions src/musesounds/internal/musesoundsconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MuseSoundsConfiguration : public IMuseSoundsConfiguration, public muse::In
muse::network::RequestHeaders headers() const override;

QUrl soundsUrl() const override;
QUrl soundPageUrl(const muse::String& soundCode) const override;

private:
bool isTestingMode() const;
Expand Down
89 changes: 59 additions & 30 deletions src/musesounds/internal/musesoundsrepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ using namespace mu::musesounds;
using namespace muse;
using namespace muse::network;

static muse::Uri correctThumbnailSize(const Uri& uri)
{
String uriStr = String::fromStdString(uri.toString());
uriStr.replace(u"/library-images/", u"/cdn-cgi/image/w=240,q=80,f=webp/library-images/");
return Uri(uriStr.toStdString());
}

void MuseSoundsRepository::init()
{
auto soundsCallBack = [this](const RetVal<MuseSoundCategoryInfoList>& result) {
Expand All @@ -46,7 +53,7 @@ void MuseSoundsRepository::init()
m_soundsCategoriesChanged.notify();
};

Concurrent::run(this, &MuseSoundsRepository::th_requestSounds, soundsRequestUrl(), soundsCallBack);
Concurrent::run(this, &MuseSoundsRepository::th_requestSounds, configuration()->soundsUrl(), soundsCallBack);
}

const MuseSoundCategoryInfoList& MuseSoundsRepository::soundsCategoryList() const
Expand All @@ -60,38 +67,37 @@ async::Notification MuseSoundsRepository::soundsCategoryListChanged() const
return m_soundsCategoriesChanged;
}

QUrl MuseSoundsRepository::soundsRequestUrl() const
QByteArray MuseSoundsRepository::soundsRequestJson() const
{
String locale = QLocale().name();

String query = String(
R"(
query MyQuery {
product_pages_configuration(version: "default") {
R"(query MyQuery {
product_pages_configuration {
librariesPageSections {
... on ProductPageSectionRegular {
title(locale: {locale: "%1"})
productCards {
... on ProductCardRegular {
title(locale: {locale: "%1"})
coverImageUrl
description(locale: {locale: "%1"})
iconImageUrl
product {
... on ProductLibrary {
title(locale: {locale: "%1"})
subtitle(locale: {locale: "%1"})
code
}
}
}
}
}
}
}
}
)").arg(locale);
})").arg(locale);

StringList params = {
"query=" + query
};

QUrl url = configuration()->soundsUrl();
url.setQuery(params.join(u"&"));
JsonObject json;
json["query"] = query;

return url;
return JsonDocument(json).toJson(JsonDocument::Format::Compact).toQByteArray();
}

void MuseSoundsRepository::th_requestSounds(const QUrl& soundsUrl, std::function<void(RetVal<MuseSoundCategoryInfoList>)> callBack) const
Expand All @@ -101,14 +107,17 @@ void MuseSoundsRepository::th_requestSounds(const QUrl& soundsUrl, std::function
network::INetworkManagerPtr networkManager = networkManagerCreator()->makeNetworkManager();
RequestHeaders headers = configuration()->headers();

QBuffer soundsInfoData;
Ret soundsItemsRet = networkManager->get(soundsUrl, &soundsInfoData, headers);
QByteArray jsonData = soundsRequestJson();
QBuffer receivedData(&jsonData);
OutgoingDevice device(&receivedData);

Ret soundsItemsRet = networkManager->post(soundsUrl, &device, &receivedData, headers);
if (!soundsItemsRet) {
callBack(soundsItemsRet);
return;
}

JsonDocument soundsInfoDoc = JsonDocument::fromJson(ByteArray::fromQByteArray(soundsInfoData.data()));
JsonDocument soundsInfoDoc = JsonDocument::fromJson(ByteArray::fromQByteArray(receivedData.data()));

RetVal<MuseSoundCategoryInfoList> result;
result.ret = make_ret(Ret::Code::Ok);
Expand All @@ -123,28 +132,48 @@ MuseSoundCategoryInfoList MuseSoundsRepository::parseSounds(const JsonDocument&

JsonObject obj = soundsDoc.rootObject();
JsonObject data = !obj.empty() ? obj.value("data").toObject() : JsonObject();
JsonObject productsSearch = !data.empty() ? data.value("products_search").toObject() : JsonObject();
JsonArray items = !productsSearch.empty() ? productsSearch.value("items").toArray() : JsonArray();
JsonObject productsSearch = !data.empty() ? data.value("product_pages_configuration").toObject() : JsonObject();
JsonArray categories = !productsSearch.empty() ? productsSearch.value("librariesPageSections").toArray() : JsonArray();

for (size_t i = 0; i < items.size(); i++) {
JsonObject itemObj = items.at(i).toObject();
for (size_t i = 0; i < categories.size(); ++i) {
JsonObject categoryObj = categories.at(i).toObject();
if (categoryObj.empty()) {
continue;
}

MuseSoundCategoryInfo category;
category.title = itemObj.value("title").toString();
category.title = categoryObj.value("title").toString();

JsonArray soundsItems = itemObj.value("items").toArray();
JsonArray soundsItems = categoryObj.value("productCards").toArray();
if (soundsItems.empty()) {
continue;
}

for (size_t i = 0; i < soundsItems.size(); i++) {
for (size_t i = 0; i < soundsItems.size(); ++i) {
JsonObject soundItemObj = soundsItems.at(i).toObject();
if (soundItemObj.empty()) {
continue;
}

JsonObject productObj = soundItemObj.value("product").toObject();
if (productObj.empty()) {
continue;
}

MuseSoundInfo sound;
sound.title = itemObj.value("title").toString();
sound.description = itemObj.value("description").toString();
sound.thumbnail = itemObj.value("coverImageUrl").toString();
sound.title = productObj.value("title").toString();
sound.description = productObj.value("subtitle").toString();
sound.thumbnail = correctThumbnailSize(Uri(soundItemObj.value("iconImageUrl").toStdString()));
sound.code = productObj.value("code").toString();
sound.uri = Uri(configuration()->soundPageUrl(sound.code).toString().toStdString());

category.sounds.emplace_back(sound);
}

if (category.sounds.empty()) {
continue;
}

result.emplace_back(category);
}

Expand Down
2 changes: 1 addition & 1 deletion src/musesounds/internal/musesoundsrepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MuseSoundsRepository : public IMuseSoundsRepository, public muse::Injectab
muse::async::Notification soundsCategoryListChanged() const override;

private:
QUrl soundsRequestUrl() const;
QByteArray soundsRequestJson() const;
void th_requestSounds(const QUrl& soundsUrl, std::function<void(muse::RetVal<MuseSoundCategoryInfoList>)> callBack) const;

MuseSoundCategoryInfoList parseSounds(const muse::JsonDocument& soundsDoc) const;
Expand Down
6 changes: 4 additions & 2 deletions src/musesounds/musesoundstypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#pragma once

#include "types/string.h"
#include "io/path.h"
#include "types/uri.h"

namespace mu::musesounds {
struct MuseSoundInfo {
muse::String code;
muse::String title;
muse::String description;
muse::io::path_t thumbnail;
muse::Uri thumbnail;
muse::Uri uri;
};
using MuseSoundInfoList = std::vector<MuseSoundInfo>;

Expand Down
8 changes: 4 additions & 4 deletions src/musesounds/qml/MuseScore/MuseSounds/MuseSoundsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ FocusScope {
delegate: MuseSoundsListView {
width: parent.width

title: modelData.title
sectionTitle: title
visible: count > 0

model: modelData.sounds
model: sounds

flickableItem: column

navigationPanel.section: navSec
navigationPanel.name: title + "Sounds"
navigationPanel.order: index

onGetSoundRequested: {
// todo
onGetSoundRequested: function(soundCode){
museSoundsModel.openSoundPage(soundCode)
}

onNavigationActivated: function(itemRect) {
Expand Down
13 changes: 0 additions & 13 deletions src/musesounds/qml/MuseScore/MuseSounds/internal/MuseSoundItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,8 @@ Rectangle {

Image {
id: thumbnailImage

anchors.fill: parent

fillMode: Image.PreserveAspectCrop

sourceSize: Qt.size(width * Screen.devicePixelRatio, height * Screen.devicePixelRatio)

layer.enabled: ui.isEffectsAllowed
layer.effect: EffectOpacityMask {
maskSource: Rectangle {
width: thumbnail.width
height: thumbnail.height
radius: thumbnail.radius
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,23 @@ Column {
property alias model: view.model
readonly property alias count: view.count

property alias title: titleLabel.text
property alias sectionTitle: titleLabel.text

property var flickableItem: null
property int headerHeight: titleLabel.height + spacing

property NavigationPanel navigationPanel: NavigationPanel {
name: "MuseSoundsListView"
direction: NavigationPanel.Both
accessible.name: root.title
accessible.name: root.sectionTitle
}

spacing: 16

signal getSoundRequested(var soundId)
signal getSoundRequested(var soundCode)
signal navigationActivated(var itemRect)

function focusOnFirst() {
// Force the view to load the items. Without this, `view.itemAtIndex(0)` might be null even when `count > 0`,
// causing navigation to break when calling this function from `resetNavigationFocus()` in `PluginsPage`.
view.forceLayout()

view.itemAtIndex(0).requestActive()
}

Expand Down Expand Up @@ -106,9 +102,9 @@ Column {
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter

title: model.title
description: model.description
thumbnailUrl: model.thumbnailUrl
title: modelData.title
description: modelData.description
thumbnailUrl: modelData.thumbnail

navigation.panel: root.navigationPanel
navigation.row: view.columns === 0 ? 0 : Math.floor(model.index / view.columns)
Expand All @@ -121,7 +117,7 @@ Column {

onGetSoundRequested: {
forceActiveFocus()
root.getSoundRequested(model.id)
root.getSoundRequested(modelData.code)
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/musesounds/view/musesoundslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ using namespace mu::musesounds;
static const QVariantMap soundInfoToMap(const MuseSoundInfo& soundInfo)
{
QVariantMap map;
map["code"] = soundInfo.code.toQString();
map["title"] = soundInfo.title.toQString();
map["description"] = soundInfo.description.toQString();
map["thumbnail"] = QUrl::fromLocalFile(soundInfo.thumbnail.toQString());
map["thumbnail"] = QString::fromStdString(soundInfo.thumbnail.toString());

return map;
}
Expand Down Expand Up @@ -60,6 +61,18 @@ void MuseSoundsListModel::load()
});
}

void MuseSoundsListModel::openSoundPage(const QString& soundCode)
{
for (const MuseSoundCategoryInfo& category : m_soundsCategories) {
for (const MuseSoundInfo& sound : category.sounds) {
if (sound.code == soundCode) {
interactive()->openUrl(sound.uri.toString());
return;
}
}
}
}

QVariant MuseSoundsListModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
Expand Down
Loading

0 comments on commit 2e67805

Please sign in to comment.