Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Created August 22, 2018 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakTaccardi/6b20d476542071dd58fac5ffd12a578a to your computer and use it in GitHub Desktop.
Save ZakTaccardi/6b20d476542071dd58fac5ffd12a578a to your computer and use it in GitHub Desktop.
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