5

http://cm08323.tmweb.ru/android/determinace/json/images.json I have some json to the server.

My database class:

@Entity
data class Image(val url: String = "", val urlAnswer: String = "", val race: String = "") : DisplayableItem, Persistable

Interface for api:

interface ImagesAdi {
    @GET("android/determinace/json/images.json")
    fun getImages(): Single<List<ImagesResponse>>
}
...
class ImagesResponse(
        val images: List<ImageSingle>)

class ImageSingle(val url: String,
            val url_answer: String,
            val race: String)

My mapper for transformation ImagesResponse to Image:

@Singleton
class ChooseRaceMapper @Inject constructor() : (ImageSingle) -> Image {
    override fun invoke(response: ImageSingle): Image = Image(response.url, response.url_answer, response.race)
}

How should look like transformer from List< ImageSingle > to List< Image >?

I have trasformer for single objects, he looks like that:

fun chooseRace(): Single<Image> = imagesAdi.getSingleImage()
        .map { chooseRaceMapper.invoke(it) }
        .doOnSuccess {
            database.addDetectedResult(it)
                    .subscribeIgnoreResult()

            requestImages()
        }

Can you help me to write transformer for type "List"? I trying make it, but it did not work out. Thanks. Sorry for my English.

Or transformation from List to List should be better in class Mapper? (In method invoke)

2
  • Well, you have a list of images, but you only need a single one. I guess you just need to pick one in the list. Which one do you want? Commented May 16, 2017 at 7:11
  • @marstran I need to transformation objects from List<ImageSingle> to List<Image> and the function chooseRace return list type of Single<List<Image>> Commented May 16, 2017 at 7:14

2 Answers 2

2

Considering your images.json, your getImages() should not return a Single of a List.

@GET("android/determinace/json/images.json")
fun getImages(): Single<ImagesResponse>

Then you could use the Kotlin List.map() mapper to map the list.

imagesAdi.getImages()
    .map { list -> list.map { chooseRaceMapper.invoke(it) } }
    .doOnSuccess { }
Sign up to request clarification or add additional context in comments.

Comments

1

The interface declaration is wrong:

interface ImagesAdi {
    @GET("android/determinace/json/images.json")
    fun getImages(): Single<List<ImagesResponse>>
}

It should be (according to your json):

interface ImagesAdi {
    @GET("android/determinace/json/images.json")
    fun getImages(): Single<ImagesResponse>
}

And for the mapping part, something like this will do:

imagesAdi.getImages().map { it.images }
    .map { /* your mapper here */ }
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.