fix: map cluster crash (#4317)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-01-26 06:57:49 -06:00 committed by GitHub
parent 4d7af80389
commit 7ffd5bc9f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 101 additions and 60 deletions

View file

@ -81,7 +81,7 @@ import org.meshtastic.proto.copy
import org.meshtastic.proto.waypoint
import java.util.Calendar
@Suppress("LongMethod")
@Suppress("LongMethod", "CyclomaticComplexMethod")
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun EditWaypointDialog(
@ -100,29 +100,52 @@ fun EditWaypointDialog(
// Get current context for dialogs
val context = LocalContext.current
val calendar = Calendar.getInstance()
val currentTime = System.currentTimeMillis()
calendar.timeInMillis = currentTime
@Suppress("MagicNumber")
calendar.add(Calendar.HOUR_OF_DAY, 8)
// Current time for initializing pickers
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
val calendar = remember {
Calendar.getInstance().apply {
if (waypoint.expire != 0 && waypoint.expire != Int.MAX_VALUE) {
timeInMillis = waypoint.expire * 1000L
} else {
timeInMillis = System.currentTimeMillis()
@Suppress("MagicNumber")
add(Calendar.HOUR_OF_DAY, 8)
}
}
}
// Determine locale-specific date format
val dateFormat = android.text.format.DateFormat.getDateFormat(context)
val dateFormat = remember { android.text.format.DateFormat.getDateFormat(context) }
// Check if 24-hour format is preferred
val is24Hour = android.text.format.DateFormat.is24HourFormat(context)
val timeFormat = android.text.format.DateFormat.getTimeFormat(context)
val is24Hour = remember { android.text.format.DateFormat.is24HourFormat(context) }
val timeFormat = remember { android.text.format.DateFormat.getTimeFormat(context) }
// State to hold selected date and time
var selectedDate by remember { mutableStateOf(dateFormat.format(calendar.time)) }
var selectedTime by remember { mutableStateOf(timeFormat.format(calendar.time)) }
var epochTime by remember { mutableStateOf<Long?>(null) }
var selectedDate by remember {
mutableStateOf(
if (waypointInput.expire != 0 && waypointInput.expire != Int.MAX_VALUE) {
dateFormat.format(calendar.time)
} else {
""
},
)
}
var selectedTime by remember {
mutableStateOf(
if (waypointInput.expire != 0 && waypointInput.expire != Int.MAX_VALUE) {
timeFormat.format(calendar.time)
} else {
""
},
)
}
var epochTime by remember {
mutableStateOf<Long?>(
if (waypointInput.expire != 0 && waypointInput.expire != Int.MAX_VALUE) {
waypointInput.expire * 1000L
} else {
null
},
)
}
if (!showEmojiPickerView) {
AlertDialog(
@ -193,9 +216,9 @@ fun EditWaypointDialog(
epochTime = calendar.timeInMillis
selectedDate = dateFormat.format(calendar.time)
},
year,
month,
day,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
)
val timePickerDialog =
@ -206,11 +229,10 @@ fun EditWaypointDialog(
calendar.set(Calendar.MINUTE, selectedMinute)
epochTime = calendar.timeInMillis
selectedTime = timeFormat.format(calendar.time)
@Suppress("MagicNumber")
waypointInput = waypointInput.copy { expire = (epochTime!! / 1000).toInt() }
waypointInput = waypointInput.copy { expire = (calendar.timeInMillis / 1000).toInt() }
},
hour,
minute,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24Hour,
)
@ -227,23 +249,19 @@ fun EditWaypointDialog(
modifier = Modifier.fillMaxWidth().wrapContentWidth(Alignment.End),
checked = waypointInput.expire != Int.MAX_VALUE && waypointInput.expire != 0,
onCheckedChange = { isChecked ->
waypointInput =
waypointInput.copy {
expire =
if (isChecked) {
@Suppress("MagicNumber")
calendar.timeInMillis / 1000
} else {
Int.MAX_VALUE
}
.toInt()
}
if (isChecked) {
// Default to now if not already set
if (epochTime == null) {
epochTime = calendar.timeInMillis
}
selectedDate = dateFormat.format(calendar.time)
selectedTime = timeFormat.format(calendar.time)
waypointInput =
waypointInput.copy { expire = (calendar.timeInMillis / 1000).toInt() }
} else {
selectedDate = ""
selectedTime = ""
waypointInput = waypointInput.copy { expire = Int.MAX_VALUE }
}
},
)

View file

@ -246,10 +246,15 @@ fun EditWaypointDialog(
DatePickerDialog(
context,
{ _: DatePicker, selectedYear: Int, selectedMonth: Int, selectedDay: Int ->
calendar.clear()
calendar.set(selectedYear, selectedMonth, selectedDay, hour, minute)
val tempCal = Calendar.getInstance()
if (waypointInput.expire != 0 && waypointInput.expire != Int.MAX_VALUE) {
tempCal.timeInMillis = waypointInput.expire * 1000L
} else {
tempCal.add(Calendar.HOUR_OF_DAY, 8)
}
tempCal.set(selectedYear, selectedMonth, selectedDay)
waypointInput =
waypointInput.copy { expire = (calendar.timeInMillis / 1000).toInt() }
waypointInput.copy { expire = (tempCal.timeInMillis / 1000).toInt() }
},
year,
month,
@ -262,7 +267,11 @@ fun EditWaypointDialog(
{ _: TimePicker, selectedHour: Int, selectedMinute: Int ->
// Keep the existing date part
val tempCal = Calendar.getInstance()
tempCal.timeInMillis = waypointInput.expire * 1000L
if (waypointInput.expire != 0 && waypointInput.expire != Int.MAX_VALUE) {
tempCal.timeInMillis = waypointInput.expire * 1000L
} else {
tempCal.add(Calendar.HOUR_OF_DAY, 8)
}
tempCal.set(Calendar.HOUR_OF_DAY, selectedHour)
tempCal.set(Calendar.MINUTE, selectedMinute)
waypointInput =

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025 Meshtastic LLC
* Copyright (c) 2025-2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,12 +14,19 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.feature.map.component
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.view.DefaultClusterRenderer
import com.google.maps.android.compose.Circle
@ -37,6 +44,23 @@ fun NodeClusterMarkers(
navigateToNodeDetails: (Int) -> Unit,
onClusterClick: (Cluster<NodeClusterItem>) -> Boolean,
) {
val context = LocalContext.current
// Workaround for https://github.com/googlemaps/android-maps-compose/issues/858
// Ensure owners are set on the Activity decor view so the internal ComposeView created by
// the clustering renderer can find them when walking up the view tree.
LaunchedEffect(Unit) {
val activity = context as? android.app.Activity
if (activity != null) {
val decorView = activity.window.decorView
if (decorView.findViewTreeLifecycleOwner() == null && activity is LifecycleOwner) {
decorView.setViewTreeLifecycleOwner(activity)
}
if (decorView.findViewTreeSavedStateRegistryOwner() == null && activity is SavedStateRegistryOwner) {
decorView.setViewTreeSavedStateRegistryOwner(activity)
}
}
}
if (mapFilterState.showPrecisionCircle) {
nodeClusterItems.forEach { clusterItem ->
key(clusterItem.node.num) {