Skip to content

Commit

Permalink
Merge branch 'release/0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Feb 2, 2020
2 parents 7848712 + c953a88 commit 3f7cb04
Show file tree
Hide file tree
Showing 85 changed files with 327 additions and 203 deletions.
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions commons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

version = "0.5.8"
version = "0.5.9"

android {
compileSdkVersion 28
Expand Down Expand Up @@ -46,7 +46,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"

implementation 'com.google.android.material:material:1.0.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-rc03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
api 'androidx.room:room-runtime:2.2.3'
Expand Down
14 changes: 8 additions & 6 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.1"
version = "0.2.2"

android {
compileSdkVersion 28
Expand Down Expand Up @@ -39,6 +39,8 @@ android {

flavorDimensions "version"
productFlavors {
pnx {
}
pne {
}
pnv {
Expand All @@ -59,14 +61,14 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.core:core-ktx:1.2.0-rc01'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-rc03"
implementation 'androidx.core:core-ktx:1.3.0-alpha01'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'androidx.work:work-runtime:2.2.0'
implementation 'androidx.work:work-runtime-ktx:2.2.0'
implementation 'androidx.work:work-runtime:2.3.0'
implementation 'androidx.work:work-runtime-ktx:2.3.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
Expand Down
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.
73 changes: 63 additions & 10 deletions sync/src/main/java/fr/geonature/sync/api/GeoNatureAPIClient.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.geonature.sync.api

import android.content.Context
import android.util.Log
import com.google.gson.GsonBuilder
import fr.geonature.sync.api.model.AuthCredentials
import fr.geonature.sync.api.model.AuthLogin
Expand All @@ -9,6 +10,8 @@ import fr.geonature.sync.api.model.Taxref
import fr.geonature.sync.api.model.TaxrefArea
import fr.geonature.sync.api.model.User
import fr.geonature.sync.auth.AuthManager
import fr.geonature.sync.util.SettingsUtils.getGeoNatureServerUrl
import fr.geonature.sync.util.SettingsUtils.getTaxHubServerUrl
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.RequestBody
Expand All @@ -19,6 +22,7 @@ import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
* GeoNature API client.
Expand All @@ -27,9 +31,11 @@ import retrofit2.converter.gson.GsonConverterFactory
*/
class GeoNatureAPIClient private constructor(
context: Context,
baseUrl: String
val geoNatureBaseUrl: String,
taxHubBaseUrl: String
) {
private val geoNatureService: GeoNatureService
private val taxHubService: TaxHubService

init {
val authManager = AuthManager(context)
Expand All @@ -40,6 +46,14 @@ class GeoNatureAPIClient private constructor(
}

val client = OkHttpClient.Builder()
.readTimeout(
60,
TimeUnit.SECONDS
)
.connectTimeout(
60,
TimeUnit.SECONDS
)
.addInterceptor(loggingInterceptor)
// save cookie interceptor
.addInterceptor { chain ->
Expand Down Expand Up @@ -70,13 +84,19 @@ class GeoNatureAPIClient private constructor(
}
.build()

val retrofit = Retrofit.Builder()
.baseUrl("$baseUrl/")
geoNatureService = Retrofit.Builder()
.baseUrl("$geoNatureBaseUrl/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()))
.build()
.create(GeoNatureService::class.java)

geoNatureService = retrofit.create(GeoNatureService::class.java)
taxHubService = Retrofit.Builder()
.baseUrl("$taxHubBaseUrl/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()))
.build()
.create(TaxHubService::class.java)
}

suspend fun authLogin(authCredentials: AuthCredentials): Response<AuthLogin> {
Expand Down Expand Up @@ -105,11 +125,11 @@ class GeoNatureAPIClient private constructor(
}

fun getTaxonomyRanks(): Call<ResponseBody> {
return geoNatureService.getTaxonomyRanks()
return taxHubService.getTaxonomyRanks()
}

fun getTaxref(): Call<List<Taxref>> {
return geoNatureService.getTaxref()
return taxHubService.getTaxref()
}

fun getTaxrefAreas(): Call<List<TaxrefArea>> {
Expand All @@ -126,12 +146,45 @@ class GeoNatureAPIClient private constructor(

companion object {

private val TAG = GeoNatureAPIClient::class.java.name

private val baseUrl: (String?) -> String? = { url ->
url?.also { if (it.endsWith('/')) it.dropLast(1) }
}

fun instance(
context: Context,
baseUrl: String
): Lazy<GeoNatureAPIClient> = lazy {
GeoNatureAPIClient(context,
baseUrl.also { if (it.endsWith('/')) it.dropLast(1) })
geoNatureBaseUrl: String? = null,
taxHubBaseUrl: String? = null
): GeoNatureAPIClient? {
val sanitizeGeoNatureBaseUrl =
baseUrl(if (geoNatureBaseUrl.isNullOrBlank()) getGeoNatureServerUrl(context) else geoNatureBaseUrl)
val sanitizeTaxHubBaseUrl =
baseUrl(if (taxHubBaseUrl.isNullOrBlank()) getTaxHubServerUrl(context) else taxHubBaseUrl)

if (sanitizeGeoNatureBaseUrl.isNullOrBlank()) {
Log.w(
TAG,
"No GeoNature server configured"
)

return null
}

if (sanitizeTaxHubBaseUrl.isNullOrBlank()) {
Log.w(
TAG,
"No TaxHub server configured"
)

return null
}

return GeoNatureAPIClient(
context,
sanitizeGeoNatureBaseUrl,
sanitizeTaxHubBaseUrl
)
}
}
}
21 changes: 7 additions & 14 deletions sync/src/main/java/fr/geonature/sync/api/GeoNatureService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fr.geonature.sync.api
import fr.geonature.sync.api.model.AuthCredentials
import fr.geonature.sync.api.model.AuthLogin
import fr.geonature.sync.api.model.NomenclatureType
import fr.geonature.sync.api.model.Taxref
import fr.geonature.sync.api.model.TaxrefArea
import fr.geonature.sync.api.model.User
import okhttp3.RequestBody
Expand All @@ -22,39 +21,33 @@ import retrofit2.http.Path
*/
interface GeoNatureService {

@POST("geonature/api/auth/login")
@POST("api/auth/login")
suspend fun authLogin(
@Body
authCredentials: AuthCredentials
): Response<AuthLogin>

@POST("geonature/api/{module}/releve")
@POST("api/{module}/releve")
fun sendInput(
@Path("module")
module: String,
@Body
input: RequestBody
): Call<ResponseBody>

@GET("geonature/api/meta/datasets")
@GET("api/meta/datasets")
fun getMetaDatasets(): Call<ResponseBody>

@GET("geonature/api/users/menu/1")
@GET("api/users/menu/1")
fun getUsers(): Call<List<User>>

@GET("taxhub/api/taxref/regnewithgroupe2")
fun getTaxonomyRanks(): Call<ResponseBody>

@GET("taxhub/api/taxref/allnamebylist/100")
fun getTaxref(): Call<List<Taxref>>

@GET("geonature/api/synthese/color_taxon")
@GET("api/synthese/color_taxon")
fun getTaxrefAreas(): Call<List<TaxrefArea>>

@GET("geonature/api/nomenclatures/nomenclatures/taxonomy")
@GET("api/nomenclatures/nomenclatures/taxonomy")
fun getNomenclatures(): Call<List<NomenclatureType>>

@GET("geonature/api/{module}/defaultNomenclatures")
@GET("api/{module}/defaultNomenclatures")
fun getDefaultNomenclaturesValues(
@Path("module")
module: String
Expand Down
21 changes: 21 additions & 0 deletions sync/src/main/java/fr/geonature/sync/api/TaxHubService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.geonature.sync.api

import fr.geonature.sync.api.model.Taxref
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.GET

/**
* TaxHub API interface definition.
*
* @author [S. Grimault](mailto:[email protected])
*/
interface TaxHubService {

@GET("api/taxref/regnewithgroupe2")
fun getTaxonomyRanks(): Call<ResponseBody>

// TODO: fetch all taxa
@GET("api/taxref/allnamebylist/100")
fun getTaxref(): Call<List<Taxref>>
}
33 changes: 20 additions & 13 deletions sync/src/main/java/fr/geonature/sync/auth/AuthLoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.geonature.sync.auth
import android.app.Application
import android.text.TextUtils
import androidx.annotation.StringRes
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -15,7 +16,6 @@ import fr.geonature.sync.api.GeoNatureAPIClient
import fr.geonature.sync.api.model.AuthCredentials
import fr.geonature.sync.api.model.AuthLogin
import fr.geonature.sync.api.model.AuthLoginError
import fr.geonature.sync.util.SettingsUtils
import kotlinx.coroutines.launch
import retrofit2.Response

Expand All @@ -24,10 +24,10 @@ import retrofit2.Response
*
* @author [S. Grimault](mailto:[email protected])
*/
class AuthLoginViewModel(application: Application) : ViewModel() {
class AuthLoginViewModel(application: Application) : AndroidViewModel(application) {

private val authManager: AuthManager = AuthManager.getInstance(application)
private var geoNatureAPIClient: GeoNatureAPIClient? = null
private val geoNatureAPIClient: GeoNatureAPIClient? = GeoNatureAPIClient.instance(application)

private val _loginFormState = MutableLiveData<LoginFormState>()
val loginFormState: LiveData<LoginFormState> = _loginFormState
Expand All @@ -38,22 +38,25 @@ class AuthLoginViewModel(application: Application) : ViewModel() {
val isLoggedIn: LiveData<Boolean> = authManager.isLoggedIn

init {
SettingsUtils.getGeoNatureServerUrl(application)
?.also {
geoNatureAPIClient = GeoNatureAPIClient.instance(
application,
it
)
.value
}
if (geoNatureAPIClient == null) {
_loginResult.value = LoginResult(error = R.string.login_failed_server_url_configuration)
_loginFormState.value = LoginFormState(
isValid = false,
usernameError = null,
passwordError = null
)
}
}

fun login(
username: String,
password: String,
applicationId: Int
) {
val geoNatureAPIClient = geoNatureAPIClient ?: return
if (geoNatureAPIClient == null) {
_loginResult.value = LoginResult(error = R.string.login_failed_server_url_configuration)
return
}

viewModelScope.launch {
try {
Expand Down Expand Up @@ -112,7 +115,11 @@ class AuthLoginViewModel(application: Application) : ViewModel() {
return
}

_loginFormState.value = LoginFormState(isValid = true)
_loginFormState.value = LoginFormState(
isValid = true,
usernameError = null,
passwordError = null
)
}

fun logout(): LiveData<Boolean> {
Expand Down
Loading

0 comments on commit 3f7cb04

Please sign in to comment.