Skip to content
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

[feat] 개최 프로세스 - 유저 정보 불러오기 API 연동 #116

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.sopt.pingle.domain.model.PlanLocationEntity
import org.sopt.pingle.domain.model.PlanMeetingEntity
import org.sopt.pingle.domain.model.UserInfoEntity
import org.sopt.pingle.domain.usecase.GetPlanLocationListUseCase
import org.sopt.pingle.domain.usecase.GetUserInfoUseCase
import org.sopt.pingle.domain.usecase.PostPlanMeetingUseCase
import org.sopt.pingle.presentation.type.CategoryType
import org.sopt.pingle.presentation.type.PlanType
Expand All @@ -26,7 +28,8 @@ import org.sopt.pingle.util.view.UiState
@HiltViewModel
class PlanViewModel @Inject constructor(
private val getPlanLocationListUseCase: GetPlanLocationListUseCase,
private val postPlanMeetingUseCase: PostPlanMeetingUseCase
private val postPlanMeetingUseCase: PostPlanMeetingUseCase,
private val getUserInfoUseCase: GetUserInfoUseCase
) : ViewModel() {
private val _currentPage = MutableStateFlow(FIRST_PAGE_POSITION)
val currentPage get() = _currentPage.asStateFlow()
Expand Down Expand Up @@ -61,6 +64,9 @@ class PlanViewModel @Inject constructor(
private val _planMeetingState = MutableSharedFlow<UiState<Unit?>>()
val planMeetingState get() = _planMeetingState.asSharedFlow()

private val _userInfoState = MutableStateFlow<UiState<UserInfoEntity>>(UiState.Empty)
val userInfoState get() = _userInfoState.asStateFlow()

val isPlanBtnEnabled: StateFlow<Boolean> = listOf(
currentPage,
selectedCategory,
Expand Down Expand Up @@ -220,6 +226,19 @@ class PlanViewModel @Inject constructor(
}
}

fun getUserInfo() {
viewModelScope.launch {
_userInfoState.value = UiState.Loading
runCatching {
getUserInfoUseCase().collect() { userInfo ->
_userInfoState.value = UiState.Success(userInfo)
}
}.onFailure { exception: Throwable ->
_userInfoState.value = UiState.Error(exception.message)
}
}
}

companion object {
const val FIRST_PAGE_POSITION = 0
const val DEFAULT_OLD_POSITION = -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package org.sopt.pingle.presentation.ui.main.plan.plansummaryconfirmation
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.sopt.pingle.R
import org.sopt.pingle.databinding.FragmentPlanSummaryConfirmationBinding
import org.sopt.pingle.presentation.ui.main.plan.PlanViewModel
import org.sopt.pingle.util.base.BindingFragment
import org.sopt.pingle.util.fragment.colorOf
import org.sopt.pingle.util.view.UiState

@AndroidEntryPoint
class PlanSummaryConfirmationFragment :
Expand All @@ -18,30 +23,45 @@ class PlanSummaryConfirmationFragment :
super.onViewCreated(view, savedInstanceState)

initLayout()
collectData()
}

private fun initLayout() {
viewModel.selectedCategory.value?.let { category ->
with(binding) {
viewModel.getUserInfo()

with(binding) {
viewModel.selectedCategory.value?.let { category ->
badgePlanSummaryConfirmationCategory.setBadgeCategoryType(category)
tvPlanSummaryConfirmationName.setTextColor(colorOf((category.textColor)))
tvPlanSummaryConfirmationName.text = viewModel.planTitle.value
// TODO API연결해서 개최자명 넣기
tvPlanSummaryConfirmationOwnerName.text = "개최자"
tvPlanSummaryConfirmationCalenderDetail.text =
convertDateFormat(viewModel.planDate.value) + "\n" + convertTimeFormat(viewModel.startTime.value) + " ~ " + convertTimeFormat(
viewModel.endTime.value
)
tvPlanSummaryConfirmationMapDetail.text = viewModel.selectedLocation.value?.location
tvPlanSummaryConfirmationRecruitmentDetail.text = getString(
R.string.plan_summary_confirmation_recruitment_number,
viewModel.selectedRecruitment.value
)
}
tvPlanSummaryConfirmationName.text = viewModel.planTitle.value
tvPlanSummaryConfirmationCalenderDetail.text =
convertDateFormat(viewModel.planDate.value) + "\n" + convertTimeFormat(viewModel.startTime.value) + " ~ " + convertTimeFormat(
viewModel.endTime.value
)
tvPlanSummaryConfirmationMapDetail.text = viewModel.selectedLocation.value?.location
tvPlanSummaryConfirmationRecruitmentDetail.text = getString(
R.string.plan_summary_confirmation_recruitment_number,
viewModel.selectedRecruitment.value
)
}
}

private fun convertTimeFormat(time: String): String = time.substring(TIME_START_INDEX, TIME_END_INDEX)
private fun collectData() {
viewModel.userInfoState.flowWithLifecycle(lifecycle).onEach { userInfoState ->
when (userInfoState) {
is UiState.Success ->
binding.tvPlanSummaryConfirmationOwnerName.text =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 위에 왜 없나 뉸 크게 뜨고 찾고 있었는디 ㅋㅋ
UiState Success인 여기에 있었군요 쩝

userInfoState.data.name

else -> Unit
}
}.launchIn(lifecycleScope)
}

private fun convertTimeFormat(time: String): String =
time.substring(TIME_START_INDEX, TIME_END_INDEX)

private fun convertDateFormat(date: String): String {
val year = date.substring(YEAR_START_INDEX, YEAR_END_INDEX)
val month = date.substring(MONTH_START_INDEX, MONTH_END_INDEX)
Expand Down
Loading