Skip to content

Commit

Permalink
Display a snackbar when draft is restored (#2666)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Sep 5, 2024
1 parent e4c8399 commit c83bca4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
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 @@ -188,5 +188,6 @@
## Public data sharing
\nSurvey organizers may share and use data publicly under the *Creative Commons CC0 1.0 License*:
</string>
<string name="draft_restored">Unsaved data restored</string>
<string name="load_tos_failed">Could not connect, try again later</string>
</resources>

0 comments on commit c83bca4

Please sign in to comment.