Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display a snackbar when draft is restored #2666

Merged
merged 10 commits into from
Sep 5, 2024
3 changes: 1 addition & 2 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>MemberNameEqualsClassName:FusedLocationProviderClient.kt$FusedLocationProviderClient$private val fusedLocationProviderClient: FusedLocationProviderClient</ID>
<ID>MemberNameEqualsClassName:SettingsClient.kt$SettingsClient$private val settingsClient: SettingsClient</ID>
<ID>ReturnCount:HomeScreenViewModel.kt$HomeScreenViewModel$suspend fun getDraftSubmission(): DraftSubmission?</ID>
<ID>UnusedPrivateProperty:HomeScreenFragmentTest.kt$NavigationDrawerItemClickTest$private val testLabel: String</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ import com.google.android.ground.R
import com.google.android.ground.databinding.HomeScreenFragBinding
import com.google.android.ground.databinding.NavDrawerHeaderBinding
import com.google.android.ground.model.User
import com.google.android.ground.persistence.local.room.converter.SubmissionDeltasConverter
import com.google.android.ground.repository.UserRepository
import com.google.android.ground.ui.common.AbstractFragment
import com.google.android.ground.ui.common.BackPressListener
import com.google.android.ground.ui.common.EphemeralPopups
import com.google.android.ground.ui.common.Navigator
import com.google.android.ground.ui.theme.AppTheme
import com.google.android.ground.util.systemInsets
import com.google.android.material.imageview.ShapeableImageView
Expand All @@ -56,6 +59,8 @@ class HomeScreenFragment :
// TODO: It's not obvious which locations of interest are in HomeScreen vs MapContainer;
// make this more intuitive.

@Inject lateinit var ephemeralPopups: EphemeralPopups
@Inject lateinit var navigator: Navigator
@Inject lateinit var userRepository: UserRepository
private lateinit var binding: HomeScreenFragBinding
private lateinit var homeScreenViewModel: HomeScreenViewModel
Expand Down Expand Up @@ -100,7 +105,21 @@ class HomeScreenFragment :
updateNavHeader()
// Re-open data collection screen if any drafts are present
viewLifecycleOwner.lifecycleScope.launch {
homeScreenViewModel.maybeNavigateToDraftSubmission()
homeScreenViewModel.getDraftSubmission()?.let { draft ->
navigator.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
draft.loiId,
draft.loiName ?: "",
draft.jobId,
true,
SubmissionDeltasConverter.toString(draft.deltas),
)
)

ephemeralPopups
.InfoPopup(binding.root, R.string.draft_restored, EphemeralPopups.PopupDuration.SHORT)
.show()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.google.android.ground.ui.home
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.android.ground.model.submission.DraftSubmission
import com.google.android.ground.persistence.local.LocalValueStore
import com.google.android.ground.persistence.local.room.converter.SubmissionDeltasConverter
import com.google.android.ground.repository.OfflineAreaRepository
import com.google.android.ground.repository.SubmissionRepository
import com.google.android.ground.repository.SurveyRepository
Expand Down Expand Up @@ -53,33 +53,25 @@ internal constructor(
// TODO(#1730): Allow tile source configuration from a non-survey accessible source.
val showOfflineAreaMenuItem: LiveData<Boolean> = MutableLiveData(true)

suspend fun maybeNavigateToDraftSubmission() {
/** Attempts to return draft submission for the currently active survey. */
suspend fun getDraftSubmission(): DraftSubmission? {
val draftId = localValueStore.draftSubmissionId
val survey = surveyRepository.activeSurvey

// Missing draft submission
if (draftId.isNullOrEmpty() || survey == null) {
return
// Missing draft submission
return null
}

val draft = submissionRepository.getDraftSubmission(draftId, survey)
val draft = submissionRepository.getDraftSubmission(draftId, survey) ?: return null

// TODO: Check whether the previous user id matches with current user or not.
if (draft != null && draft.surveyId == survey.id) {
navigator.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
draft.loiId,
draft.loiName ?: "",
draft.jobId,
true,
SubmissionDeltasConverter.toString(draft.deltas),
)
)
}

if (draft != null && draft.surveyId != survey.id) {
if (draft.surveyId != survey.id) {
Timber.e("Skipping draft submission, survey id doesn't match")
return null
}

// TODO: Check whether the previous user id matches with current user or not.
return draft
}

fun openNavDrawer() {
Expand Down
1 change: 1 addition & 0 deletions ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,5 @@
## Public data sharing
\nSurvey organizers may share and use data publicly under the *Creative Commons CC0 1.0 License*:
</string>
<string name="draft_restored">App was closed. Unsaved data restored.</string>
shobhitagarwal1612 marked this conversation as resolved.
Show resolved Hide resolved
</resources>