-
Notifications
You must be signed in to change notification settings - Fork 0
/
BioMatrixSharedPrefs.kt
98 lines (83 loc) · 2.88 KB
/
BioMatrixSharedPrefs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.mindbyromanzanoni.sharedPreference
import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.mindbyromanzanoni.data.response.userData.UserDataModel
import com.mindbyromanzanoni.utils.constant.AppConstants
import javax.inject.Inject
/**
* [BioMatrixSharedPrefs]
* Shared Preferences allow you to save and retrieve data in the form of key,value pair.
* In order to use shared preferences, you have to call a method getSharedPreferences() that returns a
* SharedPreference instance pointing to the file that contains the values of preferences.
* */
class BioMatrixSharedPrefs(context: Context) {
// Get a reference to SharedPreferences
private val sharedPreferences: SharedPreferences = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_BIO_MATRIX, Context.MODE_PRIVATE)
// Get an editor to modify SharedPreferences
private val editor: SharedPreferences.Editor = sharedPreferences.edit()
/**
* General function to save preference value
*/
private fun saveToPreference(key:String, value: Any) {
with(editor) {
when (value) {
is Int -> putInt(key, value)
is Float -> putFloat(key, value)
is Long -> putLong(key, value)
is Boolean -> putBoolean(key, value)
is String -> putString(key, value)
}
apply()
}
}
/**
* Function which will save the boolean value to preference with given key
*/
fun save(preferenceKey: String, booleanValue: Boolean) {
saveToPreference(
preferenceKey,
booleanValue
)
}
/**
* Function which will save the integer value to preference with given key
*/
fun save(@SharedPrefs.PrefKey preferenceKey: String, integerValue: Int) {
saveToPreference(
preferenceKey,
integerValue
)
}
/**
* Function which will return the string value saved in the preference corresponds to the given preference key
*/
fun getString(preferenceKey: String): Int {
return sharedPreferences.getInt(preferenceKey, -1) ?: -1
}
/**
* Function which will return the boolean value saved in the preference corresponds to the given preference key
*/
fun getBoolean(preferenceKey: String): Boolean {
return sharedPreferences.getBoolean(preferenceKey, false)
}
/**
* this function is use to clear preference according to [preferenceKey]
* @param preferenceKey
* */
fun remove(preferenceKey: String) {
with(sharedPreferences.edit()) {
remove(preferenceKey)
apply()
}
}
/**
* this function use to clear Preference
* */
fun clearPreference() {
with(sharedPreferences.edit()) {
clear()
apply()
}
}
}