From d5ce3317597bc020afaf34013f0ae9003ee6f9d0 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Thu, 13 Jun 2024 17:50:44 +0300 Subject: [PATCH] loop through list to find reachable url --- .../ui/dashboard/BellDashboardFragment.kt | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BellDashboardFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BellDashboardFragment.kt index f2b794a476..53b24ad257 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BellDashboardFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BellDashboardFragment.kt @@ -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 @@ -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)) @@ -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() @@ -147,7 +197,6 @@ class BellDashboardFragment : BaseDashboardFragment() { val responseCode = connection.responseCode connection.disconnect() responseCode in 200..299 - } catch (e: Exception) { false }