-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 각 Repository 구현체마다 인터페이스를 구현하도록 변경 * refactor: MainViewModel 팩토리 구현 * refactor: Main 관련 클래스 패키지 분리 * refactor: 코드 포맷팅 * refactor: CartViewModel 팩토리 구현 * chore: mockk 1.13.5 라이브러리 추가 * test: MainViewModel 테스트 코드 작성 * feat: 자동 DI 구현 * feat: @Inject Annotation 포함을 검증하지 않도록 변경 * chore: 불필요한 라이브러리 제거 * refactor: ViewModelFactory 유틸 클래스를 di 패키지로 이동 * refactor: @Inject 관련 코드 제거 * refactor: 각 ViewModel에 constructor 키워드 제거 * refactor: @mainthread 애노테이션 제거
- Loading branch information
Showing
30 changed files
with
322 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
19 changes: 19 additions & 0 deletions
19
app/src/main/java/woowacourse/shopping/ShoppingApplication.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package woowacourse.shopping | ||
|
||
import android.app.Application | ||
import woowacourse.shopping.data.repository.DefaultCartRepository | ||
import woowacourse.shopping.data.repository.DefaultProductRepository | ||
import woowacourse.shopping.di.injector.modules | ||
import woowacourse.shopping.repository.CartRepository | ||
import woowacourse.shopping.repository.ProductRepository | ||
|
||
class ShoppingApplication : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
modules { | ||
inject<ProductRepository>(DefaultProductRepository()) | ||
inject<CartRepository>(DefaultCartRepository()) | ||
} | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
app/src/main/java/woowacourse/shopping/data/CartRepository.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
18 changes: 18 additions & 0 deletions
18
app/src/main/java/woowacourse/shopping/data/repository/DefaultCartRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package woowacourse.shopping.data.repository | ||
|
||
import woowacourse.shopping.model.Product | ||
import woowacourse.shopping.repository.CartRepository | ||
|
||
class DefaultCartRepository : CartRepository { | ||
private val cartProducts: MutableList<Product> = mutableListOf() | ||
|
||
override fun addCartProduct(product: Product) { | ||
cartProducts.add(product) | ||
} | ||
|
||
override fun getAllCartProducts(): List<Product> = cartProducts.toList() | ||
|
||
override fun deleteCartProduct(id: Int) { | ||
cartProducts.removeAt(id) | ||
} | ||
} |
16 changes: 7 additions & 9 deletions
16
...course/shopping/data/ProductRepository.kt → ...ta/repository/DefaultProductRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
package woowacourse.shopping.data | ||
package woowacourse.shopping.data.repository | ||
|
||
import woowacourse.shopping.model.Product | ||
import woowacourse.shopping.repository.ProductRepository | ||
|
||
class ProductRepository { | ||
|
||
class DefaultProductRepository : ProductRepository { | ||
private val products: List<Product> = listOf( | ||
Product( | ||
name = "우테코 과자", | ||
price = 10_000, | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/api/main/df6d76fb-925b-40f8-9d1c-f0920c3c697a.jpg?h=700&w=700" | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/api/main/df6d76fb-925b-40f8-9d1c-f0920c3c697a.jpg?h=700&w=700", | ||
), | ||
Product( | ||
name = "우테코 쥬스", | ||
price = 8_000, | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/main/52dca718-31c5-4f80-bafa-7e300d8c876a.jpg?h=700&w=700" | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/main/52dca718-31c5-4f80-bafa-7e300d8c876a.jpg?h=700&w=700", | ||
), | ||
Product( | ||
name = "우테코 아이스크림", | ||
price = 20_000, | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/main/e703c53e-5d01-4b20-bd33-85b5e778e73f.jpg?h=700&w=700" | ||
imageUrl = "https://cdn-mart.baemin.com/sellergoods/main/e703c53e-5d01-4b20-bd33-85b5e778e73f.jpg?h=700&w=700", | ||
), | ||
) | ||
|
||
fun getAllProducts(): List<Product> { | ||
return products | ||
} | ||
override fun getAllProducts(): List<Product> = products | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/woowacourse/shopping/di/injector/DependencyInjector.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package woowacourse.shopping.di.injector | ||
|
||
import woowacourse.shopping.di.util.validateHasPrimaryConstructor | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.javaType | ||
|
||
object ClassInjector { | ||
val dependencies = mutableMapOf<Class<*>, Any>() | ||
|
||
inline fun <reified T : Any> inject(instance: T) { | ||
dependencies[T::class.java] = instance | ||
} | ||
|
||
inline fun <reified T : Any> inject(): T { | ||
val primaryConstructor = validateHasPrimaryConstructor<T>() | ||
val parameterValues = getParameterValues(primaryConstructor.parameters) | ||
|
||
return primaryConstructor.call(*parameterValues.toTypedArray()) | ||
} | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
fun getParameterValues(parameters: List<KParameter>): MutableList<Any> { | ||
val parameterTypes = parameters.map { it.type } | ||
val parameterValues = mutableListOf<Any>() | ||
|
||
parameterTypes.forEach { paramType -> | ||
val parameterType = paramType.javaType | ||
val parameterValue = dependencies[parameterType] | ||
|
||
requireNotNull(parameterValue) { "[ERROR] 주입할 의존성이 존재하지 않습니다." } | ||
parameterValues.add(parameterValue) | ||
} | ||
|
||
return parameterValues | ||
} | ||
} | ||
|
||
class ClassInjectorDsl { | ||
inline fun <reified T : Any> inject(instance: T) { | ||
ClassInjector.inject(instance) | ||
} | ||
} | ||
|
||
fun modules(block: ClassInjectorDsl.() -> Unit) { | ||
ClassInjectorDsl().apply(block) | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/woowacourse/shopping/di/lazy/ActivityViewModelLazy.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package woowacourse.shopping.di.lazy | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelLazy | ||
import woowacourse.shopping.di.injector.ClassInjector | ||
import woowacourse.shopping.di.util.viewModelFactory | ||
|
||
inline fun <reified VM : ViewModel> ComponentActivity.viewModel(): Lazy<VM> { | ||
return ViewModelLazy( | ||
VM::class, | ||
{ viewModelStore }, | ||
{ viewModelFactory { createViewModel<VM>() } }, | ||
) | ||
} | ||
|
||
inline fun <reified VM : ViewModel> createViewModel(): VM { | ||
return ClassInjector.inject() | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/woowacourse/shopping/di/util/UtilFunctions.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package woowacourse.shopping.di.util | ||
|
||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.primaryConstructor | ||
|
||
inline fun <reified T : Any> validateHasPrimaryConstructor(): KFunction<T> { | ||
val primaryConstructor = T::class.primaryConstructor | ||
return requireNotNull(primaryConstructor) { "[ERROR] 주생성자가 존재하지 않습니다." } | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/woowacourse/shopping/di/util/ViewModelFactory.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package woowacourse.shopping.di.util | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
|
||
inline fun <reified VM : ViewModel> viewModelFactory(crossinline create: () -> VM): ViewModelProvider.Factory = | ||
viewModelFactory { | ||
initializer { | ||
create() | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
...hopping/ui/bindingadapter/ImageBinding.kt → .../ui/common/bindingadapter/ImageBinding.kt
This file contains 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
This file contains 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
Oops, something went wrong.