6

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,941
  • 1
  • 29
  • 33

3 Answers3

2

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
  • 168
  • 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
  • The question is not where to define the method. But the question is where to use it. – Hussein Yaqoobi Jul 05 '22 at 10:47
1

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
  • 315
  • 1
  • 2
  • 10
0

I think we should avoid using Context in ViewModel and data layer as much as possible. Therefore, the best place to check the Internet is where we have access to the Context, for example, in Activity or Fragment.

Hussein Yaqoobi
  • 442
  • 5
  • 20