Skip to content

Commit

Permalink
Merge branch 'sufy/2446/new-loi-flow' of https://github.com/google/gr…
Browse files Browse the repository at this point in the history
…ound-android into sufy/2446/new-loi-flow
  • Loading branch information
sufyanAbbasi committed Dec 19, 2024
2 parents 7e66573 + 55393d8 commit 2581739
Showing 1 changed file with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,41 @@ internal object SurveyConverter {
fun toSurvey(doc: DocumentSnapshot, jobs: List<Job> = listOf()): SurveyModel {
if (!doc.exists()) throw DataStoreException("Missing survey")

val surveyFromProto = SurveyProto::class.parseFrom(doc, 1)
val jobMap = jobs.associateBy { it.id }
val dataSharingTerms =
if (surveyFromProto.dataSharingTerms.type == Survey.DataSharingTerms.Type.TYPE_UNSPECIFIED) {
null
} else {
surveyFromProto.dataSharingTerms
}
return SurveyModel(
surveyFromProto.id.ifEmpty { doc.id },
surveyFromProto.name,
surveyFromProto.description,
jobMap.toPersistentMap(),
surveyFromProto.aclMap.entries.associate { it.key to it.value.toString() },
val surveyFromProto = parseSurveyFromDocument(doc)
val jobMap = convertJobsToMap(jobs)
val dataSharingTerms = getDataSharingTerms(surveyFromProto)

return createSurveyModel(doc, surveyFromProto, jobMap, dataSharingTerms)
}

/** Parse survey data from the DocumentSnapshot. */
private fun parseSurveyFromDocument(doc: DocumentSnapshot): SurveyProto =
SurveyProto::class.parseFrom(doc, 1)

/** Convert a list of jobs into a map for easy lookup. */
private fun convertJobsToMap(jobs: List<Job>): Map<String, Job> = jobs.associateBy { it.id }

/** Extract dataSharingTerms from survey. */
private fun getDataSharingTerms(surveyProto: SurveyProto): Survey.DataSharingTerms? =
if (surveyProto.dataSharingTerms.type == Survey.DataSharingTerms.Type.TYPE_UNSPECIFIED) {
null
} else {
surveyProto.dataSharingTerms
}

/** Build SurveyModel from parsed data. */
private fun createSurveyModel(
doc: DocumentSnapshot,
surveyProto: SurveyProto,
jobMap: Map<String, Job>,
dataSharingTerms: Survey.DataSharingTerms?,
): SurveyModel =
SurveyModel(
id = surveyProto.id.ifEmpty { doc.id },
title = surveyProto.name,
description = surveyProto.description,
jobMap = jobMap.toPersistentMap(),
acl = surveyProto.aclMap.entries.associate { it.key to it.value.toString() },
dataSharingTerms = dataSharingTerms,
)
}
}

0 comments on commit 2581739

Please sign in to comment.