Skip to content
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 @@ -20,11 +20,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand All @@ -34,17 +31,18 @@ import androidx.lifecycle.withStarted
import java.math.RoundingMode
import java.text.DecimalFormat
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import org.groundplatform.android.R
import org.groundplatform.android.databinding.MapTaskFragBinding
import org.groundplatform.android.databinding.BasemapLayoutBinding
import org.groundplatform.android.ui.common.AbstractMapContainerFragment
import org.groundplatform.android.ui.common.BaseMapViewModel
import org.groundplatform.android.ui.components.MapFloatingActionButton
import org.groundplatform.android.ui.components.MapFloatingActionButtonType
import org.groundplatform.android.ui.components.RecenterButton
import org.groundplatform.android.ui.datacollection.DataCollectionFragment
import org.groundplatform.android.ui.datacollection.DataCollectionViewModel
import org.groundplatform.android.ui.datacollection.tasks.map.TaskMapScreen
import org.groundplatform.android.ui.datacollection.tasks.map.components.LocationInfo
import org.groundplatform.android.ui.map.Feature
import org.groundplatform.android.ui.map.MapFragment
import org.groundplatform.android.ui.map.gms.getAccuracyOrNull
Expand All @@ -58,8 +56,6 @@ import org.jetbrains.annotations.MustBeInvokedByOverriders
abstract class AbstractTaskMapFragment<TVM : AbstractTaskViewModel> :
AbstractMapContainerFragment() {

protected lateinit var binding: MapTaskFragBinding

protected val dataCollectionViewModel: DataCollectionViewModel by
hiltNavGraphViewModels(R.id.data_collection)

Expand All @@ -70,12 +66,15 @@ abstract class AbstractTaskMapFragment<TVM : AbstractTaskViewModel> :
dataCollectionViewModel.getTaskViewModel(taskId) as TVM
}

private lateinit var viewModel: BaseMapViewModel

protected val taskId: String by lazy {
arguments?.getString(DataCollectionFragment.TASK_ID) ?: error("null taskId fragment arg")
}

private lateinit var viewModel: BaseMapViewModel

private val _isCenterMarkerVisible = MutableStateFlow(true)
private val _locationInfo = MutableStateFlow<LocationInfo?>(null)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel(BaseMapViewModel::class.java)
Expand All @@ -88,64 +87,63 @@ abstract class AbstractTaskMapFragment<TVM : AbstractTaskViewModel> :
): View {
super.onCreateView(inflater, container, savedInstanceState)

binding = MapTaskFragBinding.inflate(inflater, container, false)
setupMapActionButtons()

viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
getMapViewModel().location.collect {
val locationText = it?.toCoordinates()?.toDmsFormat()

val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.DOWN
val accuracy = it?.getAccuracyOrNull()
val accuracyText = accuracy?.let { value -> df.format(value) + "m" } ?: "?"

updateLocationInfoCard(R.string.current_location, locationText, accuracyText, accuracy)
}
}
}

return binding.root
}

private fun setupMapActionButtons() {
binding.mapTypeBtn.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setComposableContent {
MapFloatingActionButton(
type = MapFloatingActionButtonType.MapType,
onClick = { showMapTypeSelectorDialog() },
)
}
}

binding.locationLockBtn.apply {
val binding = BasemapLayoutBinding.inflate(inflater, container, false)
binding.composeContent.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setComposableContent {
val locationLockButton by viewModel.locationLockIconType.collectAsStateWithLifecycle()

MapFloatingActionButton(
type = locationLockButton,
onClick = { viewModel.onLocationLockClick() },
val shouldShowRecenter by viewModel.shouldShowRecenterButton.collectAsStateWithLifecycle()
val isCenterMarkerVisible by _isCenterMarkerVisible.collectAsStateWithLifecycle()
val locationInfo by _locationInfo.collectAsStateWithLifecycle()

TaskMapScreen(
locationLockButtonType = locationLockButton,
shouldShowRecenter = shouldShowRecenter,
isCenterMarkerVisible = isCenterMarkerVisible,
locationInfo = locationInfo,
onMapTypeClicked = { showMapTypeSelectorDialog() },
onLocationLockClicked = { viewModel.onLocationLockClick() },
)
}
}

binding.recenterBtn.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setComposableContent {
val shouldShowRecenter by viewModel.shouldShowRecenterButton.collectAsStateWithLifecycle()
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

if (shouldShowRecenter)
RecenterButton(
modifier = Modifier.padding(start = 20.dp),
onClick = { viewModel.onLocationLockClick() },
)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
combine(getMapViewModel().location, getMapViewModel().locationLock) { location, locationLock
->
Pair(location, locationLock.getOrDefault(false))
}
.collect { (location, isLocked) ->
if (isLocked) {
val locationText = location?.toCoordinates()?.toDmsFormat()
val accuracy = location?.getAccuracyOrNull()
val accuracyText =
accuracy?.let { value -> ACCURACY_FORMATTER.format(value) + "m" } ?: "?"

updateLocationInfoCard(
R.string.current_location,
locationText,
accuracyText,
accuracy,
)
}
}
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_locationInfo.value = null
_isCenterMarkerVisible.value = true
}

override fun getMapViewModel(): BaseMapViewModel = viewModel

@MustBeInvokedByOverriders
Expand All @@ -171,34 +169,24 @@ abstract class AbstractTaskMapFragment<TVM : AbstractTaskViewModel> :
locationText: String?,
accuracyText: String? = null,
accuracyInMeters: Double? = null,
) =
with(binding) {
if (locationText.isNullOrEmpty()) {
infoCard.visibility = View.GONE
} else {
infoCard.visibility = View.VISIBLE
currentLocationTitle.text = getString(title)
currentLocationValue.text = locationText
}

if (accuracyText.isNullOrEmpty()) {
accuracy.visibility = View.GONE
} else {
accuracy.visibility = View.VISIBLE
accuracyTitle.setText(R.string.accuracy)
accuracyValue.text = accuracyText
val color =
if (accuracyInMeters == null || accuracyInMeters > ACCURACY_THRESHOLD_IN_M) {
R.color.accuracy_bad
} else {
R.color.accuracy_good
}
accuracyValue.setTextColor(resources.getColor(color, null))
}
) {
if (locationText.isNullOrEmpty()) {
_locationInfo.value = null
return
}

val isGood = accuracyInMeters != null && accuracyInMeters <= ACCURACY_THRESHOLD_IN_M
_locationInfo.value =
LocationInfo(
titleRes = title,
locationText = locationText,
accuracyText = accuracyText,
isAccuracyGood = isGood,
)
}

fun setCenterMarkerVisibility(visible: Boolean) {
binding.centerMarker.visibility = if (visible) View.VISIBLE else View.GONE
_isCenterMarkerVisible.value = visible
}

@MustBeInvokedByOverriders
Expand All @@ -209,4 +197,9 @@ abstract class AbstractTaskMapFragment<TVM : AbstractTaskViewModel> :
}
updateLocationInfoCard(R.string.map_location, position.coordinates.toDmsFormat())
}

companion object {
private val ACCURACY_FORMATTER =
DecimalFormat("#.##").apply { roundingMode = RoundingMode.DOWN }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.groundplatform.android.ui.datacollection.tasks.map

import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.groundplatform.android.R
import org.groundplatform.android.ui.common.ExcludeFromJacocoGeneratedReport
import org.groundplatform.android.ui.components.MapFloatingActionButton
import org.groundplatform.android.ui.components.MapFloatingActionButtonType
import org.groundplatform.android.ui.components.RecenterButton
import org.groundplatform.android.ui.datacollection.tasks.map.components.LocationInfo
import org.groundplatform.android.ui.datacollection.tasks.map.components.LocationInfoCard
import org.groundplatform.ui.theme.AppTheme

@VisibleForTesting const val TASK_MAP_CENTER_MARKER_TEST_TAG = "task_map_center_marker"

/**
* Screen overlay for map-based tasks, including center crosshairs, map type button, recenter
* button, location lock button, and location info card.
*/
@Composable
fun TaskMapScreen(
locationLockButtonType: MapFloatingActionButtonType,
shouldShowRecenter: Boolean,
isCenterMarkerVisible: Boolean,
locationInfo: LocationInfo?,
onMapTypeClicked: () -> Unit,
onLocationLockClicked: () -> Unit,
modifier: Modifier = Modifier,
) {
Box(modifier = modifier.fillMaxSize()) {
if (isCenterMarkerVisible) {
Image(
painter = painterResource(R.drawable.ic_plus_sign),
contentDescription = null,
modifier =
Modifier.align(Alignment.Center).scale(0.5f).testTag(TASK_MAP_CENTER_MARKER_TEST_TAG),
)
}

MapFloatingActionButton(
modifier = Modifier.align(Alignment.TopEnd),
type = MapFloatingActionButtonType.MapType,
onClick = onMapTypeClicked,
)

Column(modifier = Modifier.align(Alignment.BottomStart).fillMaxWidth()) {
if (shouldShowRecenter) {
RecenterButton(
modifier = Modifier.padding(start = 20.dp, bottom = 8.dp),
onClick = onLocationLockClicked,
)
}

Row(
modifier = Modifier.fillMaxWidth().padding(start = 20.dp, bottom = 4.dp),
verticalAlignment = Alignment.Bottom,
) {
if (locationInfo != null) {
LocationInfoCard(
locationInfo = locationInfo,
modifier = Modifier.weight(1f).padding(bottom = 16.dp, end = 8.dp),
)
} else {
Spacer(modifier = Modifier.weight(1f))
}

MapFloatingActionButton(type = locationLockButtonType, onClick = onLocationLockClicked)
}
}
}
}

@Preview
@Composable
@ExcludeFromJacocoGeneratedReport
private fun TaskMapScreenPreview() {
AppTheme {
TaskMapScreen(
locationLockButtonType = MapFloatingActionButtonType.LocationNotLocked,
shouldShowRecenter = true,
isCenterMarkerVisible = true,
locationInfo =
LocationInfo(
titleRes = R.string.current_location,
locationText = "29º58’15” N 114º36’17”W",
accuracyText = "3m",
isAccuracyGood = true,
),
onMapTypeClicked = {},
onLocationLockClicked = {},
)
}
}
Loading
Loading