feat(creator): 후원 탭 화면을 구현한다
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.donation
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.base.BaseFragment
|
||||
import kr.co.vividnext.sodalive.databinding.FragmentCreatorChannelDonationBinding
|
||||
import kr.co.vividnext.sodalive.extensions.moneyFormat
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.ui.CreatorChannelDonationAdapter
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
|
||||
class CreatorChannelDonationFragment : BaseFragment<FragmentCreatorChannelDonationBinding>(
|
||||
FragmentCreatorChannelDonationBinding::inflate
|
||||
) {
|
||||
|
||||
private val viewModel: CreatorChannelDonationViewModel by viewModel()
|
||||
private val donationAdapter = CreatorChannelDonationAdapter(
|
||||
onRankingAllClick = { host.onCreatorChannelDonationRankingAllClicked() }
|
||||
)
|
||||
private var lastContentLayoutKey: CreatorChannelDonationContentLayoutKey? = null
|
||||
private val creatorId: Long by lazy { arguments?.getLong(ARG_CREATOR_ID) ?: 0L }
|
||||
private val host: Host
|
||||
get() = requireActivity() as Host
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
bindLoading()
|
||||
setupDonationList()
|
||||
setupClickListeners()
|
||||
observeViewModel()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
lastContentLayoutKey = null
|
||||
binding.rvCreatorChannelDonation.adapter = null
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
fun onCreatorChannelDonationTabSelected() {
|
||||
if (creatorId > 0L) {
|
||||
viewModel.loadDonations(creatorId, isOwner = host.isCreatorChannelOwner())
|
||||
}
|
||||
}
|
||||
|
||||
fun onCreatorChannelDonationScrolledToBottom() {
|
||||
viewModel.loadMore()
|
||||
}
|
||||
|
||||
fun onCreatorChannelDonationRefreshRequested() {
|
||||
viewModel.refreshDonations()
|
||||
}
|
||||
|
||||
fun onCreatorChannelDonationViewportHeightChanged(minHeight: Int) {
|
||||
binding.root.minimumHeight = minHeight
|
||||
}
|
||||
|
||||
private fun setupDonationList() = with(binding.rvCreatorChannelDonation) {
|
||||
layoutManager = LinearLayoutManager(requireContext())
|
||||
adapter = donationAdapter
|
||||
}
|
||||
|
||||
private fun setupClickListeners() = with(binding) {
|
||||
btnCreatorChannelDonationRetry.setOnClickListener {
|
||||
viewModel.retryDonations()
|
||||
}
|
||||
btnCreatorChannelDonationWrite.setOnClickListener {
|
||||
host.onCreatorChannelDonationRequested { can, isSecret, message ->
|
||||
viewModel.postChannelDonation(can, isSecret, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeViewModel() {
|
||||
viewModel.donationStateLiveData.observe(viewLifecycleOwner) { state ->
|
||||
when (state) {
|
||||
CreatorChannelDonationUiState.Loading -> bindLoading()
|
||||
is CreatorChannelDonationUiState.Empty -> bindEmpty(state)
|
||||
is CreatorChannelDonationUiState.Error -> bindError(state)
|
||||
is CreatorChannelDonationUiState.Content -> bindContent(state)
|
||||
}
|
||||
handleDonationSuccessEvent()
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindLoading() = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelDonationCountBar.isVisible = false
|
||||
rvCreatorChannelDonation.isVisible = false
|
||||
layoutCreatorChannelDonationEmpty.isVisible = false
|
||||
tvCreatorChannelDonationErrorMessage.isVisible = false
|
||||
btnCreatorChannelDonationRetry.isVisible = false
|
||||
btnCreatorChannelDonationWrite.isVisible = false
|
||||
}
|
||||
|
||||
private fun bindEmpty(state: CreatorChannelDonationUiState.Empty) = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelDonationCountBar.isVisible = false
|
||||
rvCreatorChannelDonation.isVisible = false
|
||||
layoutCreatorChannelDonationEmpty.isVisible = true
|
||||
tvCreatorChannelDonationEmptyMessage.setText(
|
||||
if (state.isOwner) {
|
||||
R.string.creator_channel_donation_empty_owner_title
|
||||
} else {
|
||||
R.string.creator_channel_donation_empty_title
|
||||
}
|
||||
)
|
||||
tvCreatorChannelDonationErrorMessage.isVisible = false
|
||||
btnCreatorChannelDonationRetry.isVisible = false
|
||||
btnCreatorChannelDonationWrite.isVisible = false
|
||||
host.onCreatorChannelDonationContentChanged()
|
||||
}
|
||||
|
||||
private fun bindError(state: CreatorChannelDonationUiState.Error) = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelDonationCountBar.isVisible = false
|
||||
rvCreatorChannelDonation.isVisible = false
|
||||
layoutCreatorChannelDonationEmpty.isVisible = false
|
||||
tvCreatorChannelDonationErrorMessage.isVisible = true
|
||||
tvCreatorChannelDonationErrorMessage.text = state.message ?: getString(R.string.creator_channel_donation_error_message)
|
||||
btnCreatorChannelDonationRetry.isVisible = true
|
||||
btnCreatorChannelDonationWrite.isVisible = false
|
||||
host.onCreatorChannelDonationContentChanged()
|
||||
}
|
||||
|
||||
private fun bindContent(state: CreatorChannelDonationUiState.Content) = with(binding) {
|
||||
layoutCreatorChannelDonationCountBar.isVisible = true
|
||||
tvCreatorChannelDonationTotalCount.text = state.donationCount.moneyFormat()
|
||||
rvCreatorChannelDonation.isVisible = true
|
||||
donationAdapter.submitItems(state.rankings, state.donations)
|
||||
layoutCreatorChannelDonationEmpty.isVisible = false
|
||||
tvCreatorChannelDonationErrorMessage.isVisible = false
|
||||
btnCreatorChannelDonationRetry.isVisible = false
|
||||
btnCreatorChannelDonationWrite.isVisible = !state.isOwner
|
||||
notifyContentChangedIfLayoutChanged(state)
|
||||
state.paginationErrorMessage?.let {
|
||||
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
|
||||
viewModel.consumePaginationErrorMessage()
|
||||
}
|
||||
state.actionToastMessage?.let {
|
||||
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
|
||||
viewModel.consumeActionToastMessage()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleDonationSuccessEvent() {
|
||||
if (viewModel.consumeDonationSuccessEvent()) {
|
||||
host.onCreatorChannelDonationCompleted()
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyContentChangedIfLayoutChanged(state: CreatorChannelDonationUiState.Content) {
|
||||
val contentLayoutKey = state.toContentLayoutKey()
|
||||
if (contentLayoutKey == lastContentLayoutKey) return
|
||||
|
||||
lastContentLayoutKey = contentLayoutKey
|
||||
host.onCreatorChannelDonationContentChanged()
|
||||
}
|
||||
|
||||
interface Host {
|
||||
fun isCreatorChannelOwner(): Boolean
|
||||
fun onCreatorChannelDonationContentChanged()
|
||||
fun onCreatorChannelDonationRequested(onSubmit: (can: Int, isSecret: Boolean, message: String) -> Unit)
|
||||
fun onCreatorChannelDonationRankingAllClicked()
|
||||
fun onCreatorChannelDonationCompleted()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARG_CREATOR_ID: String = "arg_creator_id"
|
||||
|
||||
fun newInstance(creatorId: Long): CreatorChannelDonationFragment {
|
||||
return CreatorChannelDonationFragment().apply {
|
||||
arguments = Bundle().apply { putLong(ARG_CREATOR_ID, creatorId) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class CreatorChannelDonationContentLayoutKey(
|
||||
val donationCount: Int,
|
||||
val rankingUserIds: List<Long>,
|
||||
val donationItems: List<CreatorChannelDonationItemLayoutKey>
|
||||
)
|
||||
|
||||
private data class CreatorChannelDonationItemLayoutKey(
|
||||
val nickname: String,
|
||||
val can: Int,
|
||||
val message: String,
|
||||
val createdAtText: String
|
||||
)
|
||||
|
||||
private fun CreatorChannelDonationUiState.Content.toContentLayoutKey(): CreatorChannelDonationContentLayoutKey {
|
||||
return CreatorChannelDonationContentLayoutKey(
|
||||
donationCount = donationCount,
|
||||
rankingUserIds = rankings.map { it.userId },
|
||||
donationItems = donations.map { donation ->
|
||||
CreatorChannelDonationItemLayoutKey(
|
||||
nickname = donation.nickname,
|
||||
can = donation.can,
|
||||
message = donation.message,
|
||||
createdAtText = donation.createdAtText
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user