Skip to content

Commit

Permalink
Merge branch 'release/1.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Jun 23, 2021
2 parents 70bb4b9 + 099ca0e commit a35ea2c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion sync/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "kotlin-kapt"

version = "1.2.0"
version = "1.2.1"

android {
compileSdkVersion 29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.Cookie
Expand Down Expand Up @@ -71,7 +71,12 @@ class GeoNatureAPIClient private constructor(
.getCookie()
?.let {
if (it.expiresAt() < System.currentTimeMillis()) {
GlobalScope.launch(IO) {
Log.i(
TAG,
"cookie expiry date ${it.expiresAt()} reached: perform logout"
)

GlobalScope.launch(Default) {
authManager.logout()
}

Expand Down
62 changes: 44 additions & 18 deletions sync/src/main/java/fr/geonature/sync/auth/AuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fr.geonature.sync.auth

import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
Expand All @@ -10,8 +12,8 @@ import fr.geonature.sync.api.model.AuthLogin
import fr.geonature.sync.auth.io.AuthLoginJsonReader
import fr.geonature.sync.auth.io.AuthLoginJsonWriter
import fr.geonature.sync.auth.io.CookieHelper
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import fr.geonature.sync.sync.worker.CheckAuthLoginWorker
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -26,20 +28,24 @@ import java.util.Calendar
class AuthManager private constructor(applicationContext: Context) {

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

private val _authLogin = MutableLiveData<AuthLogin?>()
private var cookie: Cookie? = null

val isLoggedIn: LiveData<Boolean> = Transformations.map(_authLogin) { it != null }

init {
GlobalScope.launch(Main) {
GlobalScope.launch(Default) {
getAuthLogin()
}
}

fun setCookie(cookie: Cookie) {
this.cookie = cookie

preferenceManager
.edit()
.putString(
Expand All @@ -50,18 +56,23 @@ class AuthManager private constructor(applicationContext: Context) {
}

fun getCookie(): Cookie? {
return runCatching {
preferenceManager
.getString(
KEY_PREFERENCE_COOKIE,
null
)
?.let { CookieHelper.deserialize(it) }
}.getOrNull()
return cookie
?: runCatching {
preferenceManager
.getString(
KEY_PREFERENCE_COOKIE,
null
)
?.let { CookieHelper.deserialize(it) }
}.getOrNull()
}

suspend fun getAuthLogin(): AuthLogin? =
withContext(IO) {
withContext(Default) {
val authLogin = _authLogin.value

if (authLogin != null) return@withContext authLogin

val authLoginAsJson = preferenceManager.getString(
KEY_PREFERENCE_AUTH_LOGIN,
null
Expand All @@ -76,6 +87,11 @@ class AuthManager private constructor(applicationContext: Context) {
.read(authLoginAsJson)
.let {
if (it?.expires?.before(Calendar.getInstance().time) == true) {
Log.i(
TAG,
"auth login expiry date ${it.expires} reached: perform logout"
)

logout()
return@let null
}
Expand All @@ -85,29 +101,37 @@ class AuthManager private constructor(applicationContext: Context) {
}
}

suspend fun setAuthLogin(authLogin: AuthLogin): Boolean =
withContext(IO) {
suspend fun setAuthLogin(authLogin: AuthLogin): Boolean {
Log.i(
TAG,
"successfully authenticated, login expiration date: ${authLogin.expires}"
)

_authLogin.value = authLogin

return withContext(Default) {

val authLoginAsJson = authLoginJsonWriter.write(authLogin)

if (authLoginAsJson.isNullOrBlank()) {
_authLogin.postValue(null)
return@withContext false
}

notificationManager.cancel(CheckAuthLoginWorker.NOTIFICATION_ID)

preferenceManager
.edit()
.putString(
KEY_PREFERENCE_AUTH_LOGIN,
authLoginAsJson
)
.commit()
.also {
_authLogin.postValue(if (it) authLogin else null)
}
}
}

suspend fun logout(): Boolean =
withContext(IO) {
withContext(Default) {
preferenceManager
.edit()
.remove(KEY_PREFERENCE_COOKIE)
Expand All @@ -121,6 +145,8 @@ class AuthManager private constructor(applicationContext: Context) {
}

companion object {
private val TAG = AuthManager::class.java.name

private const val KEY_PREFERENCE_COOKIE = "key_preference_cookie"
private const val KEY_PREFERENCE_AUTH_LOGIN = "key_preference_auth_login"

Expand Down
2 changes: 1 addition & 1 deletion sync/src/main/java/fr/geonature/sync/data/LocalDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import kotlinx.coroutines.withContext
NomenclatureTaxonomy::class,
DefaultNomenclature::class
],
version = 18,
version = 19,
exportSchema = false
)
abstract class LocalDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AppSettingsJsonWriter(private val context: Context) {

return
}

val appRootFolder = getRootFolder(
context,
MountPoint.StorageType.INTERNAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class HomeActivity : AppCompatActivity() {
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
menu?.run {
findItem(R.id.menu_sync_data_refresh)?.also {
it.isEnabled = appSettings != null && dataSyncViewModel.isSyncRunning.value != true
it.isEnabled = isLoggedIn && appSettings != null && dataSyncViewModel.isSyncRunning.value != true
}
findItem(R.id.menu_login)?.also {
it.isEnabled = appSettings != null
Expand Down
4 changes: 2 additions & 2 deletions sync/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sat Jun 12 16:36:37 CEST 2021
VERSION_CODE=2950
#Wed Jun 23 22:07:11 CEST 2021
VERSION_CODE=2980

0 comments on commit a35ea2c

Please sign in to comment.