Skip to content

Commit

Permalink
Add triangulation navigate share action
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Oct 12, 2023
1 parent e447b05 commit a489f5b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class Beacon(
companion object {
fun temporary(
coordinate: Coordinate,
id: Long = -1L,
id: Long = 0L,
name: String = "",
visible: Boolean = true,
comment: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ enum class BeaconOwner(val id: Int) {
User(0),
Path(1),
CellSignal(2),
Maps(3)
Maps(3),
Triangulate(4)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@ package com.kylecorry.trail_sense.navigation.infrastructure

import android.content.Context
import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.sol.units.Coordinate
import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon
import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner
import com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence.BeaconRepo
import com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence.BeaconService
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class Navigator private constructor(context: Context) {

private val prefs = PreferencesSubsystem.getInstance(context).preferences
private val repo = BeaconRepo.getInstance(context)
private val service = BeaconService(context)

// Flows
private val _destinationId = MutableStateFlow(getDestinationId())
val destinationId: Flow<Long?> = _destinationId
.distinctUntilChanged { old, new -> old == new }

val destination = destinationId.map { it?.let { repo.getBeacon(it)?.toBeacon() } }
val destination = destinationId.map { it?.let { service.getBeacon(it) } }

fun navigateTo(location: Coordinate, owner: BeaconOwner = BeaconOwner.User) {
val beacon = Beacon.temporary(location, visible = false, owner = owner)
CoroutineScope(Dispatchers.IO).launch {
val id = service.add(beacon)
navigateTo(id)
}
}

fun navigateTo(beacon: Beacon) {
navigateTo(beacon.id)
Expand All @@ -43,7 +57,7 @@ class Navigator private constructor(context: Context) {

suspend fun getDestination(): Beacon? = onIO {
val id = getDestinationId() ?: return@onIO null
repo.getBeacon(id)?.toBeacon()
service.getBeacon(id)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.navigation.fragment.findNavController
import com.kylecorry.andromeda.alerts.Alerts
import com.kylecorry.andromeda.core.system.GeoUri
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.setCompoundDrawables
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.sol.science.geology.CoordinateBounds
import com.kylecorry.sol.science.geology.Geofence
import com.kylecorry.sol.science.geology.Geology
Expand All @@ -23,6 +25,9 @@ import com.kylecorry.sol.units.Distance
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.databinding.FragmentToolTriangulateBinding
import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon
import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner
import com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence.BeaconService
import com.kylecorry.trail_sense.navigation.infrastructure.Navigator
import com.kylecorry.trail_sense.navigation.infrastructure.share.LocationCopy
import com.kylecorry.trail_sense.navigation.paths.domain.LineStyle
import com.kylecorry.trail_sense.navigation.ui.IMappablePath
Expand All @@ -34,16 +39,21 @@ import com.kylecorry.trail_sense.shared.AppUtils
import com.kylecorry.trail_sense.shared.CustomUiUtils
import com.kylecorry.trail_sense.shared.DistanceUtils.toRelativeDistance
import com.kylecorry.trail_sense.shared.FormatService
import com.kylecorry.trail_sense.shared.NavigationUtils
import com.kylecorry.trail_sense.shared.Units
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.colors.AppColor
import com.kylecorry.trail_sense.shared.extensions.from
import com.kylecorry.trail_sense.shared.extensions.onIO
import com.kylecorry.trail_sense.shared.navigation.NavControllerAppNavigation
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem

class FragmentToolTriangulate : BoundFragment<FragmentToolTriangulateBinding>() {

private val formatService by lazy { FormatService.getInstance(requireContext()) }
private val prefs by lazy { UserPreferences(requireContext()) }
private val appNavigation by lazy { NavControllerAppNavigation(findNavController()) }
private val navigator by lazy { Navigator.getInstance(requireContext()) }

private var location: Coordinate? = null

Expand Down Expand Up @@ -95,17 +105,22 @@ class FragmentToolTriangulate : BoundFragment<FragmentToolTriangulateBinding>()
}
}

binding.navigate.setOnClickListener {
inBackground {
location?.let {
navigator.navigateTo(it, BeaconOwner.Triangulate)
appNavigation.navigate(R.id.action_navigation)
}
}
}

binding.updateGpsOverride.setOnClickListener {
location?.let { coord ->
prefs.locationOverride = coord
Alerts.toast(requireContext(), getString(R.string.location_override_updated))
}
}

if (prefs.useAutoLocation) {
binding.updateGpsOverride.isVisible = false
}

binding.locationButtonGroup.check(if (shouldCalculateMyLocation) binding.locationButtonSelf.id else binding.locationButtonOther.id)
binding.locationButtonGroup.addOnButtonCheckedListener { _, checkedId, isChecked ->
if (!isChecked) {
Expand Down Expand Up @@ -363,6 +378,11 @@ class FragmentToolTriangulate : BoundFragment<FragmentToolTriangulateBinding>()
binding.triangulateTitle.rightButton.isInvisible = false
binding.actions.isVisible = true
}

// Update action button visibility
binding.navigate.isVisible = !shouldCalculateMyLocation
binding.updateGpsOverride.isVisible = !prefs.useAutoLocation && shouldCalculateMyLocation

updateMap()
updateDistances()
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/fragment_tool_triangulate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@
app:chipIconTint="?android:textColorSecondary"
app:iconStartPadding="4dp" />

<com.google.android.material.chip.Chip
android:id="@+id/navigate"
style="@style/Widget.Material3.Chip.Assist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/navigate"
app:chipIcon="@drawable/ic_beacon"
app:chipIconSize="@dimen/chip_icon_size"
app:chipIconTint="?android:textColorSecondary"
app:iconStartPadding="4dp" />

<com.google.android.material.chip.Chip
android:id="@+id/update_gps_override"
style="@style/Widget.Material3.Chip.Assist"
Expand Down

0 comments on commit a489f5b

Please sign in to comment.