refs #6108 feat: add language
This commit is contained in:
parent
54bff16431
commit
3b35907547
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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<String>(TOKEN))
|
||||
.build()
|
||||
request = newRequest
|
||||
val newRequest =
|
||||
request.newBuilder().addHeader("Content-Type", "application/json").addHeader(
|
||||
"Authorization",
|
||||
(context as MobileApplication).dataStoreApp.readDataStoreKey<String>(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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()}")
|
||||
}
|
||||
}*/
|
||||
|
||||
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<String>(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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue