Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
WIP: CSV parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 15, 2023
1 parent 27a578a commit f051ae3
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 25 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ sealed interface CSVEvent {
data class DataMetaChange(val meta: DateMetadata) : CSVEvent
data class MapAccount(val index: Int, val name: String) : CSVEvent
data class MapAccountCurrency(val index: Int, val name: String) : CSVEvent

data class MapToAccount(val index: Int, val name: String) : CSVEvent
data class MapToAccountCurrency(val index: Int, val name: String) : CSVEvent

data class MapCategory(val index: Int, val name: String) : CSVEvent
data class MapTitle(val index: Int, val name: String) : CSVEvent
data class MapDescription(val index: Int, val name: String) : CSVEvent

object Continue : CSVEvent

}
73 changes: 68 additions & 5 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ private fun UI(
csvTable(state.csv)
}
if (state.columns != null && state.important != null) {
important(state.columns, importantFields = state.important, onEvent = onEvent)
importantFields(state.columns, importantFields = state.important, onEvent = onEvent)
}
if (state.columns != null && state.transfer != null) {
transferFields(state.columns, state.transfer, onEvent = onEvent)
}
if (state.columns != null && state.optional != null) {
optionalFields(state.columns, state.optional, onEvent = onEvent)
}
}
}
Expand Down Expand Up @@ -225,14 +231,22 @@ private fun <M> LazyListScope.mappingRow(

fun LazyListScope.sectionDivider(text: String) {
item {
Spacer8()
Text(text = text, style = UI.typo.h2)
Spacer(modifier = Modifier.height(24.dp))
Text(text = text, style = UI.typo.b1)
Text(
text = """
Match the CSV column with the appropriate Ivy type.
If the parsing is successful the border will turn green.
""".trimIndent(),
style = UI.typo.nB2,
color = UI.colors.pureInverse
)
Spacer8()
}
}

// region Important
fun LazyListScope.important(
fun LazyListScope.importantFields(
columns: CSVRow,
importantFields: ImportantFields,
onEvent: (CSVEvent) -> Unit
Expand Down Expand Up @@ -419,4 +433,53 @@ private fun EnabledButton(
Text(text = text)
}
}
// endregion
// endregion

fun LazyListScope.transferFields(
columns: CSVRow,
transferFields: TransferFields,
onEvent: (CSVEvent) -> Unit
) {
sectionDivider("Transfer fields")
mappingRow(
columns = columns,
mapping = transferFields.toAccount,
status = transferFields.toAccountStatus,
onMapTo = { index, name -> onEvent(CSVEvent.MapToAccount(index, name)) },
)
mappingRow(
columns = columns,
mapping = transferFields.toAccountCurrency,
status = transferFields.toAccountCurrencyStatus,
onMapTo = { index, name -> onEvent(CSVEvent.MapToAccountCurrency(index, name)) },
)
}

fun LazyListScope.optionalFields(
columns: CSVRow,
optionalFields: OptionalFields,
onEvent: (CSVEvent) -> Unit
) {
sectionDivider("Optional fields")
mappingRow(
columns = columns,
mapping = optionalFields.category,
status = optionalFields.categoryStatus,
onMapTo = { index, name -> onEvent(CSVEvent.MapCategory(index, name)) },
)
mappingRow(
columns = columns,
mapping = optionalFields.title,
status = optionalFields.titleStatus,
onMapTo = { index, name -> onEvent(CSVEvent.MapTitle(index, name)) },
)
mappingRow(
columns = columns,
mapping = optionalFields.description,
status = optionalFields.descriptionStatus,
onMapTo = { index, name -> onEvent(CSVEvent.MapDescription(index, name)) },
)
}



9 changes: 6 additions & 3 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ data class CSVState(
val important: ImportantFields?,
val transfer: TransferFields?,
val optional: OptionalFields?,

val successPercent: Double?,
val failedRows: List<CSVRow>?,
val continueEnabled: Boolean,
)

data class ImportantFields(
Expand Down Expand Up @@ -36,13 +34,18 @@ enum class DateMetadata {

data class TransferFields(
val toAccount: ColumnMapping<Unit>,
val toAccountStatus: MappingStatus,
val toAccountCurrency: ColumnMapping<Unit>,
val toAccountCurrencyStatus: MappingStatus,
)

data class OptionalFields(
val category: ColumnMapping<Unit>,
val categoryStatus: MappingStatus,
val title: ColumnMapping<Unit>,
val titleStatus: MappingStatus,
val description: ColumnMapping<Unit>,
val descriptionStatus: MappingStatus,
)

data class ColumnMapping<M>(
Expand Down
Loading

0 comments on commit f051ae3

Please sign in to comment.