This file contains hidden or 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
package com.experiments.preferencehelper | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.util.Log | |
import com.experiments.preferencehelper.PreferenceHelper.get | |
import com.experiments.preferencehelper.PreferenceHelper.set | |
class MainActivity : AppCompatActivity() { |
This file contains hidden or 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
val prefs = defaultPrefs(this) | |
prefs[Consts.SharedPrefs.KEY] = "any_type_of_value" //setter | |
val value: String? = prefs[Consts.SharedPrefs.KEY] //getter | |
val anotherValue: Int? = prefs[Consts.SharedPrefs.KEY, 10] //getter with default value |
This file contains hidden or 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 hidden or 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) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 { |
NewerOlder