Merge pull request #105 from TacoTheDank/preference-refactoring

Refactor and optimize PreferencesHelper
This commit is contained in:
y20k 2021-07-05 11:13:01 +02:00 committed by GitHub
commit e0177055ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 35 deletions

View file

@ -17,7 +17,6 @@
package org.y20k.trackbook
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
@ -26,7 +25,6 @@ import android.os.StrictMode.VmPolicy
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.osmdroid.config.Configuration
import org.y20k.trackbook.helpers.AppThemeHelper
@ -91,7 +89,7 @@ class MainActivity : AppCompatActivity() {
}
// register listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(this as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.registerPreferenceChangeListener(sharedPreferenceChangeListener)
}
@ -99,7 +97,7 @@ class MainActivity : AppCompatActivity() {
override fun onDestroy() {
super.onDestroy()
// unregister listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(this as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.unregisterPreferenceChangeListener(sharedPreferenceChangeListener)
}

View file

@ -32,7 +32,6 @@ import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
@ -260,7 +259,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
private fun handleServiceUnbind() {
bound = false
// unregister listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.unregisterPreferenceChangeListener(sharedPreferenceChangeListener)
// stop receiving location updates
handler.removeCallbacks(periodicLocationRequestRunnable)
}
@ -343,7 +342,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
trackingState = trackerService.trackingState
layout.updateRecordingButton(trackingState)
// register listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.registerPreferenceChangeListener(sharedPreferenceChangeListener)
// start listening for location updates
handler.removeCallbacks(periodicLocationRequestRunnable)
handler.postDelayed(periodicLocationRequestRunnable, 0)

View file

@ -37,7 +37,6 @@ import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import org.y20k.trackbook.core.Track
@ -99,9 +98,7 @@ class TrackerService: Service(), SensorEventListener {
currentBestLocation = LocationHelper.getLastKnownLocation(this)
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
altitudeValues.capacity = PreferencesHelper.loadAltitudeSmoothingValue()
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(
sharedPreferenceChangeListener
)
PreferencesHelper.registerPreferenceChangeListener(sharedPreferenceChangeListener)
}
@ -175,7 +172,7 @@ class TrackerService: Service(), SensorEventListener {
// remove notification
stopForeground(true)
// stop listening for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(
PreferencesHelper.unregisterPreferenceChangeListener(
sharedPreferenceChangeListener
)
// stop receiving location updates

View file

@ -23,7 +23,6 @@ import android.graphics.drawable.Icon
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.preference.PreferenceManager
import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.PreferencesHelper
@ -66,7 +65,7 @@ class TrackingToggleTileService: TileService() {
// set up tile
updateTile()
// register listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.registerPreferenceChangeListener(sharedPreferenceChangeListener)
}
@ -84,7 +83,7 @@ class TrackingToggleTileService: TileService() {
override fun onStopListening() {
super.onStopListening()
// unregister listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
PreferencesHelper.unregisterPreferenceChangeListener(sharedPreferenceChangeListener)
}

View file

@ -33,6 +33,14 @@ import org.y20k.trackbook.extensions.putDouble
*/
object PreferencesHelper {
/* The sharedPreferences object to be initialized */
private lateinit var sharedPreferences: SharedPreferences
/* Initialize a single sharedPreferences object when the app is launched */
fun Context.initPreferences() {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
}
/* Define log tag */
private val TAG: String = LogHelper.makeLogTag(PreferencesHelper::class.java)
@ -48,15 +56,15 @@ object PreferencesHelper {
/* Loads zoom level of map */
fun loadZoomLevel(): Double {
// load zoom level
return sharedPreferences.getDouble(Keys.PREF_MAP_ZOOM_LEVEL, Keys.DEFAULT_ZOOM_LEVEL)
}
/* Saves zoom level of map */
fun saveZoomLevel(zoomLevel: Double) {
sharedPreferences.edit {
putDouble(Keys.PREF_MAP_ZOOM_LEVEL, zoomLevel)
}
// save zoom level
sharedPreferences.edit { putDouble(Keys.PREF_MAP_ZOOM_LEVEL, zoomLevel) }
}
@ -69,27 +77,26 @@ object PreferencesHelper {
/* Saves tracking state */
fun saveTrackingState(trackingState: Int) {
sharedPreferences.edit {
putInt(Keys.PREF_TRACKING_STATE, trackingState)
}
// save tracking state
sharedPreferences.edit { putInt(Keys.PREF_TRACKING_STATE, trackingState) }
}
/* Loads length unit system - metric or imperial */
fun loadUseImperialUnits(): Boolean {
// load length unit system
return sharedPreferences.getBoolean(Keys.PREF_USE_IMPERIAL_UNITS, LengthUnitHelper.useImperialUnits())
}
/* Loads length unit system - metric or imperial */
fun loadGpsOnly(): Boolean {
// load length unit system
return sharedPreferences.getBoolean(Keys.PREF_GPS_ONLY, false)
}
/* Loads accuracy threshold used to determine if location is good enough */
fun loadAccuracyThreshold(): Int {
// get preferences
// load tracking state
return sharedPreferences.getInt(Keys.PREF_LOCATION_ACCURACY_THRESHOLD, Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY)
}
@ -98,12 +105,14 @@ object PreferencesHelper {
/* Loads state of recording accuracy */
fun loadRecordingAccuracyHigh(): Boolean {
// load current setting
return sharedPreferences.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
}
/* Loads current accuracy multiplier */
fun loadAccuracyMultiplier(): Int {
// load current setting
val recordingAccuracyHigh: Boolean = sharedPreferences.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
// return multiplier based on state
return if (recordingAccuracyHigh) 2 else 1
@ -112,22 +121,24 @@ object PreferencesHelper {
/* Load altitude smoothing value */
fun loadAltitudeSmoothingValue(): Int {
// load current setting
return sharedPreferences.getInt(Keys.PREF_ALTITUDE_SMOOTHING_VALUE, Keys.DEFAULT_ALTITUDE_SMOOTHING_VALUE)
}
/* Loads the state of a map */
fun loadCurrentBestLocation(): Location {
// create location
val provider: String = sharedPreferences.getString(Keys.PREF_CURRENT_BEST_LOCATION_PROVIDER, LocationManager.NETWORK_PROVIDER) ?: LocationManager.NETWORK_PROVIDER
val currentBestLocation: Location = Location(provider)
// load location attributes
currentBestLocation.latitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LATITUDE, Keys.DEFAULT_LATITUDE)
currentBestLocation.longitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LONGITUDE, Keys.DEFAULT_LONGITUDE)
currentBestLocation.accuracy = sharedPreferences.getFloat(Keys.PREF_CURRENT_BEST_LOCATION_ACCURACY, Keys.DEFAULT_ACCURACY)
currentBestLocation.altitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_ALTITUDE, Keys.DEFAULT_ALTITUDE)
currentBestLocation.time = sharedPreferences.getLong(Keys.PREF_CURRENT_BEST_LOCATION_TIME, Keys.DEFAULT_TIME)
return currentBestLocation
// create location
return Location(provider).apply {
// load location attributes
latitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LATITUDE, Keys.DEFAULT_LATITUDE)
longitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LONGITUDE, Keys.DEFAULT_LONGITUDE)
accuracy = sharedPreferences.getFloat(Keys.PREF_CURRENT_BEST_LOCATION_ACCURACY, Keys.DEFAULT_ACCURACY)
altitude = sharedPreferences.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_ALTITUDE, Keys.DEFAULT_ALTITUDE)
time = sharedPreferences.getLong(Keys.PREF_CURRENT_BEST_LOCATION_TIME, Keys.DEFAULT_TIME)
}
}
@ -158,9 +169,17 @@ object PreferencesHelper {
/* Saves state of housekeeping */
fun saveHouseKeepingNecessaryState(state: Boolean = false) {
sharedPreferences.edit {
putBoolean(Keys.PREF_ONE_TIME_HOUSEKEEPING_NECESSARY, state)
}
sharedPreferences.edit { putBoolean(Keys.PREF_ONE_TIME_HOUSEKEEPING_NECESSARY, state) }
}
/* Start watching for changes in shared preferences - context must implement OnSharedPreferenceChangeListener */
fun registerPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
}
/* Stop watching for changes in shared preferences - context must implement OnSharedPreferenceChangeListener */
fun unregisterPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener)
}
}