Skip to content

Commit

Permalink
feat(#122): additional fields data synchronization is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Mar 2, 2024
1 parent 95b000e commit 216c96c
Show file tree
Hide file tree
Showing 23 changed files with 550 additions and 116 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ Example:
### Parameters description

| Parameter | UI | Description | Default value |
| ---------------------------------- | ------- | -------------------------------------------------------------------------------------------------- | ------------- |
|------------------------------------| ------- |----------------------------------------------------------------------------------------------------| ------------- |
| `area_observation_duration` | ☐ | Area observation duration period (in days) | 365 |
| `sync` | ☐ | Data synchronization settings (cf. https://github.com/PnX-SI/gn_mobile_core/tree/develop/datasync) | |
| `map` | ☐ | Maps settings (cf. https://github.com/PnX-SI/gn_mobile_maps/tree/develop/maps) | |
| `input` | ☐ | Input form settings | |
| `input/date` | ☐ | Date settings | |
| `nomenclature` | ☐ | Nomenclature settings | |
| `nomenclature/save_default_values` | ☐ | Save default nomenclature values | false |
| `nomenclature/additional_fields` | ☐ | Show additional fields | false |
| `nomenclature/information` | ☐ | Information settings (as array) | |
| `nomenclature/counting` | ☐ | Counting settings (as array) | |

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package fr.geonature.occtax.features.nomenclature

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import fr.geonature.commons.data.GeoNatureModuleName
import fr.geonature.commons.data.LocalDatabase
Expand All @@ -14,8 +12,6 @@ import fr.geonature.commons.features.nomenclature.data.AdditionalFieldLocalDataS
import fr.geonature.commons.features.nomenclature.data.IAdditionalFieldLocalDataSource
import fr.geonature.commons.features.nomenclature.data.INomenclatureLocalDataSource
import fr.geonature.commons.features.nomenclature.data.NomenclatureLocalDataSourceImpl
import fr.geonature.datasync.api.IGeoNatureAPIClient
import fr.geonature.datasync.sync.repository.ISynchronizeAdditionalDataRepository
import fr.geonature.occtax.features.nomenclature.data.INomenclatureSettingsLocalDataSource
import fr.geonature.occtax.features.nomenclature.data.IPropertyValueLocalDataSource
import fr.geonature.occtax.features.nomenclature.data.InMemoryPropertyValueLocalDataSourceImpl
Expand All @@ -26,7 +22,6 @@ 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.nomenclature.repository.NomenclatureRepositoryImpl
import fr.geonature.occtax.features.nomenclature.repository.SynchronizeAdditionalFieldRepositoryImpl
import javax.inject.Singleton

/**
Expand Down Expand Up @@ -107,20 +102,4 @@ object NomenclatureModule {
): IAdditionalFieldRepository {
return AdditionalFieldRepositoryImpl(additionalFieldLocalDataSource)
}

@Singleton
@Provides
fun provideSynchronizeAdditionalFieldRepository(
@ApplicationContext appContext: Context,
@GeoNatureModuleName moduleName: String,
additionalFieldLocalDataSource: IAdditionalFieldLocalDataSource,
geoNatureAPIClient: IGeoNatureAPIClient,
): ISynchronizeAdditionalDataRepository {
return SynchronizeAdditionalFieldRepositoryImpl(
appContext,
moduleName,
additionalFieldLocalDataSource,
geoNatureAPIClient
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ class NomenclatureViewModel @Inject constructor(
*/
fun getEditableFields(
datasetId: Long? = null,
withAdditionalFields: Boolean = false,
type: EditableField.Type,
defaultPropertySettings: List<PropertySettings> = listOf(),
taxonomy: Taxonomy? = null
) {
getEditableFieldsUseCase(
GetEditableFieldsUseCase.Params(
datasetId,
withAdditionalFields,
type,
defaultPropertySettings,
taxonomy
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ class GetEditableFieldsUseCase @Inject constructor(
)
}
} +
additionalFieldRepository.getAllAdditionalFields(
if (params.withAdditionalFields) additionalFieldRepository.getAllAdditionalFields(
params.datasetId,
params.type
)
.getOrDefault(emptyList()))
.getOrDefault(emptyList()) else emptyList())

// set media type at last position
.sortedWith { a, b ->
if (a.viewType == EditableField.ViewType.MEDIA) 1
Expand Down Expand Up @@ -69,6 +70,7 @@ class GetEditableFieldsUseCase @Inject constructor(

data class Params(
val datasetId: Long? = null,
val withAdditionalFields: Boolean = false,
val type: EditableField.Type,
val settings: List<PropertySettings> = listOf(),
val taxonomy: Taxonomy? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,19 @@ class ObservationRecordViewModel @Inject constructor(
* Start edit the given [ObservationRecord].
*
* @param observationRecord the [ObservationRecord] to edit
* @param withAdditionalFields whether we want to manage additional fields
*/
fun startEdit(observationRecord: ObservationRecord) {
fun startEdit(
observationRecord: ObservationRecord,
withAdditionalFields: Boolean = false
) {
Logger.info { "loading default nomenclature values from record '${observationRecord.internalId}'..." }

editObservationRecordUseCase(
EditObservationRecordUseCase.Params(observationRecord),
EditObservationRecordUseCase.Params(
observationRecord,
withAdditionalFields
),
viewModelScope
) {
it.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class EditObservationRecordUseCase @Inject constructor(
Logger.info { "loading default nomenclature values from record '${params.observationRecord.internalId}'..." }

var result =
setDefaultNomenclatureValuesUseCase.run(SetDefaultNomenclatureValuesUseCase.Params(params.observationRecord))
setDefaultNomenclatureValuesUseCase.run(
SetDefaultNomenclatureValuesUseCase.Params(
params.observationRecord,
params.withAdditionalFields
)
)

if (result.isFailure) {
Logger.error { "failed to load default nomenclature values from record '${params.observationRecord.internalId}" }
Expand All @@ -48,5 +53,8 @@ class EditObservationRecordUseCase @Inject constructor(
return result
}

data class Params(val observationRecord: ObservationRecord)
data class Params(
val observationRecord: ObservationRecord,
val withAdditionalFields: Boolean = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(

val editableFieldsInformation =
nomenclatureRepository.getEditableFields(EditableField.Type.INFORMATION)
.getOrElse { emptyList() } + additionalFieldRepository.getAllAdditionalFields(
.getOrElse { emptyList() } + if (params.withAdditionalFields) additionalFieldRepository.getAllAdditionalFields(
observationRecord.dataset.datasetId,
EditableField.Type.INFORMATION
)
.getOrElse { emptyList() }
.getOrElse { emptyList() } else emptyList()

if (editableFieldsInformation.isEmpty()) {
Logger.warn {
Expand All @@ -67,11 +67,11 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(

val editableFieldsCounting =
nomenclatureRepository.getEditableFields(EditableField.Type.COUNTING)
.getOrElse { emptyList() } + additionalFieldRepository.getAllAdditionalFields(
.getOrElse { emptyList() } + if (params.withAdditionalFields) additionalFieldRepository.getAllAdditionalFields(
observationRecord.dataset.datasetId,
EditableField.Type.COUNTING
)
.getOrElse { emptyList() }
.getOrElse { emptyList() } else emptyList()

if (editableFieldsCounting.isEmpty()) {
Logger.warn {
Expand Down Expand Up @@ -162,5 +162,8 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(
}
}

data class Params(val observationRecord: ObservationRecord)
data class Params(
val observationRecord: ObservationRecord,
val withAdditionalFields: Boolean = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ import kotlinx.parcelize.Parcelize
*/
@Parcelize
data class NomenclatureSettings(
/**
* Whether we want to save locally and only during a session of use selected nomenclature values
* as default values.
*/
val saveDefaultValues: Boolean = false,

/**
* Whether we want to show additional fields
*/
val withAdditionalFields: Boolean = false,

val information: List<PropertySettings>,
val counting: List<PropertySettings>
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class OnAppSettingsJsonReaderListenerImpl :
}

var saveDefaultValues = false
var withAdditionalFields = false
val information = mutableListOf<PropertySettings>()
val counting = mutableListOf<PropertySettings>()

Expand All @@ -134,6 +135,7 @@ class OnAppSettingsJsonReaderListenerImpl :
while (reader.hasNext()) {
when (reader.nextName()) {
"save_default_values" -> saveDefaultValues = reader.nextBooleanOrElse { false }
"additional_fields" -> withAdditionalFields = reader.nextBooleanOrElse { false }
"information" -> information.addAll(readPropertySettingsAsList(reader))
"counting" -> counting.addAll(readPropertySettingsAsList(reader))
else -> reader.skipValue()
Expand All @@ -144,6 +146,7 @@ class OnAppSettingsJsonReaderListenerImpl :

return NomenclatureSettings(
saveDefaultValues = saveDefaultValues,
withAdditionalFields = withAdditionalFields,
information = information,
counting = counting
)
Expand Down Expand Up @@ -206,11 +209,13 @@ class OnAppSettingsJsonReaderListenerImpl :
default
)
}

JsonToken.STRING -> PropertySettings(
reader.nextString(),
visible = true,
default = true
)

else -> {
reader.skipValue()
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class HomeActivity : AppCompatActivity(),
appSettings?.dataSyncSettings?.also { dataSyncSettings ->
dataSyncViewModel.startSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields ?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)
Expand Down Expand Up @@ -201,6 +202,7 @@ class HomeActivity : AppCompatActivity(),
} else {
dataSyncViewModel.startSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields ?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)
Expand Down Expand Up @@ -402,13 +404,15 @@ class HomeActivity : AppCompatActivity(),
it.dataSyncSettings?.also { dataSyncSettings ->
dataSyncViewModel.configurePeriodicSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields ?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)

if (dataSyncViewModel.lastSynchronizedDate.value?.second == null) {
dataSyncViewModel.startSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields ?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)
Expand All @@ -434,6 +438,8 @@ class HomeActivity : AppCompatActivity(),

dataSyncViewModel.startSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields
?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)
Expand All @@ -446,6 +452,8 @@ class HomeActivity : AppCompatActivity(),

dataSyncViewModel.startSync(
dataSyncSettings,
appSettings?.nomenclatureSettings?.withAdditionalFields
?: false,
HomeActivity::class.java,
MainApplication.CHANNEL_DATA_SYNCHRONIZATION
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,15 @@ class InputPagerFragmentActivity : AbstractPagerFragmentActivity(),
R.string.pager_fragment_taxa_title to TaxaFragment.newInstance(appSettings.areaObservationDuration),
R.string.pager_fragment_information_title to InformationFragment.newInstance(
saveDefaultValues = appSettings.nomenclatureSettings?.saveDefaultValues ?: false,
withAdditionalFields = appSettings.nomenclatureSettings?.withAdditionalFields
?: false,
*appSettings.nomenclatureSettings?.information?.toTypedArray()
?: emptyArray()
),
R.string.pager_fragment_counting_title to CountingFragment.newInstance(
saveDefaultValues = appSettings.nomenclatureSettings?.saveDefaultValues ?: false,
withAdditionalFields = appSettings.nomenclatureSettings?.withAdditionalFields
?: false,
*appSettings.nomenclatureSettings?.counting?.toTypedArray()
?: emptyArray()
),
Expand Down
Loading

0 comments on commit 216c96c

Please sign in to comment.