feat: per device persistant dismissal of bootloader nags (#3859)

This commit is contained in:
Mac DeCourcy 2025-11-29 18:03:25 -08:00 committed by GitHub
parent ebab2ee9ad
commit 89e82ede59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 181 additions and 37 deletions

View file

@ -0,0 +1,67 @@
/*
* 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 org.meshtastic.core.datastore
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class BootloaderWarningDataSource @Inject constructor(private val dataStore: DataStore<Preferences>) {
private object PreferencesKeys {
val DISMISSED_BOOTLOADER_ADDRESSES = stringPreferencesKey("dismissed-bootloader-addresses")
}
private val dismissedAddressesFlow =
dataStore.data.map { preferences ->
val jsonString = preferences[PreferencesKeys.DISMISSED_BOOTLOADER_ADDRESSES] ?: return@map emptySet()
runCatching { Json.decodeFromString<List<String>>(jsonString).toSet() }
.onFailure { e ->
if (e is IllegalArgumentException || e is SerializationException) {
Timber.w(e, "Failed to parse dismissed bootloader warning addresses, resetting preference")
} else {
Timber.w(e, "Unexpected error while parsing dismissed bootloader warning addresses")
}
}
.getOrDefault(emptySet())
}
/** Returns true if the bootloader warning has been dismissed for the given [address]. */
suspend fun isDismissed(address: String): Boolean = dismissedAddressesFlow.first().contains(address)
/** Marks the bootloader warning as dismissed for the given [address]. */
suspend fun dismiss(address: String) {
val current = dismissedAddressesFlow.first()
if (current.contains(address)) return
val updated = (current + address).toList()
dataStore.edit { preferences ->
preferences[PreferencesKeys.DISMISSED_BOOTLOADER_ADDRESSES] = Json.encodeToString(updated)
}
}
}

View file

@ -964,6 +964,7 @@
<string name="firmware_update_usb_bootloader_warning">%1$s usually ships with a bootloader that does not support OTA updates. You may need to flash an OTA-capable bootloader over USB before flashing OTA.</string>
<string name="learn_more">Learn more</string>
<string name="firmware_update_rak4631_bootloader_hint">For RAK WisBlock RAK4631, use the vendor&apos;s serial DFU tool (for example, adafruit-nrfutil dfu serial with the provided bootloader .zip file). Copying the .uf2 file alone will not update the bootloader.</string>
<string name="dont_show_again_for_device">Don&apos;t show again for this device</string>
<string name="preserve_favorites">Preserve Favorites?</string>
<string name="usb_devices">USB Devices</string>