Skip to content
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

Closed
michaelbukachi opened this issue Mar 22, 2020 · 4 comments
Closed

Shared preferences in core module #56

michaelbukachi opened this issue Mar 22, 2020 · 4 comments

Comments

@michaelbukachi
Copy link
Contributor

We need to setup a PreferencesImpl class in the core module. This class will be responsible for handling all the logic for settings/getting data from SharedPreferences. The class should implement a Preferences interface. Alls methods should be declared in the interface since PreferencesImpl 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 called setUsername and getUsername respectively.

@jama5262
Copy link
Contributor

@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 { ... }
}

@michaelbukachi
Copy link
Contributor Author

@jama5262 We are using DI, so any dependencies should be passed in the constructor. This means we cannot use object for any implementation that needs to be injected. We will let the DI library handle the Singleton logic. So the code above would be something like this:

class PreferencesImpl(application: Application) : Preferences {
....
} 

@jama5262
Copy link
Contributor

jama5262 commented Mar 23, 2020

@michaelbukachi On the topic of methods as you suggested like for username that is, getUsername and setUsername, what if we could just have get and set methods.

Below is an example of the get method that returns state data based on the default value passed

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 get and set methods for each and every state we would like to get and set.

What do you think? 🤔

@michaelbukachi
Copy link
Contributor Author

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 getUsername should return a username, isLoggedIn should return a boolean value. Sometimes, less code is not always better 😅. Also, consider the fact that multiple people are working on this codebase. If anyone needs to manage state using SharedPreferences, they should add their own functions and test them accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants