Refactor and optimize PreferencesHelper
This commit is contained in:
parent
c81105186f
commit
09012a35a7
11 changed files with 91 additions and 102 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
@ -109,7 +107,7 @@ class MainActivity : AppCompatActivity() {
|
|||
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
when (key) {
|
||||
Keys.PREF_THEME_SELECTION -> {
|
||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection(this@MainActivity))
|
||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -71,7 +70,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
|
|||
// get current best location
|
||||
currentBestLocation = LocationHelper.getLastKnownLocation(activity as Context)
|
||||
// get saved tracking state
|
||||
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
}
|
||||
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -319,7 +318,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
|
|||
when (key) {
|
||||
Keys.PREF_TRACKING_STATE -> {
|
||||
if (activity != null) {
|
||||
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
layout.updateRecordingButton(trackingState)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
|
|
@ -22,6 +22,7 @@ import android.app.Application
|
|||
import org.y20k.trackbook.helpers.AppThemeHelper
|
||||
import org.y20k.trackbook.helpers.LogHelper
|
||||
import org.y20k.trackbook.helpers.PreferencesHelper
|
||||
import org.y20k.trackbook.helpers.PreferencesHelper.initPreferences
|
||||
|
||||
|
||||
/*
|
||||
|
@ -38,8 +39,9 @@ class Trackbook: Application() {
|
|||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
LogHelper.v(TAG, "Trackbook application started.")
|
||||
initPreferences()
|
||||
// set Dark / Light theme state
|
||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection(this))
|
||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -83,9 +82,9 @@ class TrackerService: Service(), SensorEventListener {
|
|||
/* Overrides onCreate from Service */
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly(this)
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
||||
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier(this)
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly()
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits()
|
||||
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier()
|
||||
|
||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||
|
@ -95,11 +94,11 @@ class TrackerService: Service(), SensorEventListener {
|
|||
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
||||
gpsLocationListener = createLocationListener()
|
||||
networkLocationListener = createLocationListener()
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
currentBestLocation = LocationHelper.getLastKnownLocation(this)
|
||||
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
||||
altitudeValues.capacity = PreferencesHelper.loadAltitudeSmoothingValue(this)
|
||||
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(
|
||||
altitudeValues.capacity = PreferencesHelper.loadAltitudeSmoothingValue()
|
||||
PreferencesHelper.registerPreferenceChangeListener(
|
||||
sharedPreferenceChangeListener
|
||||
)
|
||||
}
|
||||
|
@ -175,7 +174,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
|
||||
|
@ -239,7 +238,7 @@ class TrackerService: Service(), SensorEventListener {
|
|||
}
|
||||
// set state
|
||||
trackingState = Keys.STATE_TRACKING_ACTIVE
|
||||
PreferencesHelper.saveTrackingState(this, trackingState)
|
||||
PreferencesHelper.saveTrackingState(trackingState)
|
||||
// start recording steps and location fixes
|
||||
startStepCounter()
|
||||
handler.postDelayed(periodicTrackUpdate, 0)
|
||||
|
@ -255,7 +254,7 @@ class TrackerService: Service(), SensorEventListener {
|
|||
CoroutineScope(IO).launch { FileHelper.saveTempTrackSuspended(this@TrackerService, track) }
|
||||
// save state
|
||||
trackingState = Keys.STATE_TRACKING_STOPPED
|
||||
PreferencesHelper.saveTrackingState(this, trackingState)
|
||||
PreferencesHelper.saveTrackingState(trackingState)
|
||||
// reset altitude values queue
|
||||
altitudeValues.reset()
|
||||
// stop recording steps and location fixes
|
||||
|
@ -272,7 +271,7 @@ class TrackerService: Service(), SensorEventListener {
|
|||
track = Track()
|
||||
FileHelper.deleteTempFile(this)
|
||||
trackingState = Keys.STATE_TRACKING_NOT
|
||||
PreferencesHelper.saveTrackingState(this, trackingState)
|
||||
PreferencesHelper.saveTrackingState(trackingState)
|
||||
stopForeground(true)
|
||||
}
|
||||
|
||||
|
@ -469,7 +468,7 @@ class TrackerService: Service(), SensorEventListener {
|
|||
when (key) {
|
||||
// preference "Restrict to GPS"
|
||||
Keys.PREF_GPS_ONLY -> {
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly(this@TrackerService)
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly()
|
||||
when (gpsOnly) {
|
||||
true -> removeNetworkLocationListener()
|
||||
false -> addNetworkLocationListener()
|
||||
|
@ -477,11 +476,11 @@ class TrackerService: Service(), SensorEventListener {
|
|||
}
|
||||
// preference "Use Imperial Measurements"
|
||||
Keys.PREF_USE_IMPERIAL_UNITS -> {
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits(this@TrackerService)
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits()
|
||||
}
|
||||
// preference "Recording Accuracy"
|
||||
Keys.PREF_RECORDING_ACCURACY_HIGH -> {
|
||||
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier(this@TrackerService)
|
||||
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -47,7 +46,7 @@ class TrackingToggleTileService: TileService() {
|
|||
override fun onTileAdded() {
|
||||
super.onTileAdded()
|
||||
// get saved tracking state
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
// set up tile
|
||||
updateTile()
|
||||
}
|
||||
|
@ -62,11 +61,11 @@ class TrackingToggleTileService: TileService() {
|
|||
override fun onStartListening() {
|
||||
super.onStartListening()
|
||||
// get saved tracking state
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
|
@ -141,7 +140,7 @@ class TrackingToggleTileService: TileService() {
|
|||
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
when (key) {
|
||||
Keys.PREF_TRACKING_STATE -> {
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
trackingState = PreferencesHelper.loadTrackingState()
|
||||
updateTile()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ object AppThemeHelper {
|
|||
|
||||
/* Returns a readable String for currently selected App Theme */
|
||||
fun getCurrentTheme(context: Context): String {
|
||||
return when (PreferencesHelper.loadThemeSelection(context)) {
|
||||
return when (PreferencesHelper.loadThemeSelection()) {
|
||||
Keys.STATE_THEME_LIGHT_MODE -> context.getString(R.string.pref_theme_selection_mode_light)
|
||||
Keys.STATE_THEME_DARK_MODE -> context.getString(R.string.pref_theme_selection_mode_dark)
|
||||
else -> context.getString(R.string.pref_theme_selection_mode_device_default)
|
||||
|
|
|
@ -62,7 +62,7 @@ object LocationHelper {
|
|||
/* Tries to return the last location that the system has stored */
|
||||
fun getLastKnownLocation(context: Context): Location {
|
||||
// get last location that Trackbook has stored
|
||||
var lastKnownLocation: Location = PreferencesHelper.loadCurrentBestLocation(context)
|
||||
var lastKnownLocation: Location = PreferencesHelper.loadCurrentBestLocation()
|
||||
// try to get the last location the system has stored - it is probably more recent
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.y20k.trackbook.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import androidx.core.content.edit
|
||||
|
@ -32,124 +33,108 @@ 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)
|
||||
|
||||
|
||||
/* Loads zoom level of map */
|
||||
fun loadZoomLevel(context: Context): Double {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadZoomLevel(): Double {
|
||||
// load zoom level
|
||||
return settings.getDouble(Keys.PREF_MAP_ZOOM_LEVEL, Keys.DEFAULT_ZOOM_LEVEL)
|
||||
return sharedPreferences.getDouble(Keys.PREF_MAP_ZOOM_LEVEL, Keys.DEFAULT_ZOOM_LEVEL)
|
||||
}
|
||||
|
||||
|
||||
/* Saves zoom level of map */
|
||||
fun saveZoomLevel(context: Context, zoomLevel: Double) {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun saveZoomLevel(zoomLevel: Double) {
|
||||
// save zoom level
|
||||
settings.edit { putDouble(Keys.PREF_MAP_ZOOM_LEVEL, zoomLevel) }
|
||||
sharedPreferences.edit { putDouble(Keys.PREF_MAP_ZOOM_LEVEL, zoomLevel) }
|
||||
}
|
||||
|
||||
|
||||
/* Loads tracking state */
|
||||
fun loadTrackingState(context: Context): Int {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadTrackingState(): Int {
|
||||
// load tracking state
|
||||
return settings.getInt(Keys.PREF_TRACKING_STATE, Keys.STATE_TRACKING_NOT)
|
||||
return sharedPreferences.getInt(Keys.PREF_TRACKING_STATE, Keys.STATE_TRACKING_NOT)
|
||||
}
|
||||
|
||||
|
||||
/* Saves tracking state */
|
||||
fun saveTrackingState(context: Context, trackingState: Int) {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun saveTrackingState(trackingState: Int) {
|
||||
// save tracking state
|
||||
settings.edit { putInt(Keys.PREF_TRACKING_STATE, trackingState) }
|
||||
sharedPreferences.edit { putInt(Keys.PREF_TRACKING_STATE, trackingState) }
|
||||
}
|
||||
|
||||
|
||||
/* Loads length unit system - metric or imperial */
|
||||
fun loadUseImperialUnits(context: Context): Boolean {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadUseImperialUnits(): Boolean {
|
||||
// load length unit system
|
||||
return settings.getBoolean(Keys.PREF_USE_IMPERIAL_UNITS, LengthUnitHelper.useImperialUnits())
|
||||
return sharedPreferences.getBoolean(Keys.PREF_USE_IMPERIAL_UNITS, LengthUnitHelper.useImperialUnits())
|
||||
}
|
||||
|
||||
|
||||
/* Loads length unit system - metric or imperial */
|
||||
fun loadGpsOnly(context: Context): Boolean {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadGpsOnly(): Boolean {
|
||||
// load length unit system
|
||||
return settings.getBoolean(Keys.PREF_GPS_ONLY, false)
|
||||
return sharedPreferences.getBoolean(Keys.PREF_GPS_ONLY, false)
|
||||
}
|
||||
|
||||
/* Loads accuracy threshold used to determine if location is good enough */
|
||||
fun loadAccuracyThreshold(context: Context): Int {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadAccuracyThreshold(): Int {
|
||||
// load tracking state
|
||||
return settings.getInt(Keys.PREF_LOCATION_ACCURACY_THRESHOLD, Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY)
|
||||
return sharedPreferences.getInt(Keys.PREF_LOCATION_ACCURACY_THRESHOLD, Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Loads state of recording accuracy */
|
||||
fun loadRecordingAccuracyHigh(context: Context): Boolean {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadRecordingAccuracyHigh(): Boolean {
|
||||
// load current setting
|
||||
return settings.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
|
||||
return sharedPreferences.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
|
||||
}
|
||||
|
||||
|
||||
/* Loads current accuracy multiplier */
|
||||
fun loadAccuracyMultiplier(context: Context): Int {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadAccuracyMultiplier(): Int {
|
||||
// load current setting
|
||||
val recordingAccuracyHigh: Boolean = settings.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
|
||||
val recordingAccuracyHigh: Boolean = sharedPreferences.getBoolean(Keys.PREF_RECORDING_ACCURACY_HIGH, false)
|
||||
// return multiplier based on state
|
||||
return if (recordingAccuracyHigh) 2 else 1
|
||||
}
|
||||
|
||||
|
||||
/* Load altitude smoothing value */
|
||||
fun loadAltitudeSmoothingValue(context: Context): Int {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
fun loadAltitudeSmoothingValue(): Int {
|
||||
// load current setting
|
||||
return settings.getInt(Keys.PREF_ALTITUDE_SMOOTHING_VALUE, Keys.DEFAULT_ALTITUDE_SMOOTHING_VALUE)
|
||||
return sharedPreferences.getInt(Keys.PREF_ALTITUDE_SMOOTHING_VALUE, Keys.DEFAULT_ALTITUDE_SMOOTHING_VALUE)
|
||||
}
|
||||
|
||||
|
||||
/* Loads the state of a map */
|
||||
fun loadCurrentBestLocation(context: Context): Location {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val provider: String = settings.getString(Keys.PREF_CURRENT_BEST_LOCATION_PROVIDER, LocationManager.NETWORK_PROVIDER) ?: LocationManager.NETWORK_PROVIDER
|
||||
fun loadCurrentBestLocation(): Location {
|
||||
val provider: String = sharedPreferences.getString(Keys.PREF_CURRENT_BEST_LOCATION_PROVIDER, LocationManager.NETWORK_PROVIDER) ?: LocationManager.NETWORK_PROVIDER
|
||||
// create location
|
||||
val currentBestLocation: Location = Location(provider)
|
||||
// load location attributes
|
||||
currentBestLocation.latitude = settings.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LATITUDE, Keys.DEFAULT_LATITUDE)
|
||||
currentBestLocation.longitude = settings.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_LONGITUDE, Keys.DEFAULT_LONGITUDE)
|
||||
currentBestLocation.accuracy = settings.getFloat(Keys.PREF_CURRENT_BEST_LOCATION_ACCURACY, Keys.DEFAULT_ACCURACY)
|
||||
currentBestLocation.altitude = settings.getDouble(Keys.PREF_CURRENT_BEST_LOCATION_ALTITUDE, Keys.DEFAULT_ALTITUDE)
|
||||
currentBestLocation.time = settings.getLong(Keys.PREF_CURRENT_BEST_LOCATION_TIME, Keys.DEFAULT_TIME)
|
||||
return currentBestLocation
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Saves the state of a map */
|
||||
fun saveCurrentBestLocation(context: Context, currentBestLocation: Location) {
|
||||
// get preferences
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
settings.edit {
|
||||
fun saveCurrentBestLocation(currentBestLocation: Location) {
|
||||
sharedPreferences.edit {
|
||||
// save location
|
||||
putDouble(Keys.PREF_CURRENT_BEST_LOCATION_LATITUDE, currentBestLocation.latitude)
|
||||
putDouble(Keys.PREF_CURRENT_BEST_LOCATION_LONGITUDE, currentBestLocation.longitude)
|
||||
|
@ -161,22 +146,29 @@ object PreferencesHelper {
|
|||
|
||||
|
||||
/* Load currently selected app theme */
|
||||
fun loadThemeSelection(context: Context): String {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(Keys.PREF_THEME_SELECTION, Keys.STATE_THEME_FOLLOW_SYSTEM) ?: Keys.STATE_THEME_FOLLOW_SYSTEM
|
||||
fun loadThemeSelection(): String {
|
||||
return sharedPreferences.getString(Keys.PREF_THEME_SELECTION, Keys.STATE_THEME_FOLLOW_SYSTEM) ?: Keys.STATE_THEME_FOLLOW_SYSTEM
|
||||
}
|
||||
|
||||
|
||||
/* Checks if housekeeping work needs to be done - used usually in DownloadWorker "REQUEST_UPDATE_COLLECTION" */
|
||||
fun isHouseKeepingNecessary(context: Context): Boolean {
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
return settings.getBoolean(Keys.PREF_ONE_TIME_HOUSEKEEPING_NECESSARY, true)
|
||||
fun isHouseKeepingNecessary(): Boolean {
|
||||
return sharedPreferences.getBoolean(Keys.PREF_ONE_TIME_HOUSEKEEPING_NECESSARY, true)
|
||||
}
|
||||
|
||||
|
||||
/* Saves state of housekeeping */
|
||||
fun saveHouseKeepingNecessaryState(context: Context, state: Boolean = false) {
|
||||
val settings = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
settings.edit { putBoolean(Keys.PREF_ONE_TIME_HOUSEKEEPING_NECESSARY, state) }
|
||||
fun saveHouseKeepingNecessaryState(state: Boolean = false) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
|||
/* Main class variables */
|
||||
private val context: Context = fragment.activity as Context
|
||||
private lateinit var tracklistListener: TracklistAdapterListener
|
||||
private var useImperial: Boolean = PreferencesHelper.loadUseImperialUnits(context)
|
||||
private var useImperial: Boolean = PreferencesHelper.loadUseImperialUnits()
|
||||
private var tracklist: Tracklist = Tracklist()
|
||||
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ data class MapFragmentLayoutHolder(private var context: Context, private var mar
|
|||
mapView.setTileSource(TileSourceFactory.MAPNIK)
|
||||
mapView.setMultiTouchControls(true)
|
||||
mapView.zoomController.setVisibility(org.osmdroid.views.CustomZoomButtonsController.Visibility.NEVER)
|
||||
zoomLevel = PreferencesHelper.loadZoomLevel(context)
|
||||
zoomLevel = PreferencesHelper.loadZoomLevel()
|
||||
controller.setZoom(zoomLevel)
|
||||
|
||||
// set dark map tiles, if necessary
|
||||
|
@ -149,8 +149,8 @@ data class MapFragmentLayoutHolder(private var context: Context, private var mar
|
|||
|
||||
/* Save current best location and state of map to shared preferences */
|
||||
fun saveState(currentBestLocation: Location) {
|
||||
PreferencesHelper.saveCurrentBestLocation(context, currentBestLocation)
|
||||
PreferencesHelper.saveZoomLevel(context, mapView.zoomLevelDouble)
|
||||
PreferencesHelper.saveCurrentBestLocation(currentBestLocation)
|
||||
PreferencesHelper.saveZoomLevel(mapView.zoomLevelDouble)
|
||||
// reset user interaction state
|
||||
userInteraction = false
|
||||
}
|
||||
|
|
|
@ -137,7 +137,7 @@ data class TrackFragmentLayoutHolder(private var context: Context, private var m
|
|||
trackManagementViews = rootView.findViewById(R.id.management_icons)
|
||||
|
||||
// get measurement unit system
|
||||
useImperialUnits = PreferencesHelper.loadUseImperialUnits(context)
|
||||
useImperialUnits = PreferencesHelper.loadUseImperialUnits()
|
||||
|
||||
// set dark map tiles, if necessary
|
||||
if (AppThemeHelper.isDarkModeOn(context as Activity)) {
|
||||
|
|
Loading…
Reference in a new issue