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

[CaptureLocation & DropPin task] Fix crashes on config change #2765

Merged
merged 30 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5160407
Get view model from parent fragment
gino-m Sep 23, 2024
50617df
Revert variable rename for cleaner diff
gino-m Sep 23, 2024
0e45a5e
Merge branch 'master' into gino-m/2725/fix-task-constructors
gino-m Sep 23, 2024
e4b17f3
Replace uiState with navigationRequests
gino-m Sep 23, 2024
8bab435
Reinit view model on config change
gino-m Sep 23, 2024
f884dbc
Remove debugging log
gino-m Sep 23, 2024
731437f
Move arg key into const
gino-m Sep 23, 2024
e0201c0
Log error
gino-m Sep 23, 2024
1c05ae8
Merge branch 'master' of https://github.com/google/ground-android int…
gino-m Sep 23, 2024
d19e353
Fix failing check
gino-m Sep 23, 2024
0d69e4f
Revert changes to manifest
gino-m Sep 23, 2024
262c669
Fix tests
gino-m Sep 24, 2024
8ba3d92
Add TODOs
gino-m Sep 24, 2024
e678e0f
Revert formatting change
gino-m Sep 24, 2024
b908177
copied same to other map frag
anandwana001 Sep 24, 2024
24ff226
Merge remote-tracking branch 'origin/gino-m/2725/fix-task-constructor…
anandwana001 Sep 24, 2024
7d10c42
viewmodel check
anandwana001 Sep 24, 2024
43eed3b
Remove final CR
gino-m Sep 24, 2024
6cd2594
Remove final CR
gino-m Sep 25, 2024
6446159
test try
anandwana001 Sep 27, 2024
f7848cb
Merge branch 'refs/heads/test-fix' into anandwana001/2764/task-config…
anandwana001 Sep 27, 2024
66b07ac
nit fix
anandwana001 Sep 27, 2024
0fb5a68
Merge remote-tracking branch 'origin/anandwana001/2764/task-config-su…
anandwana001 Sep 27, 2024
07aed97
Merge branch 'refs/heads/master' into anandwana001/2764/task-config-s…
anandwana001 Oct 3, 2024
436517f
updated
anandwana001 Oct 3, 2024
aec6466
nit fix
anandwana001 Oct 3, 2024
5b8a565
moved to base
anandwana001 Oct 4, 2024
300ae1e
Merge branch 'master' into anandwana001/2764/task-config-support
anandwana001 Oct 8, 2024
6ddeeae
Merge branch 'master' into anandwana001/2764/task-config-support
anandwana001 Oct 9, 2024
7adfdd1
Merge branch 'master' into anandwana001/2764/task-config-support
gino-m Oct 10, 2024
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 @@ -38,6 +38,8 @@ class DataCollectionViewPagerAdapter
@AssistedInject
constructor(
private val drawAreaTaskFragmentProvider: Provider<DrawAreaTaskFragment>,
private val captureLocationTaskFragmentProvider: Provider<CaptureLocationTaskFragment>,
private val dropPinTaskFragmentProvider: Provider<DropPinTaskFragment>,
@Assisted fragment: Fragment,
@Assisted val tasks: List<Task>,
) : FragmentStateAdapter(fragment) {
Expand All @@ -51,12 +53,12 @@ constructor(
Task.Type.TEXT -> TextTaskFragment()
Task.Type.MULTIPLE_CHOICE -> MultipleChoiceTaskFragment()
Task.Type.PHOTO -> PhotoTaskFragment()
Task.Type.DROP_PIN -> DropPinTaskFragment()
Task.Type.DROP_PIN -> dropPinTaskFragmentProvider.get()
Task.Type.DRAW_AREA -> drawAreaTaskFragmentProvider.get()
Task.Type.NUMBER -> NumberTaskFragment()
Task.Type.DATE -> DateTaskFragment()
Task.Type.TIME -> TimeTaskFragment()
Task.Type.CAPTURE_LOCATION -> CaptureLocationTaskFragment()
Task.Type.CAPTURE_LOCATION -> captureLocationTaskFragmentProvider.get()
Task.Type.UNKNOWN ->
throw UnsupportedOperationException("Unsupported task type: ${task.type}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.ground.ui.datacollection.tasks.location

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
Expand All @@ -25,14 +26,16 @@ import com.google.android.ground.ui.datacollection.components.ButtonAction
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.map.MapFragment
import com.google.android.ground.ui.datacollection.tasks.location.CaptureLocationTaskMapFragment.Companion.TASK_ID_FRAGMENT_ARG_KEY
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Provider

@AndroidEntryPoint
class CaptureLocationTaskFragment : AbstractTaskFragment<CaptureLocationTaskViewModel>() {

@Inject lateinit var map: MapFragment
class CaptureLocationTaskFragment @Inject constructor() :
AbstractTaskFragment<CaptureLocationTaskViewModel>() {
@Inject
lateinit var captureLocationTaskMapFragmentProvider: Provider<CaptureLocationTaskMapFragment>

override fun onCreateTaskView(inflater: LayoutInflater): TaskView =
TaskViewFactory.createWithCombinedHeader(inflater, R.drawable.outline_pin_drop)
Expand All @@ -41,20 +44,22 @@ 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 = captureLocationTaskMapFragmentProvider.get()
val args = Bundle()
args.putString(TASK_ID_FRAGMENT_ARG_KEY, taskId)
fragment.arguments = args
parentFragmentManager
.beginTransaction()
.add(
rowLayout.id,
CaptureLocationTaskMapFragment.newInstance(viewModel, map),
CaptureLocationTaskMapFragment::class.java.simpleName,
)
.add(rowLayout.id, fragment, CaptureLocationTaskMapFragment::class.java.simpleName)
.commit()
return rowLayout
}

override fun onTaskResume() {
// Ensure that the location lock is enabled, if it hasn't been.
viewModel.enableLocationLock()
if (isVisible) {
viewModel.enableLocationLock()
}
}

override fun onCreateActionButtons() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,30 @@
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.lifecycle.lifecycleScope
import com.google.android.ground.R
import com.google.android.ground.ui.common.AbstractMapFragmentWithControls
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.datacollection.DataCollectionViewModel
import com.google.android.ground.ui.map.MapFragment
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch

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

private lateinit var mapViewModel: CaptureLocationTaskMapViewModel
private val dataCollectionViewModel: DataCollectionViewModel by

Check warning on line 37 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#L37

Added line #L37 was not covered by tests
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
hiltNavGraphViewModels(R.id.data_collection)
private val viewModel: CaptureLocationTaskViewModel by lazy {
// Access to this viewModel is lazy for testing. This is because the NavHostController could
// not be initialized before the Fragment under test is created, leading to
// hiltNavGraphViewModels() to fail when called on launch.
val taskId = arguments?.getString(TASK_ID_FRAGMENT_ARG_KEY) ?: error("null taskId fragment arg")
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
dataCollectionViewModel.getTaskViewModel(taskId) as CaptureLocationTaskViewModel

Check warning on line 44 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#L44

Added line #L44 was not covered by tests
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -52,12 +64,12 @@
}

override fun onMapReady(map: MapFragment) {
super.onMapReady(map)

Check warning on line 67 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#L67

Added line #L67 was not covered by tests
binding.locationLockBtn.isClickable = false
viewLifecycleOwner.lifecycleScope.launch { viewModel.onMapReady(mapViewModel) }
}

companion object {
fun newInstance(viewModel: CaptureLocationTaskViewModel, map: MapFragment) =
CaptureLocationTaskMapFragment(viewModel).apply { this.map = map }
const val TASK_ID_FRAGMENT_ARG_KEY = "taskId"
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.ground.ui.datacollection.tasks.point

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -30,15 +31,15 @@ import com.google.android.ground.ui.datacollection.components.InstructionsDialog
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.map.MapFragment
import com.google.android.ground.ui.datacollection.tasks.point.DropPinTaskMapFragment.Companion.TASK_ID_FRAGMENT_ARG_KEY
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Provider

@AndroidEntryPoint
class DropPinTaskFragment : AbstractTaskFragment<DropPinTaskViewModel>() {

@Inject lateinit var map: MapFragment
class DropPinTaskFragment @Inject constructor() : AbstractTaskFragment<DropPinTaskViewModel>() {
@Inject lateinit var dropPinTaskMapFragmentProvider: Provider<DropPinTaskMapFragment>

override fun onCreateTaskView(inflater: LayoutInflater): TaskView =
TaskViewFactory.createWithCombinedHeader(inflater, R.drawable.outline_pin_drop)
Expand All @@ -47,9 +48,13 @@ 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 = dropPinTaskMapFragmentProvider.get()
val args = Bundle()
args.putString(TASK_ID_FRAGMENT_ARG_KEY, taskId)
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
fragment.arguments = args
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 @@ -16,17 +16,29 @@
package com.google.android.ground.ui.datacollection.tasks.point

import android.os.Bundle
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import com.google.android.ground.R
import com.google.android.ground.ui.common.AbstractMapFragmentWithControls
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.datacollection.DataCollectionViewModel
import com.google.android.ground.ui.map.CameraPosition
import com.google.android.ground.ui.map.MapFragment
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

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

private lateinit var mapViewModel: BaseMapViewModel
private val dataCollectionViewModel: DataCollectionViewModel by

Check warning on line 33 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#L33

Added line #L33 was not covered by tests
hiltNavGraphViewModels(R.id.data_collection)
private val viewModel: DropPinTaskViewModel by lazy {
// Access to this viewModel is lazy for testing. This is because the NavHostController could
// not be initialized before the Fragment under test is created, leading to
// hiltNavGraphViewModels() to fail when called on launch.
val taskId = arguments?.getString(TASK_ID_FRAGMENT_ARG_KEY) ?: error("null taskId fragment arg")
dataCollectionViewModel.getTaskViewModel(taskId) as DropPinTaskViewModel

Check warning on line 40 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#L40

Added line #L40 was not covered by tests
}

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

override fun onMapReady(map: MapFragment) {
super.onMapReady(map)

Check warning on line 51 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#L51

Added line #L51 was not covered by tests
viewModel.features.observe(this) { map.setFeatures(it) }
}

Expand All @@ -45,7 +58,6 @@
}

companion object {
fun newInstance(viewModel: DropPinTaskViewModel, map: MapFragment) =
DropPinTaskMapFragment(viewModel).apply { this.map = map }
const val TASK_ID_FRAGMENT_ARG_KEY = "taskId"
}
}