Skip to content

Commit

Permalink
Merge pull request #67 from jama5262/issue-56
Browse files Browse the repository at this point in the history
Add Shared preferences in the core module (Issue 56)
  • Loading branch information
michaelbukachi authored Mar 23, 2020
2 parents b41139c + fffce74 commit 26b74b0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ android {

dependencies {
implementation (project(BuildModules.Libraries.Data))
implementation (project(BuildModules.Libraries.Core))
implementation (project(BuildModules.Libraries.Network))
implementation (project(BuildModules.Libraries.Repository))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.android254.droidconKE2020.core

import android.content.SharedPreferences

interface Preferences {
val sharedPref: SharedPreferences

// fun getUserName(defaultValue: String): String?
// fun setUserName(username: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.android254.droidconKE2020.core

import android.content.Context
import android.content.SharedPreferences
import com.android254.droidconKE2020.core.di.Constants.SHARED_PREF_FILE_NAME

class PreferencesImpl(context: Context): Preferences {

override val sharedPref: SharedPreferences = context
.getSharedPreferences(SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE)

private fun editSharedPref(action: (s: SharedPreferences.Editor) -> Unit) {
with (sharedPref.edit()) {
action(this)
apply()
}
}

// override fun getUserName(defaultValue: String): String? {
// return sharedPref.getString("userName", defaultValue)
// }
//
// override fun setUserName(username: String) {
// editSharedPref {
// it.putString("userName", username)
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.android254.droidconKE2020.core.di

object Constants {
const val SHARED_PREF_FILE_NAME = "com.android254.droidconKE2020"
}

0 comments on commit 26b74b0

Please sign in to comment.