-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[부나] 2, 3단계 자동 DI 미션 제출합니다 #29
Changes from all commits
961523a
5ac37cf
f3db239
c95b7c0
af93fd7
a2b640c
ed8e750
970dc8b
8175c12
1c72fcf
ff6f067
0bb35a3
7770cf7
bffeafc
0c4c466
eb091ed
5aff10c
c56d211
95f32b3
d5b56e3
8c2b8aa
28b2799
f534674
c61014c
0c51aaa
c9176b5
7f016fd
9a8ee71
c28d68f
0b05e6e
a740717
f24e308
5d2f67e
76b7693
617ae36
29ededf
87e8553
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
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 | ||
import com.woowacourse.bunadi.dsl.modules | ||
import woowacourse.shopping.ui.common.di.module.DaoModule | ||
|
||
class ShoppingApplication : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
modules { | ||
inject<ProductRepository>(DefaultProductRepository()) | ||
inject<CartRepository>(DefaultCartRepository()) | ||
module(DaoModule(this@ShoppingApplication)) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package woowacourse.shopping.data.mapper | ||
|
||
import woowacourse.shopping.data.CartProductEntity | ||
import woowacourse.shopping.model.CartProduct | ||
import woowacourse.shopping.model.Product | ||
|
||
fun Product.toEntity(): CartProductEntity = CartProductEntity( | ||
name = name, | ||
price = price, | ||
imageUrl = imageUrl, | ||
) | ||
|
||
fun List<CartProductEntity>.toDomain(): List<CartProduct> = map { cartProductEntity -> | ||
CartProduct( | ||
product = Product( | ||
name = cartProductEntity.name, | ||
price = cartProductEntity.price, | ||
imageUrl = cartProductEntity.imageUrl, | ||
), | ||
id = cartProductEntity.id, | ||
createdAt = cartProductEntity.createdAt, | ||
) | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package woowacourse.shopping.data.repository | ||
|
||
import com.woowacourse.bunadi.annotation.Singleton | ||
import woowacourse.shopping.data.CartProductDao | ||
import woowacourse.shopping.data.mapper.toDomain | ||
import woowacourse.shopping.data.mapper.toEntity | ||
import woowacourse.shopping.model.CartProduct | ||
import woowacourse.shopping.model.Product | ||
import woowacourse.shopping.repository.CartRepository | ||
|
||
@Singleton | ||
class DatabaseCartRepository( | ||
private val dao: CartProductDao, | ||
) : CartRepository { | ||
override suspend fun addCartProduct(product: Product) { | ||
dao.insert(product.toEntity()) | ||
} | ||
|
||
override suspend fun getAllCartProducts(): List<CartProduct> { | ||
return dao.getAll().toDomain() | ||
} | ||
|
||
override suspend fun deleteCartProduct(id: Long) { | ||
dao.delete(id) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package woowacourse.shopping.data.repository | ||
|
||
import com.woowacourse.bunadi.annotation.Singleton | ||
import woowacourse.shopping.data.CartProductEntity | ||
import woowacourse.shopping.data.mapper.toDomain | ||
import woowacourse.shopping.data.mapper.toEntity | ||
import woowacourse.shopping.model.CartProduct | ||
import woowacourse.shopping.model.Product | ||
import woowacourse.shopping.repository.CartRepository | ||
|
||
@Singleton | ||
class InMemoryCartRepository : CartRepository { | ||
private val cartProducts = mutableListOf<CartProductEntity>() | ||
private var lastId: Long = 0 | ||
|
||
override suspend fun addCartProduct(product: Product) { | ||
cartProducts.add(product.toEntity().apply { id = ++lastId }) | ||
} | ||
|
||
override suspend fun getAllCartProducts(): List<CartProduct> { | ||
return cartProducts.toDomain() | ||
} | ||
|
||
override suspend fun deleteCartProduct(id: Long) { | ||
cartProducts.removeIf { it.id == id } | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,26 +3,33 @@ package woowacourse.shopping.ui.cart | |
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import woowacourse.shopping.model.Product | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import woowacourse.shopping.model.CartProduct | ||
import woowacourse.shopping.repository.CartRepository | ||
import woowacourse.shopping.ui.common.di.qualifier.DatabaseCartRepositoryQualifier | ||
|
||
class CartViewModel( | ||
private val cartRepository: CartRepository, | ||
@DatabaseCartRepositoryQualifier val cartRepository: CartRepository, | ||
) : ViewModel() { | ||
Comment on lines
12
to
14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뷰모델은 레포지토리를 통해 그저 데이터를 관리하는데에만 관심사가 있습니다. 다르게 말하면 우리가 레포지토리 패턴을 사용한다는 가정하에 레포지토리의 데이터 소스로 Local이 오냐 Remote가 오냐는 레포지토리 내부 사정이라고 생각하는데 부나의 어노테이션은 데이터베이스, 즉 데이터 소스의 성격을 표시하고 있습니다. 부나가 생각하시기에 어떤가요?? 상관없을 것 같다면 반영하시지 않으셔도 좋습니다. 다만 부나의 의견은 궁금하니 코멘트 남겨주세요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 반달과 동일한 생각이예요! 제가 아는 선에서 사실 Hilt도 위와 같은 형식으로 애노테이션을 지정해주고 있는 것으로 알고 있어요! 참고 자료를 위해 링크를 함께 첨부해드려요 : ) |
||
|
||
private val _cartProducts: MutableLiveData<List<Product>> = | ||
private val _cartProducts: MutableLiveData<List<CartProduct>> = | ||
MutableLiveData(emptyList()) | ||
val cartProducts: LiveData<List<Product>> get() = _cartProducts | ||
val cartProducts: LiveData<List<CartProduct>> get() = _cartProducts | ||
|
||
private val _onCartProductDeleted: MutableLiveData<Boolean> = MutableLiveData(false) | ||
val onCartProductDeleted: LiveData<Boolean> get() = _onCartProductDeleted | ||
|
||
fun fetchAllCartProducts() { | ||
_cartProducts.value = cartRepository.getAllCartProducts() | ||
viewModelScope.launch { | ||
_cartProducts.value = cartRepository.getAllCartProducts() | ||
} | ||
} | ||
|
||
fun deleteCartProduct(id: Int) { | ||
cartRepository.deleteCartProduct(id) | ||
_onCartProductDeleted.value = true | ||
fun deleteCartProduct(id: Long) { | ||
viewModelScope.launch { | ||
cartRepository.deleteCartProduct(id) | ||
_onCartProductDeleted.value = true | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package woowacourse.shopping.ui.common.di.module | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.woowacourse.bunadi.module.Module | ||
import woowacourse.shopping.data.CartProductDao | ||
import woowacourse.shopping.data.ShoppingDatabase | ||
|
||
class DaoModule(private val context: Context) : Module { | ||
fun provideCartProductDao(): CartProductDao = Room.databaseBuilder( | ||
context, | ||
ShoppingDatabase::class.java, | ||
ShoppingDatabase.DATABASE_NAME, | ||
).build().cartProductDao() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package woowacourse.shopping.ui.common.di.qualifier | ||
|
||
import com.woowacourse.bunadi.annotation.Qualifier | ||
import woowacourse.shopping.data.repository.DatabaseCartRepository | ||
import woowacourse.shopping.data.repository.DefaultProductRepository | ||
import woowacourse.shopping.data.repository.InMemoryCartRepository | ||
|
||
@Qualifier(DefaultProductRepository::class) | ||
annotation class DefaultProductRepositoryQualifier | ||
|
||
@Qualifier(InMemoryCartRepository::class) | ||
annotation class InMemoryCartRepositoryQualifier | ||
|
||
@Qualifier(DatabaseCartRepository::class) | ||
annotation class DatabaseCartRepositoryQualifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
세심한 상수화 😎