Skip to content

Commit

Permalink
- add value set identifier to local DB; (eu-digital-green-certificate…
Browse files Browse the repository at this point in the history
…s#217)

* - add value set identifier to local DB;

* - add worker retry policy;

* - user agent update;
  • Loading branch information
MykhailoNester authored Sep 22, 2021
1 parent 9513880 commit dd4ed0a
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class DgcaApplication : Application(), Configuration.Provider {
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MAX_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
)
.build()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import dgca.verifier.app.android.data.local.countries.CountriesDao
import dgca.verifier.app.android.data.local.countries.CountryLocal
import dgca.verifier.app.android.data.local.valuesets.ValueSetIdentifierLocal
import dgca.verifier.app.android.data.local.valuesets.ValueSetLocal
import dgca.verifier.app.android.data.local.valuesets.ValueSetsDao

@Database(
entities = [RuleIdentifierLocal::class, RuleLocal::class, DescriptionLocal::class, CountryLocal::class, ValueSetLocal::class],
entities = [
RuleIdentifierLocal::class,
RuleLocal::class,
DescriptionLocal::class,
CountryLocal::class,
ValueSetLocal::class,
ValueSetIdentifierLocal::class
],
version = 1
)
@TypeConverters(Converters::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,36 @@
package dgca.verifier.app.android.data.local.valuesets

import dgca.verifier.app.engine.data.ValueSet
import dgca.verifier.app.engine.data.ValueSetIdentifier
import dgca.verifier.app.engine.data.source.local.valuesets.ValueSetsLocalDataSource

class DefaultValueSetsLocalDataSource(private val dao: ValueSetsDao) : ValueSetsLocalDataSource {
class DefaultValueSetsLocalDataSource(
private val dao: ValueSetsDao
) : ValueSetsLocalDataSource {

override suspend fun updateValueSets(valueSets: List<ValueSet>) {
dao.apply {
deleteAll()
insert(*valueSets.toValueSetsLocal().toTypedArray())
insert(*valueSets.map { it.toValueSetLocal() }.toTypedArray())
}
}

override suspend fun getValueSets(): List<ValueSet> = dao.getAll().toValueSets()
override suspend fun addValueSets(
valueSetIdentifiers: List<ValueSetIdentifier>,
valueSets: List<ValueSet>
) {
dao.insertSets(
*valueSetIdentifiers.map { it.toValueSetIdentifierLocal() }.toTypedArray(),
*valueSets.map { it.toValueSetLocal() }.toTypedArray()
)
}

override suspend fun removeValueSetsBy(setIds: List<String>) {
dao.deleteSetsBy(setIds)
}

override suspend fun getValueSets(): List<ValueSet> = dao.getAll().map { it.toValueSet() }

override suspend fun getValueSetIdentifiers(): List<ValueSetIdentifier> =
dao.getAllIdentifiers().map { it.toValueSetIdentifier() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* ---license-start
* eu-digital-green-certificates / dgca-verifier-app-android
* ---
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*
* Created by mykhailo.nester on 21/09/2021, 22:19
*/

package dgca.verifier.app.android.data.local.valuesets

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "value_set_identifier")
class ValueSetIdentifierLocal(
@PrimaryKey
val valueSetIdentifierId: String,
val valueSetHash: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import java.time.LocalDate

@Entity(tableName = "valuesets")
class ValueSetLocal(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@PrimaryKey
val valueSetId: String,
val valueSetDate: LocalDate,
val valueSetValues: JsonNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,26 @@
package dgca.verifier.app.android.data.local.valuesets

import dgca.verifier.app.engine.data.ValueSet
import dgca.verifier.app.engine.data.ValueSetIdentifier

fun ValueSet.toValueSetLocal(): ValueSetLocal = ValueSetLocal(
valueSetId = this.valueSetId,
valueSetDate = this.valueSetDate,
valueSetValues = this.valueSetValues
)


fun ValueSetLocal.toValueSet(): ValueSet = ValueSet(
valueSetId = this.valueSetId,
valueSetDate = this.valueSetDate,
valueSetValues = this.valueSetValues
)

fun List<ValueSetLocal>.toValueSets(): List<ValueSet> {
val valueSets = mutableListOf<ValueSet>()
forEach {
valueSets.add(it.toValueSet())
}
return valueSets
}
fun ValueSetIdentifier.toValueSetIdentifierLocal(): ValueSetIdentifierLocal = ValueSetIdentifierLocal(
valueSetIdentifierId = id,
valueSetHash = hash
)

fun List<ValueSet>.toValueSetsLocal(): List<ValueSetLocal> {
val valueSetsLocal = mutableListOf<ValueSetLocal>()
forEach {
valueSetsLocal.add(it.toValueSetLocal())
}
return valueSetsLocal
}
fun ValueSetIdentifierLocal.toValueSetIdentifier(): ValueSetIdentifier = ValueSetIdentifier(
id = valueSetIdentifierId,
hash = valueSetHash
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,47 @@ package dgca.verifier.app.android.data.local.valuesets
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction

@Dao
abstract class ValueSetsDao {

@Query("SELECT * from valuesets")
abstract fun getAll(): List<ValueSetLocal>

@Query("SELECT * from value_set_identifier")
abstract fun getAllIdentifiers(): List<ValueSetIdentifierLocal>

@Insert
abstract fun insert(vararg valueSetsLocal: ValueSetLocal)

@Query("DELETE FROM valuesets")
abstract fun deleteAll()

@Query("DELETE FROM valuesets WHERE valueSetId IN (:setIds)")
abstract fun deleteValueSetsBy(setIds: List<String>)

@Query("DELETE FROM value_set_identifier WHERE valueSetIdentifierId IN (:setIds)")
abstract fun deleteValueSetIdentifiersBy(setIds: List<String>)

@Transaction
open fun deleteSetsBy(setIds: List<String>) {
deleteValueSetsBy(setIds)
deleteValueSetIdentifiersBy(setIds)
}

@Transaction
open fun insertSets(
valueSetsIdentifiersLocal: Array<ValueSetIdentifierLocal>,
valueSetsLocal: Array<ValueSetLocal>
) {
insertIdentifiers(*valueSetsIdentifiersLocal)
insertValueSets(*valueSetsLocal)
}

@Insert
abstract fun insertIdentifiers(vararg valueSetsIdentifiersLocal: ValueSetIdentifierLocal)

@Insert
abstract fun insertValueSets(vararg valueSetsLocal: ValueSetLocal)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package dgca.verifier.app.android.network

import android.os.Build
import dgca.verifier.app.android.BuildConfig
import dgca.verifier.app.android.utils.sha256
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
Expand All @@ -34,7 +35,8 @@ import java.net.HttpURLConnection.HTTP_BAD_REQUEST

class HeaderInterceptor : Interceptor {

private val userAgent = "DGCA verifier Android ${Build.VERSION.SDK_INT}, ${Build.MODEL};"
private val userAgent =
"DGCA verifier: ${BuildConfig.VERSION_NAME}, Android: ${Build.VERSION.SDK_INT}, Model: ${Build.MODEL};"

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
Expand Down

0 comments on commit dd4ed0a

Please sign in to comment.