title: Android Pass Context in object
body: <p>I need to pass the Application Context to an object. This is to call a function to intercept in the okhttpClient, my connection service. I'am using an object class to build the connection to the server with retrofit and an external interface with my services.</p>
  
  <pre><code>object DfreeApiService  {
  
     private lateinit var interceptor: HttpLoggingInterceptor
     private lateinit var okHttpClient: OkHttpClient
     private lateinit var networkConnectivityInterceptor: NetworkConnectivityInterceptor
  
     val client: Retrofit
     get() {
         networkConnectivityInterceptor= NetworkConnectivityInterceptor() //cannot pass the context here
         interceptor = HttpLoggingInterceptor()
         interceptor.level = HttpLoggingInterceptor.Level.BODY
         okHttpClient= OkHttpClient.Builder()
            // .addInterceptor(networkConnectivityInterceptor)
             .addInterceptor(interceptor)
             .connectionSpecs(
                 Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT)
             )
             .followRedirects(true)
             .followSslRedirects(true)
             .retryOnConnectionFailure(true)
             .connectTimeout(20, TimeUnit.SECONDS)
             .readTimeout(20, TimeUnit.SECONDS)
             .writeTimeout(20, TimeUnit.SECONDS)
             .cache(null)
             .build()
  
             val retrofit: Retrofit =Retrofit.Builder()
                 .client(okHttpClient)
                 .baseUrl("http://example:2000/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build()
  
         return retrofit
  
     }
  
  }
  </code></pre>
  
  <p>And in the following code is my <strong>interface:</strong></p>
  
  <pre><code>interface AuthenticationApi {
  
      @POST("ws/login")
      suspend fun login(@Body userInfo: UserInfo): Response&lt;UserReponse&gt;
  
      @POST("ws/register")
      suspend fun register(@Body userInfo: UserRegister): Response&lt;UserReponse&gt;
  
      @POST("ws/pw_recover")
      suspend fun pw_recover(@Body email: String): Response&lt;BaseResponse&gt;
  
      @POST("ws/login")
      suspend fun loginGoogle(@Body user: GoogleUser) : Response&lt;UserReponse&gt;
  
  
      companion object{
          operator fun invoke():AuthenticationApi{
              return DfreeApiService.client.create(AuthenticationApi::class.java)
          }
      }
    }
  </code></pre>
  
  <p>I'am using mvvm and kodein.
  Without the internet interceptor the app crashes when there is no internet.
  <strong>My interceptor:</strong></p>
  
  <pre><code>    class NetworkConnectivityInterceptor(
      context: Context
      ) : Interceptor {
  
      private val applicationContext = context
  
      @RequiresApi(Build.VERSION_CODES.M)
      override fun intercept(chain: Interceptor.Chain): Response {
          if (!isInternetAvailable())
              throw NoInternetException("Make sure you have an active data connection")
          return chain.proceed(chain.request())
      }
  
      @RequiresApi(Build.VERSION_CODES.M)
      private fun isInternetAvailable(): Boolean {
          var result = false
          val connectivityManager =
              applicationContext.getSystemService(Application.CONNECTIVITY_SERVICE) as ConnectivityManager?
          connectivityManager?.let {
              it.getNetworkCapabilities(connectivityManager.activeNetwork)?.apply {
                  result = when {
                      hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -&gt; true
                      hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -&gt; true
                      else -&gt; false
                  }
  
              }
          }
          return result
      }
  }
  </code></pre>
  
  <p>Thanks for your attention</p>