Skip to content

Commit

Permalink
Extract beacon focus
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Oct 31, 2023
1 parent 0367e69 commit 505e9c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import kotlin.math.hypot
class ARBeaconLayer(
var maxVisibleDistance: Distance = Distance.kilometers(1f),
private val beaconSize: Distance = Distance.meters(4f),
private val labelFormatter: (beacon: Beacon, distance: Distance) -> String? = { beacon, _ -> beacon.name }
private val onFocus: (beacon: Beacon) -> Boolean = { false },
private val onClick: (beacon: Beacon) -> Boolean = { false }
) : ARLayer {

private val beacons = mutableListOf<Beacon>()
Expand Down Expand Up @@ -98,17 +99,10 @@ class ARBeaconLayer(
beaconSize.distance,
CircleCanvasObject(beacon.color, Color.WHITE),
onFocusedFn = {
// TODO: This needs to have access to the view - either pass it in, or move this to the draw method
val distance = hypot(
view.location.distanceTo(beacon.coordinate),
(beacon.elevation ?: view.altitude) - view.altitude
)
val textToRender = labelFormatter(beacon, Distance.meters(distance))
if (!textToRender.isNullOrBlank()) {
view.focusText = textToRender
return@geographic true
}
false
onFocus(beacon)
},
onClickFn = {
onClick(beacon)
}
),
beacon.icon?.let { icon ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.kylecorry.sol.units.Distance
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.astronomy.domain.AstronomyService
import com.kylecorry.trail_sense.databinding.FragmentAugmentedRealityBinding
import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon
import com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence.BeaconRepo
import com.kylecorry.trail_sense.shared.DistanceUtils.toRelativeDistance
import com.kylecorry.trail_sense.shared.FormatService
Expand All @@ -32,6 +33,7 @@ import java.time.Duration
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZonedDateTime
import kotlin.math.hypot

// TODO: Support arguments for default layer visibility (ex. coming from astronomy, enable only sun/moon)
class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>() {
Expand All @@ -46,16 +48,10 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
private val formatter by lazy { FormatService.getInstance(requireContext()) }

private val beaconLayer by lazy {
ARBeaconLayer(Distance.meters(userPrefs.navigation.maxBeaconDistance)) { beacon, distance ->
// TODO: This should be onFocus rather than returning a string
val userDistance = distance.convertTo(userPrefs.baseDistanceUnits).toRelativeDistance()
val formattedDistance = formatter.formatDistance(
userDistance,
Units.getDecimalPlaces(userDistance.units),
strict = false
)
beacon.name + "\n" + formattedDistance
}
ARBeaconLayer(
Distance.meters(userPrefs.navigation.maxBeaconDistance),
onFocus = this::onBeaconFocused
)
}

private val sunLayer = ARMarkerLayer()
Expand Down Expand Up @@ -247,6 +243,22 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
}
}

private fun onBeaconFocused(beacon: Beacon): Boolean {
val distance = hypot(
binding.arView.location.distanceTo(beacon.coordinate),
(beacon.elevation ?: binding.arView.altitude) - binding.arView.altitude
)
val userDistance = Distance.meters(distance).convertTo(userPrefs.baseDistanceUnits)
.toRelativeDistance()
val formattedDistance = formatter.formatDistance(
userDistance,
Units.getDecimalPlaces(userDistance.units),
strict = false
)
binding.arView.focusText = beacon.name + "\n" + formattedDistance
return true
}

override fun generateBinding(
layoutInflater: LayoutInflater, container: ViewGroup?
): FragmentAugmentedRealityBinding {
Expand Down

0 comments on commit 505e9c1

Please sign in to comment.