KokoBand
KokoBand

Reputation: 229

How to use context in Kotlin classes on Android

In my application i have use Retrofit and okHttpClient for get some request to server.
In this retrofit config i want send some data into Header to server.
This data is device UUID and for get device UUID i write below code in one class (this class name is Extensions) :

@SuppressLint("HardwareIds")
fun Context.getDeviceUUID(): String {
    return try {
        Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
    } catch (e: Exception) {
        "exception"
    }
}

For get device UUID i should pass context.

And i want send this device UUID into ApiClient class.
ApiClient class :

class ApiClient {

    private val apiServices: ApiServices

    init {
        //Gson
        val gson = GsonBuilder()
            .setLenient()
            .create()

        //Http log
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level =
            if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

        //Http Builder
        val clientBuilder = OkHttpClient.Builder()
        clientBuilder.interceptors().add(loggingInterceptor)
        clientBuilder.addInterceptor { chain ->
            val request = chain.request()
            request.newBuilder().addHeader("uuid", ).build()
            chain.proceed(request)
        }

        //Http client
        val client = clientBuilder
            .readTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .writeTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .connectTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build()

        //Retrofit
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()

        //Init apiServices
        apiServices = retrofit.create(ApiServices::class.java)
    }

    companion object {
        private var apiClient: ApiClient? = null

        val instance: ApiClient
            get() {
                if (apiClient == null) {
                    apiClient = ApiClient()
                }
                return apiClient as ApiClient
            }
    }
}

I should use getDeviceUUID in this code : request.newBuilder().addHeader("uuid", ).build()

how can i pass context into ApiClient class ?

Upvotes: 1

Views: 1316

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21073

You need to modify ApiClient have a constructor and a parameter in get().

class APiClient(var deviceId: String) {
init {
    //Stuff goes here
}
companion object {
    private var apiClient: APiClient? = null
    fun getInstance(deviceId: String): APiClient =
            apiClient ?: synchronized(this) {
                apiClient ?: APiClient(deviceId).also {
                    apiClient = it
                }
            }
}
}

PS, deviceUUID can be Global cause it will not change. A better approach will be just let it be in Global Access like in Application class . From where you can directly use it . No need to pass it in constructor each time .

Upvotes: 1

Related Questions