Skip to content

Commit

Permalink
Merge pull request #119 from alexandr7035/develop
Browse files Browse the repository at this point in the history
Add mock flavor
  • Loading branch information
alexandr7035 authored Sep 20, 2023
2 parents ac24096 + 9e5e2a2 commit f58abb8
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 22 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ android {
versionNameSuffix = ".debug"
}
}

flavorDimensions += "version"
productFlavors {
create("real") {
dimension = "version"
}
create("mock") {
dimension = "version"
applicationIdSuffix = ".mock"
versionNameSuffix = "-mock"
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
91 changes: 70 additions & 21 deletions app/src/main/java/by/alexandr7035/affinidi_id/di/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package by.alexandr7035.affinidi_id.di

import android.content.Context
import androidx.room.Room
import by.alexandr7035.affinidi_id.BuildConfig
import by.alexandr7035.affinidi_id.domain.repository.*
import by.alexandr7035.affinidi_id.presentation.common.validation.InputValidationHelper
import by.alexandr7035.affinidi_id.presentation.common.validation.InputValidationHelperImpl
Expand All @@ -28,7 +29,14 @@ import by.alexandr7035.data.helpers.vc_mapping.CredentialSubjectCasterImpl
import by.alexandr7035.data.helpers.vc_mapping.SignedCredentialToDomainMapper
import by.alexandr7035.data.helpers.vc_mapping.SignedCredentialToDomainMapperImpl
import by.alexandr7035.data.repository.*
import com.cioccarellia.ksprefs.BuildConfig
import by.alexandr7035.data.repository_mock.ChangeProfileRepositoryMock
import by.alexandr7035.data.repository_mock.IssueCredentialsRepositoryMock
import by.alexandr7035.data.repository_mock.LoginRepositoryMock
import by.alexandr7035.data.repository_mock.ProfileRepositoryMock
import by.alexandr7035.data.repository_mock.RegistrationRepositoryMock
import by.alexandr7035.data.repository_mock.ResetPasswordRepositoryMock
import by.alexandr7035.data.repository_mock.StoredCredentialsRepositoryMock
import by.alexandr7035.data.repository_mock.VerificationRepositoryMock
import com.cioccarellia.ksprefs.KsPrefs
import com.cioccarellia.ksprefs.config.EncryptionType
import com.cioccarellia.ksprefs.config.model.AutoSavePolicy
Expand Down Expand Up @@ -80,12 +88,6 @@ object DataModule {
return ApiCallHelperImpl()
}

@Provides
@Singleton
fun provideProfileRepository(appSettings: AppSettings): ProfileRepository {
return ProfileRepositoryImpl(appSettings)
}

@Provides
@Singleton
fun provideAvatarsHelper(): DicebearAvatarsHelper {
Expand All @@ -104,6 +106,19 @@ object DataModule {
return retrofit.create(CredentialsApiService::class.java)
}

@Provides
@Singleton
fun provideProfileRepository(
appSettings: AppSettings,
avatarsHelper: DicebearAvatarsHelper
): ProfileRepository {
return if (isMockBuild()) {
ProfileRepositoryMock(avatarsHelper)
} else {
ProfileRepositoryImpl(appSettings)
}
}

@Provides
@Singleton
fun provideLoginRepository(
Expand All @@ -112,29 +127,46 @@ object DataModule {
appSettings: AppSettings,
credentialsCacheDataSource: CredentialsCacheDataSource
): LoginRepository {
return LoginRepositoryImpl(userApiService, apiCallHelper, appSettings, credentialsCacheDataSource)
return if (isMockBuild()) {
LoginRepositoryMock(appSettings)
} else {
LoginRepositoryImpl(userApiService, apiCallHelper, appSettings, credentialsCacheDataSource)
}
}

@Provides
@Singleton
fun provideRegistrationRepository(
userApiService: UserApiService, apiCallHelper: ApiCallHelper, appSettings: AppSettings
): RegistrationRepository {
return RegistrationRepositoryImpl(userApiService, apiCallHelper, appSettings)

return if (isMockBuild()) {
RegistrationRepositoryMock()
} else {
RegistrationRepositoryImpl(userApiService, apiCallHelper, appSettings)
}
}

@Provides
@Singleton
fun provideResetPasswordRepository(userApiService: UserApiService, apiCallHelper: ApiCallHelper): ResetPasswordRepository {
return ResetPasswordRepositoryImpl(userApiService, apiCallHelper)
return if (isMockBuild()) {
ResetPasswordRepositoryMock()
} else {
ResetPasswordRepositoryImpl(userApiService, apiCallHelper)
}
}

@Provides
@Singleton
fun provideChangeProfileRepository(
userApiService: UserApiService, apiCallHelper: ApiCallHelper, appSettings: AppSettings
): ChangeProfileRepository {
return ChangeProfileRepositoryImpl(userApiService, apiCallHelper, appSettings)
return if (isMockBuild()) {
ChangeProfileRepositoryMock()
} else {
ChangeProfileRepositoryImpl(userApiService, apiCallHelper, appSettings)
}
}

@Provides
Expand Down Expand Up @@ -175,14 +207,19 @@ object DataModule {
credentialToDomainMapper: SignedCredentialToDomainMapper,
appSettings: AppSettings,
): StoredCredentialsRepository {
return StoredCredentialsRepositoryImpl(
credentialsApiService,
apiCallHelper,
credentialsCloudDataSource,
credentialsCacheDataSource,
credentialToDomainMapper,
appSettings
)

return if (isMockBuild()) {
StoredCredentialsRepositoryMock()
} else {
StoredCredentialsRepositoryImpl(
credentialsApiService,
apiCallHelper,
credentialsCloudDataSource,
credentialsCacheDataSource,
credentialToDomainMapper,
appSettings
)
}
}

@Provides
Expand All @@ -200,7 +237,11 @@ object DataModule {
@Provides
@Singleton
fun provideIssueCredentialsRepository(vcIssuanceHelper: VCIssuanceHelper): IssueCredentialsRepository {
return IssueCredentialsRepositoryImpl(vcIssuanceHelper)
return if (isMockBuild()) {
IssueCredentialsRepositoryMock()
} else {
IssueCredentialsRepositoryImpl(vcIssuanceHelper)
}
}

@Provides
Expand All @@ -211,7 +252,11 @@ object DataModule {
signedCredentialToDomainMapper: SignedCredentialToDomainMapper,
gson: Gson
): VerificationRepository {
return VerificationRepositoryImpl(credentialsApiService, apiCallHelper, signedCredentialToDomainMapper, gson)
return if (isMockBuild()) {
VerificationRepositoryMock()
} else {
VerificationRepositoryImpl(credentialsApiService, apiCallHelper, signedCredentialToDomainMapper, gson)
}
}

@Provides
Expand Down Expand Up @@ -248,4 +293,8 @@ object DataModule {
fun provideAppSettings(prefs: KsPrefs, avatarsHelper: DicebearAvatarsHelper): AppSettings {
return AppSettingsImpl(prefs, avatarsHelper)
}

private fun isMockBuild(): Boolean {
return BuildConfig.FLAVOR == "mock"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ class CredentialCardMapperImpl @Inject constructor(
val formattedHolderDid = credential.holderDid.split(";").first()
val prettifiedHolderDid = formattedHolderDid.getPrettifiedDid()

val formattedIssuerDid = credential.issuerDid.split(";").first()
val prettifiedIssuerDid = formattedIssuerDid.getPrettifiedDid()

val credentialStatusUi = credentialStatusMapper.map(credential.credentialStatus)

return CredentialCardUi(
id = credential.id,
issuerDid = prettifiedHolderDid,
issuerDid = prettifiedIssuerDid,
holderDid = prettifiedHolderDid,
issuanceDateText = issuanceDate,
credentialStatusUi = credentialStatusUi,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package by.alexandr7035.data.repository_mock

import by.alexandr7035.affinidi_id.domain.core.ErrorType
import by.alexandr7035.affinidi_id.domain.model.profile.change_password.ChangePasswordReqModel
import by.alexandr7035.affinidi_id.domain.model.profile.change_password.ChangePasswordResModel
import by.alexandr7035.affinidi_id.domain.repository.ChangeProfileRepository
import kotlinx.coroutines.delay

class ChangeProfileRepositoryMock : ChangeProfileRepository {
override suspend fun changePassword(changePasswordReqModel: ChangePasswordReqModel): ChangePasswordResModel {
delay(MockConstants.MOCK_REQ_DELAY_MILLS)

return if (changePasswordReqModel.oldPassword == MockConstants.MOCK_PASSWORD) {
ChangePasswordResModel.Success
} else {
ChangePasswordResModel.Fail(ErrorType.WRONG_CURRENT_PASSWORD)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package by.alexandr7035.data.repository_mock

import by.alexandr7035.affinidi_id.domain.core.ErrorType
import by.alexandr7035.affinidi_id.domain.model.credentials.issue_vc.IssueCredentialReqModel
import by.alexandr7035.affinidi_id.domain.model.credentials.issue_vc.IssueCredentialResModel
import by.alexandr7035.affinidi_id.domain.repository.IssueCredentialsRepository
import kotlinx.coroutines.delay

class IssueCredentialsRepositoryMock: IssueCredentialsRepository {
override suspend fun issueCredential(issueCredentialReqModel: IssueCredentialReqModel): IssueCredentialResModel {
delay(MockConstants.MOCK_REQ_DELAY_MILLS)
return IssueCredentialResModel.Fail(ErrorType.ALREADY_HAVE_CREDENTIAL)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package by.alexandr7035.data.repository_mock

import by.alexandr7035.affinidi_id.domain.core.ErrorType
import by.alexandr7035.affinidi_id.domain.core.GenericRes
import by.alexandr7035.affinidi_id.domain.model.login.AuthCredentials
import by.alexandr7035.affinidi_id.domain.model.login.LogOutModel
import by.alexandr7035.affinidi_id.domain.repository.AppSettings
import by.alexandr7035.affinidi_id.domain.repository.LoginRepository
import kotlinx.coroutines.delay

class LoginRepositoryMock(
private val appSettings: AppSettings
): LoginRepository {
override suspend fun signIn(userName: String, password: String): GenericRes<Unit> {
delay(MockConstants.MOCK_REQ_DELAY_MILLS)

return if (userName == MockConstants.MOCK_LOGIN && password == MockConstants.MOCK_PASSWORD) {
appSettings.setAuthCredentials(
AuthCredentials(
userDid = MockConstants.MOCK_DID,
userEmail = MockConstants.MOCK_LOGIN,
accessToken = "access_token_mock",
refreshToken = "refresh_token_mock",
accessTokenObtained = System.currentTimeMillis()
)
)

GenericRes.Success(Unit)
} else {
GenericRes.Fail(ErrorType.WRONG_USERNAME_OR_PASSWORD)
}
}

override suspend fun signInWithRefreshToken(): GenericRes<Unit> {
delay(MockConstants.MOCK_REQ_DELAY_MILLS)

appSettings.updateAuthCredentials(
accessToken = "access_token_mock",
accessTokenRefreshedDate = System.currentTimeMillis()
)

return GenericRes.Success(Unit)
}

override suspend fun logOut(): LogOutModel {
delay(MockConstants.MOCK_REQ_DELAY_MILLS)

appSettings.clearSettings()
return LogOutModel.Success
}
}
Loading

0 comments on commit f58abb8

Please sign in to comment.