์ด๋ฒ์ ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์กด์ฑ ์ฃผ์ (DI)๋ฅผ ์ ์ฉ์์ผ๋ณด๋ฉด์ ์ฝ์ง์ ์ฐ์์์ ๋๊ผ๋ ์ ๊ณผ ํ์ํ ๋ถ๋ถ์ ์์ฑํ ์์ ์ ๋๋ค.
1. Fragment๋ผ๋ฆฌ ViewModel์ ๊ณต์ ํ๊ณ ์ถ์ ๋!
class JoinEntrepreneurFragment: Fragment() {
...
private val viewModel: JoinViewModel by sharedViewModel()
โ
...
}
โ
class JoinMainFragment : SoftKeyboardImplementFragment() {
...
private val viewModel: JoinViewModel by sharedViewModel()
โ
...
}
์์ ๊ฐ์ ํ์์ผ๋ก sharedViewModel() ๋ฅผ ์ ์ด์ค๋ค๋ฉด Fragment๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ง & ๊ณต์ ๊ฐ ๊ฐ๋ฅํด์ง๋๋ค.
2. Retrofit2 ์๋ฒ ํธ์ถ ๋ฐฉ๋ฒ
์๋ฒํธ์ถ์ ํ์ํ Retrofit๋ ์์กด์ฑ ์ฃผ์ ์ ํ๋๋ฐ ๋ฐฉ๋ฒ์ ๊ฐ๋จํ์ง ์์ง๋ง ๊ฐ๋จํ๋ค๊ณ ์๊ฐํ๋ ค ํฉ๋๋ค. ๐ข
val remoteModule = module {
โ
// ์๋ฒํธ์ถ์ Log๋ฅผ ๋ณด์ฌ์ค
single {
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
}
}
โ
// ํ ํฐ๊ด๋ จ ์ธํฐ์
ํฐ
single {
TokenInterceptor(get(), get(), get())
}
โ
// Gsonํ์์ ๋ณํํ ๋ ํ์
single<GsonConverterFactory> { GsonConverterFactory.create(get()) }
โ
// retrofit์ ์ฌ์ฉํ๊ธฐ ์ํ OkHttp ๋น๋
single {
OkHttpClient.Builder()
.addInterceptor(get<HttpLoggingInterceptor>())
.addInterceptor(get<TokenAuthenticator>())
.readTimeout(10, TimeUnit.SECONDS)
.build()
}
โ
// retrofit ์์กด์ฑ ์ฃผ์
// named ๋ ๋๊ฐ์ Retrofit์ ์ ์ธํ ๋ ์ฌ์ฉํ๋ ์ด๋ฆ์ ๋ฌ๋ฆฌํด์ ์ค์ ์ฌ์ฉ์ ๊ตฌ๋ถ์ ์ํ ์ฝ๋
single<Retrofit>(named(BaseQualifier.HWACHA_API)) {
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(get<OkHttpClient>())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(get<GsonConverterFactory>())
.build()
}
โ
single<OnBoardingApi> { // ์๋์ named ์ฌ์ฉ ์ฌ๋ก.
get<Retrofit>(named(BaseQualifier.HWACHA_API)).create(
OnBoardingApi::class.java
)
}
โ
}
3. Repository ์์ฑ
์๋ฒ ํธ์ถ ๊ฐ๋ฐ์ ํ๋ฉด์ interface๋ฅผ ์์ ๋ฐ์ผ๋ฉด์ ์์กด์ฑ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด ๋ถ๋ถ์์ ๊ต์ฅํ ์ดํดํ๊ธฐ ํ๋ค์๊ณ ์์ง ๊ฐ๋ฐ์ ํ๋ฉด์ ๋ ์ต์ํด์ ธ์ผํ๋ ๋ถ๋ถ์ด๋ผ ์๊ฐํ๋ค.
// OnBoardingRepository.kt
interface OnBoardingRepository {
var postAuthResponse: MutableLiveData<Resource<AuthResponse>>
...
โ
fun postAuth(postAuthRequest: AuthRequest)
...
}
โ
// OnBoardingRepositoryImpl.kt
โ
class OnBoardingRepositoryImpl(
private val onBoardingApi: OnBoardingApi,
private val gson: Gson,
) : OnBoardingRepository {
override var postAuthResponse: MutableLiveData<Resource<AuthResponse>> = MutableLiveData()
...
โ
override fun postAuth(postAuthRequest: AuthRequest) {
...
}
...
}
โ
// RepositoryModule.kt
// ์ interface์ ์์ ๋ฐ์ impl์ ์์ฑ ํ ๋ชจ๋์ ์ ์ด์ค๋๋ค.
val repositoryModule = module {
single<OnBoardingRepository> { OnBoardingRepositoryImpl(get(), get()) }
...
}
์ด๋ ์ค์ ์ฌ์ฉ๋ฐฉ๋ฒ์ interface๋ฅผ get()์ผ๋ก ์์กด์ฑ ์ฃผ์ ์ ํฉ๋๋ค.
โ
// ViewModelModule.kt
val viewModelModule = module {
viewModel { JoinViewModel(get()) }
...
}
โ
// JoinViewModel.kt
โ
class JoinViewModel(
private val onBoardingRepo: OnBoardingRepository
) : ViewModel() {
fun getAuth() {
onBoardingRepo.postAuth(PostAuthRequest(...))
}
}
์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ๋ฉด interface๋ฅผ ์์๋ฐ์ impl์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฏธ์ง ์์ด ์ฝ๋๋ก๋ง ์ ์ฉํ์ฌ ๊ธฐ์ตํ๊ธฐ ์ด๋ ต๊ณ ์ ์ฉ์ํค๊ธฐ ์ด๋ ค์ธ ๊ฒ์ผ๋ก ์์์ด ๋์ง๋ง ๊ณ์ํด์ ์ฌ์ฉํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ถํ์ ๊ณ์ ๋ณ๊ฒฝ์ฌํญ์ด ์๊ธฐ๋ฉด ์ถ๊ฐ, ์์ ํ ์์ ์ ๋๋ค~
4. ๋๋์
์ด๋ฒ ์ฃผ Koin์ ์ ์ฉํด์ ๊ฐ๋ฐํ ๋ ๋๊ผ๋ ์ ์ โ์ ๋ง ์์ ํ๋ก์ ํธ์ ์์กด์ฑ ์ฃผ์ ์ ํ๋๊ฒ ๋ง์๊น?โ ๋ผ๋ ์๊ฐ์ด ๊ณ์ ๋ค์์ง๋ง ์์ ํ๋ก์ ํธ์ ๋ฃ๊ณ ์์ํ์ง ์๋๋ค๋ฉด ์กฐ๊ธ์ด๋ผ๋ ์ปค์ก์ ๋ ์ ์ฉ์ํค๊ธฐ ๋๋ฌด ํ๋ค ๊ฒ์ผ๋ก ์์์ด ๋์ ํ๋ค์ง๋ง๐ซ ์ฝ์ง๐ ํ๋ฉด์ ํ ๊ฑธ์ ํ ๊ฑธ์ ๋์๊ฐ ์์ ์ ๋๋ค.
'Development > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฆ Databinding์ ์ฐ๋ฉด์ - 2 (0) | 2022.09.08 |
---|---|
๐ฆ Databinding์ ์ฐ๋ฉด์ - 1 (0) | 2022.09.08 |
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ํ ๋ข์ - 1 (0) | 2022.09.08 |
Event Bus๐์ ๋ฒ์ค ์ข ๋ฐ์๋ณผ๊น? (0) | 2022.09.04 |
๋ฆฌ์์ค ๊ด๋ฆฌ (0) | 2022.09.04 |
์ด๋ฒ์ ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์กด์ฑ ์ฃผ์ (DI)๋ฅผ ์ ์ฉ์์ผ๋ณด๋ฉด์ ์ฝ์ง์ ์ฐ์์์ ๋๊ผ๋ ์ ๊ณผ ํ์ํ ๋ถ๋ถ์ ์์ฑํ ์์ ์ ๋๋ค.
1. Fragment๋ผ๋ฆฌ ViewModel์ ๊ณต์ ํ๊ณ ์ถ์ ๋!
class JoinEntrepreneurFragment: Fragment() {
...
private val viewModel: JoinViewModel by sharedViewModel()
โ
...
}
โ
class JoinMainFragment : SoftKeyboardImplementFragment() {
...
private val viewModel: JoinViewModel by sharedViewModel()
โ
...
}
์์ ๊ฐ์ ํ์์ผ๋ก sharedViewModel() ๋ฅผ ์ ์ด์ค๋ค๋ฉด Fragment๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ง & ๊ณต์ ๊ฐ ๊ฐ๋ฅํด์ง๋๋ค.
2. Retrofit2 ์๋ฒ ํธ์ถ ๋ฐฉ๋ฒ
์๋ฒํธ์ถ์ ํ์ํ Retrofit๋ ์์กด์ฑ ์ฃผ์ ์ ํ๋๋ฐ ๋ฐฉ๋ฒ์ ๊ฐ๋จํ์ง ์์ง๋ง ๊ฐ๋จํ๋ค๊ณ ์๊ฐํ๋ ค ํฉ๋๋ค. ๐ข
val remoteModule = module {
โ
// ์๋ฒํธ์ถ์ Log๋ฅผ ๋ณด์ฌ์ค
single {
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
}
}
โ
// ํ ํฐ๊ด๋ จ ์ธํฐ์
ํฐ
single {
TokenInterceptor(get(), get(), get())
}
โ
// Gsonํ์์ ๋ณํํ ๋ ํ์
single<GsonConverterFactory> { GsonConverterFactory.create(get()) }
โ
// retrofit์ ์ฌ์ฉํ๊ธฐ ์ํ OkHttp ๋น๋
single {
OkHttpClient.Builder()
.addInterceptor(get<HttpLoggingInterceptor>())
.addInterceptor(get<TokenAuthenticator>())
.readTimeout(10, TimeUnit.SECONDS)
.build()
}
โ
// retrofit ์์กด์ฑ ์ฃผ์
// named ๋ ๋๊ฐ์ Retrofit์ ์ ์ธํ ๋ ์ฌ์ฉํ๋ ์ด๋ฆ์ ๋ฌ๋ฆฌํด์ ์ค์ ์ฌ์ฉ์ ๊ตฌ๋ถ์ ์ํ ์ฝ๋
single<Retrofit>(named(BaseQualifier.HWACHA_API)) {
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(get<OkHttpClient>())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(get<GsonConverterFactory>())
.build()
}
โ
single<OnBoardingApi> { // ์๋์ named ์ฌ์ฉ ์ฌ๋ก.
get<Retrofit>(named(BaseQualifier.HWACHA_API)).create(
OnBoardingApi::class.java
)
}
โ
}
3. Repository ์์ฑ
์๋ฒ ํธ์ถ ๊ฐ๋ฐ์ ํ๋ฉด์ interface๋ฅผ ์์ ๋ฐ์ผ๋ฉด์ ์์กด์ฑ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด ๋ถ๋ถ์์ ๊ต์ฅํ ์ดํดํ๊ธฐ ํ๋ค์๊ณ ์์ง ๊ฐ๋ฐ์ ํ๋ฉด์ ๋ ์ต์ํด์ ธ์ผํ๋ ๋ถ๋ถ์ด๋ผ ์๊ฐํ๋ค.
// OnBoardingRepository.kt
interface OnBoardingRepository {
var postAuthResponse: MutableLiveData<Resource<AuthResponse>>
...
โ
fun postAuth(postAuthRequest: AuthRequest)
...
}
โ
// OnBoardingRepositoryImpl.kt
โ
class OnBoardingRepositoryImpl(
private val onBoardingApi: OnBoardingApi,
private val gson: Gson,
) : OnBoardingRepository {
override var postAuthResponse: MutableLiveData<Resource<AuthResponse>> = MutableLiveData()
...
โ
override fun postAuth(postAuthRequest: AuthRequest) {
...
}
...
}
โ
// RepositoryModule.kt
// ์ interface์ ์์ ๋ฐ์ impl์ ์์ฑ ํ ๋ชจ๋์ ์ ์ด์ค๋๋ค.
val repositoryModule = module {
single<OnBoardingRepository> { OnBoardingRepositoryImpl(get(), get()) }
...
}
์ด๋ ์ค์ ์ฌ์ฉ๋ฐฉ๋ฒ์ interface๋ฅผ get()์ผ๋ก ์์กด์ฑ ์ฃผ์ ์ ํฉ๋๋ค.
โ
// ViewModelModule.kt
val viewModelModule = module {
viewModel { JoinViewModel(get()) }
...
}
โ
// JoinViewModel.kt
โ
class JoinViewModel(
private val onBoardingRepo: OnBoardingRepository
) : ViewModel() {
fun getAuth() {
onBoardingRepo.postAuth(PostAuthRequest(...))
}
}
์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ๋ฉด interface๋ฅผ ์์๋ฐ์ impl์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฏธ์ง ์์ด ์ฝ๋๋ก๋ง ์ ์ฉํ์ฌ ๊ธฐ์ตํ๊ธฐ ์ด๋ ต๊ณ ์ ์ฉ์ํค๊ธฐ ์ด๋ ค์ธ ๊ฒ์ผ๋ก ์์์ด ๋์ง๋ง ๊ณ์ํด์ ์ฌ์ฉํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ถํ์ ๊ณ์ ๋ณ๊ฒฝ์ฌํญ์ด ์๊ธฐ๋ฉด ์ถ๊ฐ, ์์ ํ ์์ ์ ๋๋ค~
4. ๋๋์
์ด๋ฒ ์ฃผ Koin์ ์ ์ฉํด์ ๊ฐ๋ฐํ ๋ ๋๊ผ๋ ์ ์ โ์ ๋ง ์์ ํ๋ก์ ํธ์ ์์กด์ฑ ์ฃผ์ ์ ํ๋๊ฒ ๋ง์๊น?โ ๋ผ๋ ์๊ฐ์ด ๊ณ์ ๋ค์์ง๋ง ์์ ํ๋ก์ ํธ์ ๋ฃ๊ณ ์์ํ์ง ์๋๋ค๋ฉด ์กฐ๊ธ์ด๋ผ๋ ์ปค์ก์ ๋ ์ ์ฉ์ํค๊ธฐ ๋๋ฌด ํ๋ค ๊ฒ์ผ๋ก ์์์ด ๋์ ํ๋ค์ง๋ง๐ซ ์ฝ์ง๐ ํ๋ฉด์ ํ ๊ฑธ์ ํ ๊ฑธ์ ๋์๊ฐ ์์ ์ ๋๋ค.
'Development > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฆ Databinding์ ์ฐ๋ฉด์ - 2 (0) | 2022.09.08 |
---|---|
๐ฆ Databinding์ ์ฐ๋ฉด์ - 1 (0) | 2022.09.08 |
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ํ ๋ข์ - 1 (0) | 2022.09.08 |
Event Bus๐์ ๋ฒ์ค ์ข ๋ฐ์๋ณผ๊น? (0) | 2022.09.04 |
๋ฆฌ์์ค ๊ด๋ฆฌ (0) | 2022.09.04 |