Skip to content
Snippets Groups Projects
Commit 86896b70 authored by Akmal Mahardika Nurwahyu Pratama's avatar Akmal Mahardika Nurwahyu Pratama
Browse files

fix: bug when connection is lost

parent cdfac966
No related merge requests found
Pipeline #61534 failed with stages
......@@ -2,6 +2,8 @@ package com.example.myapplication
import android.annotation.SuppressLint
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.AnimationUtils
......@@ -29,17 +31,39 @@ class SplashScreenActivity : AppCompatActivity() {
Handler(Looper.getMainLooper()).postDelayed({
lifecycleScope.launch {
if (authRepository.checkToken()) {
Log.d("SplashScreenActivity", "Token is valid, going to MainActivity")
if (!isNetworkAvailable()) {
Log.d("SplashScreenActivity", "No network available")
startActivity(Intent(this@SplashScreenActivity, MainActivity::class.java))
startService(Intent(this@SplashScreenActivity, JWTAuthChecker::class.java))
} else {
Log.d("SplashScreenActivity", "Token is invalid or not found, going to LoginActivity")
startActivity(Intent(this@SplashScreenActivity, LoginActivity::class.java))
val isTokenValid = authRepository.checkToken()
Log.d("SplashScreenActivity", "Token is valid: $isTokenValid")
if (!isTokenValid) {
val intent = Intent(this@SplashScreenActivity, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
} else {
val jwtIntent = Intent(this@SplashScreenActivity, JWTAuthChecker::class.java)
startService(jwtIntent)
val mainIntent = Intent(this@SplashScreenActivity, MainActivity::class.java)
startActivity(mainIntent)
}
}
finish()
}
}, 3000)
}
private fun isNetworkAvailable(): Boolean {
val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork ?: return false
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
return when {
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment