From e978f541d7a00c847b0d0c6544965d697517ca0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Font=C3=A1n?= Date: Fri, 15 Nov 2024 17:12:05 +0100 Subject: [PATCH] fix: Error handling has been added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gabriel Fontán --- .../viewmodel/QuickLyricsSearchViewModel.kt | 82 ++++++++++++++----- .../lyrics_providers/LyricsProviderService.kt | 40 +++++---- .../songsync/ui/screens/home/HomeViewModel.kt | 20 +++-- 3 files changed, 91 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/pl/lambada/songsync/activities/quicksearch/viewmodel/QuickLyricsSearchViewModel.kt b/app/src/main/java/pl/lambada/songsync/activities/quicksearch/viewmodel/QuickLyricsSearchViewModel.kt index 6055c15..29661c4 100644 --- a/app/src/main/java/pl/lambada/songsync/activities/quicksearch/viewmodel/QuickLyricsSearchViewModel.kt +++ b/app/src/main/java/pl/lambada/songsync/activities/quicksearch/viewmodel/QuickLyricsSearchViewModel.kt @@ -1,7 +1,6 @@ package pl.lambada.songsync.activities.quicksearch.viewmodel import android.content.Context -import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers @@ -9,6 +8,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import pl.lambada.songsync.R import pl.lambada.songsync.data.UserSettingsController import pl.lambada.songsync.data.remote.lyrics_providers.LyricsProviderService import pl.lambada.songsync.domain.model.SongInfo @@ -33,38 +33,76 @@ class QuickLyricsSearchViewModel( private fun fetchSongData(song: Pair, context: Context) { updateScreenState(ScreenState.Loading) + viewModelScope.launch(Dispatchers.IO) { - val result = lyricsProviderService - .getSongInfo( - query = SongInfo(song.first, song.second), - offset = 0, - provider = userSettingsController.selectedProvider - ) - ?: return@launch updateScreenState(ScreenState.Error(Exception("Error fetching lyrics for the song."))) + val songInfoCall = runCatching { + lyricsProviderService + .getSongInfo( + query = SongInfo(song.first, song.second), + offset = 0, + provider = userSettingsController.selectedProvider + ) + } - updateScreenState(ScreenState.Success(result)) - fetchLyrics(result.songLink, context) + if (songInfoCall.isSuccess) { + val result = songInfoCall.getOrNull() + + if (result == null) { + updateScreenState( + ScreenState.Error( + Exception("The song information retrieved is null") + ) + ) + } else { + updateScreenState(ScreenState.Success(result)) + fetchLyrics(result.songLink, context) + } + + } else { + val exception = songInfoCall.exceptionOrNull() + updateScreenState( + ScreenState.Error( + exception ?: Exception("An unknown error has occurred") + ) + ) + } } } private fun fetchLyrics(songLink: String?, context: Context) { updateLyricsState(ResourceState.Loading()) viewModelScope.launch(Dispatchers.IO) { - val syncedLyrics = getSyncedLyrics( - link = songLink, - version = context.getVersion() - ) - - if (syncedLyrics != null) { - updateLyricsState(ResourceState.Success(syncedLyrics)) - parseLyrics(syncedLyrics).let { parsedLyrics -> - Log.d("QuickLyricsSearchVM", "Parsed lyrics: $parsedLyrics") - mutableState.update { - it.copy(parsedLyrics = parsedLyrics) + + val lyricsCall = runCatching { + getSyncedLyrics( + link = songLink, + version = context.getVersion() + ) + } + + if (lyricsCall.isSuccess) { + + val syncedLyrics = lyricsCall.getOrNull() + + if (syncedLyrics == null) updateLyricsState( + ResourceState.Error("The fetched lyrics content is null.") + ) else { + updateLyricsState(ResourceState.Success(syncedLyrics)) + parseLyrics(syncedLyrics).let { parsedLyrics -> + mutableState.update { + it.copy(parsedLyrics = parsedLyrics) + } } } + } else { - updateLyricsState(ResourceState.Error("Error fetching lyrics for the song.")) + val exception = lyricsCall.exceptionOrNull() + updateLyricsState( + ResourceState.Error( + exception?.localizedMessage + ?: (context.getString(R.string.unknown) + exception?.stackTrace.toString()) + ) + ) } } } diff --git a/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/LyricsProviderService.kt b/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/LyricsProviderService.kt index 53a129b..53e99a2 100644 --- a/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/LyricsProviderService.kt +++ b/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/LyricsProviderService.kt @@ -49,8 +49,11 @@ class LyricsProviderService { * @return The SongInfo object containing the song information. */ @Throws( - UnknownHostException::class, FileNotFoundException::class, NoTrackFoundException::class, - EmptyQueryException::class, InternalErrorException::class + UnknownHostException::class, + FileNotFoundException::class, + NoTrackFoundException::class, + EmptyQueryException::class, + InternalErrorException::class ) suspend fun getSongInfo(query: SongInfo, offset: Int = 0, provider: Providers): SongInfo? { return try { @@ -97,25 +100,20 @@ class LyricsProviderService { multiPersonWordByWord: Boolean = false, syncedMusixmatch: Boolean = true ): String? { - return try { - when (provider) { - Providers.SPOTIFY -> SpotifyLyricsAPI().getSyncedLyrics(songLink!!, version) - Providers.LRCLIB -> LRCLibAPI().getSyncedLyrics(lrcLibID) - Providers.NETEASE -> NeteaseAPI().getSyncedLyrics( - neteaseID, - includeTranslationNetEase - ) - Providers.APPLE -> AppleAPI().getSyncedLyrics( - appleID, - multiPersonWordByWord - ) - Providers.MUSIXMATCH -> MusixmatchAPI().getLyrics( - musixmatchSongInfo, - syncedMusixmatch - ) - } - } catch (e: Exception) { - null + return when (provider) { + Providers.SPOTIFY -> SpotifyLyricsAPI().getSyncedLyrics(songLink!!, version) + Providers.LRCLIB -> LRCLibAPI().getSyncedLyrics(lrcLibID) + Providers.NETEASE -> NeteaseAPI().getSyncedLyrics( + neteaseID, includeTranslationNetEase + ) + + Providers.APPLE -> AppleAPI().getSyncedLyrics( + appleID, multiPersonWordByWord + ) + + Providers.MUSIXMATCH -> MusixmatchAPI().getLyrics( + musixmatchSongInfo, syncedMusixmatch + ) } } } \ No newline at end of file diff --git a/app/src/main/java/pl/lambada/songsync/ui/screens/home/HomeViewModel.kt b/app/src/main/java/pl/lambada/songsync/ui/screens/home/HomeViewModel.kt index 14e6af9..8fd34e4 100644 --- a/app/src/main/java/pl/lambada/songsync/ui/screens/home/HomeViewModel.kt +++ b/app/src/main/java/pl/lambada/songsync/ui/screens/home/HomeViewModel.kt @@ -276,14 +276,18 @@ class HomeViewModel( lyricsProviderService.getSongInfo(query, provider = userSettingsController.selectedProvider) suspend fun getSyncedLyrics(link: String?, version: String): String? { - return lyricsProviderService.getSyncedLyrics( - link, - version, - provider = userSettingsController.selectedProvider, - includeTranslationNetEase = userSettingsController.includeTranslation, - multiPersonWordByWord = userSettingsController.multiPersonWordByWord, - syncedMusixmatch = userSettingsController.syncedMusixmatch - ) + return try { + lyricsProviderService.getSyncedLyrics( + link, + version, + provider = userSettingsController.selectedProvider, + includeTranslationNetEase = userSettingsController.includeTranslation, + multiPersonWordByWord = userSettingsController.multiPersonWordByWord, + syncedMusixmatch = userSettingsController.syncedMusixmatch + ) + } catch (e: Exception) { + null + } } fun selectSong(song: Song, newValue: Boolean) {