diff --git a/README.md b/README.md index 5119639..5618c36 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,8 @@ For, all the read, write, clear, has, remove support context and file name manua If you pass context manually then no need to initialize lib on the application class, For, more information refer [here](app/src/androidTest/java/com/sample/easyprefs) +This library tested on the API level 20, 26, 29, 30 if you found any bug or issue raise issue or submit PR + ## Future Scope - add sorting on the Set so get direct sorted data. - callback extend as we already have in the preferences. diff --git a/library/src/main/java/io/easyprefs/contract/Read.kt b/library/src/main/java/io/easyprefs/contract/Read.kt index f5a223f..33329c8 100644 --- a/library/src/main/java/io/easyprefs/contract/Read.kt +++ b/library/src/main/java/io/easyprefs/contract/Read.kt @@ -1,5 +1,7 @@ package io.easyprefs.contract +import android.content.SharedPreferences + interface Read { fun content(key: String, defaultValue: Int): Int fun content(key: String, defaultValue: String): String @@ -9,4 +11,5 @@ interface Read { fun content(key: String, defaultValue: Boolean): Boolean fun content(key: String, defaultValue: Set): Set fun allContent(): Map + fun pref(): SharedPreferences } \ No newline at end of file diff --git a/library/src/main/java/io/easyprefs/contract/Write.kt b/library/src/main/java/io/easyprefs/contract/Write.kt index c2556d4..1601932 100644 --- a/library/src/main/java/io/easyprefs/contract/Write.kt +++ b/library/src/main/java/io/easyprefs/contract/Write.kt @@ -1,5 +1,7 @@ package io.easyprefs.contract +import android.content.SharedPreferences + interface Write : Atomic { fun content(key: String, value: Int): Write fun content(key: String, value: String): Write @@ -8,4 +10,5 @@ interface Write : Atomic { fun content(key: String, value: Double): Write fun content(key: String, value: Boolean): Write fun content(key: String, value: Set): Write + fun prefEditor(): SharedPreferences.Editor } \ No newline at end of file diff --git a/library/src/main/java/io/easyprefs/impl/ReadImpl.kt b/library/src/main/java/io/easyprefs/impl/ReadImpl.kt index 5438adf..1d1c422 100644 --- a/library/src/main/java/io/easyprefs/impl/ReadImpl.kt +++ b/library/src/main/java/io/easyprefs/impl/ReadImpl.kt @@ -12,6 +12,10 @@ class ReadImpl( private val encType: Encryption ) : Read { + override fun pref(): SharedPreferences { + return pref + } + override fun content(key: String, defaultValue: Int): Int { return if (encType == Encryption.NONE) { pref.getInt(key, defaultValue) @@ -24,14 +28,6 @@ class ReadImpl( } } - private fun decrypt(key: String, defaultValue: String): String { - var value = pref.getString(Crypt.encryptKey(key), null) ?: defaultValue - if (value != defaultValue) { - value = Crypt.decrypt(key, value) - } - return value - } - override fun content(key: String, defaultValue: String): String { return if (encType == Encryption.NONE) { pref.getString(key, defaultValue) ?: defaultValue @@ -92,7 +88,7 @@ class ReadImpl( pref.getStringSet(key, defaultValue) ?: defaultValue } else { val value = decrypt(key, "") - if (value.isEmpty()) { + return if (value.isEmpty()) { defaultValue } else { val set = mutableSetOf() @@ -106,8 +102,47 @@ class ReadImpl( } } - //TODO: it give encrypted data as well override fun allContent(): Map { - return pref.all + return if (encType == Encryption.NONE) { + pref.all + } else { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + pref.all + } else { + val map = mutableMapOf() + pref.all.keys.forEach { + val key = Crypt.encryptKey(it) + val value = decrypt(it, "") + if (isNumeric(value)) { + if (value.contains('.')) { + map[key] = value.toDoubleOrNull() + } else { + map[key] = value.toLongOrNull() + } + } else { + map[key] = value + } + } + map + } + } + } + + private fun decrypt(key: String, defaultValue: String): String { + val value = pref.getString(Crypt.encryptKey(key), defaultValue) + return if (value == null) { + defaultValue + } else { + return if (value == defaultValue) { + defaultValue + } else { + Crypt.decrypt(key, value) + } + } + } + + private fun isNumeric(toCheck: String): Boolean { + val regex = "-?[0-9]+(\\.[0-9]+)?".toRegex() + return toCheck.matches(regex) } } \ No newline at end of file diff --git a/library/src/main/java/io/easyprefs/impl/WriteImpl.kt b/library/src/main/java/io/easyprefs/impl/WriteImpl.kt index d17bc37..ff86a7b 100644 --- a/library/src/main/java/io/easyprefs/impl/WriteImpl.kt +++ b/library/src/main/java/io/easyprefs/impl/WriteImpl.kt @@ -12,6 +12,10 @@ class WriteImpl( private val encType: Encryption ) : Write, AtomicImpl(edit) { + override fun prefEditor(): SharedPreferences.Editor { + return edit + } + override fun content(key: String, value: Int): Write { if (encType == Encryption.NONE) { edit.putInt(key, value) @@ -25,10 +29,6 @@ class WriteImpl( return this } - private fun crypt(key: String, value: String) { - edit.putString(Crypt.encryptKey(key), Crypt.encrypt(key, value)) - } - override fun content(key: String, value: String): Write { if (encType == Encryption.NONE) { edit.putString(key, value) @@ -98,5 +98,9 @@ class WriteImpl( } return this } + + private fun crypt(key: String, value: String) { + edit.putString(Crypt.encryptKey(key), Crypt.encrypt(key, value)) + } }