-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
045d534
commit 069e53c
Showing
3 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
KotlinUtils/src/main/java/com/alfayedoficial/kotlinutils/KUEncryptedPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.alfayedoficial.kotlinutils | ||
|
||
import android.content.SharedPreferences | ||
import com.google.gson.Gson | ||
|
||
class KUEncryptedPreferences (private val mSharedPreferences: SharedPreferences, private val mSharedPreferencesEditor: SharedPreferences.Editor) { | ||
|
||
init { | ||
mSharedPreferencesEditor.apply() | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param value | ||
* save in SharedPreferences by key | ||
*/ | ||
fun setValue(key: String, value: Any?) { | ||
when (value) { | ||
is Int? -> { | ||
mSharedPreferencesEditor.putInt(key, value!!) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
is String? -> { | ||
mSharedPreferencesEditor.putString(key, value!!) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
is Double? -> { | ||
mSharedPreferencesEditor.putString(key, value.toString()) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
is Long? -> { | ||
mSharedPreferencesEditor.putLong(key, value!!) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
is Boolean? -> { | ||
mSharedPreferencesEditor.putBoolean(key, value!!) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
is Any ->{ | ||
mSharedPreferencesEditor.putString(key, Gson().toJson(value)) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getStringValue(key: String, defaultValue: String = ""): String { | ||
return mSharedPreferences.getString(key, defaultValue)!! | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getIntValue(key: String, defaultValue: Int): Int { | ||
return mSharedPreferences.getInt(key, defaultValue) | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getLongValue(key: String, defaultValue: Long): Long { | ||
return mSharedPreferences.getLong(key, defaultValue) | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getBooleanValue(key: String, defaultValue: Boolean = false): Boolean { | ||
return mSharedPreferences.getBoolean(key, defaultValue) | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getFloatValue(key: String, defaultValue: Float): Float { | ||
return mSharedPreferences.getFloat(key, defaultValue) | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun getDoubleValue(key: String, defaultValue: Double): Double { | ||
val doubleAsString = mSharedPreferences.getString(key, defaultValue.toString()) | ||
return doubleAsString?.toDouble() ?: defaultValue | ||
} | ||
|
||
/** | ||
* @param key | ||
* @param defaultValue | ||
* get from SharedPreferences by key | ||
*/ | ||
fun <T : Any> getObject(key: String, defaultValue: T): T { | ||
val json = mSharedPreferences.getString(key, "") | ||
return if (json != null) { | ||
Gson().fromJson(json, defaultValue::class.java) | ||
} else { | ||
defaultValue | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param key | ||
* remove value from SharedPreferences by key | ||
*/ | ||
fun removeKey(key: String) { | ||
mSharedPreferencesEditor.remove(key) | ||
mSharedPreferencesEditor.apply() | ||
} | ||
|
||
/** | ||
* clear SharedPreferences | ||
*/ | ||
fun clear() { | ||
mSharedPreferencesEditor.clear().apply() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters