Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Jun 14, 2020
2 parents 5ec37e1 + 397bd62 commit 788acdd
Show file tree
Hide file tree
Showing 83 changed files with 179 additions and 129 deletions.
5 changes: 4 additions & 1 deletion sync/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Sync

![PNV](https://raw.githubusercontent.com/PnX-SI/gn_mobile_core/develop/sync/src/pnv/res/mipmap-xxxhdpi/ic_launcher.png)
![PNC](https://raw.githubusercontent.com/PnX-SI/gn_mobile_core/develop/sync/src/pnc/res/mipmap-xxxhdpi/ic_launcher.png)
![PNE](https://raw.githubusercontent.com/PnX-SI/gn_mobile_core/develop/sync/src/pne/res/mipmap-xxxhdpi/ic_launcher.png)
![PNM](https://raw.githubusercontent.com/PnX-SI/gn_mobile_core/develop/sync/src/pnm/res/mipmap-xxxhdpi/ic_launcher.png)
![PNV](https://raw.githubusercontent.com/PnX-SI/gn_mobile_core/develop/sync/src/pnv/res/mipmap-xxxhdpi/ic_launcher.png)

Synchronize local database through GeoNature API:

Expand Down Expand Up @@ -35,6 +37,7 @@ Example:
| `uh_application_id` | ☐ | GeoNature application ID in UsersHub | |
| `observers_list_id` | ☐ | GeoNature selected observer list ID in UsersHub | |
| `taxa_list_id` | ☐ | GeoNature selected taxa list ID | |
| `code_area_type` | ☐ | GeoNature selected area type | |
| `page_size` | ☐ | Default page size while fetching paginated values | 1000 |
| `page_max_retry` | ☐ | Max attempt to fetch data according to given page size | 20 |

Expand Down
15 changes: 7 additions & 8 deletions sync/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"

version = "0.2.9"
version = "0.3.0"

android {
compileSdkVersion 29
Expand Down Expand Up @@ -43,12 +43,11 @@ android {

flavorDimensions "version"
productFlavors {
generic {
}
pne {
}
pnv {
}
generic {}
pnc {}
pne {}
pnm {}
pnv {}
}

testOptions {
Expand All @@ -72,7 +71,7 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.work:work-runtime:2.3.4'
implementation 'androidx.work:work-runtime-ktx:2.3.4'
implementation 'com.google.android.material:material:1.2.0-beta01'
implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
Expand Down
58 changes: 30 additions & 28 deletions sync/src/debug/res/drawable/ic_launcher_background.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="161.19403"
android:viewportHeight="161.19403">
<group
android:translateX="26.597015"
android:translateY="26.597015">
<path
android:fillColor="@color/primary"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillAlpha="0.20"
android:fillColor="#000000"
android:pathData="M55,26V25h53v1z" />
<path
android:fillAlpha="1.0"
android:fillColor="@color/accent"
android:pathData="M54,25V0h54v25z" />
<path
android:fillColor="#ffffff"
android:pathData="m62.117,10.128v4.745h0.719q1.229,0 1.875,-0.609 0.651,-0.609 0.651,-1.771 0,-1.156 -0.646,-1.76Q64.07,10.128 62.836,10.128ZM60.112,8.612h2.115q1.771,0 2.635,0.255 0.87,0.25 1.49,0.854 0.547,0.526 0.813,1.214 0.266,0.688 0.266,1.557 0,0.88 -0.266,1.573 -0.266,0.688 -0.813,1.214 -0.625,0.604 -1.5,0.859 -0.875,0.25 -2.625,0.25h-2.115z" />
<path
android:fillColor="#ffffff"
android:pathData="m68.966,8.612h5.411v1.516h-3.406v1.448h3.203v1.516h-3.203v1.781h3.521v1.516h-5.526z" />
<path
android:fillColor="#ffffff"
android:pathData="m75.331,8.612h2.016l2.063,5.74 2.057,-5.74h2.016l-2.88,7.776h-2.391z" />
</group>
android:viewportWidth="108"
android:viewportHeight="108">
<group android:scaleX="0.67"
android:scaleY="0.67">
<group
android:translateX="26.597015"
android:translateY="26.597015">
<path
android:fillColor="@color/primary"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillAlpha="0.20"
android:fillColor="#000000"
android:pathData="M55,26V25h53v1z" />
<path
android:fillAlpha="1.0"
android:fillColor="@color/accent"
android:pathData="M54,25V0h54v25z" />
<path
android:fillColor="#ffffff"
android:pathData="m62.117,10.128v4.745h0.719q1.229,0 1.875,-0.609 0.651,-0.609 0.651,-1.771 0,-1.156 -0.646,-1.76Q64.07,10.128 62.836,10.128ZM60.112,8.612h2.115q1.771,0 2.635,0.255 0.87,0.25 1.49,0.854 0.547,0.526 0.813,1.214 0.266,0.688 0.266,1.557 0,0.88 -0.266,1.573 -0.266,0.688 -0.813,1.214 -0.625,0.604 -1.5,0.859 -0.875,0.25 -2.625,0.25h-2.115z" />
<path
android:fillColor="#ffffff"
android:pathData="m68.966,8.612h5.411v1.516h-3.406v1.448h3.203v1.516h-3.203v1.781h3.521v1.516h-5.526z" />
<path
android:fillColor="#ffffff"
android:pathData="m75.331,8.612h2.016l2.063,5.74 2.057,-5.74h2.016l-2.88,7.776h-2.391z" />
</group>
</group>
</vector>
Binary file modified sync/src/debug/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-hdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-mdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sync/src/debug/res/mipmap-xxxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GeoNatureAPIClient private constructor(
private val taxHubService: TaxHubService

init {
val authManager = AuthManager(context)
val authManager = AuthManager.getInstance(context)
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BASIC
redactHeader("Authorization")
Expand Down Expand Up @@ -147,8 +147,13 @@ class GeoNatureAPIClient private constructor(
)
}

fun getTaxrefAreas(limit: Int? = null, offset: Int? = null): Call<List<TaxrefArea>> {
fun getTaxrefAreas(
codeAreaType: String? = null,
limit: Int? = null,
offset: Int? = null
): Call<List<TaxrefArea>> {
return geoNatureService.getTaxrefAreas(
codeAreaType,
limit,
offset
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface GeoNatureService {

@GET("api/synthese/color_taxon")
fun getTaxrefAreas(
@Query("code_area_type") codeAreaType: String? = null,
@Query("limit") limit: Int? = null,
@Query("offset") offset: Int? = null
): Call<List<TaxrefArea>>
Expand Down
11 changes: 5 additions & 6 deletions sync/src/main/java/fr/geonature/sync/auth/AuthManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.geonature.sync.auth

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.lifecycle.LiveData
Expand All @@ -22,10 +21,10 @@ import java.util.Calendar
*
* @author [S. Grimault](mailto:[email protected])
*/
class AuthManager(context: Context) {
class AuthManager private constructor(applicationContext: Context) {

internal val preferenceManager: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
PreferenceManager.getDefaultSharedPreferences(applicationContext)
private val authLoginJsonReader = AuthLoginJsonReader()
private val authLoginJsonWriter = AuthLoginJsonWriter()

Expand Down Expand Up @@ -120,13 +119,13 @@ class AuthManager(context: Context) {

/** Gets the singleton instance of [AuthManager].
*
* @param application The main context context.
* @param applicationContext The main application context.
*
* @return The singleton instance of [AuthManager].
*/
@Suppress("UNCHECKED_CAST")
fun getInstance(application: Application): AuthManager = INSTANCE ?: synchronized(this) {
INSTANCE ?: AuthManager(application).also { INSTANCE = it }
fun getInstance(applicationContext: Context): AuthManager = INSTANCE ?: synchronized(this) {
INSTANCE ?: AuthManager(applicationContext).also { INSTANCE = it }
}
}
}
3 changes: 3 additions & 0 deletions sync/src/main/java/fr/geonature/sync/settings/AppSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ data class AppSettings(
var applicationId: Int = 0,
var usersListId: Int = 0,
var taxrefListId: Int = 0,
var codeAreaType: String? = null,
var pageSize: Int = DEFAULT_PAGE_SIZE,
var pageMaxRetry: Int = DEFAULT_PAGE_MAX_RETRY
) : IAppSettings {
Expand All @@ -25,6 +26,7 @@ data class AppSettings(
source.readInt(),
source.readInt(),
source.readInt(),
source.readString(),
source.readInt(),
source.readInt()
)
Expand All @@ -43,6 +45,7 @@ data class AppSettings(
it.writeInt(applicationId)
it.writeInt(usersListId)
it.writeInt(taxrefListId)
it.writeString(codeAreaType)
it.writeInt(pageSize)
it.writeInt(pageMaxRetry)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class OnAppSettingsJsonReaderListenerImpl :
"uh_application_id" -> appSettings.applicationId = reader.nextInt()
"observers_list_id" -> appSettings.usersListId = reader.nextInt()
"taxa_list_id" -> appSettings.taxrefListId = reader.nextInt()
"code_area_type" -> appSettings.codeAreaType = reader.nextString()
"page_size" -> appSettings.pageSize = reader.nextInt()
"page_max_retry" -> appSettings.pageMaxRetry = reader.nextInt()
else -> reader.skipValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application
DataSyncWorker.INPUT_TAXREF_LIST_ID,
appSettings.taxrefListId
)
.putString(
DataSyncWorker.INPUT_CODE_AREA_TYPE,
appSettings.codeAreaType
)
.putInt(
DataSyncWorker.INPUT_PAGE_SIZE,
appSettings.pageSize
Expand All @@ -112,7 +116,7 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application

val continuation = workManager.beginUniqueWork(
DataSyncWorker.DATA_SYNC_WORKER,
ExistingWorkPolicy.KEEP,
ExistingWorkPolicy.REPLACE,
dataSyncWorkRequest
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PackageInfoViewModel(application: Application) : AndroidViewModel(applicat
_appSettingsUpdated.postValue(true)
}

if (it.versionCode > BuildConfig.VERSION_CODE) {
if (!it.apkUrl.isNullOrEmpty()) {
value = it
}
}
Expand Down
73 changes: 41 additions & 32 deletions sync/src/main/java/fr/geonature/sync/sync/worker/DataSyncWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class DataSyncWorker(
INPUT_TAXREF_LIST_ID,
0
),
inputData.getString(INPUT_CODE_AREA_TYPE),
inputData.getInt(
INPUT_PAGE_SIZE,
AppSettings.DEFAULT_PAGE_SIZE
Expand Down Expand Up @@ -196,12 +197,12 @@ class DataSyncWorker(

val users = response.body() ?: return Result.failure()
val inputObservers = users.map {
InputObserver(
it.id,
it.lastname,
it.firstname
)
}
InputObserver(
it.id,
it.lastname,
it.firstname
)
}
.toTypedArray()

Log.i(
Expand Down Expand Up @@ -272,6 +273,7 @@ class DataSyncWorker(
private suspend fun syncTaxa(
geoNatureServiceClient: GeoNatureAPIClient,
listId: Int,
codeAreaType: String?,
pageSize: Int,
pageMaxRetry: Int
): Result {
Expand All @@ -284,10 +286,10 @@ class DataSyncWorker(
// fetch all taxa from paginated list
do {
val taxrefResponse = geoNatureServiceClient.getTaxref(
listId,
pageSize,
offset
)
listId,
pageSize,
offset
)
.awaitResponse()

if (checkResponse(taxrefResponse) is Result.Failure) {
Expand Down Expand Up @@ -351,9 +353,10 @@ class DataSyncWorker(
// fetch all taxa metadata from paginated list
do {
val taxrefAreasResponse = geoNatureServiceClient.getTaxrefAreas(
pageSize,
offset
)
codeAreaType,
pageSize,
offset
)
.awaitResponse()

if (checkResponse(taxrefAreasResponse) is Result.Failure) {
Expand All @@ -368,6 +371,11 @@ class DataSyncWorker(
continue
}

Log.i(
TAG,
"found ${taxrefAreas.size} taxa with areas from offset $offset"
)

val taxonAreas = taxrefAreas.asSequence()
.filter { taxrefArea -> validTaxaIds.any { it == taxrefArea.taxrefId } }
.map {
Expand All @@ -388,7 +396,7 @@ class DataSyncWorker(

Log.i(
TAG,
"taxa with areas to update: ${offset + taxonAreas.size}"
"updating ${taxonAreas.size} taxa with areas from offset $offset"
)

offset += pageSize
Expand Down Expand Up @@ -427,12 +435,12 @@ class DataSyncWorker(
.filter { it.nomenclatures.isNotEmpty() }

val nomenclatureTypesToUpdate = validNomenclatureTypesToUpdate.map {
NomenclatureType(
it.id,
it.mnemonic,
it.defaultLabel
)
}
NomenclatureType(
it.id,
it.mnemonic,
it.defaultLabel
)
}
.toList()
.toTypedArray()

Expand All @@ -451,18 +459,18 @@ class DataSyncWorker(
)

val nomenclaturesToUpdate = validNomenclatureTypesToUpdate.map { nomenclatureType ->
nomenclatureType.nomenclatures.asSequence()
.filter { it.id > 0 }
.map {
Nomenclature(
it.id,
it.code,
if (TextUtils.isEmpty(it.hierarchy)) nomenclatureType.id.toString() else it.hierarchy!!,
it.defaultLabel,
nomenclatureType.id
)
}
}
nomenclatureType.nomenclatures.asSequence()
.filter { it.id > 0 }
.map {
Nomenclature(
it.id,
it.code,
if (TextUtils.isEmpty(it.hierarchy)) nomenclatureType.id.toString() else it.hierarchy!!,
it.defaultLabel,
nomenclatureType.id
)
}
}
.flatMap { it.asSequence() }
.toList()
.toTypedArray()
Expand Down Expand Up @@ -620,6 +628,7 @@ class DataSyncWorker(

const val INPUT_USERS_MENU_ID = "usersMenuId"
const val INPUT_TAXREF_LIST_ID = "taxrefListId"
const val INPUT_CODE_AREA_TYPE = "codeAreaType"
const val INPUT_PAGE_SIZE = "pageSize"
const val INPUT_PAGE_MAX_RETRY = "pageMaxRetry"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class HomeActivity : AppCompatActivity() {
adapter.clear(false)
progressBar?.visibility = View.VISIBLE

delay(250)
delay(500)

dataSyncViewModel.startSync(appSettings)

Expand Down
Loading

0 comments on commit 788acdd

Please sign in to comment.