Skip to content

Commit

Permalink
Merge branch 'master' into anandwana001/2829/flash-card-ui-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anandwana001 authored Dec 19, 2024
2 parents 6cfd49b + 543795e commit 7742204
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
18 changes: 13 additions & 5 deletions ground/src/main/java/com/google/android/ground/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -85,6 +86,18 @@ class MainActivity : AbstractActivity() {
lifecycleScope.launch {
viewModel.navigationRequests.filterNotNull().collect { updateUi(binding.root, it) }
}

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (!dispatchBackPressed()) {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
},
)
}

private fun updateUi(viewGroup: ViewGroup, uiState: MainUiState) {
Expand Down Expand Up @@ -182,11 +195,6 @@ class MainActivity : AbstractActivity() {
}
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!dispatchBackPressed()) super.onBackPressed()
}

private fun dispatchBackPressed(): Boolean {
val fragmentManager = navHostFragment.childFragmentManager
val currentFragment = fragmentManager.findFragmentById(R.id.nav_host_fragment)
Expand Down
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 7742204

Please sign in to comment.