Created
August 22, 2018 22:50
-
-
Save ZakTaccardi/6b20d476542071dd58fac5ffd12a578a to your computer and use it in GitHub Desktop.
Companion code to https://medium.com/p/91c736276a52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.app.Application | |
import android.content.Context | |
import android.os.Bundle | |
// A Graph is just an interface that exposes your dependencies (like a Dagger 2 @Component). | |
interface Graph { | |
val context: Context | |
val analytics: Analytics | |
val favoriteNumberRepo: FavoriteNumberRepo | |
} | |
// a custom application where you can store your graph | |
// | |
// the graph needs to be accessible from any | |
// activity/fragment/service/receiver, | |
// so store it in a custom Application | |
class CustomApplication : Application() { | |
lateinit var graph: Graph | |
override fun onCreate() { | |
super.onCreate() | |
// build the graph before onCreate | |
// completes to guarantee availability | |
graph = GraphBuilder() | |
.context(this) | |
.build() | |
} | |
} | |
// base activity for all activities to extend | |
abstract class BaseActivity : Activity() { | |
protected lateinit var graph: Graph | |
private set | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// store a graph for easy access later | |
graph = (application as CustomApplication).graph | |
} | |
} | |
class FavoriteNumberActivity : BaseActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// access dependencies on the graph | |
graph.favoriteNumberRepo | |
.loadFavoriteNumber("some user id") | |
} | |
} | |
// a Repository class where you could load a user's favorite number | |
class FavoriteNumberRepo( | |
private val analytics: Analytics | |
) { | |
fun loadFavoriteNumber(userId: String): Int { | |
analytics.track("favorite number loading") | |
return 42 | |
} | |
} | |
class Analytics { | |
fun track(eventName: String) { | |
// .. | |
} | |
} | |
class GraphBuilder { | |
private lateinit var _context: Context | |
private var analytics: Analytics? = null | |
fun context(appContext: Context) = apply { | |
_context = appContext | |
} | |
fun build(): Graph { | |
require(::_context.isInitialized) { | |
"context == null" | |
} | |
return GraphImpl(_context, analytics) | |
} | |
} | |
// implementation of the Graph | |
class GraphImpl( | |
override val context: Context, | |
private val _analytics: Analytics? = null | |
) : Graph { | |
override val analytics: Analytics | |
by lazy { | |
_analytics ?: Analytics() | |
} | |
override val favoriteNumberRepo: FavoriteNumberRepo | |
by lazy { | |
FavoriteNumberRepo(analytics) | |
} | |
} | |
// base activity for all activities to extend | |
abstract class BaseActivity : Activity() { | |
protected lateinit var graph: Graph | |
private set | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// store a graph for easy access later | |
graph = (application as CustomApplication).graph | |
} | |
} | |
class FavoriteNumberActivity : BaseActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// access dependencies on the graph | |
graph.favoriteNumberRepo.loadFavoriteNumber("some user id") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment