Adding rate limiter to API client and clearing map after addition of a roadtrip

master
Arthur VALIN 2 years ago
parent 3552beba2f
commit ab028cdebe

@ -1,6 +1,7 @@
package uca.baptistearthur.geocaching.network package uca.baptistearthur.geocaching.network
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import okhttp3.Interceptor
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter import retrofit2.Converter
@ -11,33 +12,53 @@ import java.lang.reflect.Type
object AddressNetwork { object AddressNetwork {
private val base_url = "https://forward-reverse-geocoding.p.rapidapi.com/v1/reverse/"
private val key = "19516a9900mshce10de76f99976bp10f192jsn8c8d82222baa"
private val interceptor = HttpLoggingInterceptor().apply { private val API_Info = object {
level = HttpLoggingInterceptor.Level.BODY val base_url = "https://forward-reverse-geocoding.p.rapidapi.com/v1/reverse/"
val key = "19516a9900mshce10de76f99976bp10f192jsn8c8d82222baa"
}
private val rateLimiter = object {
val maxRequestsPerSecond = 2
val intervalInMillis = (1000.0 / maxRequestsPerSecond).toLong()
var lastRequestTime: Long = 0
}
val rateLimitInterceptor = Interceptor { chain ->
val now = System.currentTimeMillis()
val elapsed = now - rateLimiter.lastRequestTime
if (elapsed < rateLimiter.intervalInMillis) {
Thread.sleep(rateLimiter.intervalInMillis - elapsed)
} }
rateLimiter.lastRequestTime = System.currentTimeMillis()
private val gson = GsonBuilder().apply { chain.proceed(chain.request())
registerTypeAdapter(Address::class.java, AddressDeserializer()) }
}.create() private val interceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
private val client = OkHttpClient.Builder() }
.addInterceptor { chain ->
val request = chain.request().newBuilder() private val gson = GsonBuilder().apply {
.addHeader("X-RapidAPI-Key", key) registerTypeAdapter(Address::class.java, AddressDeserializer())
.build() }.create()
chain.proceed(request)
}
.addInterceptor(interceptor)
.build()
val retrofit: AddressAPI by lazy { private val client = OkHttpClient.Builder()
Retrofit.Builder() .addInterceptor { chain ->
.baseUrl(base_url) val request = chain.request().newBuilder()
.client(client) .addHeader("X-RapidAPI-Key", API_Info.key)
.addConverterFactory(GsonConverterFactory.create(gson))
.build() .build()
.create(AddressAPI::class.java) chain.proceed(request)
} }
.addInterceptor(interceptor)
.addInterceptor(rateLimitInterceptor)
.build()
val retrofit: AddressAPI by lazy {
Retrofit.Builder()
.baseUrl(API_Info.base_url)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(AddressAPI::class.java)
} }
}

@ -57,7 +57,7 @@ class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
} }
fun computeRoad(mapView: MapView) { fun computeRoad(mapView: MapView) {
mapView.overlays.remove(mapView.overlays.find { it is Polyline}) mapView.overlays.removeAll{ it is Polyline }
if (locations.size > 1) { if (locations.size > 1) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val road = roadManager.getRoad(ArrayList(locations.map{it.position})) val road = roadManager.getRoad(ArrayList(locations.map{it.position}))
@ -78,7 +78,7 @@ class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
}else{ }else{
Log.d("GeoRoad", "TRY DELETE ROADTRIP OVERLAY" + locations.size) Log.d("GeoRoad", "TRY DELETE ROADTRIP OVERLAY" + locations.size)
Log.d("GeoRoad", ""+mapView.overlays.size) Log.d("GeoRoad", ""+mapView.overlays.size)
mapView.overlays.remove(mapView.overlays.find { it is NewRoadtripOverlay}) mapView.overlays.removeAll{ it is NewRoadtripOverlay }
newRoadtripOverlayVisible=false newRoadtripOverlayVisible=false
} }
} }

@ -1,11 +1,11 @@
package uca.baptistearthur.geocaching.ui.overlay package uca.baptistearthur.geocaching.ui.overlay
import android.app.AlertDialog import android.app.AlertDialog
import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.graphics.RectF import android.graphics.RectF
import android.provider.Settings.System.getString
import android.text.InputFilter import android.text.InputFilter
import android.util.Log import android.util.Log
import android.view.MotionEvent import android.view.MotionEvent
@ -15,18 +15,17 @@ import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay import org.osmdroid.views.overlay.Overlay
import org.osmdroid.views.overlay.Polyline
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Address
import uca.baptistearthur.geocaching.model.Place import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTripEntity import uca.baptistearthur.geocaching.model.RoadTripEntity
import java.util.* import java.util.*
class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() { class NewRoadtripOverlay(val points: MutableCollection<PlaceMarker>) : Overlay() {
private var circleRectF=RectF() private var circleRectF=RectF()
@ -62,7 +61,7 @@ class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() {
override fun onSingleTapConfirmed(e: MotionEvent?, mapView: MapView?) = override fun onSingleTapConfirmed(e: MotionEvent?, mapView: MapView?) =
if (e != null && circleRectF.contains(e.x, e.y)) { if (e != null && circleRectF.contains(e.x, e.y)) {
mapView?.let{ mapView?.let{
createDialog(it.context) createDialog(it)
} }
Log.d("GeoRoad", "CONFIRM : "+points.size) Log.d("GeoRoad", "CONFIRM : "+points.size)
true true
@ -70,7 +69,12 @@ class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() {
false false
} }
private fun createDialog(context: Context){ private fun clearMap(mapView: MapView){
mapView.overlays.removeAll { it is PlaceMarker || it is Polyline || it is NewRoadtripOverlay }
}
private fun createDialog(mapView: MapView){
val context = mapView.context
val input = EditText(context) val input = EditText(context)
input.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(50)) input.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(50))
@ -86,14 +90,21 @@ class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() {
it.initAddress() it.initAddress()
Log.d("GeoMap", it.address.displayName) Log.d("GeoMap", it.address.displayName)
} }
val newRoadTrip = RoadTripEntity(
id = 0, // auto-generated ID
name = input.text.toString(),
date = Date(),
places = places
)
// TODO: Persister le RoadTrip
Toast.makeText(
context,
R.string.roadtripAdded,
Toast.LENGTH_SHORT
).show()
points.clear()
} }
val newRoadTrip = RoadTripEntity( clearMap(mapView)
id = 0, // auto-generated ID
name = input.text.toString(),
date = Date(),
places = places
)
// TODO: Persister le RoadTrip
} else { } else {
Toast.makeText( Toast.makeText(

@ -10,4 +10,5 @@
<string name="cancel">Annuler</string> <string name="cancel">Annuler</string>
<string name="emptyTextError">Le texte ne peux pas être vide</string> <string name="emptyTextError">Le texte ne peux pas être vide</string>
<string name="newRoadtripDialog">Entrez le nom de votre voyage</string> <string name="newRoadtripDialog">Entrez le nom de votre voyage</string>
<string name="roadtripAdded">Roadtrip ajouté</string>
</resources> </resources>

@ -10,4 +10,5 @@
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="emptyTextError">The text cannot be empty</string> <string name="emptyTextError">The text cannot be empty</string>
<string name="newRoadtripDialog">Enter the name of your travel</string> <string name="newRoadtripDialog">Enter the name of your travel</string>
<string name="roadtripAdded">Roadtrip added</string>
</resources> </resources>
Loading…
Cancel
Save