refs #6108 feat: add language

This commit is contained in:
Sergio De la torre 2024-02-05 08:31:01 +01:00
parent 54bff16431
commit 3b35907547
5 changed files with 43 additions and 52 deletions

View File

@ -109,5 +109,13 @@ class MobileApplication : Application(), InteceptorListener {
}
}
fun getLanguage(): String {
val settings = this.resources.configuration
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
settings.locales.get(0).language
} else {
@Suppress("DEPRECATION")
settings.locale.language
}
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context
import es.verdnatura.MobileApplication
import es.verdnatura.domain.ConstAndValues.BASEURL
import es.verdnatura.domain.ConstAndValues.BASE_URL
import es.verdnatura.presentation.common.InteceptorListener
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import retrofit2.Retrofit
@ -13,8 +14,8 @@ import java.util.concurrent.TimeUnit
class ApiUtils {
companion object {
fun getApiService(context: Context): VerdnaturaService {
var client = OkHttpClient.Builder().addInterceptor(SilexInterceptor(context))
fun getApiService(context: Context, myObserver: InteceptorListener?): VerdnaturaService {
var client = OkHttpClient.Builder().addInterceptor(SilexInterceptor(context,myObserver))
.connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(40, TimeUnit.SECONDS).build()

View File

@ -8,7 +8,7 @@ open class RestClient(context: Context, myObserver: InteceptorListener? = null)
var salixClient: SalixService
init {
restClient = ApiUtils.getApiService(context)
restClient = ApiUtils.getApiService(context, myObserver)
salixClient = ApiSalixUtils.getApiService(context, myObserver)
}
}

View File

@ -19,12 +19,12 @@ class SalixInterceptor(private val context: Context, private val listener: Intec
//sergio: condición para que añada o no headers según se vaya quitando de las llamadas
//no quitar condición hasta que estén quitados
if (request.headers().toString().isEmpty()) {
val newRequest = request.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader(
val newRequest =
request.newBuilder().addHeader("Content-Type", "application/json").addHeader(
"Authorization",
(context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN))
.build()
(context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)
).addHeader("Accept-Language", context.getLanguage()).build()
request = newRequest
}
if (request.url().toString().contains("renew")) {
@ -32,7 +32,7 @@ class SalixInterceptor(private val context: Context, private val listener: Intec
} else {
listener!!.onInterceptionResult(View.VISIBLE)
val response = chain.proceed(request)
listener!!.onInterceptionResult(View.INVISIBLE)
listener.onInterceptionResult(View.INVISIBLE)
return response
}
}

View File

@ -1,57 +1,39 @@
package es.verdnatura.domain
import android.content.Context
import android.content.SharedPreferences
import android.view.View
import es.verdnatura.MobileApplication
import es.verdnatura.domain.ConstAndValues.TOKEN
import es.verdnatura.presentation.common.InteceptorListener
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
class SilexInterceptor : Interceptor {
private var context: Context
constructor(context: Context) {
this.context = context
}
class SilexInterceptor(private val context: Context, private val listener: InteceptorListener?) :
Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
var request = chain.request()
if (request.headers().toString().isEmpty()) {
val newRequest = request.newBuilder()
.addHeader("aplicacion", "json")
.addHeader("version", "1")
.addHeader("Authorization", (context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN))
.addHeader("Content-Type", "application/json")
.build()
// d("VERDNATURA::", newRequest.headers().toString() + newRequest.url())
/* try {
(context as MainActivity).progressAnimation(View.VISIBLE)
println("print IN")
}catch (ex:Exception){}*/
/* if (request != null) {
println("Es una solicitud: ${request.method()} ${request.url()}")
}else{
val response = chain.proceed(request)
if (response != null) {
// Realizar acciones específicas para la respuesta
// Puedes acceder a response.code(), response.body(), etc.
println("Es una respuesta: ${response.code()} ${response.message()}")
val newRequest =
request.newBuilder().addHeader("aplicacion", "json").addHeader("version", "1")
.addHeader(
"Authorization",
(context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)
).addHeader("Content-Type", "application/json")
.addHeader("Accept-Language", (context).getLanguage()).build()
request = newRequest
}
}*/
return chain.proceed(newRequest)
} else
/* try {
(context as MainActivity).progressAnimation(View.INVISIBLE)
println("print OUT")
}catch (ex:Exception){}*/
if (request.url().toString().contains("renew")) {
return chain.proceed(request)
} else {
listener!!.onInterceptionResult(View.VISIBLE)
val response = chain.proceed(request)
listener.onInterceptionResult(View.INVISIBLE)
return response
}
}
}