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 @@ -41,13 +41,10 @@ class CaptureLocationTaskFragment : AbstractTaskFragment<CaptureLocationTaskView
// 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() * 11149 }
val fragment = CaptureLocationTaskMapFragment.newInstance(map).apply { setViewModel(viewModel) }
parentFragmentManager
.beginTransaction()
.add(
rowLayout.id,
CaptureLocationTaskMapFragment.newInstance(viewModel, map),
CaptureLocationTaskMapFragment::class.java.simpleName,
)
.add(rowLayout.id, fragment, CaptureLocationTaskMapFragment::class.java.simpleName)
.commit()
return rowLayout
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import kotlinx.coroutines.launch

@AndroidEntryPoint
class CaptureLocationTaskMapFragment(private val viewModel: CaptureLocationTaskViewModel) :
AbstractMapFragmentWithControls() {
class CaptureLocationTaskMapFragment : AbstractMapFragmentWithControls() {

private lateinit var mapViewModel: CaptureLocationTaskMapViewModel
private lateinit var viewModel: CaptureLocationTaskViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -46,18 +46,27 @@
): View {
val root = super.onCreateView(inflater, container, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
getMapViewModel().getLocationUpdates().collect { viewModel.updateLocation(it) }
if (this@CaptureLocationTaskMapFragment::viewModel.isInitialized) {
getMapViewModel().getLocationUpdates().collect { viewModel.updateLocation(it) }
}
}
return root
}

override fun onMapReady(map: MapFragment) {
binding.locationLockBtn.isClickable = false
viewLifecycleOwner.lifecycleScope.launch { viewModel.onMapReady(mapViewModel) }
viewLifecycleOwner.lifecycleScope.launch {

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

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskMapFragment.kt#L58

Added line #L58 was not covered by tests
if (this@CaptureLocationTaskMapFragment::viewModel.isInitialized) {
viewModel.onMapReady(mapViewModel)
}
}
}

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

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/location/CaptureLocationTaskMapFragment.kt#L62-L63

Added lines #L62 - L63 were not covered by tests

fun setViewModel(viewModel: CaptureLocationTaskViewModel) {
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
this.viewModel = viewModel
}

companion object {
fun newInstance(viewModel: CaptureLocationTaskViewModel, map: MapFragment) =
CaptureLocationTaskMapFragment(viewModel).apply { this.map = map }
fun newInstance(map: MapFragment) = CaptureLocationTaskMapFragment().apply { this.map = map }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class DropPinTaskFragment : AbstractTaskFragment<DropPinTaskViewModel>() {
// 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() * 11617 }
val fragment = DropPinTaskMapFragment.newInstance(map).apply { setViewModel(viewModel) }
parentFragmentManager
.beginTransaction()
.add(rowLayout.id, DropPinTaskMapFragment.newInstance(viewModel, map), "Drop a pin fragment")
.add(rowLayout.id, fragment, "Drop a pin fragment")
.commit()
return rowLayout
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class DropPinTaskMapFragment(private val viewModel: DropPinTaskViewModel) :
AbstractMapFragmentWithControls() {
class DropPinTaskMapFragment : AbstractMapFragmentWithControls() {

private lateinit var mapViewModel: BaseMapViewModel
private lateinit var viewModel: DropPinTaskViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -36,16 +36,23 @@
override fun getMapViewModel(): BaseMapViewModel = mapViewModel

override fun onMapReady(map: MapFragment) {
viewModel.features.observe(this) { map.setFeatures(it) }
if (this@DropPinTaskMapFragment::viewModel.isInitialized) {
viewModel.features.observe(this) { map.setFeatures(it) }
}
}

override fun onMapCameraMoved(position: CameraPosition) {
super.onMapCameraMoved(position)
viewModel.updateCameraPosition(position)
if (this@DropPinTaskMapFragment::viewModel.isInitialized) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swallowing the invalid state here as well will make the issue impossible to identify in prod and even difficult to find locally.

viewModel.updateCameraPosition(position)
}
}

Check warning on line 49 in ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/point/DropPinTaskMapFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/point/DropPinTaskMapFragment.kt#L49

Added line #L49 was not covered by tests

fun setViewModel(viewModel: DropPinTaskViewModel) {
this.viewModel = viewModel
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
}

companion object {
fun newInstance(viewModel: DropPinTaskViewModel, map: MapFragment) =
DropPinTaskMapFragment(viewModel).apply { this.map = map }
fun newInstance(map: MapFragment) = DropPinTaskMapFragment().apply { this.map = map }
}
}
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 }
}
}