Skip to content

Commit

Permalink
Show nearest stars
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 committed Dec 13, 2024
1 parent a399a91 commit bfa496e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -498,8 +497,9 @@ object CustomUiUtils {
context: Context,
title: String,
listItems: List<ListItem>,
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<AndromedaListView>(R.id.list)
val empty = view.findViewById<TextView>(R.id.empty_text)
Expand All @@ -510,11 +510,12 @@ object CustomUiUtils {

list.setItems(listItems)

Alerts.dialog(
return Alerts.dialog(
context,
title,
contentView = view,
cancelText = null
cancelText = null,
okText = okText
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FragmentCelestialNavigationBinding>() {
Expand Down Expand Up @@ -107,7 +118,7 @@ class CelestialNavigationFragment : BoundFragment<FragmentCelestialNavigationBin
}

binding.celestialNavigationTitle.leftButton.setOnClickListener {
// TODO: Open sky map bottom sheet
showStarList()
}

binding.celestialNavigationTitle.title.setOnLongClickListener {
Expand All @@ -117,32 +128,32 @@ class CelestialNavigationFragment : BoundFragment<FragmentCelestialNavigationBin
true
}

// TODO: Show the sky map with constellations to help orient the user - use their approximate location
binding.celestialNavigationTitle.subtitle.setOnClickListener {
// TODO: Let the user delete readings
CustomUiUtils.showList(
requireContext(),
getString(R.string.stars),
stars.map { ListItem(it.star.ordinal.toLong(), formatEnumName(it.star.name)) }
)
}

binding.recordBtn.setOnClickListener {
val inclination = binding.arView.inclination
// TODO: Maybe set true north to false and calculate using a location suggested by the user
val azimuth = Bearing.getBearing(binding.arView.azimuth)
// TODO: Let the user specify the last known location (choose source: last known GPS location, timezone, manual)
// TODO: Get preview image and find the offset of the star from the center of the image to get an X (azimuth) and Y (inclination) offset
// TODO: This will be the nearest cluster of white pixels from the center of the image
// val image = binding.camera.previewImage
// val fov = binding.camera.fov
inBackground {
val allStars = Star.entries.sortedBy { it.name }.filterNot { star ->
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) }
Expand Down Expand Up @@ -210,4 +221,71 @@ class CelestialNavigationFragment : BoundFragment<FragmentCelestialNavigationBin
}
}

private fun showStarList(onlyUnselected: Boolean = false, onClick: ((Star) -> 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<Triple<Star, Float, Bearing>> {
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
}
}

}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1530,4 +1530,7 @@
<string name="approximate_location">Approximate location</string>
<string name="last_known_gps_location">Last known GPS location</string>
<string name="timezone">Timezone</string>
<string name="nearest_stars">Nearest stars</string>
<string name="select_star">Select star</string>
<string name="stars">Stars</string>
</resources>

0 comments on commit bfa496e

Please sign in to comment.