Skip to content

Commit

Permalink
Merge pull request #8 from kishandonga/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kishandonga authored Sep 30, 2023
2 parents 9945719 + 9b3698f commit 24ef25e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions library/src/main/java/io/easyprefs/contract/Read.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,4 +11,5 @@ interface Read {
fun content(key: String, defaultValue: Boolean): Boolean
fun content(key: String, defaultValue: Set<String>): Set<String>
fun allContent(): Map<String, *>
fun pref(): SharedPreferences
}
3 changes: 3 additions & 0 deletions library/src/main/java/io/easyprefs/contract/Write.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String>): Write
fun prefEditor(): SharedPreferences.Editor
}
57 changes: 46 additions & 11 deletions library/src/main/java/io/easyprefs/impl/ReadImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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<String>()
Expand All @@ -106,8 +102,47 @@ class ReadImpl(
}
}

//TODO: it give encrypted data as well
override fun allContent(): Map<String, *> {
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<String, Any?>()
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)
}
}
12 changes: 8 additions & 4 deletions library/src/main/java/io/easyprefs/impl/WriteImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -98,5 +98,9 @@ class WriteImpl(
}
return this
}

private fun crypt(key: String, value: String) {
edit.putString(Crypt.encryptKey(key), Crypt.encrypt(key, value))
}
}

0 comments on commit 24ef25e

Please sign in to comment.