Skip to content

Commit

Permalink
Handled No Internet Condition with dialog messages and Connectivity M…
Browse files Browse the repository at this point in the history
…anager.
  • Loading branch information
debz-g committed Dec 10, 2022
1 parent c4d3d0d commit a011c61
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="dev.refox.anitrack">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/dev/refox/anitrack/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package dev.refox.anitrack

import android.app.AlertDialog
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
Expand All @@ -12,6 +18,7 @@ import dev.refox.anitrack.databinding.ActivityMainBinding
import dev.refox.anitrack.ui.AnimeDetailsBottomSheet
import dev.refox.anitrack.ui.SearchAnimeFragment
import dev.refox.anitrack.ui.WatchListFragment
import dev.refox.anitrack.utils.Snacker

class MainActivity : AppCompatActivity() {

Expand All @@ -24,6 +31,7 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


replaceFragment(SearchAnimeFragment())


Expand All @@ -39,11 +47,10 @@ class MainActivity : AppCompatActivity() {
true
}

// val navHost = findViewById<View>(R.id.navHostFragmentAnime)
// binding.bottomNav.setupWithNavController(navHost.findNavController())

}



private fun replaceFragment(fragment: Fragment){

val fragmentManager = supportFragmentManager
Expand Down
52 changes: 48 additions & 4 deletions app/src/main/java/dev/refox/anitrack/SplashActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package dev.refox.anitrack

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import dev.refox.anitrack.databinding.ActivitySplashBinding

class SplashActivity : AppCompatActivity() {
Expand All @@ -14,10 +20,48 @@ class SplashActivity : AppCompatActivity() {
window.setStatusBarColor(this.getResources().getColor(R.color.trans))
binding = ActivitySplashBinding.inflate(layoutInflater)
val view = binding.root
Handler().postDelayed({
startActivity(Intent(this, IntroActivity::class.java))
finish()
}, 2000)

if (!isNetworkAvailable() == true) {
AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Internet Connection Alert")
.setMessage("Please Check Your Internet Connection!! ")
.setPositiveButton(
"Close"
) { dialogInterface, i -> finish() }.show()
} else if (isNetworkAvailable() == true) {

Handler().postDelayed({
startActivity(Intent(this, IntroActivity::class.java))
finish()
}, 2000)

Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show()
}


setContentView(view)
}

private fun isNetworkAvailable(): Boolean
{
val connectivityManager =
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (connectivityManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return true
}
}
}
}
return false
}
}

0 comments on commit a011c61

Please sign in to comment.