Cleaned up preference.xml, added setPositionFromGPS button

This commit is contained in:
Arty Bishop committed 2020-10-26 22:35:37 +00:00
1 parent cfdba41ed5
commit e06b1bcb11
10 files changed
+88 -143

No files matched your search

@@ -35,9 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Singleton
@Singleton
class SharedViewModel @ViewModelInject constructor(
private val prefsManager: PrefsManager,
private val sourcesRepo: SourcesRepo,
@@ -23,7 +23,6 @@ import android.content.Context
import android.content.SharedPreferences
import android.location.LocationManager
import androidx.preference.PreferenceManager
import com.rtbishop.look4sat.utility.PrefsManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@@ -43,9 +42,4 @@ object UtilityModule {
fun getLocationManager(@ActivityContext context: Context): LocationManager {
return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
}
@Provides
fun getPrefsManager(prefs: SharedPreferences, locationManager: LocationManager): PrefsManager {
return PrefsManager(prefs, locationManager)
}
}
@@ -19,81 +19,56 @@
package com.rtbishop.look4sat.ui.fragments
import android.content.SharedPreferences
import android.location.LocationManager
import android.os.Bundle
import android.text.InputType
import androidx.preference.EditTextPreference
import android.view.View
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.utility.PrefsManager
import com.rtbishop.look4sat.utility.Utilities.round
import com.rtbishop.look4sat.utility.Utilities.snack
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class PrefsFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
class PrefsFragment : PreferenceFragmentCompat() {
@Inject
lateinit var locationManager: LocationManager
@Inject
lateinit var prefsManager: PrefsManager
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference, rootKey)
}
findPreference<EditTextPreference>(PrefsManager.keyLatitude)?.apply {
setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
}
setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -90.0 || valueStr.toDouble() > 90.0) {
getString(R.string.pref_lat_input_error).snack(requireView())
return@setOnPreferenceChangeListener false
}
return@setOnPreferenceChangeListener true
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
findPreference<EditTextPreference>(PrefsManager.keyLongitude)?.apply {
setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
}
setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -180.0 || valueStr.toDouble() > 180.0) {
getString(R.string.pref_lon_input_error).snack(requireView())
return@setOnPreferenceChangeListener false
}
return@setOnPreferenceChangeListener true
}
}
findPreference<EditTextPreference>(PrefsManager.keyAltitude)?.apply {
setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
}
setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -413.0 || valueStr.toDouble() > 8850.0) {
getString(R.string.pref_alt_input_error).snack(requireView())
return@setOnPreferenceChangeListener false
}
return@setOnPreferenceChangeListener true
findPreference<Preference>(PrefsManager.keyPositionGPS)?.apply {
setOnPreferenceClickListener {
setPositionFromGPS()
return@setOnPreferenceClickListener true
}
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {}
override fun onResume() {
super.onResume()
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
private fun setPositionFromGPS() {
try {
val location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)
if (location != null) {
val latitude = location.latitude.round(4)
val longitude = location.longitude.round(4)
val altitude = location.altitude.round(1)
prefsManager.setStationPosition(latitude, longitude, altitude)
getString(R.string.pref_pos_gps_success).snack(requireView())
} else {
getString(R.string.pref_pos_gps_null).snack(requireView())
}
} catch (e: SecurityException) {
getString(R.string.pref_pos_gps_error).snack(requireView())
}
}
}
@@ -21,42 +21,47 @@ package com.rtbishop.look4sat.utility
import android.content.SharedPreferences
import android.hardware.GeomagneticField
import android.location.LocationManager
import androidx.core.content.edit
import com.github.amsacode.predict4java.GroundStationPosition
import com.rtbishop.look4sat.data.PassPrefs
import com.rtbishop.look4sat.utility.Utilities.round
import javax.inject.Inject
class PrefsManager @Inject constructor(
private val preferences: SharedPreferences,
private val locationManager: LocationManager
) {
class PrefsManager @Inject constructor(private val preferences: SharedPreferences) {
companion object {
const val hoursAhead = "hoursAhead"
const val minimalElevation = "minimalElevation"
const val keyLatitude = "latitude"
const val keyLongitude = "longitude"
const val keyAltitude = "altitude"
const val keyCompass = "compass"
const val keyTimeUtc = "timeUTC"
const val defaultGSP = "0.0"
const val keyIsFirstLaunch = "keyIsFirstLaunch"
const val keyTimeUTC = "timeUTC"
const val keyHoursAhead = "hoursAhead"
const val keyMinElevation = "minElevation"
const val keyPositionGPS = "setPositionGPS"
const val keyIsFirstLaunch = "isFirstLaunch"
}
fun getPassPrefs(): PassPrefs {
val hoursAhead = preferences.getInt(hoursAhead, 8)
val minEl = preferences.getInt(minimalElevation, 16).toDouble()
val hoursAhead = preferences.getInt(keyHoursAhead, 8)
val minEl = preferences.getInt(keyMinElevation, 16).toDouble()
return PassPrefs(hoursAhead, minEl)
}
fun getStationPosition(): GroundStationPosition {
val defaultGSP = "0.0"
val lat = preferences.getString(keyLatitude, defaultGSP)!!.toDouble()
val lon = preferences.getString(keyLongitude, defaultGSP)!!.toDouble()
val alt = preferences.getString(keyAltitude, defaultGSP)!!.toDouble()
return GroundStationPosition(lat, lon, alt)
}
fun setStationPosition(latitude: Double, longitude: Double, altitude: Double) {
preferences.edit {
putString(keyLatitude, latitude.toString())
putString(keyLongitude, longitude.toString())
putString(keyAltitude, altitude.toString())
apply()
}
}
fun getMagDeclination(): Float {
val stationPosition = getStationPosition()
val lat = stationPosition.latitude.toFloat()
@@ -65,25 +70,8 @@ class PrefsManager @Inject constructor(
return GeomagneticField(lat, lon, alt, System.currentTimeMillis()).declination
}
fun setStationPositionFromGPS() {
try {
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)?.let {
val lat = it.latitude.round(4)
val lon = it.longitude.round(4)
val alt = it.altitude.round(1)
preferences.edit {
putString(keyLatitude, lat.toString())
putString(keyLongitude, lon.toString())
putString(keyAltitude, alt.toString())
apply()
}
}
} catch (e: SecurityException) {
}
}
fun shouldUseUTC(): Boolean {
return preferences.getBoolean(keyTimeUtc, false)
return preferences.getBoolean(keyTimeUTC, false)
}
fun shouldUseCompass(): Boolean {