4

I have an apiClass which makes all the network calls and I was thinking my options are these:

  1. do the check inside the activity/fragment

  2. do the check inside the apiClient class

I'm sure there is a better alternative.

EDIT

This answer suggests my second option. Is there a better approach?

DoruChidean
  • 7,501
  • 1
  • 28
  • 32

2 Answers2

3

You can add method for internet connection anywhere but as per my understanding,for code/method method reuse you can create Util class or You can add it into the Application class. You can also refer below method.

public static boolean getConnectionStatus(Context context) {
    ConnectivityManager mConnectivityManager;
    NetworkInfo mNetworkInfoMobile, mNetworkInfoWifi;

    mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    mNetworkInfoMobile = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    mNetworkInfoWifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    try {
        if (mNetworkInfoMobile.isConnected()) {
            App.connectivityStatus = 1;

            return true;
        }
    } catch (Exception exception) {
        // exception.printStackTrace();
    }

    if (mNetworkInfoWifi.isConnected()) {
        App.connectivityStatus = 1;

        return true;
    } else {
        App.connectivityStatus = 0;
        return false;
    }
}
samit
  • 170
  • 4
  • 1
    mvvm design pattern you can follow below links https://barta.me/android-mvvm-pattern/ https://medium.com/upday-devs/android-architecture-patterns-part-3-model-view-viewmodel-e7eeee76b73b – samit Jul 04 '17 at 09:41
  • This should in a a service layer because the ViewModel shouldn't care whether or not you're connected other than showing an icon. Nor should it be a static method. – Nick Turner Jan 14 '21 at 21:28
0

create class connectivity manager in livedata

class Connectivity_check_internet(val context: Context) : LiveData<Boolean>() {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    var Connnectserver: Boolean? = false
    val reciveconnctivitymanager = object : BroadcastReceiver() {
        override fun onReceive(p0: Context?, p1: Intent?) {
            Connnectserver=false
            UnpdateConnection()
            postValue(Connnectserver)
        }
    }
    init {
        UnpdateConnection()
    }

    fun UnpdateConnection(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
            capabilities?.run {
                Connnectserver=capabilities!!.hasTransport(TRANSPORT_CELLULAR) || capabilities.hasTransport(TRANSPORT_WIFI)
            }
        } else {
            try {
                val activeNetworkInfo = connectivityManager.activeNetworkInfo
                Connnectserver = activeNetworkInfo != null && activeNetworkInfo.isConnected
            } catch (e: Exception) {
            }
        }
    }

    override fun onActive() {
        super.onActive()
        context.registerReceiver(
            reciveconnctivitymanager,
            IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
        )
    }

    override fun onInactive() {
        super.onInactive()
        context.unregisterReceiver(reciveconnctivitymanager)
    }
}

class MainActivity : AppCompatActivity() { val Connectivity_internet by lazy { Connectivity_check_internet(this) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Connectivity_internet.observe(this,{
        Log.e("TAG", "onCreate: "+it )
    })

}

}

developerjavad
  • 262
  • 1
  • 2
  • 9