Refactor repositories by adding parsers.

Add character sheet poc.
This commit is contained in:
Thomas Andres Gomez 2023-08-03 16:11:20 +02:00
parent bb760392f0
commit 87a1471efe
17 changed files with 761 additions and 286 deletions

View file

@ -0,0 +1,54 @@
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,
)
} 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"
)
}
}