Kotlin + RxJava2 + Dagger2 + Orma
+ Retrofit2 で作るAndroidアプリ at DroidKaigi 2017

Kotlin + RxJava2 + Dagger2 + Orma
+ Retrofit2 で作るAndroidアプリ at DroidKaigi 2017

TwitterID変えました。@lvla0805 -> @MoyuruAizawa

このスライド内ではHTTP通信にObservableを利用していますが、HTTP通信は成功か失敗しかないので本来であればSingleを利用するほうが妥当です。
このスライドでは敷居をさげるためにStreamの説明をObservableだけにとどめました。

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

March 10, 2017
Tweet

Transcript

  1. Kotlin + RxJava2 + Dagger2 + Orma
 + Retrofit2 Ͱ࡞ΔAndroidΞϓϦ

    @lvla0805
  2. MWMB MWMB Ѫᖒ๖ (Moyuru Aizawa) - Lead Kotlin engineer of

    Pairs Div. Eureka, Inc.
  3. - Pairs - Couples - a member of IAC/Match Group

  4. Agenda ‣ Sample Application ‣ ٕज़֓ཁ ‣ Kotlin ‣ RxJava

    ‣ Retrofit ‣ Orma ‣ Sample ‣ Client ‣ Database ‣ Repository ‣ Activity ‣ Dagger2
  5. Sample Application

  6. ‣ %SPJE,BJHJΞϓϦͷίϯ τϦϏϡʔλʔͷҰཡΛදࣔ͢ Δ͚ͩͷ؆୯ͳΞϓϦ Sample Application https://github.com/lvla/DroidKaigi2017Contributors

  7. ‣ (JU)VC͔Βz%SPJE,BJHJDPOGFSFODFBQQzͷίϯ τϦϏϡʔλʔΛऔಘ͢Δ ‣ ΦϑϥΠϯͰ΋ίϯτϦϏϡʔλʔΛදࣔͰ͖ΔΑ͏ʹσʔ λϕʔεʹอଘ ‣ ʜ ඞཁͳػೳ

  8. ٕज़֓ཁ

  9. ٕज़֓ཁ Languages Kotlin 1.1 Network Retrofit 2.1.0 OkHttp 3.6.0 O/R

    Mapper Orma 4.2.0 Reactive Programming RxJava 2.0.2 Dependency Injection Dagger 2.9
  10. build.gradle(Ұ෦) apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' android { dataBinding

    { enabled = true } } dependencies { // Databinding kapt 'com.android.databinding:compiler:2.3.0' // Orma compile "com.github.gfx.android.orma:orma:$ormaVersion" kapt "com.github.gfx.android.orma:orma-processor:$ormaVersion" // ReactiveX compile 'io.reactivex.rxjava2:rxjava:2.0.2' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' // Retrofit compile "com.squareup.retrofit2:retrofit:$retrofitVersion" compile "com.squareup.retrofit2:converter-gson:$retrofitVersion" compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion" // Dagger2 compile "com.google.dagger:dagger:$daggerVersion" kapt "com.google.dagger:dagger-compiler:$daggerVersion" }
  11. Kotlin

  12. ‣ JetBrains։ൃͷJVM, Android޲͚੩తܕ෇͚ݴޠ ‣ Javaͱͷ100%૬ޓӡ༻ੑ ‣ ܕਪ࿦ ‣ Null҆શ ‣

    ؔ਺Ϧςϥϧ Kotlin
  13. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  14. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  15. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  16. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  17. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  18. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    … val message = "Clicked" button.setOnClickListener { textView.text = message } } } Kotlin
  19. RxJava

  20. ‣ ϦΞΫςΟϒϓϩάϥϛϯάϑϨʔϜϫʔΫ ‣ ؂ࢹՄೳͳStreamΛ࢖ͬͨඇಉظॲཧ RxJava

  21. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  22. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  23. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  24. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  25. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  26. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  27. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  28. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  29. RxJava val observable = Observable.create<Int> { subscriber -> intArray.forEach {

    subscriber.onNext(it) } subscriber.onComplete() } observable.subscribe( { num -> println(num) }, { throwable -> throwable.printStackTrace() }, { println("onCompleted") })
  30. RxJava masterClient().fetch() .flatMap { loginClient.login() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe( {

    ... }, { ... }, { moveToMain() } )
  31. RxJava masterClient().fetch() .flatMap { loginClient.login() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe( {

    ... }, { ... }, { moveToMain() } )
  32. RxJava masterClient().fetch() .flatMap { loginClient.login() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe( {

    ... }, { ... }, { moveToMain() } )
  33. RxJava masterClient().fetch() .flatMap { loginClient.login() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe( {

    ... }, { ... }, { moveToMain() } )
  34. RxJava masterClient().fetch() .flatMap { loginClient.login() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe( {

    ... }, { ... }, { moveToMain() } )
  35. Retrofit

  36. ‣ Square։ൃͷType Safe HTTP Client ‣ OkHttpͰ௨৴Λߦ͍ɺJsonΛ೚ҙͷܕͰฦ٫ͯ͘͠Ε Δɻ ‣ RxJavaରԠ

    Retrofit
  37. Retrofitͷੜ੒

  38. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

  39. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

  40. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

  41. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

  42. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

  43. Retrofit val retrofit = Retrofit.Builder() .client(OkHttpClient()) .baseUrl("https://api.github.com") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build()

    Gson, Jackson, Moshi...
  44. Interface

  45. Retrofit interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") fun fetchContributors( @Path(“owner") owner: String,

    @Path(“repo") repository: String): Observable<List<Contributor>> }
  46. Retrofit interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") fun fetchContributors( @Path(“owner") owner: String,

    @Path(“repo") repository: String): Observable<List<Contributor>> }
  47. Retrofit interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") fun fetchContributors( @Path(“owner") owner: String,

    @Path(“repo") repository: String): Observable<List<Contributor>> }
  48. Retrofit val gitHubService = retrofit.create(GitHubService::class.java) gitHubService.fetchContributors("Owner", "Repo") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(

    { contributors -> show(contributors) }, { throwable -> throwable.printStackTrace() })
  49. Retrofit val gitHubService = retrofit.create(GitHubService::class.java) gitHubService.fetchContributors("Owner", "Repo") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(

    { contributors -> show(contributors) }, { throwable -> throwable.printStackTrace() })
  50. Retrofit val gitHubService = retrofit.create(GitHubService::class.java) gitHubService.fetchContributors("Owner", "Repo") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(

    { contributors -> show(contributors) }, { throwable -> throwable.printStackTrace() })
  51. Retrofit val gitHubService = retrofit.create(GitHubService::class.java) gitHubService.fetchContributors("Owner", "Repo") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(

    { contributors -> show(contributors) }, { throwable -> throwable.printStackTrace() })
  52. ‣ HTTP௨৴ͳͲɺΠϕϯτ͕Ұճ͔͠ى͜Βͳ͍ετ ϦʔϜΛѻ͏৔߹͸ObservableΑΓSingleͷ͕ϕλʔ ‣ ࠓճ͸આ໌Λ؆ུԽ͢ΔͨΊʹObservableΛ࢖༻͠· ͨ͠ɻ TIPS

  53. Orma

  54. ‣ "OESPJE42-JUF%BUBCBTFΉ͚ͷ03. ‣ ίϯύΠϧ࣌ʹΞϊςʔγϣϯΛݩʹ$36%ͷϔϧύʔ ΫϥεΛੜ੒ͯ͘͠ΕΔɻ Orma

  55. Tableఆٛ

  56. Orma @Table data class Contributor( @PrimaryKey(auto = false) @Setter("name") val

    name: String, @Column @Setter("avatarUrl") val avatarUrl: String?, @Column @Setter("htmlUrl") val htmlUrl: String?, @Column @Setter("contributions") val contributions: Int )
  57. Orma @Table data class Contributor( @PrimaryKey(auto = false) @Setter("name") val

    name: String, @Column @Setter("avatarUrl") val avatarUrl: String?, @Column @Setter("htmlUrl") val htmlUrl: String?, @Column @Setter("contributions") val contributions: Int )
  58. Orma @Table data class Contributor( @PrimaryKey(auto = false) @Setter("name") val

    name: String, @Column @Setter("avatarUrl") val avatarUrl: String?, @Column @Setter("htmlUrl") val htmlUrl: String?, @Column @Setter("contributions") val contributions: Int )
  59. Orma @Table data class Contributor( @PrimaryKey(auto = false) @Setter("name") val

    name: String, @Column @Setter("avatarUrl") val avatarUrl: String?, @Column @Setter("htmlUrl") val htmlUrl: String?, @Column @Setter("contributions") val contributions: Int )
  60. OrmaDatabaseੜ੒

  61. Orma val orma = OrmaDatabase.builder(context) .writeOnMainThread(AccessThreadConstraint.FATAL) .readOnMainThread(AccessThreadConstraint.FATAL) .build()

  62. Orma val orma = OrmaDatabase.builder(context) .writeOnMainThread(AccessThreadConstraint.FATAL) .readOnMainThread(AccessThreadConstraint.FATAL) .build()

  63. Orma val orma = OrmaDatabase.builder(context) .writeOnMainThread(AccessThreadConstraint.FATAL) .readOnMainThread(AccessThreadConstraint.FATAL) .build()

  64. CRUD

  65. Insert orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors)

  66. Insert orma.transactionSync { orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors) }

  67. Insert orma.transactionAsCompletable { orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors) }

  68. Select orma.selectFromContributor() .executeAsObservable() .toList()

  69. Select orma.selectFromContributor() .nameEq("lvla") .executeAsObservable() .toList()

  70. Sample

  71. Client

  72. Client class GitHubClient(private val service: GitHubService) { fun fetchContributors(): Observable<List<Contributor>>

    { return service.fetchContributors(“DroidKaigi", "conference-app-2017") }
  73. Client class GitHubClient(private val service: GitHubService) { fun fetchContributors(): Observable<List<Contributor>>

    { return service.fetchContributors(“DroidKaigi", "conference-app-2017") }
  74. Dao

  75. Dao class ContributorDao(orma: OrmaDatabase) { fun insert(contributors: List<Contributor>) { orma.transactionSync

    { orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors) } } fun findAll(): List<Contributor> { return orma.selectFromContributor() .executeAsObservable() .toList() } }
  76. Dao class ContributorDao(orma: OrmaDatabase) { fun insert(contributors: List<Contributor>) { orma.transactionSync

    { orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors) } } fun findAll(): List<Contributor> { return orma.selectFromContributor() .executeAsObservable() .toList() } }
  77. Dao class ContributorDao(orma: OrmaDatabase) { fun insert(contributors: List<Contributor>) { orma.transactionSync

    { orma.prepareInsertIntoContributor(OnConflict.REPLACE) .executeAll(contributors) } } fun findAll(): List<Contributor> { return orma.selectFromContributor() .executeAsObservable() .toList() } }
  78. Repository

  79. Repository class ContributorRepository( private val client: GitHubClient, private val dao:

    ContributorDao) { fun getContributors(): Observable<List<Contributor>> { return client.fetchContributors() .doOnNext { list -> dao.insert(list) } .onErrorReturn { dao.findAll() } } }
  80. Repository class ContributorRepository( private val client: GitHubClient, private val dao:

    ContributorDao) { fun getContributors(): Observable<List<Contributor>> { return client.fetchContributors() .doOnNext { list -> dao.insert(list) } .onErrorReturn { dao.findAll() } } }
  81. Repository class ContributorRepository( private val client: GitHubClient, private val dao:

    ContributorDao) { fun getContributors(): Observable<List<Contributor>> { return client.fetchContributors() .doOnNext { list -> dao.insert(list) } .onErrorReturn { dao.findAll() } } }
  82. Repository class ContributorRepository( private val client: GitHubClient, private val dao:

    ContributorDao) { fun getContributors(): Observable<List<Contributor>> { return client.fetchContributors() .doOnNext { list -> dao.insert(list) } .onErrorReturn { dao.findAll() } } }
  83. Activity

  84. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  85. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  86. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  87. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  88. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  89. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  90. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  91. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  92. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  93. Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  94. ґଘੑͷղܾ͸Dagger2 ʹ೚ͤΑ͏ɻ

  95. Dagger2

  96. ‣ Google։ൃͷJava/Android޲͚DIίϯςφ ‣ ΞϊςʔγϣϯΛར༻ͨ͠ґଘੑͷղܾ ‣ ߴ଎ Dagger2

  97. ґଘੑͷάϥϑ Repository Dao Client Service Orma Context Retrofit OkHttp Activity

  98. ґଘੑͷղܾΛ͢Δͷʹඞཁͳ΋ͷ Injectee Component Module Inject͞ΕΔଆ Inject͢Δଆ ґଘੑͷఏڙऀ
 (Inject͢Δ෺)

  99. Module @Module class ApplicationModule(private val applicationContext: App) { @Provides fun

    provideContext(): Context {…} @Provides fun provideOkHttp(): OkHttpClient {…} @Provides fun provideRetrofit(o: OkHttpClient): Retrofit {…} @Provides fun provideOrma(c: Context): OrmaHolder {…} @Provides fun provide…Dao(oh: OrmaHolder): ContributorDao {…} @Provides fun provideGitHubService(r: Retrofit): GitHubService {…} @Provides fun provideGitHubClient(s: GitHubService): GitHubClient {…} }
  100. Module @Module class ApplicationModule(private val applicationContext: App) { @Provides fun

    provideContext(): Context {…} @Provides fun provideOkHttp(): OkHttpClient {…} @Provides fun provideRetrofit(o: OkHttpClient): Retrofit {…} @Provides fun provideOrma(c: Context): OrmaHolder {…} @Provides fun provide…Dao(oh: OrmaHolder): ContributorDao {…} @Provides fun provideGitHubService(r: Retrofit): GitHubService {…} @Provides fun provideGitHubClient(s: GitHubService): GitHubClient {…} }
  101. Module @Module class ApplicationModule(private val applicationContext: App) { @Provides fun

    provideContext(): Context {…} @Provides fun provideOkHttp(): OkHttpClient {…} @Provides fun provideRetrofit(o: OkHttpClient): Retrofit {…} @Provides fun provideOrma(c: Context): OrmaHolder {…} @Provides fun provide…Dao(oh: OrmaHolder): ContributorDao {…} @Provides fun provideGitHubService(r: Retrofit): GitHubService {…} @Provides fun provideGitHubClient(s: GitHubService): GitHubClient {…} }
  102. Module @Module class ApplicationModule(private val applicationContext: App) { @Provides fun

    provideContext(): Context {…} @Provides fun provideOkHttp(): OkHttpClient {…} @Provides fun provideRetrofit(o: OkHttpClient): Retrofit {…} @Provides fun provideOrma(c: Context): OrmaHolder {…} @Provides fun provide…Dao(oh: OrmaHolder): ContributorDao {…} @Provides fun provideGitHubService(r: Retrofit): GitHubService {…} @Provides fun provideGitHubClient(s: GitHubService): GitHubClient {…} }
  103. TIPS @Module class ApplicationModule(private val applicationContext: App) { @Provides fun

    provideContext(): Context {…} @Provides fun provideOkHttp(): OkHttpClient {…} @Provides fun provideRetrofit(o: OkHttpClient): Retrofit {…} @Provides fun provideOrma(c: Context): OrmaHolder {…} @Provides fun provide…Dao(oh: OrmaHolder): ContributorDao {…} @Provides fun provideGitHubService(r: Retrofit): GitHubService {…} @Provides fun provideGitHubClient(s: GitHubService): GitHubClient {…} }
  104. ‣ Constructor Injection ‣ SubComponent ‣ Include Module ‣ ….

    TIPS
  105. Component @Singleton @Component(modules = arrayOf(ApplicationModule::class)) interface ApplicationComponent { fun inject(a:

    MainActivity) }
  106. Component @Singleton @Component(modules = arrayOf(ApplicationModule::class)) interface ApplicationComponent { fun inject(a:

    MainActivity) }
  107. Component @Singleton @Component(modules = arrayOf(ApplicationModule::class)) interface ApplicationComponent { fun inject(a:

    MainActivity) }
  108. Component @Singleton @Component(modules = arrayOf(ApplicationModule::class)) interface ApplicationComponent { fun inject(a:

    MainActivity) }
  109. Field Injection class MainActivity : AppCompatActivity() { @Inject lateinit var

    repository: ContributorRepository }
  110. AnnotationProcessing Injectee Component Module Dagger2 ԿॲʹԿΛInject͢΂͖͔͕Θ͔Δ

  111. AnnotationProcessing Injectee Component Module Dagger2 ͜ͷComponent͕
 ԿॲʹԿΛInjectͰ͖Δͷ͔͕Θ͔Δ

  112. AnnotationProcessing Injectee Component Module Dagger2 DaggerComponent Implement Dagger2͕Componentͷ࣮૷ΫϥεΛੜ੒

  113. ApplicationModule Repository Dao Client Service Orma Context Retrofit OkHttp Module

  114. ApplicationComponent Repository Dao Client Service Orma Context Retrofit OkHttp Module

    Component
  115. ApplicationComponent Repository Dao Client Service Orma Context Retrofit OkHttp Activity

    Inject Module Component
  116. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  117. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  118. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  119. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  120. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  121. Application class class App : Application() { lateinit var component:

    ApplicationComponent override fun onCreate() { component = DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  122. Application class class App : Application() { val component: ApplicationComponent

    by lazy { DaggerApplicationComponent.builder() .applicationModule(ApplicationModule(this)) .build() } }
  123. MainActivity class MainActivity : AppCompatActivity() { @Inject lateinit var repository:

    ContributorRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) (application as App).component.inject(this) repository.getContributors().... } }
  124. MainActivity class MainActivity : AppCompatActivity() { @Inject lateinit var repository:

    ContributorRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) (application as App).component.inject(this) repository.getContributors().... } }
  125. MainActivity class MainActivity : AppCompatActivity() { @Inject lateinit var repository:

    ContributorRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) (application as App).component.inject(this) repository.getContributors().... } }
  126. Before class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val retrofit = Retrofit.Builder().......build() val orma = OrmaDatabase.builder(context).......build() val service = retrofit.create(GitHubService::class.java) val client = ContributorClient(service) val dao = ContributorDao(orma) val repository = ContributorRepository(client, dao) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  127. After class MainActivity : AppCompatActivity() { @Inject lateinit var repository:

    ContributorRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) (application as App).component.inject(this) repository.getContributors() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { contributors -> showContributors(contributors) }, { Timber.e(it, "getContributors") } ) } }
  128. Conclusion

  129. ‣ KotlinͰ৑௕ͳهड़ΛݮΒ͢ࣄ͕Ͱ͖Δ ‣ RxJavaͰॊೈͳΠϕϯτॲཧ/ඇಉظॲཧΛߦ͏͜ͱ͕ Ͱ͖Δ ‣ RetrofitͰHTTP௨৴ΛRxJavaͰѻ͑Δ ‣ OrmaʹSQLiteͷॲཧΛ೚ͤΒΕΔ ‣

    Dagger2ʹґଘੑͷղܾΛҕৡͰ͖Δ Conclusion
  130. Thank you