Skip to content

Commit

Permalink
[FIX/#157] 프로필뷰 생성 사진 추가 버튼 API 고려
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Oct 29, 2024
1 parent 214edfb commit 534caab
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import kr.genti.core.extension.stringOf
import kr.genti.core.extension.toast
import kr.genti.core.state.UiState
import kr.genti.domain.entity.response.ImageModel
import kr.genti.domain.enums.GenerateStatus
import kr.genti.presentation.R
import kr.genti.presentation.create.CreateActivity
import kr.genti.presentation.databinding.FragmentProfileBinding
import kr.genti.presentation.generate.waiting.WaitingActivity
import kr.genti.presentation.main.CreateErrorDialog
import kr.genti.presentation.main.CreateFinishedDialog
import kr.genti.presentation.main.CreateUnableDialog
import kr.genti.presentation.setting.SettingActivity
import kr.genti.presentation.util.AmplitudeManager

Expand All @@ -34,6 +39,9 @@ class ProfileFragment() : BaseFragment<FragmentProfileBinding>(R.layout.fragment
private val viewModel by activityViewModels<ProfileViewModel>()

private var profileImageDialog: ProfileImageDialog? = null
private var createFinishedDialog: CreateFinishedDialog? = null
private var createErrorDialog: CreateErrorDialog? = null
private var createUnableDialog: CreateUnableDialog? = null

override fun onViewCreated(
view: View,
Expand All @@ -47,6 +55,7 @@ class ProfileFragment() : BaseFragment<FragmentProfileBinding>(R.layout.fragment
setListWithInfinityScroll()
observeGenerateStatus()
observePictureListPageState()
observeServerAvailableState()
}

private fun initView() {
Expand Down Expand Up @@ -78,7 +87,27 @@ class ProfileFragment() : BaseFragment<FragmentProfileBinding>(R.layout.fragment
}

private fun initMoveClickListener(x: Boolean) {
startActivity(Intent(requireActivity(), CreateActivity::class.java))
when (viewModel.currentStatus) {
GenerateStatus.NEW_REQUEST_AVAILABLE -> {
viewModel.getIsServerAvailable()
}

GenerateStatus.AWAIT_USER_VERIFICATION -> {
createFinishedDialog = CreateFinishedDialog()
createFinishedDialog?.show(parentFragmentManager, DIALOG_FINISHED)
}

GenerateStatus.IN_PROGRESS -> {
startActivity(Intent(requireActivity(), WaitingActivity::class.java))
}

GenerateStatus.CANCELED -> {
createErrorDialog = CreateErrorDialog()
createErrorDialog?.show(parentFragmentManager, DIALOG_ERROR)
}

GenerateStatus.EMPTY -> return
}
}

private fun setListWithInfinityScroll() {
Expand Down Expand Up @@ -153,13 +182,41 @@ class ProfileFragment() : BaseFragment<FragmentProfileBinding>(R.layout.fragment
}
}

private fun observeServerAvailableState() {
viewModel.serverAvailableState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> {
if (state.data.status) {
AmplitudeManager.trackEvent("click_createpictab")
startActivity(Intent(requireActivity(), CreateActivity::class.java))
} else {
createUnableDialog =
CreateUnableDialog.newInstance(state.data.message.orEmpty())
createUnableDialog?.show(parentFragmentManager, DIALOG_UNABLE)
}
}

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

override fun onDestroyView() {
super.onDestroyView()
_adapter = null
profileImageDialog = null
createFinishedDialog = null
createErrorDialog = null
createUnableDialog = null
}

companion object {
private const val IMAGE_VIEWER = "IMAGE_VIEWER"

private const val DIALOG_FINISHED = "DIALOG_FINISHED"
private const val DIALOG_ERROR = "DIALOG_ERROR"
private const val DIALOG_UNABLE = "DIALOG_UNABLE"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,92 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kr.genti.core.state.UiState
import kr.genti.domain.entity.response.PicturePagedListModel
import kr.genti.domain.entity.response.ServerAvailableModel
import kr.genti.domain.enums.GenerateStatus
import kr.genti.domain.repository.GenerateRepository
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel
@Inject
constructor(
private val generateRepository: GenerateRepository,
) : ViewModel() {
private val _getGenerateStatusState =
MutableStateFlow<UiState<Boolean>>(UiState.Empty)
val getGenerateStatusState: StateFlow<UiState<Boolean>> = _getGenerateStatusState
@Inject
constructor(
private val generateRepository: GenerateRepository,
) : ViewModel() {
private val _getGenerateStatusState =
MutableStateFlow<UiState<Boolean>>(UiState.Empty)
val getGenerateStatusState: StateFlow<UiState<Boolean>> = _getGenerateStatusState

private val _getPictureListState =
MutableStateFlow<UiState<PicturePagedListModel>>(UiState.Loading)
val getPictureListState: StateFlow<UiState<PicturePagedListModel>> = _getPictureListState
var currentStatus: GenerateStatus = GenerateStatus.NEW_REQUEST_AVAILABLE

private var currentPage = -1
private var isPagingFinish = false
private var totalPage = Int.MAX_VALUE
private val _serverAvailableState =
MutableStateFlow<UiState<ServerAvailableModel>>(UiState.Empty)
val serverAvailableState: StateFlow<UiState<ServerAvailableModel>> = _serverAvailableState

var isFirstLoading = true
private val _getPictureListState =
MutableStateFlow<UiState<PicturePagedListModel>>(UiState.Loading)
val getPictureListState: StateFlow<UiState<PicturePagedListModel>> = _getPictureListState

fun getGenerateStatusFromServer() {
viewModelScope.launch {
_getGenerateStatusState.value = UiState.Loading
generateRepository.getGenerateStatus()
.onSuccess {
_getGenerateStatusState.value =
UiState.Success(it.status == GenerateStatus.IN_PROGRESS)
}
.onFailure { t ->
_getGenerateStatusState.value = UiState.Failure(t.message.toString())
}
}
private var currentPage = -1
private var isPagingFinish = false
private var totalPage = Int.MAX_VALUE

var isFirstLoading = true

fun getGenerateStatusFromServer() {
viewModelScope.launch {
_getGenerateStatusState.value = UiState.Loading
generateRepository.getGenerateStatus()
.onSuccess {
currentStatus = it.status
_getGenerateStatusState.value =
UiState.Success(it.status == GenerateStatus.IN_PROGRESS)

}
.onFailure { t ->
_getGenerateStatusState.value = UiState.Failure(t.message.toString())
}
}
}

fun getIsServerAvailable() {
_serverAvailableState.value = UiState.Loading
viewModelScope.launch {
generateRepository
.getIsServerAvailable()
.onSuccess {
_serverAvailableState.value = UiState.Success(it)
}.onFailure {
_serverAvailableState.value = UiState.Failure(it.message.orEmpty())
}
}
}

fun resetIsServerAvailable() {
_serverAvailableState.value = UiState.Empty
}

fun getPictureListFromServer() {
viewModelScope.launch {
if (isPagingFinish) return@launch
_getPictureListState.value = UiState.Loading
generateRepository.getGeneratedPictureList(
++currentPage,
10,
null,
null,
)
.onSuccess {
totalPage = it.totalPages - 1
if (totalPage == currentPage) isPagingFinish = true
if (it.content.isEmpty()) {
_getPictureListState.value = UiState.Empty
return@launch
}
if (isFirstLoading) isFirstLoading = false
_getPictureListState.value = UiState.Success(it)
}.onFailure {
_getPictureListState.value = UiState.Failure(it.message.toString())
fun getPictureListFromServer() {
viewModelScope.launch {
if (isPagingFinish) return@launch
_getPictureListState.value = UiState.Loading
generateRepository.getGeneratedPictureList(
++currentPage,
10,
null,
null,
)
.onSuccess {
totalPage = it.totalPages - 1
if (totalPage == currentPage) isPagingFinish = true
if (it.content.isEmpty()) {
_getPictureListState.value = UiState.Empty
return@launch
}
}
if (isFirstLoading) isFirstLoading = false
_getPictureListState.value = UiState.Success(it)
}.onFailure {
_getPictureListState.value = UiState.Failure(it.message.toString())
}
}
}
}

0 comments on commit 534caab

Please sign in to comment.