kopia lustrzana https://github.com/rt-bishop/Look4Sat
Changes to location and settings handlers
rodzic
ffd1fc1551
commit
72f78e5c6b
|
@ -29,9 +29,9 @@ import com.rtbishop.look4sat.R
|
|||
import com.rtbishop.look4sat.data.ISettingsHandler
|
||||
import com.rtbishop.look4sat.domain.ILocationHandler
|
||||
import com.rtbishop.look4sat.domain.QthConverter
|
||||
import com.rtbishop.look4sat.domain.QthConverter.round
|
||||
import com.rtbishop.look4sat.domain.model.DataState
|
||||
import com.rtbishop.look4sat.domain.predict.GeoPos
|
||||
import com.rtbishop.look4sat.presentation.round
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
@ -51,10 +51,13 @@ class LocationHandler @Inject constructor(
|
|||
private val locationCoarse = Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
private val locationFine = Manifest.permission.ACCESS_FINE_LOCATION
|
||||
private val _stationPosition = MutableStateFlow<DataState<GeoPos>>(DataState.Handled)
|
||||
private var currentLocator = settingsHandler.loadStationLocator()
|
||||
private var currentPosition = settingsHandler.loadStationPosition()
|
||||
|
||||
override val stationPosition: StateFlow<DataState<GeoPos>> = _stationPosition
|
||||
|
||||
override fun getStationLocator(): String = currentLocator
|
||||
|
||||
override fun getStationPosition(): GeoPos = currentPosition
|
||||
|
||||
override fun setStationPosition(latitude: Double, longitude: Double) {
|
||||
|
@ -62,11 +65,17 @@ class LocationHandler @Inject constructor(
|
|||
val tempLon = if (longitude > 180.0) longitude - 180 else longitude
|
||||
val newLat = latitude.round(4)
|
||||
val newLon = tempLon.round(4)
|
||||
currentPosition = GeoPos(newLat, newLon)
|
||||
settingsHandler.saveStationPosition(newLat, newLon)
|
||||
_stationPosition.value = DataState.Success(currentPosition)
|
||||
} else _stationPosition.value =
|
||||
DataState.Error(context.getString(R.string.pref_pos_manual_error))
|
||||
QthConverter.positionToQth(newLat, newLon)?.let { locator ->
|
||||
currentLocator = locator
|
||||
currentPosition = GeoPos(newLat, newLon)
|
||||
settingsHandler.saveStationLocator(locator)
|
||||
settingsHandler.saveStationPosition(currentPosition)
|
||||
_stationPosition.value = DataState.Success(currentPosition)
|
||||
}
|
||||
} else {
|
||||
_stationPosition.value =
|
||||
DataState.Error(context.getString(R.string.pref_pos_manual_error))
|
||||
}
|
||||
}
|
||||
|
||||
override fun setPositionFromGps() {
|
||||
|
@ -79,8 +88,9 @@ class LocationHandler @Inject constructor(
|
|||
_stationPosition.value = DataState.Loading
|
||||
manager.requestLocationUpdates(providerGps, 0L, 0f, this)
|
||||
}
|
||||
} else _stationPosition.value =
|
||||
DataState.Error(context.getString(R.string.pref_pos_gps_null))
|
||||
} else {
|
||||
_stationPosition.value = DataState.Error(context.getString(R.string.pref_pos_gps_null))
|
||||
}
|
||||
}
|
||||
|
||||
override fun setPositionFromNet() {
|
||||
|
@ -93,16 +103,22 @@ class LocationHandler @Inject constructor(
|
|||
_stationPosition.value = DataState.Loading
|
||||
manager.requestLocationUpdates(providerNet, 0L, 0f, this)
|
||||
}
|
||||
} else _stationPosition.value =
|
||||
DataState.Error(context.getString(R.string.pref_pos_gps_null))
|
||||
} else {
|
||||
_stationPosition.value = DataState.Error(context.getString(R.string.pref_pos_gps_null))
|
||||
}
|
||||
}
|
||||
|
||||
override fun setPositionFromQth(qthString: String) {
|
||||
val position = QthConverter.qthToPosition(qthString)
|
||||
override fun setPositionFromQth(locator: String) {
|
||||
val position = QthConverter.qthToPosition(locator)
|
||||
if (position != null) {
|
||||
setStationPosition(position.latitude, position.longitude)
|
||||
} else _stationPosition.value =
|
||||
DataState.Error(context.getString(R.string.pref_pos_qth_error))
|
||||
currentLocator = locator
|
||||
currentPosition = position
|
||||
settingsHandler.saveStationLocator(locator)
|
||||
settingsHandler.saveStationPosition(currentPosition)
|
||||
_stationPosition.value = DataState.Success(currentPosition)
|
||||
} else {
|
||||
_stationPosition.value = DataState.Error(context.getString(R.string.pref_pos_qth_error))
|
||||
}
|
||||
}
|
||||
|
||||
override fun setPositionHandled() {
|
||||
|
|
|
@ -42,11 +42,18 @@ class SettingsHandler @Inject constructor(private val prefs: SharedPreferences)
|
|||
const val keyRotatorPort = "rotatorPort"
|
||||
const val keyLatitude = "stationLat"
|
||||
const val keyLongitude = "stationLon"
|
||||
// const val keyPositionGPS = "setPositionGPS"
|
||||
// const val keyPositionQTH = "setPositionQTH"
|
||||
const val keyLocator = "stationQTH"
|
||||
const val keySelection = "selection"
|
||||
}
|
||||
|
||||
override fun loadStationLocator(): String {
|
||||
return prefs.getString(keyLocator, null) ?: "null"
|
||||
}
|
||||
|
||||
override fun saveStationLocator(locator: String) {
|
||||
prefs.edit { putString(keyLocator, locator) }
|
||||
}
|
||||
|
||||
override fun loadStationPosition(): GeoPos {
|
||||
val defaultSP = "0.0"
|
||||
val latitude = prefs.getString(keyLatitude, null) ?: defaultSP
|
||||
|
@ -54,10 +61,10 @@ class SettingsHandler @Inject constructor(private val prefs: SharedPreferences)
|
|||
return GeoPos(latitude.toDouble(), longitude.toDouble())
|
||||
}
|
||||
|
||||
override fun saveStationPosition(latitude: Double, longitude: Double) {
|
||||
override fun saveStationPosition(position: GeoPos) {
|
||||
prefs.edit {
|
||||
putString(keyLatitude, latitude.toString())
|
||||
putString(keyLongitude, longitude.toString())
|
||||
putString(keyLatitude, position.latitude.toString())
|
||||
putString(keyLongitude, position.longitude.toString())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ import android.view.ViewGroup
|
|||
import android.view.WindowManager
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.data.ISettingsHandler
|
||||
import com.rtbishop.look4sat.databinding.DialogLocationBinding
|
||||
import com.rtbishop.look4sat.domain.ILocationHandler
|
||||
import com.rtbishop.look4sat.presentation.setNavResult
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
@ -17,7 +17,7 @@ import javax.inject.Inject
|
|||
class LocationDialog : AppCompatDialogFragment() {
|
||||
|
||||
@Inject
|
||||
lateinit var locationHandler: ILocationHandler
|
||||
lateinit var preferences: ISettingsHandler
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, group: ViewGroup?, state: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.dialog_location, group, false)
|
||||
|
@ -31,7 +31,7 @@ class LocationDialog : AppCompatDialogFragment() {
|
|||
(resources.displayMetrics.widthPixels * 0.94).toInt(),
|
||||
WindowManager.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
val location = locationHandler.getStationPosition()
|
||||
val location = preferences.loadStationPosition()
|
||||
locationLatEdit.setText(location.latitude.toString())
|
||||
locationLonEdit.setText(location.longitude.toString())
|
||||
locationPosBtn.setOnClickListener {
|
||||
|
|
|
@ -31,7 +31,7 @@ class LocatorDialog : AppCompatDialogFragment() {
|
|||
(resources.displayMetrics.widthPixels * 0.94).toInt(),
|
||||
WindowManager.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
// locatorEdit.setText(preferences.getQthLocator())
|
||||
locatorEdit.setText(preferences.loadStationLocator())
|
||||
locatorPosBtn.setOnClickListener {
|
||||
setNavResult("locator", locatorEdit.text.toString())
|
||||
dismiss()
|
||||
|
|
|
@ -29,9 +29,13 @@ interface ISettingsHandler {
|
|||
"https://amsat.org/tle/current/nasabare.txt"
|
||||
)
|
||||
|
||||
fun loadStationLocator(): String
|
||||
|
||||
fun saveStationLocator(locator: String)
|
||||
|
||||
fun loadStationPosition(): GeoPos
|
||||
|
||||
fun saveStationPosition(latitude: Double, longitude: Double)
|
||||
fun saveStationPosition(position: GeoPos)
|
||||
|
||||
fun getHoursAhead(): Int
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@ interface ILocationHandler {
|
|||
|
||||
val stationPosition: SharedFlow<DataState<GeoPos>>
|
||||
|
||||
fun getStationLocator(): String
|
||||
|
||||
fun getStationPosition(): GeoPos
|
||||
|
||||
fun setStationPosition(latitude: Double, longitude: Double)
|
||||
|
@ -16,7 +18,7 @@ interface ILocationHandler {
|
|||
|
||||
fun setPositionFromNet()
|
||||
|
||||
fun setPositionFromQth(qthString: String)
|
||||
fun setPositionFromQth(locator: String)
|
||||
|
||||
fun setPositionHandled()
|
||||
}
|
||||
|
|
|
@ -22,17 +22,17 @@ import kotlin.math.round
|
|||
|
||||
object QthConverter {
|
||||
|
||||
fun qthToPosition(qthString: String): GeoPos? {
|
||||
val trimmedQth = qthString.take(6)
|
||||
if (!isValidQth(trimmedQth)) return null
|
||||
fun qthToPosition(locator: String): GeoPos? {
|
||||
val trimmedQth = locator.take(6)
|
||||
if (!isValidLocator(trimmedQth)) return null
|
||||
val lonFirst = (trimmedQth[0].uppercaseChar().code - 65) * 20
|
||||
val latFirst = (trimmedQth[1].uppercaseChar().code - 65) * 10
|
||||
val lonSecond = trimmedQth[2].toString().toInt() * 2
|
||||
val latSecond = trimmedQth[3].toString().toInt()
|
||||
val lonThird = (((trimmedQth[4].lowercaseChar().code - 97) / 12.0) + (1.0 / 24.0)) - 180
|
||||
val latThird = (((trimmedQth[5].lowercaseChar().code - 97) / 24.0) + (1.0 / 48.0)) - 90
|
||||
val longitude = (lonFirst + lonSecond + lonThird).roundToDecimals(4)
|
||||
val latitude = (latFirst + latSecond + latThird).roundToDecimals(4)
|
||||
val longitude = (lonFirst + lonSecond + lonThird).round(4)
|
||||
val latitude = (latFirst + latSecond + latThird).round(4)
|
||||
return GeoPos(latitude, longitude)
|
||||
}
|
||||
|
||||
|
@ -53,15 +53,14 @@ object QthConverter {
|
|||
}
|
||||
|
||||
fun isValidPosition(lat: Double, lon: Double): Boolean {
|
||||
return (lat > -90.0 && lat < 90.0) && (lon > -180.0 && lon < 360.0)
|
||||
return (lat >= -90.0 && lat <= 90.0) && (lon >= -180.0 && lon <= 360.0)
|
||||
}
|
||||
|
||||
private fun isValidQth(qthString: String): Boolean {
|
||||
val qthPattern = "[a-xA-X][a-xA-X][0-9][0-9][a-xA-X][a-xA-X]".toRegex()
|
||||
return qthString.matches(qthPattern)
|
||||
fun isValidLocator(locator: String): Boolean {
|
||||
return locator.matches("[a-xA-X][a-xA-X][0-9][0-9][a-xA-X][a-xA-X]".toRegex())
|
||||
}
|
||||
|
||||
private fun Double.roundToDecimals(decimals: Int): Double {
|
||||
fun Double.round(decimals: Int): Double {
|
||||
var multiplier = 1.0
|
||||
repeat(decimals) { multiplier *= 10 }
|
||||
return round(this * multiplier) / multiplier
|
||||
|
|
Ładowanie…
Reference in New Issue