Skip to content

Instantly share code, notes, and snippets.

@Audhil
Last active February 5, 2018 07:27
Show Gist options
  • Save Audhil/3b42e8c97e09e1bdc797c57d52f9640d to your computer and use it in GitHub Desktop.
Save Audhil/3b42e8c97e09e1bdc797c57d52f9640d to your computer and use it in GitHub Desktop.
SharedPreferences With Kotlin Extn funcs = Life Easy!
@Singleton
@Component(
modules = [(SharedPreferenceModule::class),
(APIModule::class),
(RepositoryModule::class),
(DataBaseModule::class),
(DBRefreshingModule::class), ....]
)
interface AppComponent {
// app's application class
fun inject(into: AppDelegate)
}
class AppDelegate : Application() {
lateinit var appDaggerComponent: AppComponent
private set
@Inject
lateinit var sPreferences: SharedPreferences
@Inject
lateinit var sPreferencesEditor: SharedPreferences.Editor
override fun onCreate() {
super.onCreate()
INSTANCE = this
appDaggerComponent = DaggerAppComponent
.builder()
.applicationModule(ApplicationModule(this))
.aPIModule(APIModule())
.repositoryModule(RepositoryModule())
.build()
appDaggerComponent.inject(this)
}
}
fun Any.writeToPref(tag: String,
prefEditor: SharedPreferences.Editor = AppDelegate.INSTANCE.sPreferencesEditor) {
when (this) {
is String -> prefEditor.putString(tag, this)
is Int -> prefEditor.putInt(tag, this)
is Long -> prefEditor.putLong(tag, this)
is Boolean -> prefEditor.putBoolean(tag, this)
is Float -> prefEditor.putFloat(tag, this)
else -> throw IllegalArgumentException(ConstantsUtil.INVALID_INPUT_SHARED_PREFERENCE)
}
prefEditor.commit()
}
fun String.readStringFromPref(sPreferences: SharedPreferences = AppDelegate.INSTANCE.sPreferences): String =
sPreferences.getString(this, ConstantsUtil.EMPTY)
fun String.readIntFromPref(sPreferences: SharedPreferences = AppDelegate.INSTANCE.sPreferences): Int =
sPreferences.getInt(this, 0)
fun String.readLongFromPref(sPreferences: SharedPreferences = AppDelegate.INSTANCE.sPreferences): Long =
sPreferences.getLong(this, 0L)
fun String.readBooleanFromPref(sPreferences: SharedPreferences = AppDelegate.INSTANCE.sPreferences): Boolean =
sPreferences.getBoolean(this, false)
fun String.readFloatFromPref(sPreferences: SharedPreferences = AppDelegate.INSTANCE.sPreferences): Float =
sPreferences.getFloat(this, 0f)
fun String.removeFromPref(prefEditor: SharedPreferences.Editor = AppDelegate.INSTANCE.sPreferencesEditor) {
prefEditor.remove(this)
prefEditor.commit()
}
fun Any.removeTheseFromPref(
vararg keys: String,
prefEditor: SharedPreferences.Editor = AppDelegate.INSTANCE.sPreferencesEditor) {
keys.forEach {
prefEditor.remove(it)
}
prefEditor.commit()
}
fun SharedPreferences.Editor.clearPref() {
this.clear()
this.commit()
}
fun Any.showVLog(log: String) = Log.v(this::class.java.simpleName, log)
@Module
class SharedPreferenceModule {
@Provides
@Singleton
fun giveAppPreference(): SharedPreferences =
AppDelegate.INSTANCE.getSharedPreferences(ConstantsUtil.APP_SHARED_PREF_NAME, Context.MODE_PRIVATE)
@SuppressLint("CommitPrefEdits")
@Provides
@Singleton
fun giveAppPreferenceEditor(pref: SharedPreferences): SharedPreferences.Editor =
pref.edit()
}
class DemoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
"jack and jill went up the hill".writeToPref("poem")
1.writeToPref("one")
3f.writeToPref("threeF")
7L.writeToPref("sevenLong")
true.writeToPref("booo")
showVLog("poem".readStringFromPref())
showVLog("one".readIntFromPref().toString())
showVLog("threeF".readFloatFromPref().toString())
showVLog("sevenLong".readLongFromPref().toString())
showVLog("booo".readBooleanFromPref().toString())
// O/P
// V/---DemoActivity: jack and jill went up the hill
// V/---DemoActivity: 1
// V/---DemoActivity: 3.0
// V/---DemoActivity: 7
// V/---DemoActivity: true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment