Skip to content

Commit

Permalink
Merge pull request #143 from velocitycareerlabs/VL-8447-v2.6.8
Browse files Browse the repository at this point in the history
rewrite executor and credential type schemas fetch
  • Loading branch information
michaelavoyan authored Oct 23, 2024
2 parents 452c7dc + 9580542 commit a94a66b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 65 deletions.
4 changes: 2 additions & 2 deletions VCL/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdk 24
targetSdk 34
versionName "2.6.7"
versionCode 151
versionName "2.6.8"
versionCode 152
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
Expand Down
30 changes: 15 additions & 15 deletions VCL/src/main/java/io/velocitycareerlabs/impl/VclBlocksProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal object VclBlocksProvider {
CacheServiceImpl(context)
),
credentialTypes,
ExecutorImpl()
ExecutorImpl.instance
)
)

Expand All @@ -142,7 +142,7 @@ internal object VclBlocksProvider {
NetworkServiceImpl(),
CacheServiceImpl(context)
),
ExecutorImpl()
ExecutorImpl.instance
)
)

Expand All @@ -154,7 +154,7 @@ internal object VclBlocksProvider {
NetworkServiceImpl(),
CacheServiceImpl(context)
),
ExecutorImpl()
ExecutorImpl.instance
)
)

Expand All @@ -175,7 +175,7 @@ internal object VclBlocksProvider {
chooseJwtVerifyService(cryptoServicesDescriptor)
),
PresentationRequestByDeepLinkVerifierImpl(),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand All @@ -191,15 +191,15 @@ internal object VclBlocksProvider {
chooseJwtSignService(context, cryptoServicesDescriptor),
chooseJwtVerifyService(cryptoServicesDescriptor)
),
ExecutorImpl()
ExecutorImpl.instance
)

fun provideOrganizationsUseCase(): OrganizationsUseCase =
OrganizationsUseCaseImpl(
OrganizationsRepositoryImpl(
NetworkServiceImpl()
),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand All @@ -219,7 +219,7 @@ internal object VclBlocksProvider {
chooseJwtVerifyService(cryptoServicesDescriptor)
),
CredentialManifestByDeepLinkVerifierImpl(),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand All @@ -235,15 +235,15 @@ internal object VclBlocksProvider {
chooseJwtSignService(context, cryptoServicesDescriptor),
chooseJwtVerifyService(cryptoServicesDescriptor)
),
ExecutorImpl()
ExecutorImpl.instance
)

fun provideExchangeProgressUseCase(): ExchangeProgressUseCase =
ExchangeProgressUseCaseImpl(
ExchangeProgressRepositoryImpl(
NetworkServiceImpl()
),
ExecutorImpl()
ExecutorImpl.instance
)

fun provideGenerateOffersUseCase(): GenerateOffersUseCase =
Expand All @@ -252,7 +252,7 @@ internal object VclBlocksProvider {
NetworkServiceImpl()
),
OffersByDeepLinkVerifierImpl(),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand Down Expand Up @@ -281,7 +281,7 @@ internal object VclBlocksProvider {
credentialIssuerVerifier,
CredentialDidVerifierImpl(),
CredentialsByDeepLinkVerifierImpl(),
ExecutorImpl()
ExecutorImpl.instance
)
}

Expand All @@ -290,15 +290,15 @@ internal object VclBlocksProvider {
CredentialTypesUIFormSchemaRepositoryImpl(
NetworkServiceImpl()
),
ExecutorImpl()
ExecutorImpl.instance
)

fun provideVerifiedProfileUseCase(): VerifiedProfileUseCase =
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceImpl()
),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand All @@ -311,7 +311,7 @@ internal object VclBlocksProvider {
chooseJwtSignService(context, cryptoServicesDescriptor),
chooseJwtVerifyService(cryptoServicesDescriptor)
),
ExecutorImpl()
ExecutorImpl.instance
)

@Throws(VCLError::class)
Expand All @@ -323,6 +323,6 @@ internal object VclBlocksProvider {
KeyServiceRepositoryImpl(
chooseKeyService(context, cryptoServicesDescriptor)
),
ExecutorImpl()
ExecutorImpl.instance
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import io.velocitycareerlabs.impl.domain.infrastructure.executors.Executor
import io.velocitycareerlabs.impl.utils.VCLLog
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

internal class ExecutorImpl : Executor {
internal class ExecutorImpl private constructor() : Executor {

private val TAG = ExecutorImpl::class.simpleName

Expand Down Expand Up @@ -43,15 +42,7 @@ internal class ExecutorImpl : Executor {
}
}

override fun shutdown() {
executorService.shutdown()
try {
if (!executorService.awaitTermination(60 * 3, TimeUnit.SECONDS)) {
executorService.shutdownNow() // Force shutdown if not completed within 3 minutes
}
} catch (e: InterruptedException) {
executorService.shutdownNow() // Force shutdown on interruption
Thread.currentThread().interrupt()
}
companion object {
val instance: ExecutorImpl by lazy { ExecutorImpl() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.velocitycareerlabs.impl.domain.infrastructure.executors.Executor
import io.velocitycareerlabs.impl.domain.repositories.CredentialTypeSchemaRepository
import io.velocitycareerlabs.impl.domain.usecases.CredentialTypeSchemasUseCase
import io.velocitycareerlabs.impl.utils.VCLLog
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap

internal class CredentialTypeSchemasUseCaseImpl (
private val credentialTypeSchemaRepository: CredentialTypeSchemaRepository,
Expand All @@ -25,34 +27,38 @@ internal class CredentialTypeSchemasUseCaseImpl (
cacheSequence: Int,
completionBlock: (VCLResult<VCLCredentialTypeSchemas>) -> Unit
) {
val credentialTypeSchemasMap = HashMap<String, VCLCredentialTypeSchema>()
var credentialTypeSchemasMapIsEmpty = true

val schemaNamesArr =
this.credentialTypes.all?.filter { it.schemaName != null }?.map { it.schemaName }
?: listOf()

schemaNamesArr.forEach { schemaName ->
schemaName?.let {
executor.runOnBackground {
credentialTypeSchemaRepository.getCredentialTypeSchema(
schemaName,
cacheSequence
) { result ->
result.data?.let { credentialTypeSchemasMap[schemaName] = it }
credentialTypeSchemasMapIsEmpty =
credentialTypeSchemasMap.isEmpty() // like in Swift
executor.runOnBackground {
val credentialTypeSchemasMap = ConcurrentHashMap<String, VCLCredentialTypeSchema>()

val schemaNamesArr =
credentialTypes.all?.filter { it.schemaName != null }?.map { it.schemaName }
?: listOf()

val completableFutures = schemaNamesArr.mapNotNull { schemaName ->
schemaName?.let {
CompletableFuture.supplyAsync {
credentialTypeSchemaRepository.getCredentialTypeSchema(
schemaName,
cacheSequence
) { result ->
result.data?.let { credentialTypeSchemasMap[schemaName] = it }
}
}
}
}
}
executor.shutdown()

if (credentialTypeSchemasMapIsEmpty) {
VCLLog.e(TAG, "Credential type schemas were not fount.")
}
executor.runOnMain {
completionBlock(VCLResult.Success(VCLCredentialTypeSchemas(credentialTypeSchemasMap)))
val allFutures = CompletableFuture.allOf(*completableFutures.toTypedArray())
allFutures.join()

if (credentialTypeSchemasMap.isEmpty()) {
VCLLog.e(TAG, "Credential type schemas were not fount.")
} else {
executor.runOnMain {
completionBlock(VCLResult.Success(
VCLCredentialTypeSchemas(credentialTypeSchemasMap)
))
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ package io.velocitycareerlabs.impl.domain.infrastructure.executors
internal interface Executor {
fun runOnMain(block: () -> Unit)
fun runOnBackground(block: () -> Unit)
fun shutdown()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ internal class EmptyExecutor: Executor {
override fun runOnBackground(block: () -> Unit) {
block()
}

override fun shutdown() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CountriesUseCaseTest {
),
EmptyCacheService()
),
ExecutorImpl()
ExecutorImpl.instance
)

subject.getCountries(0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@ class ProfileServiceTypeVerifierTest {
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileIssuerInspectorJsonStr)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
subject2 = ProfileServiceTypeVerifier(
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileInspectorJsonStr)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
subject3 = ProfileServiceTypeVerifier(
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileNotaryIssuerJsonStr)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
subject4 = ProfileServiceTypeVerifier(
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileNotaryIssuerJsonStr)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
subject5 = ProfileServiceTypeVerifier(
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileIssuerJsonStr1)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
subject6 = ProfileServiceTypeVerifier(
VerifiedProfileUseCaseImpl(
VerifiedProfileRepositoryImpl(
NetworkServiceSuccess(VerifiedProfileMocks.VerifiedProfileIssuerInspectorJsonStr)
),
ExecutorImpl()
ExecutorImpl.instance
)
)
}
Expand Down

0 comments on commit a94a66b

Please sign in to comment.