Skip to content

Commit

Permalink
fix(#122): bunch of fixes...
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed May 15, 2024
1 parent 9edb28c commit a7db9cc
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 69 deletions.
2 changes: 1 addition & 1 deletion gn_mobile_core
Submodule gn_mobile_core updated 26 files
+1 −1 commons/build.gradle
+1 −1 commons/src/main/java/fr/geonature/commons/data/MainContentProvider.kt
+2 −2 commons/version.properties
+16 −13 datasync/README.md
+1 −1 datasync/build.gradle
+12 −2 datasync/src/main/java/fr/geonature/datasync/api/GeoNatureAPIClientImpl.kt
+4 −0 datasync/src/main/java/fr/geonature/datasync/api/IGeoNatureAPIClient.kt
+7 −1 datasync/src/main/java/fr/geonature/datasync/api/ITaxHubService.kt
+26 −0 datasync/src/main/java/fr/geonature/datasync/api/model/TaxrefList.kt
+19 −7 datasync/src/main/java/fr/geonature/datasync/sync/DataSyncModule.kt
+42 −1 datasync/src/main/java/fr/geonature/datasync/sync/DataSyncStatus.kt
+1 −0 datasync/src/main/java/fr/geonature/datasync/sync/io/AdditionalFieldJsonReader.kt
+24 −2 datasync/src/main/java/fr/geonature/datasync/sync/repository/ISynchronizeLocalDataRepository.kt
+48 −39 datasync/src/main/java/fr/geonature/datasync/sync/repository/SynchronizeAdditionalFieldsRepositoryImpl.kt
+340 −0 datasync/src/main/java/fr/geonature/datasync/sync/repository/SynchronizeTaxaRepositoryImpl.kt
+12 −246 datasync/src/main/java/fr/geonature/datasync/sync/usecase/DataSyncUseCase.kt
+1 −0 datasync/src/test/java/fr/geonature/datasync/sync/io/AdditionalFieldJsonReaderTest.kt
+29 −1 datasync/src/test/resources/fixtures/additional_fields_geonature.json
+2 −2 datasync/version.properties
+2 −5 docs/data_sync.adoc
+30 −2 docs/dev.adoc
+1,278 −277 docs/postman/gn_mobile_core.postman_collection.json
+6 −4 docs/uml/datasync_app_update.puml
+16 −5 docs/uml/datasync_auth.puml
+155 −59 docs/uml/datasync_data.puml
+0 −33 docs/uml/datasync_inputs.puml
Original file line number Diff line number Diff line change
Expand Up @@ -851,15 +851,7 @@ class EditableFieldAdapter(private val listener: OnEditableFieldAdapter) :
}

override fun afterTextChanged(s: Editable?) {
editableField?.run {
value = PropertyValue.Text(
code,
s?.toString()
?.ifEmpty { null }
?.ifBlank { null }
)
listener.onUpdate(this)
}
this@TextSimpleViewHolder.afterTextChanged(s)
}
}

Expand All @@ -875,6 +867,18 @@ class EditableFieldAdapter(private val listener: OnEditableFieldAdapter) :
}
}

internal open fun afterTextChanged(s: Editable?) {
editableField?.run {
value = PropertyValue.Text(
code,
s?.toString()
?.ifEmpty { null }
?.ifBlank { null }
)
listener.onUpdate(this)
}
}

override fun onBind(editableField: EditableField) {
if (!lockDefaultValues) {
editableField.locked = false
Expand All @@ -901,15 +905,20 @@ class EditableFieldAdapter(private val listener: OnEditableFieldAdapter) :
}

editableField.value
.takeIf { it is PropertyValue.Text && !it.isEmpty() }
?.takeIf { it is PropertyValue.Text && !it.isEmpty() }
?.let { it as PropertyValue.Text }
?.value
?.also {
edit.editText?.removeTextChangedListener(textWatcher)
edit.editText?.text = Editable.Factory.getInstance()
.newEditable(it.value)
edit.editText?.addTextChangedListener(textWatcher)
setText(it)
}
}

internal fun setText(charSequence: CharSequence) {
edit.editText?.removeTextChangedListener(textWatcher)
edit.editText?.text = Editable.Factory.getInstance()
.newEditable(charSequence)
edit.editText?.addTextChangedListener(textWatcher)
}
}

inner class TextMultipleViewHolder(parent: ViewGroup) : TextSimpleViewHolder(parent) {
Expand All @@ -927,9 +936,32 @@ class EditableFieldAdapter(private val listener: OnEditableFieldAdapter) :
inner class NumberViewHolder(parent: ViewGroup) : TextSimpleViewHolder(parent) {
init {
edit.editText?.apply {
inputType = InputType.TYPE_CLASS_NUMBER
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
}
}

override fun afterTextChanged(s: Editable?) {
editableField?.run {
value = PropertyValue.Number(
code,
s?.toString()
?.toDoubleOrNull()
)
listener.onUpdate(this)
}
}

override fun onBind(editableField: EditableField) {
super.onBind(editableField)

editableField.value
?.takeIf { it is PropertyValue.Number && !it.isEmpty() }
?.let { it as PropertyValue.Number }
?.value
?.also {
setText(it.toString())
}
}
}

inner class MinMaxViewHolder(parent: ViewGroup) : AbstractViewHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fr.geonature.occtax.features.nomenclature.repository.IAdditionalFieldRepo
import fr.geonature.occtax.features.nomenclature.repository.IDefaultPropertyValueRepository
import fr.geonature.occtax.features.nomenclature.repository.INomenclatureRepository
import fr.geonature.occtax.features.settings.domain.PropertySettings
import org.tinylog.Logger
import javax.inject.Inject

/**
Expand All @@ -23,6 +24,8 @@ class GetEditableFieldsUseCase @Inject constructor(
) :
BaseResultUseCase<List<EditableField>, GetEditableFieldsUseCase.Params>() {
override suspend fun run(params: Params): Result<List<EditableField>> {
Logger.info { "loading editable fields of type '${params.type.name}'${if (params.withAdditionalFields) " with additional fields" else ""}${params.datasetId?.let { " matching dataset ID $it" } ?: ""}..." }

val editableNomenclatures = (nomenclatureRepository.getEditableFields(
params.type,
*params.settings.toTypedArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ class ObservationRecordLocalDataSourceImpl(
preferenceManager.all
.filterKeys { it.startsWith("${KEY_PREFERENCE_INPUT}_") }
.values
.mapNotNull { if (it is String && it.isNotBlank()) runCatching { observationRecordJsonReader.read(it) }.getOrNull() else null } + exportedObservationRecords
.mapNotNull {
if (it is String && it.isNotBlank()) runCatching { observationRecordJsonReader.read(it) }
.onFailure { throwable -> Logger.error(throwable) { "failed to load observation record" } }
.getOrNull() else null
} + exportedObservationRecords
).distinctBy { it.internalId }
.sortedByDescending { it.dates.start }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ data class CountingRecord(
/**
* The main properties of this taxon counting record.
*/
val properties: SortedMap<String, PropertyValue> = sortedMapOf()
val properties: SortedMap<String, PropertyValue> = sortedMapOf(
ADDITIONAL_FIELDS_KEY to PropertyValue.AdditionalFields(
ADDITIONAL_FIELDS_KEY,
mapOf()
)
)
) : Parcelable {

/**
Expand Down Expand Up @@ -73,11 +78,11 @@ data class CountingRecord(
@IgnoredOnParcel
var additionalFields: List<PropertyValue>
get() = properties[ADDITIONAL_FIELDS_KEY]
?.takeIf { it is PropertyValue.AdditionalField }
?.let { it as PropertyValue.AdditionalField }?.value?.values?.toList()
?.takeIf { it is PropertyValue.AdditionalFields }
?.let { it as PropertyValue.AdditionalFields }?.value?.values?.toList()
?: emptyList()
set(value) {
PropertyValue.AdditionalField(
PropertyValue.AdditionalFields(
ADDITIONAL_FIELDS_KEY,
value.associate { it.toPair() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ data class ObservationRecord(
/**
* The main properties of this observation record
*/
val properties: SortedMap<String, PropertyValue> = sortedMapOf(),
val properties: SortedMap<String, PropertyValue> = sortedMapOf(
PropertyValue.Text(
"meta_device_entry",
"mobile"
)
.toPair(),
PropertyValue.AdditionalFields(
ADDITIONAL_FIELDS_KEY,
mapOf()
)
.toPair()
),

/**
* The current status of this observation record.
*/
val status: Status = Status.DRAFT
) : Parcelable {

init {
PropertyValue.Text(
"meta_device_entry",
"mobile"
)
.toPair()
.also {
properties[it.first] = it.second
}
}

@IgnoredOnParcel
val comment = CommentRecord(properties)

Expand Down Expand Up @@ -89,6 +89,10 @@ data class ObservationRecord(
SYNC_ERROR,
SYNC_SUCCESSFUL
}

companion object {
const val ADDITIONAL_FIELDS_KEY = "additional_fields"
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed class PropertyValue : Parcelable {
is NumberArray -> value.isEmpty()
is Dataset -> datasetId == null
is Nomenclature -> value == null
is AdditionalField -> value.all { it.value.isEmpty() }
is AdditionalFields -> value.all { it.value.isEmpty() }
is Taxa -> value.all { taxon -> taxon.properties.all { it.value.isEmpty() } }
is Counting -> value.all { counting -> counting.properties.all { it.value.isEmpty() } }
is Media -> value.isEmpty()
Expand All @@ -40,7 +40,7 @@ sealed class PropertyValue : Parcelable {
is NumberArray -> code
is Dataset -> code
is Nomenclature -> code
is AdditionalField -> code
is AdditionalFields -> code
is Taxa -> code
is Counting -> code
is Media -> code
Expand Down Expand Up @@ -129,10 +129,10 @@ sealed class PropertyValue : Parcelable {
PropertyValue()

/**
* As additional field.
* As additional fields.
*/
@Parcelize
data class AdditionalField(val code: String, val value: Map<String, PropertyValue>) :
data class AdditionalFields(val code: String, val value: Map<String, PropertyValue>) :
PropertyValue()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ data class TaxonRecord(
/**
* The main properties of this taxon record
*/
val properties: SortedMap<String, PropertyValue> = sortedMapOf()
val properties: SortedMap<String, PropertyValue> = sortedMapOf(
ADDITIONAL_FIELDS_KEY to PropertyValue.AdditionalFields(
ADDITIONAL_FIELDS_KEY,
mapOf()
)
)
) : Parcelable {

@IgnoredOnParcel
Expand All @@ -52,11 +57,11 @@ data class TaxonRecord(
@IgnoredOnParcel
var additionalFields: List<PropertyValue>
get() = properties[ADDITIONAL_FIELDS_KEY]
?.takeIf { it is PropertyValue.AdditionalField }
?.let { it as PropertyValue.AdditionalField }?.value?.values?.toList()
?.takeIf { it is PropertyValue.AdditionalFields }
?.let { it as PropertyValue.AdditionalFields }?.value?.values?.toList()
?: emptyList()
set(value) {
PropertyValue.AdditionalField(
PropertyValue.AdditionalFields(
ADDITIONAL_FIELDS_KEY,
value.associate { it.toPair() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ class ObservationRecordJsonWriter {
}
}

is PropertyValue.AdditionalFields -> {
writer.name(propertyValue.code)
writeAdditionalFields(
writer,
propertyValue
)
}

is PropertyValue.Taxa -> {
writer.name(propertyValue.code)
.beginArray()
Expand Down Expand Up @@ -249,4 +257,42 @@ class ObservationRecordJsonWriter {
)
}
}

private fun writeAdditionalFields(
writer: JsonWriter,
additionalFields: PropertyValue.AdditionalFields
) {
writer.beginObject()

additionalFields.value.values.forEach {
when (it) {
is PropertyValue.Nomenclature -> writer.name(it.code)
.value(it.value)

is PropertyValue.Number -> writer.name(it.code)
.value(it.value)

is PropertyValue.NumberArray -> {
writer.name(it.code)
.beginArray()
it.value.forEach { value -> writer.value(value) }
writer.endArray()
}

is PropertyValue.StringArray -> {
writer.name(it.code)
.beginArray()
it.value.forEach { value -> writer.value(value) }
writer.endArray()
}

is PropertyValue.Text -> writer.name(it.code)
.value(it.value)

else -> {}
}
}

writer.endObject()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.JsonReader
import android.util.JsonToken
import fr.geonature.commons.data.entity.Taxon
import fr.geonature.commons.data.entity.Taxonomy
import fr.geonature.commons.util.nextLongOrNull
import fr.geonature.commons.util.nextStringOrNull
import fr.geonature.occtax.features.record.domain.CountingRecord
import fr.geonature.occtax.features.record.domain.ObservationRecord
Expand Down Expand Up @@ -284,7 +285,8 @@ class TaxonRecordJsonReader {
JsonToken.NUMBER -> additionalFields.add(
PropertyValue.Number(
keyName,
reader.nextLong()
runCatching { reader.nextLongOrNull() }.getOrNull()
?: reader.nextDouble()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class TaxonRecordJsonWriter {
}
}

is PropertyValue.AdditionalField -> {
is PropertyValue.AdditionalFields -> {
writer.name(propertyValue.code)
writeAdditionalFields(
writer,
Expand Down Expand Up @@ -208,7 +208,7 @@ class TaxonRecordJsonWriter {
}
}

is PropertyValue.AdditionalField -> {
is PropertyValue.AdditionalFields -> {
writer.name(propertyValue.code)
writeAdditionalFields(
writer,
Expand Down Expand Up @@ -278,11 +278,11 @@ class TaxonRecordJsonWriter {

private fun writeAdditionalFields(
writer: JsonWriter,
additionalField: PropertyValue.AdditionalField
additionalFields: PropertyValue.AdditionalFields
) {
writer.beginObject()

additionalField.value.values.forEach {
additionalFields.value.values.forEach {
when (it) {
is PropertyValue.Nomenclature -> writer.name(it.code)
.value(it.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(
): List<PropertyValue> {
return propertyValues.map {
when (it) {
is PropertyValue.AdditionalField -> {
PropertyValue.AdditionalField(
is PropertyValue.AdditionalFields -> {
PropertyValue.AdditionalFields(
it.code,
mapPropertyValuesFromNomenclature(
taxonomy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class EditCountingMetadataFragment : Fragment() {
*((countingRecord?.properties?.values
?.filterNotNull()
?.filterNot { it.isEmpty() }
?.filterNot { it is PropertyValue.AdditionalField }
?.filterNot { it is PropertyValue.AdditionalFields }
?: emptyList()) + (countingRecord?.additionalFields ?: emptyList())).toTypedArray()
)
}
Expand Down
Loading

0 comments on commit a7db9cc

Please sign in to comment.