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

Add application context to builders automatically #29

Closed
thoutbeckers opened this issue Oct 15, 2019 · 2 comments
Closed

Add application context to builders automatically #29

thoutbeckers opened this issue Oct 15, 2019 · 2 comments
Labels
🤖android ✏️architecture Structure of the libraries and design patterns used

Comments

@thoutbeckers
Copy link
Collaborator

thoutbeckers commented Oct 15, 2019

We can set application context once using a helper class (likely called after onCreate in the app), and then automatically add it to builders that need it. This way we don't have to call back into native code from common code to set it.

@thoutbeckers
Copy link
Collaborator Author

Might not be needed anymore with builder

@thoutbeckers
Copy link
Collaborator Author

thoutbeckers commented Dec 4, 2019

For some other projects we have a class for holding the ApplicationContext (initialized from Application.onCreate) since Application is under almost all circumstances essentially a singleton.

My draft for a class to hold this:

companion object ApplicationHolder {
        var application: Application? = null
            set(application) {
                checkNotNull(field) { "Application object can only be set once." }
                field = application
            }
        val applicationContext:Context
        get() {
            val application = this.application                
            checkNotNull(application) {
                "You've used ApplicationHolder.applicationContext without setting the Application on this holder (you should do this from Application.onCreate() or in your test)"
            }
            
            val context = application.applicationContext
            checkNotNull(context) {
                "ApplicationContext is null, this should not happen during runtime if you set a real Application instance on this holder. For testing make sure you set an Application with a real or mock ApplicationContext available."
            }
            
            return context
        }
    }

It can then be used optionally in Builders that require only ApplicationContext.

 actual open class Builder(context: Context = ApplicationHolder.applicationContext)

This could be further abstracted by having a common code wrapper around ApplicationContext (possibly by using an expect typealias) which would also allow iOS to provide a globally useful class or a dummy object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖android ✏️architecture Structure of the libraries and design patterns used
Projects
None yet
Development

No branches or pull requests

1 participant