Skip to content

Commit

Permalink
Set viewport to feature when restoring area tasks (#2868)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Nov 25, 2024
1 parent cf32b10 commit 981478b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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.common.BaseMapViewModel
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 @@ class DrawAreaTaskMapFragment @Inject constructor() : AbstractMapFragmentWithCon
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)
}

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

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

0 comments on commit 981478b

Please sign in to comment.