Skip to content

Commit 24ef25e

Browse files
authored
Merge pull request #8 from kishandonga/develop
Develop
2 parents 9945719 + 9b3698f commit 24ef25e

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ For, all the read, write, clear, has, remove support context and file name manua
136136

137137
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)
138138

139+
This library tested on the API level 20, 26, 29, 30 if you found any bug or issue raise issue or submit PR
140+
139141
## Future Scope
140142
- add sorting on the Set so get direct sorted data.
141143
- callback extend as we already have in the preferences.

library/src/main/java/io/easyprefs/contract/Read.kt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.easyprefs.contract
22

3+
import android.content.SharedPreferences
4+
35
interface Read {
46
fun content(key: String, defaultValue: Int): Int
57
fun content(key: String, defaultValue: String): String
@@ -9,4 +11,5 @@ interface Read {
911
fun content(key: String, defaultValue: Boolean): Boolean
1012
fun content(key: String, defaultValue: Set<String>): Set<String>
1113
fun allContent(): Map<String, *>
14+
fun pref(): SharedPreferences
1215
}

library/src/main/java/io/easyprefs/contract/Write.kt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.easyprefs.contract
22

3+
import android.content.SharedPreferences
4+
35
interface Write : Atomic {
46
fun content(key: String, value: Int): Write
57
fun content(key: String, value: String): Write
@@ -8,4 +10,5 @@ interface Write : Atomic {
810
fun content(key: String, value: Double): Write
911
fun content(key: String, value: Boolean): Write
1012
fun content(key: String, value: Set<String>): Write
13+
fun prefEditor(): SharedPreferences.Editor
1114
}

library/src/main/java/io/easyprefs/impl/ReadImpl.kt

+46-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class ReadImpl(
1212
private val encType: Encryption
1313
) : Read {
1414

15+
override fun pref(): SharedPreferences {
16+
return pref
17+
}
18+
1519
override fun content(key: String, defaultValue: Int): Int {
1620
return if (encType == Encryption.NONE) {
1721
pref.getInt(key, defaultValue)
@@ -24,14 +28,6 @@ class ReadImpl(
2428
}
2529
}
2630

27-
private fun decrypt(key: String, defaultValue: String): String {
28-
var value = pref.getString(Crypt.encryptKey(key), null) ?: defaultValue
29-
if (value != defaultValue) {
30-
value = Crypt.decrypt(key, value)
31-
}
32-
return value
33-
}
34-
3531
override fun content(key: String, defaultValue: String): String {
3632
return if (encType == Encryption.NONE) {
3733
pref.getString(key, defaultValue) ?: defaultValue
@@ -92,7 +88,7 @@ class ReadImpl(
9288
pref.getStringSet(key, defaultValue) ?: defaultValue
9389
} else {
9490
val value = decrypt(key, "")
95-
if (value.isEmpty()) {
91+
return if (value.isEmpty()) {
9692
defaultValue
9793
} else {
9894
val set = mutableSetOf<String>()
@@ -106,8 +102,47 @@ class ReadImpl(
106102
}
107103
}
108104

109-
//TODO: it give encrypted data as well
110105
override fun allContent(): Map<String, *> {
111-
return pref.all
106+
return if (encType == Encryption.NONE) {
107+
pref.all
108+
} else {
109+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
110+
pref.all
111+
} else {
112+
val map = mutableMapOf<String, Any?>()
113+
pref.all.keys.forEach {
114+
val key = Crypt.encryptKey(it)
115+
val value = decrypt(it, "")
116+
if (isNumeric(value)) {
117+
if (value.contains('.')) {
118+
map[key] = value.toDoubleOrNull()
119+
} else {
120+
map[key] = value.toLongOrNull()
121+
}
122+
} else {
123+
map[key] = value
124+
}
125+
}
126+
map
127+
}
128+
}
129+
}
130+
131+
private fun decrypt(key: String, defaultValue: String): String {
132+
val value = pref.getString(Crypt.encryptKey(key), defaultValue)
133+
return if (value == null) {
134+
defaultValue
135+
} else {
136+
return if (value == defaultValue) {
137+
defaultValue
138+
} else {
139+
Crypt.decrypt(key, value)
140+
}
141+
}
142+
}
143+
144+
private fun isNumeric(toCheck: String): Boolean {
145+
val regex = "-?[0-9]+(\\.[0-9]+)?".toRegex()
146+
return toCheck.matches(regex)
112147
}
113148
}

library/src/main/java/io/easyprefs/impl/WriteImpl.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class WriteImpl(
1212
private val encType: Encryption
1313
) : Write, AtomicImpl(edit) {
1414

15+
override fun prefEditor(): SharedPreferences.Editor {
16+
return edit
17+
}
18+
1519
override fun content(key: String, value: Int): Write {
1620
if (encType == Encryption.NONE) {
1721
edit.putInt(key, value)
@@ -25,10 +29,6 @@ class WriteImpl(
2529
return this
2630
}
2731

28-
private fun crypt(key: String, value: String) {
29-
edit.putString(Crypt.encryptKey(key), Crypt.encrypt(key, value))
30-
}
31-
3232
override fun content(key: String, value: String): Write {
3333
if (encType == Encryption.NONE) {
3434
edit.putString(key, value)
@@ -98,5 +98,9 @@ class WriteImpl(
9898
}
9999
return this
100100
}
101+
102+
private fun crypt(key: String, value: String) {
103+
edit.putString(Crypt.encryptKey(key), Crypt.encrypt(key, value))
104+
}
101105
}
102106

0 commit comments

Comments
 (0)