Skip to content

Instantly share code, notes, and snippets.

View krupalshah's full-sized avatar
🎯
Focusing

krupal krupalshah

🎯
Focusing
View GitHub Profile
@krupalshah
krupalshah / PreferenceHelper.kt
Last active June 2, 2023 07:44
helper for shared prefs - kotlin version - Final implementation
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
Last active June 4, 2017 20:20
helper for shared prefs - kotlin version - Final implementation usage
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
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() {