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

View File

@ -8,7 +8,7 @@ open class RestClient(context: Context, myObserver: InteceptorListener? = null)
var salixClient: SalixService var salixClient: SalixService
init { init {
restClient = ApiUtils.getApiService(context) restClient = ApiUtils.getApiService(context, myObserver)
salixClient = ApiSalixUtils.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 //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 //no quitar condición hasta que estén quitados
if (request.headers().toString().isEmpty()) { if (request.headers().toString().isEmpty()) {
val newRequest = request.newBuilder() val newRequest =
.addHeader("Content-Type", "application/json") request.newBuilder().addHeader("Content-Type", "application/json").addHeader(
.addHeader(
"Authorization", "Authorization",
(context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)) (context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)
.build() ).addHeader("Accept-Language", context.getLanguage()).build()
request = newRequest request = newRequest
} }
if (request.url().toString().contains("renew")) { if (request.url().toString().contains("renew")) {
@ -32,7 +32,7 @@ class SalixInterceptor(private val context: Context, private val listener: Intec
} else { } else {
listener!!.onInterceptionResult(View.VISIBLE) listener!!.onInterceptionResult(View.VISIBLE)
val response = chain.proceed(request) val response = chain.proceed(request)
listener!!.onInterceptionResult(View.INVISIBLE) listener.onInterceptionResult(View.INVISIBLE)
return response return response
} }
} }

View File

@ -1,57 +1,39 @@
package es.verdnatura.domain package es.verdnatura.domain
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.view.View
import es.verdnatura.MobileApplication import es.verdnatura.MobileApplication
import es.verdnatura.domain.ConstAndValues.TOKEN import es.verdnatura.domain.ConstAndValues.TOKEN
import es.verdnatura.presentation.common.InteceptorListener
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import java.io.IOException import java.io.IOException
class SilexInterceptor(private val context: Context, private val listener: InteceptorListener?) :
class SilexInterceptor : Interceptor { Interceptor {
private var context: Context
constructor(context: Context) {
this.context = context
}
@Throws(IOException::class) @Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request() var request = chain.request()
if (request.headers().toString().isEmpty()) { if (request.headers().toString().isEmpty()) {
val newRequest = request.newBuilder()
.addHeader("aplicacion", "json") val newRequest =
.addHeader("version", "1") request.newBuilder().addHeader("aplicacion", "json").addHeader("version", "1")
.addHeader("Authorization", (context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)) .addHeader(
.addHeader("Content-Type", "application/json") "Authorization",
.build() (context as MobileApplication).dataStoreApp.readDataStoreKey<String>(TOKEN)
// d("VERDNATURA::", newRequest.headers().toString() + newRequest.url()) ).addHeader("Content-Type", "application/json")
/* try { .addHeader("Accept-Language", (context).getLanguage()).build()
(context as MainActivity).progressAnimation(View.VISIBLE) request = newRequest
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()}")
} }
}*/ if (request.url().toString().contains("renew")) {
return chain.proceed(newRequest)
} else
/* try {
(context as MainActivity).progressAnimation(View.INVISIBLE)
println("print OUT")
}catch (ex:Exception){}*/
return chain.proceed(request) return chain.proceed(request)
} else {
listener!!.onInterceptionResult(View.VISIBLE)
val response = chain.proceed(request)
listener.onInterceptionResult(View.INVISIBLE)
return response
}
} }
} }