From bfa496ebb60c08475aa232778a57ce6743c590a8 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Fri, 13 Dec 2024 16:25:31 -0500 Subject: [PATCH] Show nearest stars Signed-off-by: Kyle Corry --- .../trail_sense/shared/CustomUiUtils.kt | 13 +- .../ui/CelestialNavigationFragment.kt | 116 +++++++++++++++--- app/src/main/res/values/strings.xml | 3 + 3 files changed, 107 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/kylecorry/trail_sense/shared/CustomUiUtils.kt b/app/src/main/java/com/kylecorry/trail_sense/shared/CustomUiUtils.kt index 6c5b1f991..db08cdc6e 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/shared/CustomUiUtils.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/shared/CustomUiUtils.kt @@ -10,13 +10,13 @@ import android.widget.Button import android.widget.ImageButton import android.widget.TextView import androidx.annotation.ColorInt +import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentManager import androidx.fragment.app.commit import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import com.google.android.material.datepicker.MaterialDatePicker import com.google.android.material.snackbar.Snackbar import com.google.android.material.timepicker.MaterialTimePicker import com.google.android.material.timepicker.TimeFormat @@ -33,7 +33,6 @@ import com.kylecorry.andromeda.pickers.material.MaterialPickers import com.kylecorry.andromeda.views.chart.Chart import com.kylecorry.andromeda.views.list.AndromedaListView import com.kylecorry.andromeda.views.list.ListItem -import com.kylecorry.sol.time.Time.toZonedDateTime import com.kylecorry.sol.units.Distance import com.kylecorry.sol.units.DistanceUnits import com.kylecorry.sol.units.Pressure @@ -498,8 +497,9 @@ object CustomUiUtils { context: Context, title: String, listItems: List, - emptyText: String? = null - ) { + emptyText: String? = null, + okText: String = context.getString(android.R.string.ok) + ): AlertDialog { val view = View.inflate(context, R.layout.view_list_dialog, null) val list = view.findViewById(R.id.list) val empty = view.findViewById(R.id.empty_text) @@ -510,11 +510,12 @@ object CustomUiUtils { list.setItems(listItems) - Alerts.dialog( + return Alerts.dialog( context, title, contentView = view, - cancelText = null + cancelText = null, + okText = okText ) } diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/celestial_navigation/ui/CelestialNavigationFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/celestial_navigation/ui/CelestialNavigationFragment.kt index 92a2758b9..b7721fa99 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/celestial_navigation/ui/CelestialNavigationFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/celestial_navigation/ui/CelestialNavigationFragment.kt @@ -6,34 +6,45 @@ import android.util.Range import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.appcompat.app.AlertDialog import androidx.camera.view.PreviewView +import com.kylecorry.andromeda.alerts.toast import com.kylecorry.andromeda.core.system.Resources +import com.kylecorry.andromeda.core.ui.Colors import com.kylecorry.andromeda.core.ui.Colors.withAlpha import com.kylecorry.andromeda.fragments.BoundFragment import com.kylecorry.andromeda.fragments.inBackground -import com.kylecorry.andromeda.pickers.CoroutinePickers import com.kylecorry.andromeda.pickers.Pickers import com.kylecorry.andromeda.sense.accelerometer.LowPassAccelerometer import com.kylecorry.andromeda.sense.magnetometer.LowPassMagnetometer import com.kylecorry.andromeda.sense.orientation.CustomRotationSensor import com.kylecorry.andromeda.sense.orientation.Gyroscope +import com.kylecorry.andromeda.views.list.ListItem +import com.kylecorry.andromeda.views.list.ResourceListIcon import com.kylecorry.luna.coroutines.onDefault +import com.kylecorry.sol.math.SolMath.deltaAngle +import com.kylecorry.sol.math.SolMath.map +import com.kylecorry.sol.math.SolMath.square import com.kylecorry.sol.science.astronomy.Astronomy import com.kylecorry.sol.science.astronomy.stars.Star import com.kylecorry.sol.science.astronomy.stars.StarReading +import com.kylecorry.sol.time.Time import com.kylecorry.sol.units.Bearing import com.kylecorry.sol.units.Coordinate import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.databinding.FragmentCelestialNavigationBinding +import com.kylecorry.trail_sense.shared.CustomUiUtils import com.kylecorry.trail_sense.shared.CustomUiUtils.getCardinalDirectionColor import com.kylecorry.trail_sense.shared.FormatService import com.kylecorry.trail_sense.shared.formatEnumName +import com.kylecorry.trail_sense.shared.fromColorTemperature import com.kylecorry.trail_sense.shared.sensors.LocationSubsystem import com.kylecorry.trail_sense.shared.sensors.SensorService import com.kylecorry.trail_sense.shared.sensors.providers.CompassProvider.Companion.ACCELEROMETER_LOW_PASS import com.kylecorry.trail_sense.shared.sensors.providers.CompassProvider.Companion.MAGNETOMETER_LOW_PASS import com.kylecorry.trail_sense.shared.sharing.Share import com.kylecorry.trail_sense.tools.augmented_reality.ui.layers.ARGridLayer +import java.time.ZoneId import java.time.ZonedDateTime class CelestialNavigationFragment : BoundFragment() { @@ -107,7 +118,7 @@ class CelestialNavigationFragment : BoundFragment - stars.any { it.star == star } - } - val starIdx = CoroutinePickers.item( - requireContext(), - getString(R.string.star), - allStars.map { formatEnumName(it.name) }) - if (starIdx != null) { - stars = stars + StarReading( - allStars[starIdx], - inclination, - azimuth, - ZonedDateTime.now() - ) + showStarList(true) { star -> + toast(formatEnumName(star.name)) + stars = stars + StarReading( + star, + inclination, + azimuth, + ZonedDateTime.now() + ) + inBackground { calculating = true location = onDefault { Astronomy.getLocationFromStars(stars, approximateLocation) } @@ -210,4 +221,71 @@ class CelestialNavigationFragment : BoundFragment Unit)? = null) { + val allStars = getNearestStars().filter { (star, _, _) -> + !onlyUnselected || stars.none { it.star == star } + } + + var dialog: AlertDialog? = null + + val starItems = allStars.map { (star, starAltitude, starAzimuth) -> + + val azimuthText = formatter.formatDegrees(starAzimuth.value, replace360 = true) + .padStart(4, ' ') + val directionText = formatter.formatDirection(starAzimuth.direction) + .padStart(2, ' ') + + ListItem( + star.ordinal.toLong(), + formatEnumName(star.name), + "$azimuthText $directionText\n${formatter.formatDegrees(starAltitude)}", + icon = ResourceListIcon( + R.drawable.bubble, + Colors.fromColorTemperature(Astronomy.getColorTemperature(star)), + foregroundSize = map( + -star.magnitude, + -2f, + 1.5f, + 10f, + 24f, + true + ) + ), + action = { + dialog?.dismiss() + onClick?.invoke(star) + } + ) + } + dialog = CustomUiUtils.showList( + requireContext(), + if (onClick != null) getString(R.string.select_star) else getString(R.string.nearest_stars), + starItems, + okText = if (onClick != null) { + getString(android.R.string.cancel) + } else { + getString(android.R.string.ok) + } + ) + } + + private fun getNearestStars(): List> { + val azimuth = binding.arView.azimuth + val inclination = binding.arView.inclination + + val lookupLocation = + approximateLocation ?: Time.getLocationFromTimeZone(ZoneId.systemDefault()) + + return Star.entries.map { + Triple( + it, + Astronomy.getStarAltitude(it, ZonedDateTime.now(), lookupLocation, true), + Astronomy.getStarAzimuth(it, ZonedDateTime.now(), lookupLocation) + ) + }.sortedBy { + val addition = if (it.second < -5) 100000f else 0f + square(inclination - it.second) + square(deltaAngle(azimuth, it.third.value)) + addition + } + } + } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 231a0ef62..235e0065f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1530,4 +1530,7 @@ Approximate location Last known GPS location Timezone + Nearest stars + Select star + Stars \ No newline at end of file