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
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.text.TextUtils; | |
/** | |
* helper for shared prefs - java version | |
*/ |
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
private static void edit(Context context, Performer<SharedPreferences.Editor> performer) { | |
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
performer.performOperation(editor); | |
editor.apply(); | |
} | |
public interface Performer<T> { | |
void performOperation(T victim); | |
} |
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
public static void setStringPreference(Context context, String key, String value) { | |
edit(context, (editor) -> editor.putString(key, value)); | |
} |
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
public static <T> void setValue(Context context, String key, T value) { | |
if (value instanceof String) { | |
edit(context, (editor) -> editor.putString(key, (String) value)); | |
} else if (value instanceof Boolean) { | |
edit(context, (editor) -> editor.putBoolean(key, (Boolean) value)); | |
} else if (value instanceof Integer) { | |
edit(context, (editor) -> editor.putInt(key, (Integer) value)); | |
} else if (value instanceof Float) { | |
edit(context, (editor) -> editor.putFloat(key, (Float) value)); | |
} else { |
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
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.text.TextUtils; | |
public class PreferenceHelper { | |
private PreferenceHelper() { | |
} |
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
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() | |
operation(editor) | |
editor.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
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() | |
operation(editor) | |
editor.apply() | |
} | |
fun SharedPreferences.setValue(key: String, value: Any?) { | |
when (value) { | |
is String? -> edit({ it.putString(key, value) }) | |
is Int -> edit({ it.putInt(key, value) }) |
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
inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? { | |
return when (T::class) { | |
String::class -> getString(key, defaultValue as? String) as T? | |
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T? | |
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T? | |
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T? | |
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T? | |
else -> throw UnsupportedOperationException("Not yet implemented") | |
} |
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
object PreferenceHelper { | |
fun defaultPrefs(context: Context): SharedPreferences | |
= PreferenceManager.getDefaultSharedPreferences(context) | |
fun customPrefs(context: Context, name: String): SharedPreferences | |
= context.getSharedPreferences(name, Context.MODE_PRIVATE) | |
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() |
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
class MainActivity : BaseActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val prefs = PreferenceHelper.defaultPrefs(this) | |
//put value in prefs | |
prefs.setValue(Consts.SharedPrefs.KEY,"any_type_of_value") | |
//get value from prefs | |
val value:String? = prefs.getValue(Consts.SharedPrefs.KEY) |
OlderNewer