mirror of
https://github.com/Shabinder/SpotiFlyer.git
synced 2024-11-22 17:14:32 +01:00
No Internet Connection Fix
This commit is contained in:
parent
e2de7e9f6a
commit
289ada76b0
@ -27,13 +27,10 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.State
|
import androidx.compose.runtime.State
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import com.shabinder.common.database.appContext
|
import com.shabinder.common.database.appContext
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.lang.Exception
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import javax.net.SocketFactory
|
|
||||||
|
|
||||||
const val TAG = "C-Manager"
|
const val TAG = "C-Manager"
|
||||||
|
|
||||||
@ -89,7 +86,7 @@ class ConnectionLiveData(context: Context = appContext) : LiveData<Boolean>() {
|
|||||||
if (hasInternetCapability == true) {
|
if (hasInternetCapability == true) {
|
||||||
// check if this network actually has internet
|
// check if this network actually has internet
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val hasInternet = DoesNetworkHaveInternet.execute(network.socketFactory)
|
val hasInternet = DoesNetworkHaveInternet.execute(network)
|
||||||
if (hasInternet) {
|
if (hasInternet) {
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
Log.d(TAG, "onAvailable: adding network. $network")
|
Log.d(TAG, "onAvailable: adding network. $network")
|
||||||
@ -117,19 +114,18 @@ class ConnectionLiveData(context: Context = appContext) : LiveData<Boolean>() {
|
|||||||
* If successful, that means we have internet.
|
* If successful, that means we have internet.
|
||||||
*/
|
*/
|
||||||
object DoesNetworkHaveInternet {
|
object DoesNetworkHaveInternet {
|
||||||
|
suspend fun execute(network: Network): Boolean = withContext(Dispatchers.IO) {
|
||||||
// Make sure to execute this on a background thread.
|
try {
|
||||||
fun execute(socketFactory: SocketFactory): Boolean {
|
|
||||||
return try {
|
|
||||||
Log.d(TAG, "PINGING google.")
|
Log.d(TAG, "PINGING google.")
|
||||||
val socket = socketFactory.createSocket() ?: throw IOException("Socket is null.")
|
val socket = network.socketFactory.createSocket() ?: throw IOException("Socket is null.")
|
||||||
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500)
|
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500)
|
||||||
socket.close()
|
socket.close()
|
||||||
Log.d(TAG, "PING success.")
|
Log.d(TAG, "PING success.")
|
||||||
true
|
true
|
||||||
} catch (e: IOException) {
|
}catch (e:Exception){
|
||||||
Log.e(TAG, "No internet connection. $e")
|
e.printStackTrace()
|
||||||
false
|
// Handle VPN Connection / Google DNS Blocked Cases
|
||||||
|
isInternetAccessible()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ package com.shabinder.common.di
|
|||||||
|
|
||||||
import com.shabinder.common.models.TrackDetails
|
import com.shabinder.common.models.TrackDetails
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import com.mp3.jaudiotagger.tag.images.ArtworkFactory
|
|
||||||
import com.mpatric.mp3agic.ID3v1Tag
|
import com.mpatric.mp3agic.ID3v1Tag
|
||||||
import com.mpatric.mp3agic.ID3v24Tag
|
import com.mpatric.mp3agic.ID3v24Tag
|
||||||
import com.mpatric.mp3agic.Mp3File
|
import com.mpatric.mp3agic.Mp3File
|
||||||
|
@ -18,7 +18,9 @@ package com.shabinder.common.di
|
|||||||
|
|
||||||
import com.shabinder.common.models.AllPlatforms
|
import com.shabinder.common.models.AllPlatforms
|
||||||
import com.shabinder.common.models.TrackDetails
|
import com.shabinder.common.models.TrackDetails
|
||||||
|
import io.ktor.client.request.*
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
expect fun openPlatform(packageID: String, platformLink: String)
|
expect fun openPlatform(packageID: String, platformLink: String)
|
||||||
|
|
||||||
@ -39,3 +41,18 @@ expect suspend fun downloadTracks(
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect fun queryActiveTracks()
|
expect fun queryActiveTracks()
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Refactor This
|
||||||
|
* */
|
||||||
|
suspend fun isInternetAccessible(): Boolean {
|
||||||
|
return withContext(dispatcherIO) {
|
||||||
|
try {
|
||||||
|
ktorHttpClient.head<String>("http://google.com")
|
||||||
|
true
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -61,15 +61,14 @@ class SpotifyProvider(
|
|||||||
null
|
null
|
||||||
} else {
|
} else {
|
||||||
logger.d { "Spotify Provider Created with $token" }
|
logger.d { "Spotify Provider Created with $token" }
|
||||||
httpClient = HttpClient {
|
HttpClient {
|
||||||
defaultRequest {
|
defaultRequest {
|
||||||
header("Authorization", "Bearer ${token.access_token}")
|
header("Authorization", "Bearer ${token.access_token}")
|
||||||
}
|
}
|
||||||
install(JsonFeature) {
|
install(JsonFeature) {
|
||||||
serializer = kotlinxSerializer
|
serializer = kotlinxSerializer
|
||||||
}
|
}
|
||||||
}
|
}?.also { httpClient = it }
|
||||||
httpClient
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,25 +47,10 @@ actual fun giveDonation() {
|
|||||||
|
|
||||||
actual fun queryActiveTracks() {}
|
actual fun queryActiveTracks() {}
|
||||||
|
|
||||||
/*
|
|
||||||
* Refactor This
|
|
||||||
* */
|
|
||||||
private suspend fun isInternetAvailable(): Boolean {
|
|
||||||
return withContext(dispatcherIO) {
|
|
||||||
try {
|
|
||||||
ktorHttpClient.head<String>("http://google.com")
|
|
||||||
true
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println(e.message)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
actual val isInternetAvailable: Boolean
|
actual val isInternetAvailable: Boolean
|
||||||
get() {
|
get() {
|
||||||
var result = false
|
var result = false
|
||||||
val job = GlobalScope.launch { result = isInternetAvailable() }
|
val job = GlobalScope.launch { result = isInternetAccessible() }
|
||||||
while (job.isActive) {}
|
while (job.isActive) {}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user