art-institute-of-chicago/aic-mobile-android

Add Custom Floor Adapter for Moshi Parsing.

agrosner opened this issue · 0 comments

We can parse the floor out of the gallery directly.
Objects have a gallery location which has floor in it.
Adding floor var with Int.MIN_VALUE might lead to confusion/bug if the floor is not assigned before it is accessed.

We could add the moshi adapter to convert floor string into the int in ArticGallery.
You could use it in ArticGallery.

/**
 * Parses floor as an integer. We default to [Int.MIN_VALUE] as 0 is a valid floor.
 */
class FloorAdapter {

    @ToJson
    fun toText(@Floor floor: Int): String = floor.toString()

    @FromJson
    @Floor
    fun fromText(text: String?): Int {
        return if (text != null) {
            try {
                return text.toInt()
            } catch (e: NumberFormatException) {
                if (text == "LL") {
                    0
                } else {
                    INVALID_FLOOR
                }
            }
        } else {
            INVALID_FLOOR
        }

    }
}

@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class Floor
then annotate the floor field in data class

        @Floor @Json(name = "audio_transcript")  val floor: Int