I have two classes. First is MainActivity.kt, where I have a some string. Second is a ServiceBuilder.kt like this
object ServiceBuilder {
private const val URL = "https://someLink.com" // <------------- In here I should transfer the string in my MainActivity.kt class
// Create Logger
private val logger = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
// Create a Custom Interceptor to apply Headers application wide
val headerInterceptor = object: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
request = request.newBuilder()
.addHeader("x-device-type", Build.DEVICE)
.addHeader("Accept-Language", Locale.getDefault().language)
.build()
val response = chain.proceed(request)
return response
}
}
// Create OkHttp Client
private val okHttp = OkHttpClient.Builder()
.callTimeout(5, TimeUnit.SECONDS)
.addInterceptor(headerInterceptor)
.addInterceptor(logger)
// Create Retrofit Builder
private val builder = Retrofit.Builder().baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttp.build())
// Create Retrofit Instance
private val retrofit = builder.build()
fun <T> buildService(serviceType: Class<T>): T {
return retrofit.create(serviceType)
}
}
I need rightly transfer string from MainActivity to this object. I think creating MainActivity object inside ServiceBuilder is't good idea, because it allocate a superfluous memory. Using database or SharedPreferences is't good idea too, because this calls will slow down working program and this technologies was created for solving other problems.
Please tell my right way to solving this problem P.S. I now then this question is easy, but I don't now how to design app architecture