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

ViewModel is now access based on taskId using Bundle #2748

Closed
wants to merge 14 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class DrawAreaTaskFragment : AbstractTaskFragment<DrawAreaTaskViewModel>() {
// NOTE(#2493): Multiplying by a random prime to allow for some mathematical "uniqueness".
// Otherwise, the sequentially generated ID might conflict with an ID produced by Google Maps.
val rowLayout = LinearLayout(requireContext()).apply { id = View.generateViewId() * 11411 }
drawAreaTaskMapFragment = DrawAreaTaskMapFragment.newInstance(viewModel, map)
drawAreaTaskMapFragment =
DrawAreaTaskMapFragment.newInstance(map).apply { setViewModel(viewModel) }
parentFragmentManager
.beginTransaction()
.add(rowLayout.id, drawAreaTaskMapFragment, DrawAreaTaskMapFragment::class.java.simpleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import kotlinx.coroutines.launch

@AndroidEntryPoint
class DrawAreaTaskMapFragment(private val viewModel: DrawAreaTaskViewModel) :
AbstractMapFragmentWithControls() {
class DrawAreaTaskMapFragment : AbstractMapFragmentWithControls() {

private lateinit var mapViewModel: BaseMapViewModel
private lateinit var viewModel: DrawAreaTaskViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -40,24 +40,29 @@

override fun onMapReady(map: MapFragment) {
viewLifecycleOwner.lifecycleScope.launch {
viewModel.draftArea.collect { feature: Feature? ->
map.setFeatures(if (feature == null) setOf() else setOf(feature))
if (this@DrawAreaTaskMapFragment::viewModel.isInitialized) {
viewModel.draftArea.collect { feature: Feature? ->
map.setFeatures(if (feature == null) setOf() else setOf(feature))
}

Check warning on line 46 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#L46

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

override fun onMapCameraMoved(position: CameraPosition) {
super.onMapCameraMoved(position)
if (!viewModel.isMarkedComplete()) {
if (this@DrawAreaTaskMapFragment::viewModel.isInitialized && !viewModel.isMarkedComplete()) {
val mapCenter = position.coordinates
viewModel.updateLastVertexAndMaybeCompletePolygon(mapCenter) { c1, c2 ->
map.getDistanceInPixels(c1, c2)
}
}
}

fun setViewModel(viewModel: DrawAreaTaskViewModel) {
this.viewModel = viewModel
}

companion object {
fun newInstance(viewModel: DrawAreaTaskViewModel, map: MapFragment) =
DrawAreaTaskMapFragment(viewModel).apply { this.map = map }
fun newInstance(map: MapFragment) = DrawAreaTaskMapFragment().apply { this.map = map }
}
}