diff --git a/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerListUi.kt b/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerListUi.kt index 670311d0..1fb5828d 100644 --- a/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerListUi.kt +++ b/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerListUi.kt @@ -59,8 +59,15 @@ fun SpotiFlyerListContent( ) { val model by component.models.collectAsState(SpotiFlyerList.State()) + LaunchedEffect(model.errorOccurred) { + /*Handle if Any Exception Occurred*/ + model.errorOccurred?.let { + showPopUpMessage(it.message ?: "An Error Occurred, Check your Link / Connection") + component.onBackPressed() + } + } + Box(modifier = modifier.fillMaxSize()) { - // TODO Better Null Handling val result = model.queryResult if (result == null) { /* Loading Bar */ @@ -69,14 +76,6 @@ fun SpotiFlyerListContent( Spacer(modifier.padding(8.dp)) Text("Loading..", style = appNameStyle, color = colorPrimary) } - LaunchedEffect(Unit) { - delay(350) - /*Handle if Any Exception Occurred*/ - model.errorOccurred?.let { - showPopUpMessage(it.message ?: "An Error Occurred, Check your Link / Connection") - component.onBackPressed() - } - } } else { LazyColumn( verticalArrangement = Arrangement.spacedBy(12.dp), diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt index d5da8daa..5b1e30ca 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt @@ -46,10 +46,14 @@ class GaanaProvider( if (type == "Error" || link == "Error") { return null } - return gaanaSearch( - type, - link - ) + return try { + gaanaSearch( + type, + link + ) + } catch (e: Exception) { + null + } } private suspend fun gaanaSearch( diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt index 071ba1d7..800998ee 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt @@ -100,10 +100,24 @@ class SpotifyProvider( return null } - return spotifySearch( - type, - link - ) + return try { + spotifySearch( + type, + link + ) + }catch (e: Exception){ + // Try Reinitialising Client // Handle 401 Token Expiry ,etc Exceptions + authenticateSpotifyClient(true) + // Retry Search + try { + spotifySearch( + type, + link + ) + } catch (e:Exception){ + null + } + } } private suspend fun spotifySearch( diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMp3.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMp3.kt index 38856d00..d2cf0cf3 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMp3.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMp3.kt @@ -28,11 +28,15 @@ class YoutubeMp3( override val logger: Kermit, private val dir: Dir, ) : Yt1sMp3 { - suspend fun getMp3DownloadLink(videoID: String): String? = getLinkFromYt1sMp3(videoID)?.let { - logger.i { "Download Link: $it" } - if (currentPlatform is AllPlatforms.Js/* && corsProxy !is CorsProxy.PublicProxyWithExtension*/) - "https://kind-grasshopper-73.telebit.io/cors/$it" - // "https://spotiflyer.azurewebsites.net/$it" // Data OUT Limit issue - else it + suspend fun getMp3DownloadLink(videoID: String): String? = try { + getLinkFromYt1sMp3(videoID)?.let { + logger.i { "Download Link: $it" } + if (currentPlatform is AllPlatforms.Js/* && corsProxy !is CorsProxy.PublicProxyWithExtension*/) + "https://kind-grasshopper-73.telebit.io/cors/$it" + // "https://spotiflyer.azurewebsites.net/$it" // Data OUT Limit issue + else it + } + } catch (e: Exception) { + null } } diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMusic.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMusic.kt index 9cf1eb05..141eb306 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMusic.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeMusic.kt @@ -47,12 +47,17 @@ class YoutubeMusic constructor( private val tag = "YT Music" suspend fun getYTIDBestMatch(query: String, trackDetails: TrackDetails): String? { - return sortByBestMatch( - getYTTracks(query), - trackName = trackDetails.title, - trackArtists = trackDetails.artists, - trackDurationSec = trackDetails.durationSec - ).keys.firstOrNull() + return try { + sortByBestMatch( + getYTTracks(query), + trackName = trackDetails.title, + trackArtists = trackDetails.artists, + trackDurationSec = trackDetails.durationSec + ).keys.firstOrNull() + } catch (e:Exception) { + // All Internet/Client Related Errors + null + } } private suspend fun getYTTracks(query: String): List { val youtubeTracks = mutableListOf() diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt index 956a0772..39dad364 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt @@ -28,9 +28,13 @@ import io.ktor.client.request.post import io.ktor.http.Parameters suspend fun authenticateSpotify(): TokenData? { - return if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") { - body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") }) - } else null + return try { + if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") { + body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") }) + } else null + }catch (e:Exception) { + null + } } private val spotifyAuthClient by lazy {