Skip to content

Commit

Permalink
fix: #24
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Aug 22, 2020
1 parent 99d4bec commit 4f02d43
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ class AuthLoginViewModel(application: Application) : AndroidViewModel(applicatio
return@launch
}

authManager.setAuthLogin(authLogin)
_loginResult.value = LoginResult(success = authLogin)
authManager.setAuthLogin(authLogin).also {
_loginResult.value = LoginResult(success = authLogin)
}
} catch (e: Exception) {
_loginResult.value = LoginResult(error = R.string.login_failed)
}
Expand Down
2 changes: 0 additions & 2 deletions sync/src/main/java/fr/geonature/sync/auth/AuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class AuthManager private constructor(applicationContext: Context) {
return@withContext false
}

_authLogin.postValue(authLogin)

preferenceManager.edit()
.putString(
KEY_PREFERENCE_AUTH_LOGIN,
Expand Down
61 changes: 27 additions & 34 deletions sync/src/main/java/fr/geonature/sync/sync/DataSyncViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -33,36 +34,9 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application
it.getLastSynchronizedDate()
}

val dataSyncStatus: LiveData<DataSyncStatus?> =
map(workManager.getWorkInfosByTagLiveData(DataSyncWorker.DATA_SYNC_WORKER_TAG)) { workInfos ->
val workInfo = workInfos.firstOrNull() ?: return@map null

val serverStatus = ServerStatus.values()[workInfo.progress.getInt(
DataSyncWorker.KEY_SERVER_STATUS,
workInfo.outputData.getInt(
DataSyncWorker.KEY_SERVER_STATUS,
ServerStatus.OK.ordinal
)
)]

if (arrayOf(
ServerStatus.FORBIDDEN,
ServerStatus.INTERNAL_SERVER_ERROR
).contains(serverStatus)
) {
cancelTasks()
}

DataSyncStatus(
workInfo.state,
workInfo.progress.getString(DataSyncWorker.KEY_SYNC_MESSAGE)
?: workInfo.outputData.getString(DataSyncWorker.KEY_SYNC_MESSAGE),
serverStatus
)
}
val lastSynchronizedDate: LiveData<Date?> = dataSyncManager.lastSynchronizedDate

fun startSync(appSettings: AppSettings) {
fun startSync(appSettings: AppSettings): LiveData<DataSyncStatus?> {
val lastSynchronizedDate = dataSyncManager.lastSynchronizedDate.value

if (lastSynchronizedDate?.add(
Expand All @@ -76,7 +50,7 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application
"data already synchronized at ${lastSynchronizedDate.toIsoDateString()}"
)

return
return MutableLiveData(null)
}

val constraints = Constraints.Builder()
Expand All @@ -85,6 +59,7 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application

val dataSyncWorkRequest = OneTimeWorkRequest.Builder(DataSyncWorker::class.java)
.addTag(DataSyncWorker.DATA_SYNC_WORKER_TAG)
.setConstraints(constraints)
.setInputData(
Data.Builder()
.putInt(
Expand All @@ -109,9 +84,6 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application
)
.build()
)
.setConstraints(
constraints
)
.build()

val continuation = workManager.beginUniqueWork(
Expand All @@ -120,8 +92,29 @@ class DataSyncViewModel(application: Application) : AndroidViewModel(application
dataSyncWorkRequest
)

// start the work
continuation.enqueue()
return map(workManager.getWorkInfoByIdLiveData(dataSyncWorkRequest.id)) {
if (it == null) {
return@map null
}

val serverStatus = ServerStatus.values()[it.progress.getInt(
DataSyncWorker.KEY_SERVER_STATUS,
it.outputData.getInt(
DataSyncWorker.KEY_SERVER_STATUS,
ServerStatus.OK.ordinal
)
)]

DataSyncStatus(
it.state,
it.progress.getString(DataSyncWorker.KEY_SYNC_MESSAGE)
?: it.outputData.getString(DataSyncWorker.KEY_SYNC_MESSAGE),
serverStatus
)
}.also {
// start the work
continuation.enqueue()
}
}

fun cancelTasks() {
Expand Down
13 changes: 11 additions & 2 deletions sync/src/main/java/fr/geonature/sync/sync/PackageInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ data class PackageInfo(
val packageName: String,
val label: String,
val versionCode: Long,
val localVersionCode: Long = 0,
val versionName: String? = null,
val apkUrl: String? = null,
val icon: Drawable? = null,
val launchIntent: Intent? = null
): Comparable<PackageInfo> {
) : Comparable<PackageInfo> {
var inputs: Int = 0
var state: WorkInfo.State = WorkInfo.State.ENQUEUED
var apkUrl: String? = null
var settings: Any? = null

override fun compareTo(other: PackageInfo): Int {
return packageName.compareTo(other.packageName)
}

fun isAvailableForInstall(): Boolean {
return localVersionCode == 0L && !apkUrl.isNullOrEmpty()
}

fun hasNewVersionAvailable(): Boolean {
return versionCode > localVersionCode && !apkUrl.isNullOrEmpty()
}
}
49 changes: 24 additions & 25 deletions sync/src/main/java/fr/geonature/sync/sync/PackageInfoManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class PackageInfoManager private constructor(private val applicationContext: Con
*/
@SuppressLint("DefaultLocale")
suspend fun getAvailableApplications(): List<PackageInfo> = withContext(IO) {
availablePackageInfos.clear()

val availableAppPackages = try {
val geoNatureAPIClient = GeoNatureAPIClient.instance(applicationContext)
val response = geoNatureAPIClient?.getApplications()
Expand All @@ -59,35 +61,34 @@ class PackageInfoManager private constructor(private val applicationContext: Con

availableAppPackages.asSequence()
.map {
availablePackageInfos[it.packageName]?.copy()
?.apply {
apkUrl =
if (launchIntent == null || (versionCode < it.versionCode)) it.apkUrl else null
settings = it.settings
}
?: PackageInfo(
it.packageName,
it.code.toLowerCase()
.capitalize(),
it.versionCode.toLong()
).apply {
apkUrl = it.apkUrl
settings = it.settings
}
PackageInfo(
it.packageName,
it.code.toLowerCase()
.capitalize(),
it.versionCode.toLong(),
0,
null,
it.apkUrl
).apply {
settings = it.settings
}
}
.onEach {
availablePackageInfos[it.packageName] = it
}
.toList()
.also {
packageInfos.postValue(availablePackageInfos.values.toList())
getInstalledApplications()
}
}

/**
* Finds all compatible installed applications.
*/
suspend fun getInstalledApplications(): List<PackageInfo> = withContext(IO) {
val allPackageInfos = mutableMapOf<String, PackageInfo>()
allPackageInfos.putAll(availablePackageInfos)

pm.getInstalledApplications(PackageManager.GET_META_DATA)
.asSequence()
.filter { it.packageName.startsWith(sharedUserId) }
Expand All @@ -97,31 +98,29 @@ class PackageInfoManager private constructor(private val applicationContext: Con
PackageManager.GET_META_DATA
)

val existingPackageInfo = availablePackageInfos[it.packageName]

@Suppress("DEPRECATION")
PackageInfo(
it.packageName,
pm.getApplicationLabel(it)
.toString(),
existingPackageInfo?.versionCode ?: 0,
if (isPostPie) packageInfoFromPackageManager.longVersionCode else packageInfoFromPackageManager.versionCode.toLong(),
packageInfoFromPackageManager.versionName,
existingPackageInfo?.apkUrl,
pm.getApplicationIcon(it.packageName),
pm.getLaunchIntentForPackage(it.packageName)
).apply {
val existingPackageInfo = availablePackageInfos[it.packageName]

if (existingPackageInfo != null) {
apkUrl =
if (existingPackageInfo.versionCode > versionCode) existingPackageInfo.apkUrl else null
settings = existingPackageInfo.settings
}
settings = existingPackageInfo?.settings
}
}
.onEach {
availablePackageInfos[it.packageName] = it
allPackageInfos[it.packageName] = it
}
.toList()
.also {
packageInfos.postValue(availablePackageInfos.values.toList())
packageInfos.postValue(allPackageInfos.values.toList())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class PackageInfoViewModel(application: Application) : AndroidViewModel(applicat
.map { packageInfo ->
packageInfo.copy()
.apply {
apkUrl = packageInfo.apkUrl
settings = packageInfo.settings

val workInfo =
workInfos.firstOrNull { workInfo -> workInfo.progress.getString(InputsSyncWorker.KEY_PACKAGE_NAME) == packageName }
?: workInfos.firstOrNull { workInfo -> workInfo.outputData.getString(InputsSyncWorker.KEY_PACKAGE_NAME) == packageName }
Expand Down Expand Up @@ -91,7 +91,7 @@ class PackageInfoViewModel(application: Application) : AndroidViewModel(applicat
_appSettingsUpdated.postValue(true)
}

if (!it.apkUrl.isNullOrEmpty()) {
if (it.hasNewVersionAvailable()) {
value = it
}
}
Expand All @@ -104,16 +104,16 @@ class PackageInfoViewModel(application: Application) : AndroidViewModel(applicat
/**
* Checks if we can perform an update of existing apps.
*/
fun checkAppPackages() {
fun getAvailableApplications() {
viewModelScope.launch {
packageInfoManager.getAvailableApplications()
}
}

/**
* Gets all compatible installed applications.
* Synchronize all compatible installed applications.
*/
fun getInstalledApplicationsToSynchronize() {
fun synchronizeInstalledApplications() {
viewModelScope.launch {
packageInfoManager.getInstalledApplications()
.asSequence()
Expand Down
Loading

0 comments on commit 4f02d43

Please sign in to comment.