Skip to content

Commit

Permalink
Uploads QuestionnaireResponse as a searchable map to Firestore (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi authored Mar 26, 2023
1 parent 5303623 commit 997eb67
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ dependencies {

//JSON
implementation "com.google.code.gson:gson:2.9.0"
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0'

//System Bars Padding
implementation "dev.chrisbanes.accompanist:accompanist-insets:0.6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import com.google.firebase.firestore.CollectionReference
import edu.stanford.cardinalkit.common.Constants
import edu.stanford.cardinalkit.domain.models.Response
import edu.stanford.cardinalkit.domain.models.SurveyResult
import edu.stanford.cardinalkit.domain.repositories.SurveyRepository
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.tasks.await
Expand All @@ -19,11 +18,12 @@ class SurveyRepositoryImpl @Inject constructor(
private val context: Context
) : SurveyRepository {

override suspend fun uploadSurveyResult(result: SurveyResult) = flow {
override suspend fun uploadSurveyResult(result: Map<String, Any>) = flow {
surveysRef?.let {
try {
emit(Response.Loading)
val upload = surveysRef.document(result.id).set(result).await()
val id = result["id"] as String
val upload = surveysRef.document(id).set(result).await()
emit(Response.Success(upload))
} catch (e: Exception) {
emit(Response.Error(e))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package edu.stanford.cardinalkit.domain.repositories

import edu.stanford.cardinalkit.domain.models.Response
import edu.stanford.cardinalkit.domain.models.SurveyResult
import kotlinx.coroutines.flow.Flow

interface SurveyRepository {
suspend fun getSurvey(name: String): Flow<Response<String?>>
suspend fun uploadSurveyResult(result: SurveyResult): Flow<Response<Void?>>
suspend fun uploadSurveyResult(result: Map<String, Any>): Flow<Response<Void?>>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package edu.stanford.cardinalkit.domain.usecases.surveys

import edu.stanford.cardinalkit.domain.models.SurveyResult
import edu.stanford.cardinalkit.domain.repositories.SurveyRepository

class UploadSurveyResult(
private val repository: SurveyRepository
) {
suspend operator fun invoke(
result: SurveyResult
result: Map<String, Any>
) = repository.uploadSurveyResult(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import edu.stanford.cardinalkit.R
import edu.stanford.cardinalkit.common.Constants
import edu.stanford.cardinalkit.domain.models.Response
import edu.stanford.cardinalkit.domain.models.tasks.CKTaskLog
import edu.stanford.cardinalkit.presentation.profile.ProfileViewModel
import edu.stanford.cardinalkit.presentation.tasks.TasksViewModel
import org.hl7.fhir.r4.model.QuestionnaireResponse
import java.util.*

@AndroidEntryPoint
class SurveyActivity : AppCompatActivity() {
private var surveyName: String? = null // filename of the survey
private var taskID: String? = null // id of the task
private val surveyViewModel by viewModels<SurveyViewModel>()
private val tasksViewModel by viewModels<TasksViewModel>()
private val profileViewModel by viewModels<ProfileViewModel>()

companion object {
const val QUESTIONNAIRE_FRAGMENT_TAG = "questionnaire_fragment"
Expand Down Expand Up @@ -118,6 +122,15 @@ class SurveyActivity : AppCompatActivity() {
val fragment = supportFragmentManager.findFragmentByTag(QUESTIONNAIRE_FRAGMENT_TAG)
as QuestionnaireFragment
val questionnaireResponse = fragment.getQuestionnaireResponse()
surveyName?.let { surveyViewModel.uploadSurveyResult(it, questionnaireResponse) }
val userID = profileViewModel.userID

// Add metadata to QuestionnaireResponse
questionnaireResponse.authored = Date()
questionnaireResponse.id = UUID.randomUUID().toString()
questionnaireResponse.status = QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED
questionnaireResponse.subject.reference = "Patient/$userID"

// Upload the survey to the database
surveyViewModel.uploadSurveyResult(questionnaireResponse)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import dagger.hilt.android.lifecycle.HiltViewModel
import edu.stanford.cardinalkit.common.Constants
import edu.stanford.cardinalkit.domain.models.Response
import edu.stanford.cardinalkit.domain.models.SurveyResult
import edu.stanford.cardinalkit.domain.usecases.surveys.SurveysUseCases
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.QuestionnaireResponse
import java.util.*
import javax.inject.Inject
import javax.inject.Named

Expand All @@ -30,22 +30,20 @@ class SurveyViewModel @Inject constructor(
MutableLiveData(Response.Loading)
val surveyResultUploadedState: LiveData<Response<Void?>> = _surveyResultUploadedState

fun uploadSurveyResult(surveyName: String, questionnaireResponse: QuestionnaireResponse) =
fun uploadSurveyResult(questionnaireResponse: QuestionnaireResponse) =
viewModelScope.launch {
// Serialize the results from QuestionnaireResponse to JSON
// Serialize the results from QuestionnaireResponse to a JSON string
// using HAPI FHIR
val jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
val questionnaireResponseString =
jsonParser.encodeResourceToString(questionnaireResponse)

// Upload results to the database
val surveyResult = SurveyResult(
id = UUID.randomUUID().toString(),
name = surveyName,
data = questionnaireResponseString,
date = Date()
)
// Convert the JSON string into a Map using Gson
val gson = Gson()
val mapType = object : TypeToken<Map<String, Any>>() {}.type
val jsonMap: Map<String, Any> = gson.fromJson(questionnaireResponseString, mapType)

useCases.uploadSurveyResult(surveyResult).collect { response ->
useCases.uploadSurveyResult(jsonMap).collect { response ->
_surveyResultUploadedState.value = response
}
}
Expand Down

0 comments on commit 997eb67

Please sign in to comment.