Skip to content

Commit

Permalink
[ADD/#36] 홈뷰 서버통신 presentation 모듈 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Jul 11, 2024
1 parent 408f574 commit 9765ecc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ data class ProductModel(
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 @@ -14,6 +14,9 @@ import co.orange.core.base.BaseFragment
import co.orange.core.extension.dpToPx
import co.orange.core.extension.initOnBackPressedListener
import co.orange.core.extension.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.core.state.UiState
import co.orange.domain.entity.response.ProductModel
import co.orange.presentation.auth.login.LoginActivity
import co.orange.presentation.detail.DetailActivity
Expand Down Expand Up @@ -51,8 +54,8 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
searchSellProduct()
setGridRecyclerView()
setRecyclerViewDeco()
setItemList()
observeCheckedAgainState()
observeGetHomeDataState()
}

private fun initView() {
Expand Down Expand Up @@ -145,10 +148,6 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
)
}

private fun setItemList() {
adapter.setItemList(viewModel.mockItemList)
}

private fun observeCheckedAgainState() {
viewModel.isCheckedAgain.flowWithLifecycle(lifecycle).onEach { isChecked ->
if (isChecked) {
Expand All @@ -158,6 +157,16 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
}.launchIn(lifecycleScope)
}

private fun observeGetHomeDataState() {
viewModel.getHomeDataState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.setItemList(state.data)
is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_adapter = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,56 @@ package co.orange.presentation.main.home
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.repository.HomeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel
@Inject
constructor(
// private val feedRepository: FeedRepository,
private val homeRepository: HomeRepository,
) : ViewModel() {
init {
getHomeDataFromServer()
}

var selectedImageUri = Uri.EMPTY

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

fun setCheckedState(state: Boolean) {
viewModelScope.launch {
_isCheckedAgain.emit(state)
}
}

val mockItemList =
listOf(
ProductModel(
1,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
12,
),
ProductModel(
2,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
34,
),
ProductModel(
3,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
56,
),
ProductModel(
4,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
78,
),
ProductModel(
5,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
910,
),
ProductModel(
6,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
112,
),
ProductModel(
7,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
1234,
),
ProductModel(
8,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
1234,
),
ProductModel(
9,
1,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
48900,
1234,
),
)
private fun getHomeDataFromServer() {
viewModelScope.launch {
homeRepository.getHomeData()
.onSuccess {
val itemList = it.productList.toMutableList()
itemList.add(
0,
imageOnlyProductModel(it.homeImgUrl),
)
_getHomeDataState.value = UiState.Success(itemList)
}
.onFailure {
_getHomeDataState.value = UiState.Failure(it.message.toString())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SearchViewModel
listOf(
ProductModel(
1,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -26,6 +27,7 @@ class SearchViewModel
),
ProductModel(
2,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -39,6 +41,7 @@ class SearchViewModel
listOf(
ProductModel(
1,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -47,6 +50,7 @@ class SearchViewModel
),
ProductModel(
2,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -55,6 +59,7 @@ class SearchViewModel
),
ProductModel(
3,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -63,6 +68,7 @@ class SearchViewModel
),
ProductModel(
4,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -71,6 +77,7 @@ class SearchViewModel
),
ProductModel(
5,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -79,6 +86,7 @@ class SearchViewModel
),
ProductModel(
6,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand All @@ -87,6 +95,7 @@ class SearchViewModel
),
ProductModel(
7,
0,
"퓨어 오일 퍼퓸 10 ml 긴제목테스트트트트트",
"https://github.com/Marchbreeze/Marchbreeze/assets/97405341/cd2c0454-92b4-41e7-ae2f-319f83e2426f",
54000,
Expand Down

0 comments on commit 9765ecc

Please sign in to comment.