diff --git a/src/musikcube/app/layout/LyricsLayout.cpp b/src/musikcube/app/layout/LyricsLayout.cpp index 8493def08..4767efd9b 100644 --- a/src/musikcube/app/layout/LyricsLayout.cpp +++ b/src/musikcube/app/layout/LyricsLayout.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -87,17 +88,19 @@ void LyricsLayout::OnTrackChanged(size_t index, TrackPtr track) { } } -void LyricsLayout::OnLyricsLoaded(TrackPtr track, const std::string& lyrics) { - if (!track || !lyrics.size()) { - return; - } - this->UpdateAdapter(lyrics); +void LyricsLayout::OnLyricsLoaded() { + this->UpdateAdapter(); this->listView->ScrollTo(0); this->listView->SetSelectedIndex(0); - this->listView->SetFrameTitle(u8fmt( - _TSTR("lyrics_list_title"), - track->GetString("title").c_str(), - track->GetString("artist").c_str())); + + auto track = this->playback.GetPlaying(); + if (track) { + this->listView->SetFrameTitle(u8fmt( + _TSTR("lyrics_list_title"), + track->GetString("title").c_str(), + track->GetString("artist").c_str())); + } + this->SetState(State::Loaded); } @@ -130,27 +133,42 @@ void LyricsLayout::OnVisibilityChanged(bool visible) { } } +#include + +void LyricsLayout::ProcessMessage(musik::core::runtime::IMessage &m) { + if (m.Type() == message::LyricsLoaded) { + if ((State) m.UserData1() == State::Loaded && this->currentLyrics.size()) { + this->OnLyricsLoaded(); + } + else { + this->SetState((State) m.UserData1()); + } + } + else { + LayoutBase::ProcessMessage(m); + } +} + void LyricsLayout::LoadLyricsForCurrentTrack() { auto track = playback.GetPlaying(); if (track && track->GetId() != this->currentTrackId) { this->currentTrackId = track->GetId(); + this->currentLyrics = ""; this->SetState(State::Loading); auto trackExternalId = track->GetString("external_id"); auto lyricsDbQuery = std::make_shared(trackExternalId); this->library->Enqueue(lyricsDbQuery, [this, lyricsDbQuery, track](auto q) { auto localLyrics = lyricsDbQuery->GetResult(); if (localLyrics.size()) { - this->OnLyricsLoaded(track, localLyrics); + this->currentLyrics = localLyrics; + this->Post(message::LyricsLoaded, (int64_t) State::Loaded); } else { auddio::FindLyrics(track, [this](TrackPtr track, std::string remoteLyrics) { if (this->currentTrackId == track->GetId()) { - if (remoteLyrics.size()) { - this->OnLyricsLoaded(track, remoteLyrics); - } - else { - this->SetState(State::Failed); - } + this->currentLyrics = remoteLyrics; + auto state = remoteLyrics.size() ? State::Loaded : State::Failed; + this->Post(message::LyricsLoaded, (int64_t) state); } }); } @@ -162,8 +180,8 @@ void LyricsLayout::LoadLyricsForCurrentTrack() { } } -void LyricsLayout::UpdateAdapter(const std::string& lyrics) { - std::string fixed = lyrics; +void LyricsLayout::UpdateAdapter() { + std::string fixed = this->currentLyrics; ReplaceAll(fixed, "\r\n", "\n"); ReplaceAll(fixed, "\r", "\n"); auto items = Split(fixed, "\n"); diff --git a/src/musikcube/app/layout/LyricsLayout.h b/src/musikcube/app/layout/LyricsLayout.h index 21eb6f4aa..8538cc22d 100644 --- a/src/musikcube/app/layout/LyricsLayout.h +++ b/src/musikcube/app/layout/LyricsLayout.h @@ -20,20 +20,21 @@ namespace musik { namespace cube { musik::core::audio::PlaybackService& playback, musik::core::ILibraryPtr library); - virtual void OnLayout() override; - virtual void SetShortcutsWindow(cursespp::ShortcutsWindow* w) override; - virtual bool KeyPress(const std::string& kn) override; - virtual void OnVisibilityChanged(bool visible) override; + void OnLayout() override; + void SetShortcutsWindow(cursespp::ShortcutsWindow* w) override; + bool KeyPress(const std::string& kn) override; + void OnVisibilityChanged(bool visible) override; + void ProcessMessage(musik::core::runtime::IMessage &message) override; private: enum class State: int { NotPlaying, Loading, Loaded, Failed }; void OnTrackChanged(size_t index, musik::core::TrackPtr track); - void OnLyricsLoaded(musik::core::TrackPtr track, const std::string& lyrics); + void OnLyricsLoaded(); void SetState(State state); void LoadLyricsForCurrentTrack(); - void UpdateAdapter(const std::string& lyrics); + void UpdateAdapter(); State state { State::NotPlaying }; musik::core::ILibraryPtr library; @@ -43,6 +44,7 @@ namespace musik { namespace cube { std::shared_ptr infoText; cursespp::ShortcutsWindow* shortcuts; int64_t currentTrackId; + std::string currentLyrics; }; } } \ No newline at end of file diff --git a/src/musikcube/app/util/Messages.h b/src/musikcube/app/util/Messages.h index a419904f3..b6afbaf3d 100644 --- a/src/musikcube/app/util/Messages.h +++ b/src/musikcube/app/util/Messages.h @@ -68,6 +68,8 @@ namespace musik { static const int SetLastFmState = First + 17; static const int UpdateEqualizer = First + 18; static const int DebugLog = First + 19; + static const int LyricsLoaded = First + 20; + } } }