yelpmapapp/app/src/main/java/fishrungames/yelpmapapp/asyncTasks/HandleMapMarkersResponseAsyncTask.kt

54 lines
1.4 KiB
Kotlin
Executable File

package fishrungames.yelpmapapp.asyncTasks
import android.os.AsyncTask
import com.google.gson.JsonObject
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import fishrungames.yelpmapapp.records.MapMarkerRecord
/**
* Created by mephi on 10.06.2017.
*/
class HandleMapMarkersResponseAsyncTask(val onUpdateMapMarkers : ((Map<String, MapMarkerRecord>) -> Unit)) : AsyncTask<JsonObject, Int, Map<String, MapMarkerRecord>>()
{
override fun doInBackground(vararg response : JsonObject): Map<String, MapMarkerRecord> {
val newMapMarkers = mutableMapOf<String, MapMarkerRecord>()
try {
for (businessElement in response[0].getAsJsonArray("businesses")) {
val mapMarkerRecordType = object : TypeToken<MapMarkerRecord>() {}.type
val mapMarkerRecord = Gson().fromJson<MapMarkerRecord>(businessElement, mapMarkerRecordType)
if (mapMarkerRecord.id != null) {
newMapMarkers[mapMarkerRecord.id] = mapMarkerRecord
}
}
}
catch (e : Exception)
{
e.printStackTrace()
}
return newMapMarkers
}
override fun onPostExecute(result: Map<String, MapMarkerRecord>) {
super.onPostExecute(result)
onUpdateMapMarkers(result)
}
}