Migrate remaining prefs usages to repo (#2777)

This commit is contained in:
Phil Oliver
2025-08-19 07:17:22 -04:00
committed by GitHub
parent 5f10e9590e
commit c913bb0472
15 changed files with 194 additions and 199 deletions

View File

@@ -18,7 +18,6 @@
package com.geeksville.mesh
import android.os.Debug
import com.geeksville.mesh.android.AppPrefs
import com.geeksville.mesh.android.BuildUtils.isEmulator
import com.geeksville.mesh.android.GeeksvilleApplication
import com.geeksville.mesh.android.prefs.AnalyticsPrefs
@@ -41,8 +40,7 @@ class MeshUtilApplication : GeeksvilleApplication() {
// leave off when running in the debugger
if (!isEmulator && (!BuildConfig.DEBUG || !Debug.isDebuggerConnected())) {
val crashlytics = FirebaseCrashlytics.getInstance()
val pref = AppPrefs(this)
crashlytics.setUserId(pref.getInstallId()) // be able to group all bugs per anonymous user
crashlytics.setUserId(analyticsPrefs.installId) // be able to group all bugs per anonymous user
fun sendCrashReports() {
if (isAnalyticsAllowed) {

View File

@@ -17,9 +17,7 @@
package com.geeksville.mesh.analytics
import android.content.Context
import android.os.Bundle
import com.geeksville.mesh.android.AppPrefs
import com.geeksville.mesh.android.Logging
import com.google.firebase.Firebase
import com.google.firebase.analytics.FirebaseAnalytics
@@ -36,16 +34,11 @@ class DataPair(val name: String, valueIn: Any?) {
}
/** Implement our analytics API using Firebase Analytics */
class FirebaseAnalytics(context: Context) :
class FirebaseAnalytics(installId: String) :
AnalyticsProvider,
Logging {
val t = Firebase.analytics
init {
val pref = AppPrefs(context)
t.setUserId(pref.getInstallId())
}
val t = Firebase.analytics.apply { setUserId(installId) }
override fun setEnabled(on: Boolean) {
t.setAnalyticsCollectionEnabled(on)

View File

@@ -133,7 +133,7 @@ abstract class GeeksvilleApplication :
override fun onCreate() {
super.onCreate()
val firebaseAnalytics = FirebaseAnalytics(this)
val firebaseAnalytics = FirebaseAnalytics(analyticsPrefs.installId)
analytics = firebaseAnalytics
// Set analytics per prefs

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.geeksville.mesh.android.prefs
import android.content.Context
import android.content.SharedPreferences
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Qualifier
import javax.inject.Singleton
// Pref store qualifiers are private to prevent prefs stores from being injected directly.
// Consuming code should always inject one of the prefs repositories.
@Qualifier
@Retention(AnnotationRetention.BINARY)
private annotation class GoogleMapsSharedPreferences
@InstallIn(SingletonComponent::class)
@Module
object GoogleMapsModule {
@Provides
@Singleton
@GoogleMapsSharedPreferences
fun provideGoogleMapsSharedPreferences(@ApplicationContext context: Context): SharedPreferences =
context.getSharedPreferences("google_maps_prefs", Context.MODE_PRIVATE)
@Provides
@Singleton
fun provideGoogleMapsPrefs(@GoogleMapsSharedPreferences sharedPreferences: SharedPreferences): GoogleMapsPrefs =
GoogleMapsPrefsImpl(sharedPreferences)
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.geeksville.mesh.android.prefs
import android.content.SharedPreferences
import com.google.maps.android.compose.MapType
/** Interface for prefs specific to Google Maps. For general map prefs, see MapPrefs. */
interface GoogleMapsPrefs {
var selectedGoogleMapType: String?
var selectedCustomTileUrl: String?
}
class GoogleMapsPrefsImpl(prefs: SharedPreferences) : GoogleMapsPrefs {
override var selectedGoogleMapType: String? by
NullableStringPrefDelegate(prefs, "selected_google_map_type", MapType.NORMAL.name)
override var selectedCustomTileUrl: String? by NullableStringPrefDelegate(prefs, "selected_custom_tile_url", null)
}

View File

@@ -18,13 +18,12 @@
package com.geeksville.mesh.ui.map
import android.app.Application
import android.content.SharedPreferences
import android.net.Uri
import androidx.core.content.edit
import androidx.lifecycle.viewModelScope
import com.geeksville.mesh.ConfigProtos
import com.geeksville.mesh.android.BuildUtils.debug
import com.geeksville.mesh.android.prefs.UiPrefs
import com.geeksville.mesh.android.prefs.GoogleMapsPrefs
import com.geeksville.mesh.android.prefs.MapPrefs
import com.geeksville.mesh.database.NodeRepository
import com.geeksville.mesh.database.PacketRepository
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
@@ -61,8 +60,6 @@ import java.util.UUID
import javax.inject.Inject
private const val TILE_SIZE = 256
private const val PREF_SELECTED_GOOGLE_MAP_TYPE = "selected_google_map_type"
private const val PREF_SELECTED_CUSTOM_TILE_URL = "selected_custom_tile_url"
@Serializable
data class MapCameraPosition(
@@ -79,13 +76,13 @@ class MapViewModel
@Inject
constructor(
private val application: Application,
uiPrefs: UiPrefs,
private val preferences: SharedPreferences,
mapPrefs: MapPrefs,
private val googleMapsPrefs: GoogleMapsPrefs,
nodeRepository: NodeRepository,
packetRepository: PacketRepository,
radioConfigRepository: RadioConfigRepository,
private val customTileProviderRepository: CustomTileProviderRepository,
) : BaseMapViewModel(uiPrefs, nodeRepository, packetRepository) {
) : BaseMapViewModel(mapPrefs, nodeRepository, packetRepository) {
private val _errorFlow = MutableSharedFlow<String>()
val errorFlow: SharedFlow<String> = _errorFlow.asSharedFlow()
@@ -187,7 +184,7 @@ constructor(
if (configToRemove != null && _selectedCustomTileProviderUrl.value == configToRemove.urlTemplate) {
_selectedCustomTileProviderUrl.value = null
// Also clear from prefs
preferences.edit { remove(PREF_SELECTED_CUSTOM_TILE_URL) }
googleMapsPrefs.selectedCustomTileUrl = null
}
}
}
@@ -197,26 +194,24 @@ constructor(
if (!isValidTileUrlTemplate(config.urlTemplate)) {
Timber.tag("MapViewModel").w("Attempted to select invalid URL template: ${config.urlTemplate}")
_selectedCustomTileProviderUrl.value = null
preferences.edit { remove(PREF_SELECTED_CUSTOM_TILE_URL) }
googleMapsPrefs.selectedCustomTileUrl = null
return
}
_selectedCustomTileProviderUrl.value = config.urlTemplate
_selectedGoogleMapType.value = MapType.NORMAL // Reset to a default or keep last? For now, reset.
preferences.edit {
putString(PREF_SELECTED_CUSTOM_TILE_URL, config.urlTemplate).remove(PREF_SELECTED_GOOGLE_MAP_TYPE)
}
googleMapsPrefs.selectedCustomTileUrl = config.urlTemplate
googleMapsPrefs.selectedGoogleMapType = null
} else {
_selectedCustomTileProviderUrl.value = null
preferences.edit { remove(PREF_SELECTED_CUSTOM_TILE_URL) }
googleMapsPrefs.selectedCustomTileUrl = null
}
}
fun setSelectedGoogleMapType(mapType: MapType) {
_selectedGoogleMapType.value = mapType
_selectedCustomTileProviderUrl.value = null // Clear custom selection
preferences.edit {
putString(PREF_SELECTED_GOOGLE_MAP_TYPE, mapType.name).remove(PREF_SELECTED_CUSTOM_TILE_URL)
}
googleMapsPrefs.selectedGoogleMapType = mapType.name
googleMapsPrefs.selectedCustomTileUrl = null
}
fun createUrlTileProvider(urlString: String): TileProvider? {
@@ -254,7 +249,7 @@ constructor(
}
private fun loadPersistedMapType() {
val savedCustomUrl = preferences.getString(PREF_SELECTED_CUSTOM_TILE_URL, null)
val savedCustomUrl = googleMapsPrefs.selectedCustomTileUrl
if (savedCustomUrl != null) {
// Check if this custom provider still exists
if (
@@ -265,18 +260,18 @@ constructor(
_selectedGoogleMapType.value = MapType.NORMAL // Default, as custom is active
} else {
// The saved custom URL is no longer valid or doesn't exist, remove preference
preferences.edit { remove(PREF_SELECTED_CUSTOM_TILE_URL) }
googleMapsPrefs.selectedCustomTileUrl = null
// Fallback to default Google Map type
_selectedGoogleMapType.value = MapType.NORMAL
}
} else {
val savedGoogleMapTypeName = preferences.getString(PREF_SELECTED_GOOGLE_MAP_TYPE, MapType.NORMAL.name)
val savedGoogleMapTypeName = googleMapsPrefs.selectedGoogleMapType
try {
_selectedGoogleMapType.value = MapType.valueOf(savedGoogleMapTypeName ?: MapType.NORMAL.name)
} catch (e: IllegalArgumentException) {
Timber.e(e, "Invalid saved Google Map type: $savedGoogleMapTypeName")
_selectedGoogleMapType.value = MapType.NORMAL // Fallback in case of invalid stored name
preferences.edit { remove(PREF_SELECTED_GOOGLE_MAP_TYPE) }
googleMapsPrefs.selectedGoogleMapType = null
}
}
}