Switched to (quite inefficient) AndroidX Preference library for prefs handling

This commit is contained in:
Arty Bishop committed 2019-12-07 17:11:06 +00:00
1 parent a32171c96f
commit b5e78a8dba
5 files changed
+130 -55

No files matched your search

@@ -27,12 +27,15 @@ import javax.inject.Inject
class MainViewModel(application: Application) : AndroidViewModel(application) { class MainViewModel(application: Application) : AndroidViewModel(application) {
private val keyLat = "latitude" private val defValueLoc = application.getString(R.string.def_gsp_loc)
private val keyLon = "longitude" private val defValueHours = application.getString(R.string.def_hours_ahead)
private val keyAlt = "altitude" private val defValueMinEl = application.getString(R.string.def_min_el)
private val keyHours = "hoursAhead" private val keyLat = application.getString(R.string.key_lat)
private val keyMaxEl = "maxElevation" private val keyLon = application.getString(R.string.key_lon)
private val tleFileName = "tleFile.txt" private val keyAlt = application.getString(R.string.key_alt)
private val keyHours = application.getString(R.string.key_hours_ahead)
private val keyMinEl = application.getString(R.string.key_min_el)
private val tleFileName = application.getString(R.string.tle_file_name)
@Inject @Inject
lateinit var locationClient: FusedLocationProviderClient lateinit var locationClient: FusedLocationProviderClient
@@ -48,9 +51,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val debugMessage = MutableLiveData("") val debugMessage = MutableLiveData("")
val gsp = MutableLiveData<GroundStationPosition>( val gsp = MutableLiveData<GroundStationPosition>(
GroundStationPosition( GroundStationPosition(
preferences.getDouble(keyLat, 0.0), preferences.getString(keyLat, defValueLoc)!!.toDouble(),
preferences.getDouble(keyLon, 0.0), preferences.getString(keyLon, defValueLoc)!!.toDouble(),
preferences.getDouble(keyAlt, 0.0) preferences.getString(keyAlt, defValueLoc)!!.toDouble()
) )
) )
@@ -58,8 +61,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
var tleMainList = loadTwoLineElementFile() var tleMainList = loadTwoLineElementFile()
var tleSelectedMap = mutableMapOf<TLE, Boolean>() var tleSelectedMap = mutableMapOf<TLE, Boolean>()
var passPrefs = SatPassPrefs( var passPrefs = SatPassPrefs(
preferences.getInt(keyHours, 8), preferences.getString(keyHours, defValueHours)!!.toInt(),
preferences.getDouble(keyMaxEl, 16.0) preferences.getString(keyMinEl, defValueMinEl)!!.toDouble()
) )
private fun loadTwoLineElementFile(): List<TLE> { private fun loadTwoLineElementFile(): List<TLE> {
@@ -102,11 +105,25 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
passPrefs = SatPassPrefs(hoursAhead, maxEl) passPrefs = SatPassPrefs(hoursAhead, maxEl)
preferences.edit { preferences.edit {
putInt(keyHours, hoursAhead) putInt(keyHours, hoursAhead)
putDouble(keyMaxEl, maxEl) putLong(keyMinEl, maxEl.toRawBits())
apply() apply()
} }
} }
fun updateGsp() {
val lat = preferences.getString(keyLat, defValueLoc)!!.toDouble()
val lon = preferences.getString(keyLon, defValueLoc)!!.toDouble()
val alt = preferences.getString(keyAlt, defValueLoc)!!.toDouble()
gsp.postValue(GroundStationPosition(lat, lon, alt))
}
fun updatePassPref() {
passPrefs = SatPassPrefs(
preferences.getString(keyHours, defValueHours)!!.toInt(),
preferences.getString(keyMinEl, defValueMinEl)!!.toDouble()
)
}
fun updateLocation() { fun updateLocation() {
locationClient.lastLocation.addOnSuccessListener { location: Location? -> locationClient.lastLocation.addOnSuccessListener { location: Location? ->
val lat = location?.latitude ?: 0.0 val lat = location?.latitude ?: 0.0
@@ -114,9 +131,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val alt = location?.altitude ?: 0.0 val alt = location?.altitude ?: 0.0
preferences.edit { preferences.edit {
putDouble(keyLat, lat) putString(keyLat, lat.toString())
putDouble(keyLon, lon) putString(keyLon, lon.toString())
putDouble(keyAlt, alt) putString(keyAlt, alt.toString())
apply() apply()
} }
gsp.postValue(GroundStationPosition(lat, lon, alt)) gsp.postValue(GroundStationPosition(lat, lon, alt))
@@ -154,10 +171,4 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
suspend fun getTransmittersForSat(id: Int): List<Transmitter> { suspend fun getTransmittersForSat(id: Int): List<Transmitter> {
return repository.getTransmittersForSat(id) return repository.getTransmittersForSat(id)
} }
} }
fun SharedPreferences.Editor.putDouble(key: String, double: Double)
: SharedPreferences.Editor = putLong(key, double.toRawBits())
fun SharedPreferences.getDouble(key: String, default: Double) =
Double.fromBits(getLong(key, default.toRawBits()))
@@ -1,11 +1,54 @@
package com.rtbishop.lookingsat.ui package com.rtbishop.lookingsat.ui
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.preference.EditTextPreference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.rtbishop.lookingsat.MainViewModel
import com.rtbishop.lookingsat.R import com.rtbishop.lookingsat.R
class SettingsFragment : PreferenceFragmentCompat() { class SettingsFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
private lateinit var viewModel: MainViewModel
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference, rootKey) setPreferencesFromResource(R.xml.preference, rootKey)
viewModel = ViewModelProvider(activity as MainActivity).get(MainViewModel::class.java)
val provider = EditTextPreference.SimpleSummaryProvider.getInstance()
val latPref = findPreference<EditTextPreference>(context!!.getText(R.string.key_lat))
val lonPref = findPreference<EditTextPreference>(context!!.getText(R.string.key_lon))
val altPref = findPreference<EditTextPreference>(context!!.getText(R.string.key_alt))
latPref?.summaryProvider = provider
lonPref?.summaryProvider = provider
altPref?.summaryProvider = provider
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == context!!.getText(R.string.key_lat) ||
key == context!!.getText(R.string.key_lon) ||
key == context!!.getText(R.string.key_alt)
) {
viewModel.updateGsp()
}
if (key == context!!.getText(R.string.key_hours_ahead) ||
key == context!!.getText(R.string.key_min_el)
) {
viewModel.updatePassPref()
}
}
override fun onResume() {
super.onResume()
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
} }
} }
+4 -4
View File
@@ -10,7 +10,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="@string/pass_pref_tv_hours" android:text="@string/pref_hours_ahead_title"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@@ -19,7 +19,7 @@
android:id="@+id/pass_pref_et_hours" android:id="@+id/pass_pref_et_hours"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/pass_pref_hint_hours" android:hint="@string/pref_hours_ahead_title"
android:importantForAutofill="no" android:importantForAutofill="no"
android:inputType="number" android:inputType="number"
app:layout_constraintEnd_toEndOf="@+id/pass_pref_tv_hours" app:layout_constraintEnd_toEndOf="@+id/pass_pref_tv_hours"
@@ -31,7 +31,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="@string/pass_pref_tv_maxEl" android:text="@string/pref_min_el_title"
app:layout_constraintEnd_toEndOf="@+id/pass_pref_et_hours" app:layout_constraintEnd_toEndOf="@+id/pass_pref_et_hours"
app:layout_constraintStart_toStartOf="@+id/pass_pref_et_hours" app:layout_constraintStart_toStartOf="@+id/pass_pref_et_hours"
app:layout_constraintTop_toBottomOf="@+id/pass_pref_et_hours" /> app:layout_constraintTop_toBottomOf="@+id/pass_pref_et_hours" />
@@ -40,7 +40,7 @@
android:id="@+id/pass_pref_et_maxEl" android:id="@+id/pass_pref_et_maxEl"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/pass_pref_hint_maxEl" android:hint="@string/pref_min_el_title"
android:importantForAutofill="no" android:importantForAutofill="no"
android:inputType="numberDecimal" android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="@+id/pass_pref_tv_maxEl" app:layout_constraintEnd_toEndOf="@+id/pass_pref_tv_maxEl"
+25 -4
View File
@@ -39,10 +39,31 @@
<string name="title_pass_pref">Passes preferences</string> <string name="title_pass_pref">Passes preferences</string>
<string name="title_select_sat">Select satellites to track</string> <string name="title_select_sat">Select satellites to track</string>
<string name="pass_pref_tv_hours">Show passes that occur within X hours</string> <string name="pref_gsp_title">Ground station position settings</string>
<string name="pass_pref_hint_hours">Hours ahead</string> <string name="pref_lat_title">Latitude</string>
<string name="pass_pref_tv_maxEl">Show passes with max elevation above</string> <string name="pref_lon_title">Longitude</string>
<string name="pass_pref_hint_maxEl">Max elevation</string> <string name="pref_alt_title">Altitude</string>
<string name="pref_sat_pass_title">Satellite pass settings</string>
<string name="pref_hours_ahead_title">Hours ahead</string>
<string name="pref_hours_ahead_summary">Show passes that occur within X hours</string>
<string name="pref_min_el_title">Minimum elevation</string>
<string name="pref_min_el_summary">Show passes with max elevation above</string>
<string name="pref_other_title">Other settings</string>
<string name="pref_update_freq_title">Update frequency</string>
<string name="pref_update_freq_summary">Views update interval, ms</string>
<string name="def_gsp_loc">0.0</string>
<string name="def_hours_ahead">8</string>
<string name="def_min_el">16.0</string>
<string name="def_update_freq">3000</string>
<string name="key_lat">latitude</string>
<string name="key_lon">longitude</string>
<string name="key_alt">altitude</string>
<string name="key_hours_ahead">hours_ahead</string>
<string name="key_min_el">min_elevation</string>
<string name="key_update_freq">update_freq</string>
<string name="tle_file_name">tleFile.txt</string>
<string name="no">No</string> <string name="no">No</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>
+24 -24
View File
@@ -1,48 +1,48 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Ground station location settings"> <PreferenceCategory android:title="@string/pref_gsp_title">
<EditTextPreference <EditTextPreference
android:defaultValue="0.0" android:defaultValue="@string/def_gsp_loc"
android:key="lat" android:key="@string/key_lat"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:title="Latitude" /> android:title="@string/pref_lat_title" />
<EditTextPreference <EditTextPreference
android:defaultValue="0.0" android:defaultValue="@string/def_gsp_loc"
android:key="lon" android:key="@string/key_lon"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:title="Longitude" /> android:title="@string/pref_lon_title" />
<EditTextPreference <EditTextPreference
android:defaultValue="0.0" android:defaultValue="@string/def_gsp_loc"
android:key="alt" android:key="@string/key_alt"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:title="Altitude" /> android:title="@string/pref_alt_title" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="Satellite pass settings"> <PreferenceCategory android:title="@string/pref_sat_pass_title">
<EditTextPreference <EditTextPreference
android:defaultValue="8" android:defaultValue="@string/def_hours_ahead"
android:key="hours" android:key="@string/key_hours_ahead"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:summary="Show passes that occur within this time window" android:summary="@string/pref_hours_ahead_summary"
android:title="Hours ahead" /> android:title="@string/pref_hours_ahead_title" />
<EditTextPreference <EditTextPreference
android:defaultValue="16" android:defaultValue="@string/def_hours_ahead"
android:key="minEl" android:key="@string/key_min_el"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:summary="Show passes with maximum elevation above" android:summary="@string/pref_min_el_summary"
android:title="Minimum elevation" /> android:title="@string/pref_min_el_title" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="Other settings"> <PreferenceCategory android:title="@string/pref_other_title">
<EditTextPreference <EditTextPreference
android:defaultValue="3000" android:defaultValue="@string/def_update_freq"
android:key="updateFreq" android:key="@string/key_update_freq"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:summary="RadarView and MapView update frequency, ms" android:summary="@string/pref_update_freq_summary"
android:title="View update interval" /> android:title="@string/pref_update_freq_title" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>