Como determinar se o armazenamento de dados contém um Preferebces.Key
1. Como julgar se o armazenamento de dados contém um Preferebces.Key
Cole o código diretamente
inline fun <reified T> contains(key: String): Boolean =
runBlocking { return@runBlocking dataStore.data.map { it.contains(getPreferencesKey<T>(key)) }.first() }
- O código para obter o tipo preferKey é o seguinte
inline fun <reified T> getPreferencesKey(key: String): Preferences.Key<out Any> =
when (T::class) {
String::class -> stringPreferencesKey(key)//putString(key, value)
Boolean::class -> booleanPreferencesKey(key)
Int::class -> intPreferencesKey(key)
Long::class -> longPreferencesKey(key)
Float::class -> floatPreferencesKey(key)
Double::class -> doublePreferencesKey(key)
else -> throw IllegalArgumentException("This type cannot be saved to the Data Store")
}