Last active
September 27, 2017 12:05
-
-
Save shikajiro/f3348df6476f5633fea30b86ba3ffff6 to your computer and use it in GitHub Desktop.
Toothpickを使ったActivity, Fragment, ViewModelでの基本的な流れ
This file contains hidden or 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
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" | |
} |
This file contains hidden or 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
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) | |
} | |
} |
This file contains hidden or 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
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) | |
} | |
} |
This file contains hidden or 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 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) | |
} | |
} |
This file contains hidden or 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
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(){ | |
} |
This file contains hidden or 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
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