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 Jun 13, 2024
1 parent 94d04db commit 5738aae
Showing 1 changed file with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.ole.planet.myplanet.ui.dashboard

import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -58,25 +59,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 @@ -118,8 +165,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 @@ -133,7 +183,6 @@ class BellDashboardFragment : BaseDashboardFragment() {
val responseCode = connection.responseCode
connection.disconnect()
responseCode in 200..299

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

0 comments on commit 5738aae

Please sign in to comment.