Skip to content

Commit c5a2fac

Browse files
committed
fix: always set downloaded song as active
1 parent e617940 commit c5a2fac

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

jukebox/jukebox/managers/nong_manager.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <unordered_map>
1010
#include <utility>
1111

12-
#include <fmt/chrono.h>
13-
#include <fmt/core.h>
1412
#include <fmt/format.h>
1513
#include <Geode/Result.hpp>
1614
#include <Geode/binding/LevelTools.hpp>
@@ -22,6 +20,7 @@
2220

2321
#include <jukebox/compat/compat.hpp>
2422
#include <jukebox/compat/v2.hpp>
23+
#include <jukebox/events/song_download_finished.hpp>
2524
#include <jukebox/managers/index_manager.hpp>
2625
#include <jukebox/nong/nong.hpp>
2726
#include <jukebox/nong/nong_serialize.hpp>
@@ -39,16 +38,17 @@ std::optional<Nongs*> NongManager::getNongs(int songID) {
3938
return m_manifest.m_nongs[songID].get();
4039
}
4140

42-
bool NongManager::isNongVerifiedForLevelSong(int levelID, int songID, std::string_view uniqueID) {
41+
bool NongManager::isNongVerifiedForLevelSong(int levelID, int songID,
42+
std::string_view uniqueID) {
4343
// List the verified nongs for the given level and song
44-
std::vector<std::string> verifiedNongs = NongManager::get().getVerifiedNongsForLevel(
45-
levelID,
46-
{songID}
47-
);
48-
return std::find(verifiedNongs.begin(), verifiedNongs.end(), uniqueID) != verifiedNongs.end();
44+
std::vector<std::string> verifiedNongs =
45+
NongManager::get().getVerifiedNongsForLevel(levelID, {songID});
46+
return std::find(verifiedNongs.begin(), verifiedNongs.end(), uniqueID) !=
47+
verifiedNongs.end();
4948
}
5049

51-
std::vector<std::string> NongManager::getVerifiedNongsForLevel(int levelID, std::vector<int> songIDs) {
50+
std::vector<std::string> NongManager::getVerifiedNongsForLevel(
51+
int levelID, std::vector<int> songIDs) {
5252
std::vector<std::string> verifiedNongs;
5353

5454
for (const int songID : songIDs) {
@@ -57,8 +57,9 @@ std::vector<std::string> NongManager::getVerifiedNongsForLevel(int levelID, std:
5757
if (!nongs.has_value()) {
5858
continue;
5959
}
60-
61-
// For each indexSong, check if it contains the given levelID in its verifiedLevelIDs field.
60+
61+
// For each indexSong, check if it contains the given levelID in its
62+
// verifiedLevelIDs field.
6263
for (auto indexSong : nongs.value()->indexSongs()) {
6364
auto& ids = indexSong->verifiedLevelIDs;
6465

@@ -480,4 +481,22 @@ std::filesystem::path NongManager::generateSongFilePath(
480481
return destination;
481482
}
482483

484+
ListenerResult NongManager::onDownloadFinished(event::SongDownloadFinished* e) {
485+
const auto nongsOpt = this->getNongs(e->destination()->metadata()->gdID);
486+
487+
if (!nongsOpt) {
488+
return ListenerResult::Propagate;
489+
}
490+
491+
Nongs* nongs = nongsOpt.value();
492+
493+
if (GEODE_UNWRAP_IF_ERR(
494+
err, nongs->setActive(e->destination()->metadata()->uniqueID))) {
495+
log::error("Failed to set newly downloaded song {} as active: {}",
496+
e->destination()->metadata()->uniqueID, err);
497+
}
498+
499+
return ListenerResult::Propagate;
500+
}
501+
483502
}; // namespace jukebox

jukebox/jukebox/managers/nong_manager.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <Geode/utils/Task.hpp>
1313

1414
#include <jukebox/events/get_song_info.hpp>
15+
#include <jukebox/events/song_download_finished.hpp>
1516
#include <jukebox/events/song_error.hpp>
1617
#include <jukebox/nong/nong.hpp>
1718

@@ -37,13 +38,16 @@ class NongManager {
3738
}
3839

3940
geode::Result<> saveNongs(std::optional<int> saveId = std::nullopt);
40-
geode::EventListener<geode::EventFilter<jukebox::event::SongError>>
41+
geode::EventListener<geode::EventFilter<event::SongError>>
4142
m_songErrorListener;
42-
geode::EventListener<geode::EventFilter<jukebox::event::GetSongInfo>>
43+
geode::EventListener<geode::EventFilter<event::GetSongInfo>>
4344
m_songInfoListener;
45+
geode::EventListener<geode::EventFilter<event::SongDownloadFinished>>
46+
m_downloadFinishedListener = {this, &NongManager::onDownloadFinished};
4447
geode::Result<std::unique_ptr<Nongs>> loadNongsFromPath(
4548
const std::filesystem::path& path);
4649

50+
geode::ListenerResult onDownloadFinished(event::SongDownloadFinished* e);
4751
geode::Result<> migrateV2();
4852

4953
public:
@@ -99,26 +103,31 @@ class NongManager {
99103
std::optional<Nongs*> getNongs(int songID);
100104

101105
/**
102-
* Returns all the uniqueIDs of nongs that are verified for the given level ID
106+
* Returns all the uniqueIDs of nongs that are verified for the given level
107+
* ID
103108
*
104109
* @param levelID the id of the level
105110
* @param songIDs list of all the song ids to check their nongs
106-
* @return List of all uniqueIDs of nongs that are verified for the given level ID
111+
* @return List of all uniqueIDs of nongs that are verified for the given
112+
* level ID
107113
*/
108-
std::vector<std::string> getVerifiedNongsForLevel(int levelID, std::vector<int> songIDs);
114+
std::vector<std::string> getVerifiedNongsForLevel(int levelID,
115+
std::vector<int> songIDs);
109116

110117
/**
111118
* Returns whether the nong is verified for the a song in a level
112119
*
113120
* @param levelID the id of the level
114121
* @param songID the id of a song in the level
115122
* @param uniqueID the id of the nong
116-
* @return Boolean for whether the nong is verified
123+
* @return Boolean for whether the nong is verified
117124
*/
118-
bool isNongVerifiedForLevelSong(int levelID, int songID, std::string_view uniqueID);
125+
bool isNongVerifiedForLevelSong(int levelID, int songID,
126+
std::string_view uniqueID);
119127

120128
/**
121-
* Checks if the given level has a verified song for any of the given song IDs
129+
* Checks if the given level has a verified song for any of the given song
130+
* IDs
122131
*
123132
* @param levelID the id of the level
124133
* @param songIDs list of all the song ids to check

jukebox/jukebox/ui/list/nong_list.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ bool NongList::init(std::vector<int>& songIds, const cocos2d::CCSize& size,
9292
return true;
9393
}
9494

95-
void NongList::setDownloadProgress(std::string uniqueID, float progress) {}
96-
9795
void NongList::build() {
9896
if (m_list->m_contentLayer->getChildrenCount() > 0) {
9997
m_list->m_contentLayer->removeAllChildrenWithCleanup(true);
@@ -428,12 +426,6 @@ ListenerResult NongList::onDownloadFinish(event::SongDownloadFinished* e) {
428426
i->removeFromParentAndCleanup(true);
429427
}
430428

431-
auto res = nongs->setActive(e->destination()->metadata()->uniqueID);
432-
if (!res) {
433-
log::error("Failed to set newly downloaded song {} as active: {}",
434-
e->destination()->metadata()->uniqueID, res.unwrapErr());
435-
}
436-
437429
this->addSongToList(e->destination(), nongs, true);
438430

439431
// Remove "you have no local songs label"

jukebox/jukebox/ui/list/nong_list.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
namespace jukebox {
2222

23-
class NongList : public cocos2d::CCNode {
23+
class NongList final : public cocos2d::CCNode {
2424
public:
2525
enum class ListType { Single = 0, Multiple = 1 };
2626

2727
protected:
2828
std::vector<int> m_songIds;
29-
geode::ScrollLayer* m_list;
30-
cocos2d::extension::CCScale9Sprite* m_bg;
29+
geode::ScrollLayer* m_list = nullptr;
30+
cocos2d::extension::CCScale9Sprite* m_bg = nullptr;
3131
std::optional<int> m_currentSong = std::nullopt;
3232
std::optional<int> m_levelID;
3333

@@ -58,7 +58,6 @@ class NongList : public cocos2d::CCNode {
5858
void build();
5959
void onBack(cocos2d::CCObject*);
6060
void onSelectSong(int songId);
61-
void setDownloadProgress(std::string uniqueID, float progress);
6261

6362
static NongList* create(
6463
std::vector<int>& songIds, const cocos2d::CCSize& size,

0 commit comments

Comments
 (0)