diff --git a/app/src/main/java/es/verdnatura/MobileApplication.kt b/app/src/main/java/es/verdnatura/MobileApplication.kt index 364d79ea..d26c330f 100644 --- a/app/src/main/java/es/verdnatura/MobileApplication.kt +++ b/app/src/main/java/es/verdnatura/MobileApplication.kt @@ -51,7 +51,7 @@ class MobileApplication : Application(), InteceptorListener { var androidId: String = "" var mperror: MediaPlayer? = null var mpok: MediaPlayer? = null - var dataStoreApp= DataStoreLocal(this) + var dataStoreApp = DataStoreLocal(this) private lateinit var customDialog: CustomDialogMainActivity private val handler = Handler(Looper.getMainLooper()) @@ -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 + } + } } \ No newline at end of file diff --git a/app/src/main/java/es/verdnatura/domain/ApiUtils.kt b/app/src/main/java/es/verdnatura/domain/ApiUtils.kt index 67b89068..ff8300e0 100644 --- a/app/src/main/java/es/verdnatura/domain/ApiUtils.kt +++ b/app/src/main/java/es/verdnatura/domain/ApiUtils.kt @@ -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() diff --git a/app/src/main/java/es/verdnatura/domain/RestClient.kt b/app/src/main/java/es/verdnatura/domain/RestClient.kt index 821d541b..b648b40b 100644 --- a/app/src/main/java/es/verdnatura/domain/RestClient.kt +++ b/app/src/main/java/es/verdnatura/domain/RestClient.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/java/es/verdnatura/domain/SalixInterceptor.kt b/app/src/main/java/es/verdnatura/domain/SalixInterceptor.kt index 4e392204..9b6d99b2 100644 --- a/app/src/main/java/es/verdnatura/domain/SalixInterceptor.kt +++ b/app/src/main/java/es/verdnatura/domain/SalixInterceptor.kt @@ -19,20 +19,20 @@ 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( - "Authorization", - (context as MobileApplication).dataStoreApp.readDataStoreKey(TOKEN)) - .build() - request = newRequest + val newRequest = + request.newBuilder().addHeader("Content-Type", "application/json").addHeader( + "Authorization", + (context as MobileApplication).dataStoreApp.readDataStoreKey(TOKEN) + ).addHeader("Accept-Language", context.getLanguage()).build() + + request = newRequest } if (request.url().toString().contains("renew")) { return chain.proceed(request) } else { listener!!.onInterceptionResult(View.VISIBLE) val response = chain.proceed(request) - listener!!.onInterceptionResult(View.INVISIBLE) + listener.onInterceptionResult(View.INVISIBLE) return response } } diff --git a/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt b/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt index ce835337..6b7ed34f 100644 --- a/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt +++ b/app/src/main/java/es/verdnatura/domain/SilexInterceptor.kt @@ -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(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()}") - } - }*/ - - return chain.proceed(newRequest) - } else - /* try { - (context as MainActivity).progressAnimation(View.INVISIBLE) - println("print OUT") - }catch (ex:Exception){}*/ + val newRequest = + request.newBuilder().addHeader("aplicacion", "json").addHeader("version", "1") + .addHeader( + "Authorization", + (context as MobileApplication).dataStoreApp.readDataStoreKey(TOKEN) + ).addHeader("Content-Type", "application/json") + .addHeader("Accept-Language", (context).getLanguage()).build() + request = newRequest + } + 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 + } } }