Skip to content

Commit

Permalink
Merge pull request #207 from redlink-gmbh/FEATURE-Relative_Study_Start
Browse files Browse the repository at this point in the history
Fixed the relative schedule bug, not showing up on first login
  • Loading branch information
alireza-dhp authored Nov 28, 2023
2 parents f809dfd + b419dee commit 3b2f9a9
Showing 1 changed file with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package io.redlink.more.more_app_mutliplatform.services.network

import io.github.aakira.napier.Napier
import io.redlink.more.app.android.services.network.errors.NetworkServiceError
import io.redlink.more.more_app_mutliplatform.Shared
import io.redlink.more.more_app_mutliplatform.database.DatabaseManager
Expand All @@ -26,7 +27,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

class RegistrationService (
class RegistrationService(
private val shared: Shared
) {
var study: Study? = null
Expand All @@ -39,10 +40,19 @@ class RegistrationService (

fun getEndpointRepository(): EndpointRepository = shared.endpointRepository

fun sendRegistrationToken(token: String, manualEndpoint: String? = null, onSuccess: (Study) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) {
fun sendRegistrationToken(
token: String,
manualEndpoint: String? = null,
onSuccess: (Study) -> Unit,
onError: ((NetworkServiceError?) -> Unit),
onFinish: () -> Unit
) {
if (token.isNotEmpty()) {
Scope.launch {
val (result, networkError) = shared.networkService.validateRegistrationToken( token.uppercase(), manualEndpoint)
val (result, networkError) = shared.networkService.validateRegistrationToken(
token.uppercase(),
manualEndpoint
)
result?.let {
endpoint = manualEndpoint
study = it
Expand All @@ -57,14 +67,20 @@ class RegistrationService (
}
}

fun acceptConsent(consentInfoMd5: String, uniqueDeviceId: String, onSuccess: (Boolean) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) {
fun acceptConsent(
consentInfoMd5: String,
uniqueDeviceId: String,
onSuccess: (Boolean) -> Unit,
onError: ((NetworkServiceError?) -> Unit),
onFinish: () -> Unit
) {
Scope.launch {
shared.credentialRepository.remove()
shared.endpointRepository.removeEndpoint()
DatabaseManager.deleteAll()
}
study?.let { study ->
participationToken?.let {token ->
participationToken?.let { token ->
val studyConsent = StudyConsent(
consent = true,
observations = study.observations.map {
Expand All @@ -73,25 +89,48 @@ class RegistrationService (
consentInfoMD5 = consentInfoMd5,
deviceId = "${getPlatform().productName}#$uniqueDeviceId"
)
sendConsent(token, studyConsent, study, endpoint, onSuccess, onError, onFinish)
sendConsent(token, studyConsent, endpoint, onSuccess, onError, onFinish)
}
}
}

private fun sendConsent(token: String, studyConsent: StudyConsent, study: Study, endpoint: String? = null, onSuccess: (Boolean) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) {
private fun sendConsent(
token: String,
studyConsent: StudyConsent,
endpoint: String? = null,
onSuccess: (Boolean) -> Unit,
onError: ((NetworkServiceError?) -> Unit),
onFinish: () -> Unit
) {
scope.launch {
val (config, networkError) = shared.networkService.sendConsent(token, studyConsent, endpoint)
val (config, networkError) = shared.networkService.sendConsent(
token,
studyConsent,
endpoint
)
if (config != null) {
config.endpoint?.let {
shared.endpointRepository.storeEndpoint(it)
}
val credentialModel =
CredentialModel(config.credentials.apiId, config.credentials.apiKey)
if (shared.credentialRepository.store(credentialModel) && shared.credentialRepository.hasCredentials()) {
StudyRepository().storeStudy(study)
shared.observationFactory.addNeededObservationTypes(study.observations.map { it.observationType }.toSet())
shared.resetFirstStartUp()
onSuccess(shared.credentialRepository.hasCredentials())

val (study, error) = shared.networkService.getStudyConfig()
if (error != null) {
Napier.e { error.message }
onError(NetworkServiceError(null, "Could not get study: ${error.message}"))
} else {
study?.let { study ->
StudyRepository().storeStudy(study)
shared.observationFactory
.addNeededObservationTypes(study.observations.map { it.observationType }.toSet())
shared.resetFirstStartUp()
onSuccess(shared.credentialRepository.hasCredentials())
} ?: kotlin.run {
onError(NetworkServiceError(null, "Could not get study"))
}
}
} else {
onError(NetworkServiceError(null, "Could not store credentials"))
}
Expand Down

0 comments on commit 3b2f9a9

Please sign in to comment.