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.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 @@
// 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 @@
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,

Check warning on line 111 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt#L109-L111

Added lines #L109 - L111 were not covered by tests
draft.loiName ?: "",
draft.jobId,
true,
SubmissionDeltasConverter.toString(draft.deltas),

Check warning on line 115 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt#L113-L115

Added lines #L113 - L115 were not covered by tests
)
)

ephemeralPopups

Check warning on line 119 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt#L119

Added line #L119 was not covered by tests
.InfoPopup(binding.root, R.string.draft_restored, EphemeralPopups.PopupDuration.SHORT)
.show()
}

Check warning on line 122 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenFragment.kt#L121-L122

Added lines #L121 - L122 were not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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 @@
// 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

Check warning on line 70 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt#L70

Added line #L70 was not covered by tests
}

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

Check warning on line 74 in ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt#L74

Added line #L74 was not covered by tests
}

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>