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

Move camera position to geometry when restoring draw area tasks #2868

Merged
merged 1 commit into from
Nov 25, 2024
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 @@ -25,6 +25,7 @@ import com.google.android.ground.system.GeocodingManager
import com.google.android.ground.system.PermissionDeniedException
import com.google.android.ground.system.SettingsChangeRequestCanceled
import com.google.android.ground.ui.home.mapcontainer.MapTypeDialogFragmentDirections
import com.google.android.ground.ui.map.Bounds
import com.google.android.ground.ui.map.CameraUpdateRequest
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.map.NewCameraPositionViaBounds
Expand Down Expand Up @@ -131,6 +132,11 @@ abstract class AbstractMapContainerFragment : AbstractFragment() {
Toast.makeText(context, messageId, Toast.LENGTH_LONG).show()
}

/** Moves the camera to the given bounds. */
fun moveToBounds(bounds: Bounds, padding: Int, shouldAnimate: Boolean) {
onCameraUpdateRequest(NewCameraPositionViaBounds(bounds, padding, shouldAnimate), map)
}

/** Moves the camera to a given position. */
fun moveToPosition(coordinates: Coordinates) {
onCameraUpdateRequest(NewCameraPositionViaCoordinates(coordinates, shouldAnimate = true), map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.android.ground.ui.map.CameraPosition
import com.google.android.ground.ui.map.Feature
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.map.gms.GmsExt.toBounds
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -53,6 +54,9 @@
map.setFeatures(if (feature == null) setOf() else setOf(feature))
}
}

// If the task has any previously drawn area, restore map viewport to the feature.
moveViewportToFeature(viewModel.draftArea.value)

Check warning on line 59 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskMapFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskMapFragment.kt#L59

Added line #L59 was not covered by tests
}

override fun onMapCameraMoved(position: CameraPosition) {
Expand All @@ -64,4 +68,10 @@
}
}
}

private fun moveViewportToFeature(feature: Feature?) {
val geometry = feature?.geometry ?: return
val bounds = listOf(geometry).toBounds() ?: return
moveToBounds(bounds, padding = 200, shouldAnimate = false)
}

Check warning on line 76 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskMapFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskMapFragment.kt#L75-L76

Added lines #L75 - L76 were not covered by tests
}