-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shared preferences in core module #56
Comments
@michaelbukachi I was thinking of a singleton, an example of some like this object PreferencesImpl : Preferences {
lateinit var sharedPref : SharedPreferences
override fun setUp(application: Application) {
sharedPref = application.getSharedPreferences(SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE)
}
override fun getUsername(): String { ... }
override fun setUsername(): String { ... }
} |
@jama5262 We are using DI, so any dependencies should be passed in the constructor. This means we cannot use class PreferencesImpl(application: Application) : Preferences {
....
} |
@michaelbukachi On the topic of methods as you suggested like for username that is, Below is an example of the class PreferencesImpl(application: Application) : Preferences {
override fun get(key: String, defaultVal: Any): Any {
return when (defaultVal) {
is String -> sharedPref.getString(key, defaultVal) as Any
is Int -> sharedPref.getInt(key, defaultVal)
is Boolean -> sharedPref.getBoolean(key, defaultVal)
is Float -> sharedPref.getFloat(key, defaultVal)
is Long -> sharedPref.getLong(key, defaultVal)
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
} This could get rid of the boilerplate of having What do you think? 🤔 |
Hey, @jama5262, This method is nice and short, however, it's not as readable. Also, it violates the "Single Responsibility Rule". A function should do one thing and one thing only. Basically, a |
We need to setup a
PreferencesImpl
class in thecore
module. This class will be responsible for handling all the logic for settings/getting data from SharedPreferences. The class should implement aPreferences
interface. Alls methods should be declared in the interface sincePreferencesImpl
is just an implementation detail. Methods should be as description as possible. For instance, if we need method for storing and retrieving a username, the methods should be calledsetUsername
andgetUsername
respectively.The text was updated successfully, but these errors were encountered: