Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

krupal krupalshah

🎯
Focusing
Block or report user

Report or block krupalshah

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View MainActivity.kt
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() {
@krupalshah
krupalshah / MainActivity.kt
Last active Jun 4, 2017
helper for shared prefs - kotlin version - Final implementation usage
View MainActivity.kt
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
@krupalshah
krupalshah / PreferenceHelper.kt
Last active Jul 23, 2019
helper for shared prefs - kotlin version - Final implementation
View PreferenceHelper.kt
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()
@krupalshah
krupalshah / MainActivity.kt
Created Jun 4, 2017
helper for shared prefs - kotlin version - usage after refactoring step 4
View MainActivity.kt
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)
@krupalshah
krupalshah / PreferenceHelper.kt
Last active Jun 17, 2017
helper for shared prefs - kotlin version
View PreferenceHelper.kt
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()
@krupalshah
krupalshah / PreferenceHelper.kt
Last active Jun 17, 2017
helper for shared prefs - kotlin version - refactoring step 4
View PreferenceHelper.kt
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")
}
@krupalshah
krupalshah / PreferenceHelper.kt
Last active Sep 28, 2019
helper for shared prefs - kotlin version - Refactoring step 4
View PreferenceHelper.kt
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) })
@krupalshah
krupalshah / PreferenceHelper.kt
Created Jun 4, 2017
helper for shared prefs - kotlin version - Refactoring step 3
View PreferenceHelper.kt
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
@krupalshah
krupalshah / PreferenceHelper.java
Last active Aug 12, 2018
helper for shared prefs - java version - Refactoring final version
View PreferenceHelper.java
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
public class PreferenceHelper {
private PreferenceHelper() {
}
@krupalshah
krupalshah / PreferenceHelper.java
Last active Aug 12, 2018
helper for shared prefs - java version - Refactoring step 2
View PreferenceHelper.java
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 {
You can’t perform that action at this time.