Skip to content

Commit

Permalink
[FEAT/#36] 홈뷰 서버통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Jul 11, 2024
1 parent e199e34 commit b7ac233
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class ProductDto(
@SerialName("productId")
val productId: Long,
val productId: String,
@SerialName("kakaoProductId")
val kakaoProductId: Long,
@SerialName("name")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package co.orange.domain.entity.response

data class ProductModel(
val productId: Long,
val productId: String,
val kakaoProductId: Long,
val name: String,
val imgUrl: String,
val originPrice: Int,
val salePrice: Int,
val interestCount: Int,
) {
companion object {
fun imageOnlyProductModel(imgUrl: String) =
ProductModel(
-1,
-1,
"",
imgUrl,
0,
0,
0,
)
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DetailActivity : BaseActivity<ActivityDetailBinding>(R.layout.activity_det
@JvmStatic
fun createIntent(
context: Context,
productId: Long,
productId: String,
productUrl: String,
originPrice: Int,
salePrice: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class HomeAdapter(
private val likeClick: (Unit) -> (Unit),
) : ListAdapter<ProductModel, RecyclerView.ViewHolder>(diffUtil) {
private var itemList = mutableListOf<ProductModel>()
private var bannerItem: String? = null

override fun onCreateViewHolder(
parent: ViewGroup,
Expand Down Expand Up @@ -52,7 +53,7 @@ class HomeAdapter(
) {
when (holder) {
is HomeBannerViewHolder -> {
if (itemList.isNotEmpty()) holder.onBind(itemList[0])
bannerItem?.let { holder.onBind(it) }
}

is HomeProductViewHolder -> {
Expand All @@ -73,8 +74,8 @@ class HomeAdapter(
else -> VIEW_TYPE_PRODUCT
}

fun addItemList(newItems: List<ProductModel>) {
this.itemList.addAll(newItems)
fun addBannerItem(bannerUrl: String) {
this.bannerItem = bannerUrl
notifyDataSetChanged()
}

Expand All @@ -83,14 +84,6 @@ class HomeAdapter(
notifyDataSetChanged()
}

fun removeItem(position: Int) {
if (this.itemList.isNotEmpty()) {
this.itemList.removeAt(position)
notifyItemRemoved(position + HEADER_COUNT)
notifyItemRangeChanged(position + HEADER_COUNT, itemCount)
}
}

companion object {
private val diffUtil =
ItemDiffCallback<ProductModel>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package co.orange.presentation.main.home

import androidx.recyclerview.widget.RecyclerView
import co.orange.core.extension.setOnSingleClickListener
import co.orange.domain.entity.response.ProductModel
import coil.load
import kr.genti.presentation.databinding.ItemHomeBannerBinding

Expand All @@ -11,9 +10,9 @@ class HomeBannerViewHolder(
val bannerClick: (Unit) -> (Unit),
) :
RecyclerView.ViewHolder(binding.root) {
fun onBind(item: ProductModel) {
fun onBind(item: String) {
with(binding) {
ivHomeBanner.load(item.imgUrl)
ivHomeBanner.load(item)
root.setOnSingleClickListener {
bannerClick
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,15 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
private fun observeGetHomeDataState() {
viewModel.getHomeDataState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.setItemList(state.data)
is UiState.Success -> {
adapter.addBannerItem(state.data.homeImgUrl)
adapter.setItemList(state.data.productList)
}

is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}
}.launchIn(lifecycleScope)
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.orange.core.state.UiState
import co.orange.domain.entity.response.ProductModel
import co.orange.domain.entity.response.ProductModel.Companion.imageOnlyProductModel
import co.orange.domain.entity.response.HomeModel
import co.orange.domain.repository.HomeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -30,8 +29,8 @@ class HomeViewModel
private val _isCheckedAgain = MutableSharedFlow<Boolean>()
val isCheckedAgain: SharedFlow<Boolean> = _isCheckedAgain

private val _getHomeDataState = MutableStateFlow<UiState<List<ProductModel>>>(UiState.Empty)
val getHomeDataState: StateFlow<UiState<List<ProductModel>>> = _getHomeDataState
private val _getHomeDataState = MutableStateFlow<UiState<HomeModel>>(UiState.Empty)
val getHomeDataState: StateFlow<UiState<HomeModel>> = _getHomeDataState

fun setCheckedState(state: Boolean) {
viewModelScope.launch {
Expand All @@ -43,12 +42,7 @@ class HomeViewModel
viewModelScope.launch {
homeRepository.getHomeData()
.onSuccess {
val itemList = it.productList.toMutableList()
itemList.add(
0,
imageOnlyProductModel(it.homeImgUrl),
)
_getHomeDataState.value = UiState.Success(itemList)
_getHomeDataState.value = UiState.Success(it)
}
.onFailure {
_getHomeDataState.value = UiState.Failure(it.message.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SearchViewModel
listOf("향수", "이승준", "곰인형", "성년의 날 선물", "멀티비타민"),
listOf(
ProductModel(
1,
"1",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -26,7 +26,7 @@ class SearchViewModel
12,
),
ProductModel(
2,
"2",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -40,7 +40,7 @@ class SearchViewModel
val mockItemList =
listOf(
ProductModel(
1,
"1",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -49,7 +49,7 @@ class SearchViewModel
12,
),
ProductModel(
2,
"2",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -58,7 +58,7 @@ class SearchViewModel
34,
),
ProductModel(
3,
"3",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -67,7 +67,7 @@ class SearchViewModel
56,
),
ProductModel(
4,
"4",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -76,7 +76,7 @@ class SearchViewModel
78,
),
ProductModel(
5,
"5",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -85,7 +85,7 @@ class SearchViewModel
910,
),
ProductModel(
6,
"6",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand All @@ -94,7 +94,7 @@ class SearchViewModel
112,
),
ProductModel(
7,
"7",
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
Expand Down

0 comments on commit b7ac233

Please sign in to comment.