From 33f009ad2993d76cf7a5a3a9c832b2b55134d312 Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Mon, 23 Mar 2020 17:31:11 +0300 Subject: [PATCH 1/2] Created PreferencesImpl class and Preferences interface for shared pref functionality --- .../java/com/android254/droidconKE2020/core/Preferences.kt | 4 ++++ .../com/android254/droidconKE2020/core/PreferencesImpl.kt | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt create mode 100644 core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt diff --git a/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt b/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt new file mode 100644 index 0000000..a356f00 --- /dev/null +++ b/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt @@ -0,0 +1,4 @@ +package com.android254.droidconKE2020.core + +interface Preferences { +} \ No newline at end of file diff --git a/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt b/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt new file mode 100644 index 0000000..9e6aafb --- /dev/null +++ b/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt @@ -0,0 +1,4 @@ +package com.android254.droidconKE2020.core + +class PreferencesImpl { +} \ No newline at end of file From fffce74a2054166520f3f2a94894e7dcdfdd385a Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Mon, 23 Mar 2020 19:44:36 +0300 Subject: [PATCH 2/2] Implemented sharedPref functionality --- app/build.gradle.kts | 1 + .../droidconKE2020/core/Preferences.kt | 6 +++++ .../droidconKE2020/core/PreferencesImpl.kt | 26 ++++++++++++++++++- .../droidconKE2020/core/di/Constants.kt | 5 ++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/com/android254/droidconKE2020/core/di/Constants.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ef4d517..e66ca23 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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)) diff --git a/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt b/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt index a356f00..275fa52 100644 --- a/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt +++ b/core/src/main/java/com/android254/droidconKE2020/core/Preferences.kt @@ -1,4 +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) } \ No newline at end of file diff --git a/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt b/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt index 9e6aafb..c067849 100644 --- a/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt +++ b/core/src/main/java/com/android254/droidconKE2020/core/PreferencesImpl.kt @@ -1,4 +1,28 @@ package com.android254.droidconKE2020.core -class PreferencesImpl { +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) +// } +// } } \ No newline at end of file diff --git a/core/src/main/java/com/android254/droidconKE2020/core/di/Constants.kt b/core/src/main/java/com/android254/droidconKE2020/core/di/Constants.kt new file mode 100644 index 0000000..9774bdc --- /dev/null +++ b/core/src/main/java/com/android254/droidconKE2020/core/di/Constants.kt @@ -0,0 +1,5 @@ +package com.android254.droidconKE2020.core.di + +object Constants { + const val SHARED_PREF_FILE_NAME = "com.android254.droidconKE2020" +} \ No newline at end of file