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

[Capture location task] Prompted to Correct enable location task #2885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.lifecycleScope
import com.google.android.ground.R
import com.google.android.ground.model.submission.isNotNullOrEmpty
import com.google.android.ground.model.submission.isNullOrEmpty
Expand All @@ -27,9 +33,11 @@
import com.google.android.ground.ui.datacollection.components.TaskView
import com.google.android.ground.ui.datacollection.components.TaskViewFactory
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskFragment
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Provider
import kotlinx.coroutines.launch

@AndroidEntryPoint
class CaptureLocationTaskFragment @Inject constructor() :
Expand Down Expand Up @@ -59,6 +67,13 @@
// Ensure that the location lock is enabled, if it hasn't been.
if (isVisible) {
viewModel.enableLocationLock()
lifecycleScope.launch {
viewModel.enableLocationLockFlow.collect {
if (it == LocationLockEnabledState.NEEDS_ENABLE) {
showLocationPermissionDialog()

Check warning on line 73 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L73

Added line #L73 was not covered by tests
}
}
}
}
}

Expand All @@ -76,4 +91,28 @@
}
.hide()
}

@Suppress("LabeledExpression")
private fun showLocationPermissionDialog() {
val dialogComposeView =
ComposeView(requireContext()).apply {
setContent {

Check warning on line 99 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L97-L99

Added lines #L97 - L99 were not covered by tests
val openAlertDialog = remember { mutableStateOf(true) }
when {

Check warning on line 101 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L101

Added line #L101 was not covered by tests
openAlertDialog.value -> {
AppTheme {

Check warning on line 103 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L103

Added line #L103 was not covered by tests
LocationPermissionDialog(
onCancel = { openAlertDialog.value = false },
onDismiss = { openAlertDialog.value = false },
)
}

Check warning on line 108 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L105-L108

Added lines #L105 - L108 were not covered by tests
}
}

DisposableEffect(Unit) { onDispose { (parent as? ViewGroup)?.removeView(this@apply) } }
}
}

Check warning on line 114 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L113-L114

Added lines #L113 - L114 were not covered by tests

(view as ViewGroup).addView(dialogComposeView)
}

Check warning on line 117 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskFragment.kt#L116-L117

Added lines #L116 - L117 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.lifecycleScope
import com.google.android.ground.ui.common.AbstractMapFragmentWithControls
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -62,46 +57,8 @@ class CaptureLocationTaskMapFragment @Inject constructor() : AbstractMapFragment
return root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lifecycleScope.launch {
viewModel.enableLocationLockFlow.collect {
if (it == LocationLockEnabledState.NEEDS_ENABLE) {
showLocationPermissionDialog()
}
}
}
}

override fun onMapReady(map: MapFragment) {
super.onMapReady(map)
viewLifecycleOwner.lifecycleScope.launch { viewModel.onMapReady(mapViewModel) }
}

@Suppress("LabeledExpression")
private fun showLocationPermissionDialog() {
val dialogComposeView =
ComposeView(requireContext()).apply {
setContent {
val openAlertDialog = remember { mutableStateOf(true) }
when {
openAlertDialog.value -> {
AppTheme {
LocationPermissionDialog(
onCancel = {
binding.locationLockBtn.isClickable = false
openAlertDialog.value = false
},
onDismiss = { openAlertDialog.value = false },
)
}
}
}

DisposableEffect(Unit) { onDispose { (parent as? ViewGroup)?.removeView(this@apply) } }
}
}

(view as ViewGroup).addView(dialogComposeView)
}
}