Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
How to use Dagger2 and Koin
Search
Hyeonji Jeong
April 05, 2019
Programming
2
1.1k
How to use Dagger2 and Koin
2019_정현지_droidknights
Dagger2 vs Koin 어떻게 쓰는 걸까요?
Hyeonji Jeong
April 05, 2019
Tweet
Share
More Decks by Hyeonji Jeong
See All by Hyeonji Jeong
2018 DevFest - Update to Oreo & Pie
hyunji92
0
110
droidknight_2018_nbt
hyunji92
0
53
devfest-AAC-2017-codelab
hyunji92
0
56
AAC-2017-soma
hyunji92
0
27
Other Decks in Programming
See All in Programming
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
110
The Future of Frontend i18n : Intl.MessageFormat
sajikix
1
2.3k
フロントエンドのテストからアクセシビリティをしれっと広めていく
nano72mkn
3
690
月間4.5億回再生を超える大規模サービス TVer iOSアプリのリアーキテクチャ戦略 - iOSDC2024
techtver
PRO
1
540
BQ+dbt+Lookerを用いた、月間800億件以上の広告リクエストに対するデータモデリング
hanon52_
4
980
めざせ!WKWebViewマスター! / WKWebView Master
marcy731
3
450
Some more adventure of Happy Eyeballs
coe401_
2
140
LangChainでWebサイトの内容取得やGitHubソースコード取得
shukob
0
110
複雑さに立ち向かうための ソフトウェア開発入門
shiz
2
510
楽しく簡単に!QRコードの読み取り機能を実装しよう
penguinsan_pg
1
190
デザインシステムとコンポーネント指向によるフロントエンド開発プロセスの革新 / Innovation in Frontend Development Processes through Design Systems and Component-Oriented Architecture
nrslib
7
5k
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
830
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
230
17k
Art, The Web, and Tiny UX
lynnandtonic
294
20k
Infographics Made Easy
chrislema
239
18k
Web development in the modern age
philhawksworth
204
10k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.8k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.7k
What the flash - Photography Introduction
edds
67
11k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
225
22k
A Philosophy of Restraint
colly
202
16k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
Transcript
%BHHFS WT ,PJO যڌѱॳחѦөਃ 4QFBLFS അ
ٜٜ۠࠙ਵदݶજणפ %BHHFS৬,PJOӝୡੋधҗद ਊߨਸঌҊरযೞחפযٜ࠙ਸਤೠ ߊܳೞ۰Ҋפ
য়טݾର ↟%*ۆޖੌө ↟%BHHFSۆޖੌө ↟,PJOۆޖੌө ↟%BHHFS৬,PJO җױޖੌө
↟ۧѱࢎਊ೧ࠁ ↟%BHHFS,PJOਊ೧ࠁӝ ↟ޖܻ۽ંী؊যܾө ↟%*ܳҕࠗೞҊۧѱਊೞݶܻоחѪޖੌө ↟݃ޖܻ
%*ۆޖੌө %FQFOEFODZ*OKFDUJPO
%*ۆޖੌө %FQFOEFODZ*OKFDUJPO
%*ۆޖੌө %FQFOEFODZ $MBTT$PGGFF GVONBLF-BUUF $MBTT.BUFSJBM GVONJML GVOFTQSFTTP
%*ۆޖੌө %FQFOEFODZ class Coffee { val material = Material() fun
makeLatte() { material.milk() material.espresso() } } class Material { fun milk() {} fun espresso() {} }
%*ۆޖੌө %FQFOEFODZ*OKFDUJPO
%*ۆޖੌө Ӓܿ߸҃ OFX.JML -BUUFё OFX&TQSFTTP Latte ё
উীࢲ ࢜۽ ёܳ ࢤࢿೞ ঋח!
%*ۆޖੌө فѐ࢚ݽٕաېझрѾبܳծ୶ӝਤ೧ ৻ࠗীࢲOFXёࢤࢿਸೞҊੑೞחѪ %* ઓࢿੑۆ
-BUUFё OFX4ZSVQ OFX.JML OFX&TQSFTTP
OFX*DF %*ۆޖੌө
-BUUFё OFX4ZSVQ OFX.JML OFX&TQSFTTP
OFX*DF %*ۆޖੌө "NFSJDBOPё OFX4ZSVQ OFX&TQSFTTP OFX*DF
%*ۆޖੌө OFX.JML -BUUFё OFX &TQSFTTP OFX4ZSVQ OFX*DF
%*ۆޖੌө OFX.JML -BUUFё OFX &TQSFTTP OFX4ZSVQ OFX*DF
"NFSJDBOPё
%*ۆޖੌө *0$ ৻ࠗ'SBNFXPSL OFX.JML OFX &TQSFTTP OFX4ZSVQ
OFX*DF -BUUFё
%BHHFSۆޖੌө
%BHHFSۆޖੌө উ٘۽٘ীࢲ ઓࢿੑਸҳഅೞӝਤ೧ ࢎਊೞחۄ࠳۞ܻ
ਫ਼Ӭ݅ঌҊֈযо %BHHFS %BHHFS
ਫ਼Ӭ݅ঌҊֈযо %BHHFS 4RVBSF %BHHFS (PPHMF
ਫ਼Ӭ݅ঌҊֈযо ਫ਼Ӭ݅ঌҊֈযо %BHHFS 3FGMFDUJPOוܻ ۠ఋ(SBQIҳࢿ
द
%BHHFSۆޖੌө
%BHHFSۆޖੌө ↟%FQFOEFODZJOKFDUJPOUPPM ↟BOOPUBUJPOQSPDFTTPSࢎਊ ↟OPSVOUJNFFSSPS ↟ࣽࣻ+BWB۽ܖয ↟OPSFGMFDUJPONPSFGBTU ↟੍ӝएHFOFSBUFEDPEF ↟QSPHVBSEࢸਃহ
%BHHFSۆޖੌө %BHHFSࣻѐ֛ *OKFDU $PNQPOFOU 4VCDPNQPOFOU .PEVMF 4DPQF 1SPWJEFT
%BHHFSۆޖੌө рױೞѱ !NPEVMF ҕә !$PNQPOFOU োѾ೧חઁҕ !*OKFDU ੑ߉חࣗ࠺
ಕۄڍٜܳ݅যࠅөਃ FTQSFTTP DPGGFF NJML -BUUFё
%FQFOEFODZ(SBQI " $ % #
ಕۄڍٜܳ݅যࠅөਃ FTQSFTTP DPGGFF NJML -BUUFё
%BHHFSۆޖੌө ઁ class Milk class Espresso class Coffee(var espresso: Espresso)
class Latte @Inject constructor( var coffee: Coffee, var milk: Milk) { fun makeLatteDrink() {} }
%BHHFSۆޖੌө ઁ @Module public class DrinkModule { @Provides Milk ProvideMilk(){
return Milk () } @Provides Espresso ProvideEspresso(){ return Espresso () } @Provides Coffee ProvideCoffee( var espresso: Espresso){ return Coffee (espresso) } }
%BHHFSۆޖੌө ઁ @Component(modules = {DrinkModule::class}) public interface DrinkComponent { fun
drink(): Latte fun inject(Activity: MainActivity) }
%BHHFSۆޖੌө ઁ class MainActivity : AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val latteComponent = DaggerDrinkComponent.builder() .drinkModule(DrinkModule()) .build() latteComponent.inject(this) latteComponent.drink() }
ಕۄڍٜܳ݅যࠅөਃ FTQSFTTP DPGGFF NJML -BUUFё
None
,PJOۆޖੌө ,PUMJOਸࢎਊೞחѐߊٜਸਤೠ पਊੋ"1*ઁҕਸೞח ҃ചػઓࢿੑۨਕ
,PJOۆޖੌө ↟4FSWJDF-PDBUPS1BUUFSOҳഅ ↟LPUMJO%4- بݫੋౠযࢎਊ ↟OPSFGMFDUJPO ↟QVSFLPUMJOਵ۽ܖযઉ
ਫ਼Ӭ 4FSWJDF-PDBUPS1BUUFSOਃj
,PJOۆޖੌө FTQSFTTP NJML NBUFSJBMT -PDBUPS 4FSWJDF-PDBUPS1BUUFSO ,PJOۆޖੌө
,PJOۆޖੌө ਫ਼Ӭ%4-ਃj
ಕۄڍٜܳ݅যࠅөਃ ,PJOۆޖੌө FTQSFTTP NJML NBUFSJBMT -PDBUPS
NPEVMF GBDUPSZ TJOHMF BMTPBMJBTFEBTCFBO CJOE
HFU TDPQF ,PJOࣻѐ֛ ,PJOۆޖੌө
,PJOۆޖੌө ઁ class Milk class Espresso class Coffee(var espresso: Espresso)
class Latte(var coffee: Coffee, var milk: Milk) { fun makeLatteDrink() {} }
,PJOۆޖੌө ઁ class MainApplication: Application() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) //start koin startKoin(this, listOf(myModules)) } } val myModules = module { single { Milk() } single { Espresso() } single { Coffee(get()) } single { Latte(get(), get())} }
,PJOۆޖੌө ઁ inline fun <reified T> get( qualifier: Qualifier? =
null, scope: Scope = this, noinline parameters: ParametersDefinition? = null ): T { return getKoin().get(T::class, qualifier, scope, parameters) ?: error("$this is not registered - Koin is null") }
,PJOۆޖੌө ઁ <reified T> SFJGJFEGVODUJPO
,PJOۆޖੌө ઁ inline fun <reified T> get( qualifier: Qualifier? =
null, scope: Scope = this, noinline parameters: ParametersDefinition? = null ): T { return getKoin().get(T::class, qualifier, scope, parameters) ?: error("$this is not registered - Koin is null") }
,PJOۆޖੌө ઁ class MainActivity : AppCompatActivity(){ val latte: Latte by
inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val latte: Latte = get() }
%BHHFS৬,PJO җױޖੌө
↟SFGMFDUJPOਸࢎਊೞঋ ↟য֢ప࣌ࢎਊਵ۽рױೠ٘ࢶ ↟ҕਬغחੋझఢझӔਸױࣽച ↟ࠂೠઓࢿएജ҃ࢸ ↟एਬాపझ %BHHFS
↟۞ழ࠳о֫ ↟৮߷ೠಌಖઁоহ ↟ઓҙ҅ীೠ٘ۨఊয۵ ↟য֢ప࣌ӝ߈ۄஹੌदрטযդ ஹੌয়ߡ٘ ↟݆ஹನք৬݆ੌਃ %BHHFSױ
↟য֢ప࣌җহযஹੌࡅܴ ↟णҗࢸऔ ↟,PUMJO%4-ࢎਊ ,PJO
↟۠ఋী۞оߊࢤ ↟࢜۽যܳߓਕঠೣ ↟ઁܳӝ൨ٞ ,PJOױ
Ӓېࢲ۽যڌѱࢎਊೞחѤؘਃ
ۧѱࢎਊ೧ࠁ ઁ(JUIVCਬѨ࢝ (JU)VC6TFS4FBSDI"1*ࢎਊ IUUQTEFWFMPQFSHJUIVCDPNWTFBSDI
ۧѱࢎਊ೧ࠁ%BHHFS @Singleton class GithubUserApiClient constructor( provideOkHttpClient: OkHttpClient) { var baseUrl
= "https://api.github.com" val userDataService: GithubUserApiService init { val retrofit = Retrofit.Builder() .baseUrl(baseUrl) .client(provideOkHttpClient) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() userDataService = retrofit.create(GithubUserApiService::class.java) } }
ۧѱࢎਊ೧ࠁ%BHHFS @Module class ApiClientModule { @Provides fun provideOkHttpClient(): OkHttpClient {
val logging = HttpLoggingInterceptor() logging.level = HttpLoggingInterceptor.Level.BASIC return OkHttpClient.Builder() .addInterceptor(logging) .build() } @Provides fun provideGithubUserApiClient(): GithubUserApiClient { return GithubUserApiClient(provideOkHttpClient()) } }
ۧѱࢎਊ೧ࠁ%BHHFS @Module(includes = [ApiClientModule::class]) class GithubUserListModule(val view: MainPresenter.View) { @Provides
fun provideMainPresenter( presenter: MainPresenterImpl) : MainPresenter { return presenter } @Provides fun provideMainView(): MainPresenter.View { return view } }
ۧѱࢎਊ೧ࠁ%BHHFS @Component(modules = [GithubUserListModule::class]) interface GithubUserListComponent { fun inject(Activity: MainActivity)
}
ۧѱࢎਊ೧ࠁ%BHHFS class MainPresenterImpl @Inject constructor( val view: MainPresenter.View, val client:
GithubUserApiClient) : MainPresenter { @SuppressLint("CheckResult") override fun getGithubUserList(q: String) { client.userDataService.getUserData(q, "repositories", "desc") .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({view.onDataLoaded(it)},{view.onDataFailed()},{}) } }
ۧѱࢎਊ೧ࠁ%BHHFS class MainActivity : AppCompatActivity(), MainPresenter.View { @Inject lateinit var
presenter: MainPresenter override fun searchGithubUser(searchWord: String) { if (searchWord.isNullOrBlank()) { toast("Ѩ࢝ػ о হणפ") userFragment.userAdapter.apply { items.clear() notifyDataSetChanged() } } else { presenter.getGithubUserList(searchWord) } } ... }
ۧѱࢎਊ೧ࠁ%BHHFS override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) //initialize
AppComponent var component = DaggerGithubUserListComponent.builder() .githubUserListModule(GithubUserListModule(this)) .build() component.inject(this) .. }
ۧѱࢎਊ೧ࠁ%BHHFS @Module(includes = [ApiClientModule::class]) class GithubUserListModule(val view: MainPresenter.View) { @Provides
fun provideMainPresenter( presenter: MainPresenterImpl) : MainPresenter { return presenter } @Provides fun provideMainView(): MainPresenter.View { return view } }
ۧѱࢎਊ೧ࠁزੌೠӝמઁ ઁ(JUIVCਬѨ࢝ (JU)VC6TFS4FBSDI"1*ࢎਊ IUUQTEFWFMPQFSHJUIVCDPNWTFBSDI
ۧѱࢎਊ೧ࠁ,PJO val userlistModule: Module = module { factory { MainPresenterImpl(get())
as MainPresenter<*> } }
ۧѱࢎਊ೧ࠁ,PJO val apiModule: Module = module { single { val
loggingInterceptor = HttpLoggingInterceptor() if (BuildConfig.DEBUG) { loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC } else { loggingInterceptor.level = HttpLoggingInterceptor.Level.NONE } Retrofit.Builder() .baseUrl("https://api.github.com") .client(OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(GithubUserApiService::class.java) } } val appModules = listOf(apiModule, userlistModule)
ۧѱࢎਊ೧ࠁ,PJO class MainApplication :Application() { override fun onCreate() { super.onCreate()
startKoin { // Android context androidContext(this@MainApplication) // modules modules(appModules) } } }
ۧѱࢎਊ೧ࠁ,PJO lass MainActivity : AppCompatActivity(), UserDataList { private val presenter:
MainPresenter<UserData> by inject() override fun searchGithubUser(searchWord: String) { if (searchWord.isNullOrBlank()) { presenter.getGithubUserList("a") userFragment.userAdapter.apply { items.clear() notifyDataSetChanged() } } else { presenter.getGithubUserList(searchWord) } } ... }
ۧѱࢎਊ೧ࠁ,PJO override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) presenter.userData
= this ... }
ࣁೠ٘ח(JUIVCীj IUUQTHJUIVCDPNIZVOKJ 'JOE/FX6TFST
ޖܻ۽ંী؊যܾө
%*ܳ൨ٜѱҕࠗ೧ࢲਊ೧оҊ ܻ۽ંӒܻҊܻਗٜ חҗোޖੋо
ઓࢿࢎਊਵ۽ח
↟୶࢚ചܳా೧ػёٜ࢚ഐਊী೧زੋܴоמ ↟۠ఋী߄ੋ٬غযઓࢿ৻ࠗীࢲੑغযઓبܳծ୶Ҋࢎਊоמ ↟ੑػ٘ӝמਸ߄ԲҊरਸٸ ݆٘ܳ߄Եਃоহ ↟ࢤࢿػੋझఢझझ ੋझఢࢤࢿҗࢤݺӝܳ %*ۨਕীࢲঌইࢲ೧ળ ઓࢿࢎਊਵ۽ח
݃ޖܻ Ӓېࢲަॳݶજױ݈ջ
݃ޖܻ ࢎप
݃ޖܻ ࢶఖীח଼ٮܲj
য়ےदрٜয࣊ࢲ хࢎפ