DnDApplication/app/src/main/java/com/pixelized/rplexicon/facotry/LocationParser.kt

55 lines
No EOL
1.8 KiB
Kotlin

package com.pixelized.rplexicon.facotry
import android.net.Uri
import com.google.api.services.sheets.v4.model.ValueRange
import com.pixelized.rplexicon.model.Location
import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
import com.pixelized.rplexicon.utilitary.extentions.checkSheetStructure
import com.pixelized.rplexicon.utilitary.extentions.sheet
import javax.inject.Inject
class LocationParser @Inject constructor() {
@Throws(IncompatibleSheetStructure::class)
fun parse(data: ValueRange): List<Location> {
var id = 0
var sheetStructure: Map<String, Int>? = null
return data.values.sheet()?.mapIndexedNotNull { index, item ->
when {
index == 0 -> {
sheetStructure = item.checkSheetStructure(COLUMNS)
null
}
item is List<*> -> {
val name = item.getOrNull(sheetStructure.name) as? String
val uri = item.getOrNull(sheetStructure.uri)?.toString()?.let { Uri.parse(it) }
if (name != null && uri != null) {
Location(
id = id++,
sheetIndex = index,
name = name,
uri = uri,
marquees = emptyList(),
)
} else {
null
}
}
else -> null
}
} ?: emptyList()
}
private val Map<String, Int>?.name: Int get() = this?.getValue(COLUMNS[0]) ?: 0
private val Map<String, Int>?.uri: Int get() = this?.getValue(COLUMNS[1]) ?: 1
companion object {
private val COLUMNS = listOf(
"Nom", "Carte"
)
}
}