Add support for a tags data.

This commit is contained in:
Thomas Andres Gomez 2023-07-31 18:29:01 +02:00
parent 82738a8f03
commit dc5e52cf18
9 changed files with 92 additions and 28 deletions

View file

@ -55,6 +55,7 @@ class LexiconRepository @Inject constructor(
private fun updateData(data: ValueRange?) {
val sheet = data?.values?.sheet()
var sheetStructure: Map<String, Int>? = null
var id = 0
val lexicon: List<Lexicon> = sheet?.mapIndexedNotNull { index, row ->
when {
index == 0 -> {
@ -64,9 +65,13 @@ class LexiconRepository @Inject constructor(
row is List<*> -> parseCharacterRow(
sheetStructure = sheetStructure,
index = index - 1,
id = id,
sheetIndex = index,
row = row,
)
)?.also {
// update next id if parsing is successful.
id = it.id + 1
}
else -> null
}
@ -82,24 +87,19 @@ class LexiconRepository @Inject constructor(
}
// parse the first line to find element that we recognize.
val sheetStructure = hashMapOf<String, Int>()
firstRow.forEachIndexed { index, cell ->
when (cell as? String) {
Sheet.NAME,
Sheet.DIMINUTIVE,
Sheet.GENDER,
Sheet.RACE,
Sheet.PORTRAIT,
Sheet.DESCRIPTION,
Sheet.HISTORY -> sheetStructure[cell] = index
if (cell is String && Sheet.COLUMNS.contains(cell)) {
sheetStructure[cell] = index
}
}
// check if we found everything we need.
when {
sheetStructure.size < Sheet.KNOWN_COLUMN -> throw IncompatibleSheetStructure(
sheetStructure.size < Sheet.COLUMNS.size -> throw IncompatibleSheetStructure(
message = "Sheet header row does not have enough column: ${firstRow.size}.\nstructure: $firstRow\nheader: $sheetStructure"
)
sheetStructure.size > Sheet.KNOWN_COLUMN -> throw IncompatibleSheetStructure(
sheetStructure.size > Sheet.COLUMNS.size -> throw IncompatibleSheetStructure(
message = "Sheet header row does have too mush columns: ${firstRow.size}.\nstructure: $firstRow\nheader: $sheetStructure"
)
}
@ -109,7 +109,8 @@ class LexiconRepository @Inject constructor(
private fun parseCharacterRow(
sheetStructure: Map<String, Int>?,
index: Int,
id: Int,
sheetIndex: Int,
row: List<*>?,
): Lexicon? {
val name = row?.getOrNull(sheetStructure.name) as? String
@ -119,10 +120,12 @@ class LexiconRepository @Inject constructor(
val portrait = row?.getOrNull(sheetStructure.portrait) as? String?
val description = row?.getOrNull(sheetStructure.description) as? String?
val history = row?.getOrNull(sheetStructure.history) as? String?
val tags = row?.getOrNull(sheetStructure.tags) as? String?
return if (name != null) {
Lexicon(
id = index,
id = id,
sheetIndex = sheetIndex,
name = name,
diminutive = diminutive?.takeIf { it.isNotBlank() },
gender = when (gender?.takeIf { it.isNotBlank() }) {
@ -149,6 +152,7 @@ class LexiconRepository @Inject constructor(
portrait = portrait?.split("\n")?.mapNotNull { it.toUriOrNull() } ?: emptyList(),
description = description?.takeIf { it.isNotBlank() },
history = history?.takeIf { it.isNotBlank() },
tags = tags?.takeIf { it.isNotBlank() },
)
} else {
null
@ -175,6 +179,7 @@ class LexiconRepository @Inject constructor(
private val Map<String, Int>?.portrait: Int get() = this?.getValue(Sheet.PORTRAIT) ?: 4
private val Map<String, Int>?.description: Int get() = this?.getValue(Sheet.DESCRIPTION) ?: 5
private val Map<String, Int>?.history: Int get() = this?.getValue(Sheet.HISTORY) ?: 6
private val Map<String, Int>?.tags: Int get() = this?.getValue(Sheet.TAGS) ?: 7
class ServiceNotReady : Exception()
@ -186,15 +191,29 @@ class LexiconRepository @Inject constructor(
private object Sheet {
const val ID = "1oL9Nu5y37BPEbKxHre4TN9o8nrgy2JQoON4RRkdAHMs"
const val LEXIQUE = "Lexique"
const val KNOWN_COLUMN = 7
const val NAME = "Nom"
const val DIMINUTIVE = "Diminutif"
const val GENDER = "Sexe"
const val RACE = "Race"
const val PORTRAIT = "Portrait"
const val DESCRIPTION = "Description"
const val HISTORY = "Histoire"
const val META = "MetaData"
val COLUMNS = listOf(
"Nom",
"Diminutif",
"Sexe",
"Race",
"Portrait",
"Description",
"Histoire",
"Mots clés",
)
val NAME = COLUMNS[0]
val DIMINUTIVE = COLUMNS[1]
val GENDER = COLUMNS[2]
val RACE = COLUMNS[3]
val PORTRAIT = COLUMNS[4]
val DESCRIPTION = COLUMNS[5]
val HISTORY = COLUMNS[6]
val TAGS = COLUMNS[7]
}
private object Gender {