Skip to content

Commit

Permalink
loop through list to find reachable url
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Aug 7, 2024
1 parent e0c01ad commit d5ce331
Showing 1 changed file with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.ole.planet.myplanet.ui.dashboard
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -64,25 +65,71 @@ class BellDashboardFragment : BaseDashboardFragment() {
super.onViewCreated(view, savedInstanceState)
fragmentHomeBellBinding.cardProfileBell.txtDate.text = TimeUtils.formatDate(Date().time)
fragmentHomeBellBinding.cardProfileBell.txtCommunityName.text = model?.planetCode

val serverURL = settings?.getString("serverURL", "")
val serverUrls = listOf(
"https://example.com/server1",
"https://example.com/server2",
"https://example.com/server3",
"https://example.com/server4",
"https://example.com/server5",
"https://example.com/server6",
"https://example.com/server7",
"https://example.com/server8",
serverURL,
"https://example.com/server9"
)

isNetworkConnectedFlow.onEach { isConnected ->
if (isConnected) {
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_yellow_600)
val serverUrl = settings?.getString("serverURL", "")
if (!serverUrl.isNullOrEmpty()) {
lifecycleScope.launch {

lifecycleScope.launch {
var serverReachable = false
for (serverUrl in serverUrls) {
val canReachServer = withContext(Dispatchers.IO) {
isServerReachable(serverUrl)
}

if (canReachServer) {
serverReachable = true
Log.d(TAG, "Server reachable: $serverUrl")
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.green)
break
}
}

if (!serverReachable) {
Log.d(TAG, "No server reachable")
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_yellow_600)
}
}
} else {
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_red_700)
}
}.launchIn(coroutineScope)

// isNetworkConnectedFlow.onEach { isConnected ->
// if (isConnected) {
// fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_yellow_600)
// val serverUrl = settings?.getString("serverURL", "")
// if (!serverUrl.isNullOrEmpty()) {
// lifecycleScope.launch {
// val canReachServer = withContext(Dispatchers.IO) {
// isServerReachable(serverUrl)
//
// }
// if (canReachServer) {
// Log.d(TAG, "isServerReachable: ${isServerReachable(serverUrl)}")
// fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.green)
// }
// }
// }
// } else {
// fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_red_700)
// }
// }.launchIn(coroutineScope)

(activity as DashboardActivity?)?.supportActionBar?.hide()
fragmentHomeBellBinding.addResource.setOnClickListener {
AddResourceFragment().show(childFragmentManager, getString(R.string.add_res))
Expand Down Expand Up @@ -132,8 +179,11 @@ class BellDashboardFragment : BaseDashboardFragment() {
}
}

private suspend fun isServerReachable(urlString: String): Boolean {
private suspend fun isServerReachable(urlString: String?): Boolean {
return try {
if (urlString != null) {
Log.d("ollonde", urlString)
}
val url = URL(urlString)
val connection = withContext(Dispatchers.IO) {
url.openConnection()
Expand All @@ -147,7 +197,6 @@ class BellDashboardFragment : BaseDashboardFragment() {
val responseCode = connection.responseCode
connection.disconnect()
responseCode in 200..299

} catch (e: Exception) {
false
}
Expand Down

0 comments on commit d5ce331

Please sign in to comment.