Skip to content

Instantly share code, notes, and snippets.

@shikajiro
Last active September 27, 2017 12:05
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 shikajiro/f3348df6476f5633fea30b86ba3ffff6 to your computer and use it in GitHub Desktop.
Save shikajiro/f3348df6476f5633fea30b86ba3ffff6 to your computer and use it in GitHub Desktop.
Toothpickを使ったActivity, Fragment, ViewModelでの基本的な流れ
kapt {
generateStubs = true
arguments {
arg('toothpick_registry_package_name', "${android.defaultConfig.applicationId}.toothpick")
}
}
dependencies {
compile "com.github.stephanenicolas.toothpick:toothpick-runtime:$toothpick_ver"
kapt "com.github.stephanenicolas.toothpick:toothpick-compiler:$toothpick_ver"
}
class HomeFragment : Fragment() {
private lateinit var scope: Scope
private lateinit var vm: HomeViewModel
@Inject lateinit var dialogDelegation: DialogDelegation
override fun onCreate(savedInstanceState: Bundle?) {
scope = Toothpick.openScopes(activity.application, activity, this)
scope.installModules(FragmentModule(this))
super.onCreate(savedInstanceState)
Toothpick.inject(this, scope)
vm = ViewModelProviders.of(this).get(HomeViewModel::class.java)
}
}
class HomeViewModel(application: Application) : AndroidViewModel(application) {
@Inject lateinit var repository: ModelRepository
private val scope = Toothpick.openScopes(application, this)
init {
scope.installModules(ViewModelModule(this))
Toothpick.inject(this, scope)
}
override fun onCleared() {
super.onCleared()
Toothpick.closeScope(scope)
}
}
import your.application.package.toothpick.FactoryRegistry
import your.application.package.toothpick.MemberInjectorRegistry
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
Toothpick.setConfiguration(Configuration.forProduction().disableReflection())
//この2つの引数は、自動生成されたRegistryを渡す。toothPickにあるRegistryを渡しても駄目
MemberInjectorRegistryLocator.setRootRegistry(MemberInjectorRegistry())
FactoryRegistryLocator.setRootRegistry(FactoryRegistry())
Toothpick.openScope(this).installModules(ApplicationModule(this))
}
override fun onTerminate() {
super.onTerminate()
Toothpick.closeScope(this)
}
}
class ApplicationModule(val application: Application) : Module() {
val local = LocalCache(application)
init {
bindModelRepository()
}
private fun bindModelRepository() {
val webApi = WebApi(BuildConfig.API_BASE_URL, local)
val repository = ModelRepositoryImp(webApi, local)
bind(ModelRepository::class.java).toInstance(repository)
}
}
class ActivityModule(val activity: Activity) : Module(){
init {
bindNavigator()
bindDialog()
}
private fun bindNavigator(){
bind(Navigator::class.java).toInstance(Navigator(activity))
}
private fun bindDialog(){
bind(DialogDelegation::class.java).toInstance(DialogDelegationImpl(activity))
}
}
class FragmentModule(val fragment: Fragment) : Module(){
}
class ViewModelModule(val viewModel: ViewModel) : Module(){
}
class SplashActivity : AppCompatActivity() {
private lateinit var scope: Scope
@Inject lateinit var navigator: Navigator
override fun onCreate(savedInstanceState: Bundle?) {
scope = Toothpick.openScopes(application, this)
scope.installModules(ActivityModule(this))
super.onCreate(savedInstanceState)
Toothpick.inject(this, scope)
setContentView(R.layout.activity_splash)
Observable.create<Void> { source ->
source.onComplete()
}.delay(3, TimeUnit.SECONDS).subscribe({}, {}, {
navigator.goToMain()
finish()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment