Skip to content

Commit

Permalink
[FEAT/#40] 피드뷰 아이템리스트 서버통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Jun 2, 2024
1 parent 0575e3b commit 9adc693
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class CreateViewModel
private val _getRandomPromptState = MutableStateFlow<UiState<PromptModel>>(UiState.Empty)
val getRandomPromptState: StateFlow<UiState<PromptModel>> = _getRandomPromptState

init {
getExamplePromptsFromServer()
}

fun modCurrentPercent(amount: Int) {
_currentPercent.value += amount
}
Expand Down Expand Up @@ -74,7 +78,7 @@ class CreateViewModel
selectedRatio.value != -1 && selectedAngle.value != -1 && selectedFrame.value != -1
}

fun getExamplePromptsFromServer() {
private fun getExamplePromptsFromServer() {
if (examplePromptList.isEmpty()) {
viewModelScope.launch {
createRepository.getExamplePrompts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class DefineFragment() : BaseFragment<FragmentDefineBinding>(R.layout.fragment_d
private fun initView() {
binding.vm = viewModel
initOnBackPressedListener(binding.root)
viewModel.getExamplePromptsFromServer()
}

private fun initCreateBtnListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package kr.genti.presentation.main.feed
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.core.base.BaseFragment
import kr.genti.core.extension.initOnBackPressedListener
import kr.genti.core.extension.setStatusBarColor
import kr.genti.core.extension.stringOf
import kr.genti.core.extension.toast
import kr.genti.core.state.UiState
import kr.genti.presentation.R
import kr.genti.presentation.databinding.FragmentFeedBinding
import kotlin.math.max
Expand All @@ -28,8 +35,8 @@ class FeedFragment() : BaseFragment<FragmentFeedBinding>(R.layout.fragment_feed)

initView()
initAdapter()
setItemList()
setLightningVisibility()
observeGetExampleItemsState()
}

private fun initView() {
Expand All @@ -49,10 +56,6 @@ class FeedFragment() : BaseFragment<FragmentFeedBinding>(R.layout.fragment_feed)
// TODO: 링크 이동
}

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

private fun setLightningVisibility() {
with(binding) {
rvFeed.addOnScrollListener(
Expand All @@ -66,13 +69,24 @@ class FeedFragment() : BaseFragment<FragmentFeedBinding>(R.layout.fragment_feed)
) {
super.onScrolled(recyclerView, dx, dy)
accumScrollY += dy
ivFeedLightning.alpha = max(0.0, (1 - accumScrollY / 130f).toDouble()).toFloat()
ivFeedLightning.alpha =
max(0.0, (1 - accumScrollY / 130f).toDouble()).toFloat()
}
},
)
}
}

private fun observeGetExampleItemsState() {
viewModel.getExampleItemsState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.addItemList(state.data)
is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}.launchIn(lifecycleScope)
}

override fun onDestroyView() {
super.onDestroyView()
_adapter = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
package kr.genti.presentation.main.feed

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kr.genti.core.state.UiState
import kr.genti.domain.entity.response.FeedItemModel
import kr.genti.domain.repository.FeedRepository
import javax.inject.Inject

@HiltViewModel
class FeedViewModel
@Inject
constructor(
// private val profileRepository: ProfileRepository,
private val feedRepository: FeedRepository,
) : ViewModel() {
val mockItemList =
listOf(
FeedItemModel(
0,
"https://github.com/Genti2024/Genti-Android/assets/97405341/68bf3348-f732-4874-947d-891f312b241e",
"프랑스 야경을 즐기는 모습을 그려주세요. 항공점퍼를 입고 테라스에 서 있는 모습이에요.",
),
FeedItemModel(
1,
"https://github.com/Genti2024/Genti-Android/assets/97405341/0eb2d7f2-90d2-436a-aa53-4ad7a414d805",
"프랑스 야경을 즐기는 모습을 그려주세요. 항공점퍼를 입고 테라스에 서 있는 모습이에요.",
),
FeedItemModel(
2,
"https://github.com/Genti2024/Genti-Android/assets/97405341/0eb2d7f2-90d2-436a-aa53-4ad7a414d805",
"프랑스 야경을 즐기는 모습을 그려주세요. 항공점퍼를 입고 테라스에 서 있는 모습이에요.",
),
)
private val _getExampleItemsState =
MutableStateFlow<UiState<List<FeedItemModel>>>(UiState.Empty)
val getExampleItemsState: StateFlow<UiState<List<FeedItemModel>>> = _getExampleItemsState

init {
getExamplePromptsFromServer()
}

private fun getExamplePromptsFromServer() {
viewModelScope.launch {
_getExampleItemsState.value = UiState.Loading
feedRepository.getExampleItems()
.onSuccess {
_getExampleItemsState.value = UiState.Success(it)
}
.onFailure { t ->
_getExampleItemsState.value = UiState.Failure(t.message.toString())
}
}
}
}

0 comments on commit 9adc693

Please sign in to comment.