feat(creator): 라이브 탭 레이아웃을 추가한다
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.live
|
||||
|
||||
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.FragmentCreatorChannelLiveBinding
|
||||
import kr.co.vividnext.sodalive.extensions.moneyFormat
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelLiveResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.model.toLabelResId
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.model.toReplayUiModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.ui.CreatorChannelLiveReplayAdapter
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.ui.formatCreatorChannelLiveDateTime
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
|
||||
class CreatorChannelLiveFragment : BaseFragment<FragmentCreatorChannelLiveBinding>(
|
||||
FragmentCreatorChannelLiveBinding::inflate
|
||||
) {
|
||||
|
||||
private val viewModel: CreatorChannelLiveViewModel by viewModel()
|
||||
private val replayAdapter = CreatorChannelLiveReplayAdapter { item ->
|
||||
host.onCreatorChannelLiveReplayClicked(item.audioContentId)
|
||||
}
|
||||
private var lastContentLayoutKey: CreatorChannelLiveContentLayoutKey? = 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()
|
||||
setupReplayList()
|
||||
setupClickListeners()
|
||||
observeViewModel()
|
||||
}
|
||||
|
||||
fun onCreatorChannelLiveTabSelected() {
|
||||
if (creatorId > 0L) {
|
||||
viewModel.loadLive(creatorId)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
binding.rvCreatorChannelLiveReplays.adapter = null
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
private fun setupReplayList() = with(binding.rvCreatorChannelLiveReplays) {
|
||||
layoutManager = LinearLayoutManager(requireContext())
|
||||
adapter = replayAdapter
|
||||
}
|
||||
|
||||
fun onCreatorChannelLiveScrolledToBottom() {
|
||||
viewModel.loadMore()
|
||||
}
|
||||
|
||||
private fun setupClickListeners() {
|
||||
binding.ivCreatorChannelLiveSort.setImageResource(R.drawable.ic_new_sort)
|
||||
binding.btnCreatorChannelLiveRetry.setOnClickListener {
|
||||
viewModel.retryLive()
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeViewModel() {
|
||||
viewModel.liveStateLiveData.observe(viewLifecycleOwner) { state ->
|
||||
when (state) {
|
||||
CreatorChannelLiveUiState.Loading -> bindLoading()
|
||||
CreatorChannelLiveUiState.Empty -> bindEmpty()
|
||||
is CreatorChannelLiveUiState.Error -> bindError(state)
|
||||
is CreatorChannelLiveUiState.Content -> bindContent(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindLoading() = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelLiveSortBar.isVisible = false
|
||||
layoutCreatorChannelLiveCurrentCard.isVisible = false
|
||||
rvCreatorChannelLiveReplays.isVisible = false
|
||||
tvCreatorChannelLiveEmptyMessage.isVisible = false
|
||||
tvCreatorChannelLiveErrorMessage.isVisible = false
|
||||
btnCreatorChannelLiveRetry.isVisible = false
|
||||
}
|
||||
|
||||
private fun bindEmpty() = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelLiveSortBar.isVisible = false
|
||||
layoutCreatorChannelLiveCurrentCard.isVisible = false
|
||||
rvCreatorChannelLiveReplays.isVisible = false
|
||||
tvCreatorChannelLiveEmptyMessage.isVisible = true
|
||||
tvCreatorChannelLiveErrorMessage.isVisible = false
|
||||
btnCreatorChannelLiveRetry.isVisible = false
|
||||
host.onCreatorChannelLiveContentChanged()
|
||||
}
|
||||
|
||||
private fun bindError(state: CreatorChannelLiveUiState.Error) = with(binding) {
|
||||
lastContentLayoutKey = null
|
||||
layoutCreatorChannelLiveSortBar.isVisible = false
|
||||
layoutCreatorChannelLiveCurrentCard.isVisible = false
|
||||
rvCreatorChannelLiveReplays.isVisible = false
|
||||
tvCreatorChannelLiveEmptyMessage.isVisible = false
|
||||
tvCreatorChannelLiveErrorMessage.isVisible = true
|
||||
tvCreatorChannelLiveErrorMessage.text = state.message ?: getString(R.string.creator_channel_live_error_message)
|
||||
btnCreatorChannelLiveRetry.isVisible = true
|
||||
host.onCreatorChannelLiveContentChanged()
|
||||
}
|
||||
|
||||
private fun bindContent(state: CreatorChannelLiveUiState.Content) = with(binding) {
|
||||
tvCreatorChannelLiveEmptyMessage.isVisible = false
|
||||
tvCreatorChannelLiveErrorMessage.isVisible = false
|
||||
btnCreatorChannelLiveRetry.isVisible = false
|
||||
layoutCreatorChannelLiveSortBar.isVisible = true
|
||||
tvCreatorChannelLiveTotalCount.text = state.liveReplayContentCount.moneyFormat()
|
||||
tvCreatorChannelLiveSortLabel.setText(state.selectedSort.toLabelResId())
|
||||
bindCurrentLive(state.currentLive)
|
||||
rvCreatorChannelLiveReplays.isVisible = true
|
||||
replayAdapter.submitItems(state.liveReplayContents.map { it.toReplayUiModel() })
|
||||
notifyContentChangedIfLayoutChanged(state)
|
||||
state.paginationErrorMessage?.let {
|
||||
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
|
||||
viewModel.consumePaginationErrorMessage()
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyContentChangedIfLayoutChanged(state: CreatorChannelLiveUiState.Content) {
|
||||
val contentLayoutKey = state.toContentLayoutKey()
|
||||
if (contentLayoutKey == lastContentLayoutKey) return
|
||||
|
||||
lastContentLayoutKey = contentLayoutKey
|
||||
host.onCreatorChannelLiveContentChanged()
|
||||
}
|
||||
|
||||
private fun bindCurrentLive(live: CreatorChannelLiveResponse?) = with(binding) {
|
||||
layoutCreatorChannelLiveCurrentCard.isVisible = live != null
|
||||
if (live == null) return@with
|
||||
|
||||
tvCreatorChannelLiveCurrentTitle.text = live.title
|
||||
tvCreatorChannelLiveCurrentTime.text = formatCreatorChannelLiveDateTime(live.beginDateTimeUtc)
|
||||
tvCreatorChannelLiveCurrentPrice.text = if (live.price > 0) {
|
||||
live.price.moneyFormat()
|
||||
} else {
|
||||
getString(R.string.audio_content_tag_free)
|
||||
}
|
||||
layoutCreatorChannelLiveCurrentPrice.isVisible = true
|
||||
ivCreatorChannelLiveCurrentPriceCash.isVisible = live.price > 0
|
||||
layoutCreatorChannelLiveCurrentCard.setOnClickListener {
|
||||
host.onCreatorChannelCurrentLiveClicked(live)
|
||||
}
|
||||
}
|
||||
|
||||
interface Host {
|
||||
fun onCreatorChannelCurrentLiveClicked(live: CreatorChannelLiveResponse)
|
||||
fun onCreatorChannelLiveReplayClicked(audioContentId: Long)
|
||||
fun onCreatorChannelLiveContentChanged()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARG_CREATOR_ID: String = "arg_creator_id"
|
||||
|
||||
fun newInstance(creatorId: Long): CreatorChannelLiveFragment {
|
||||
return CreatorChannelLiveFragment().apply {
|
||||
arguments = Bundle().apply { putLong(ARG_CREATOR_ID, creatorId) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class CreatorChannelLiveContentLayoutKey(
|
||||
val liveReplayContentCount: Int,
|
||||
val currentLive: CreatorChannelLiveResponse?,
|
||||
val liveReplayContentIds: List<Long>
|
||||
)
|
||||
|
||||
private fun CreatorChannelLiveUiState.Content.toContentLayoutKey(): CreatorChannelLiveContentLayoutKey {
|
||||
return CreatorChannelLiveContentLayoutKey(
|
||||
liveReplayContentCount = liveReplayContentCount,
|
||||
currentLive = currentLive,
|
||||
liveReplayContentIds = liveReplayContents.map { it.audioContentId }
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.live.ui
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.databinding.ItemCreatorChannelLiveReplayBinding
|
||||
import kr.co.vividnext.sodalive.extensions.loadUrl
|
||||
import kr.co.vividnext.sodalive.extensions.moneyFormat
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.model.CreatorChannelLiveReplayStatus
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.model.CreatorChannelLiveReplayUiModel
|
||||
import kr.co.vividnext.sodalive.v2.widget.AudioContentTag
|
||||
|
||||
class CreatorChannelLiveReplayAdapter(
|
||||
private val onReplayClick: (CreatorChannelLiveReplayUiModel) -> Unit = {}
|
||||
) : RecyclerView.Adapter<CreatorChannelLiveReplayAdapter.ViewHolder>() {
|
||||
|
||||
private var items: List<CreatorChannelLiveReplayUiModel> = emptyList()
|
||||
|
||||
fun submitItems(items: List<CreatorChannelLiveReplayUiModel>) {
|
||||
this.items = items
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
return ViewHolder(
|
||||
ItemCreatorChannelLiveReplayBinding.inflate(LayoutInflater.from(parent.context), parent, false),
|
||||
onReplayClick
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
holder.bind(items[position])
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = items.size
|
||||
|
||||
class ViewHolder(
|
||||
private val binding: ItemCreatorChannelLiveReplayBinding,
|
||||
private val onReplayClick: (CreatorChannelLiveReplayUiModel) -> Unit
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(item: CreatorChannelLiveReplayUiModel) = with(binding) {
|
||||
ivCreatorChannelLiveReplayThumbnail.loadUrl(item.imageUrl)
|
||||
tvCreatorChannelLiveReplayTitle.text = item.title
|
||||
tvCreatorChannelLiveReplayDuration.text = item.secondaryText.orEmpty()
|
||||
tvCreatorChannelLiveReplayDuration.isVisible = !item.secondaryText.isNullOrBlank()
|
||||
ivCreatorChannelLiveReplayAdultBadge.setImageResource(R.drawable.ic_new_shield_small)
|
||||
ivCreatorChannelLiveReplayAdultBadge.isVisible = item.showAdultBadge
|
||||
bindTag(ivCreatorChannelLiveReplayOriginalTag, AudioContentTag.Original, item.tags)
|
||||
bindTag(ivCreatorChannelLiveReplayFirstTag, AudioContentTag.First, item.tags)
|
||||
bindTag(ivCreatorChannelLiveReplayPointTag, AudioContentTag.Point, item.tags)
|
||||
tvCreatorChannelLiveReplayFreeTag.isVisible = AudioContentTag.Free in item.tags
|
||||
bindStatus(item.status)
|
||||
root.setOnClickListener { onReplayClick(item) }
|
||||
}
|
||||
|
||||
private fun bindTag(view: View, tag: AudioContentTag, tags: Set<AudioContentTag>) {
|
||||
view.isVisible = tag in tags
|
||||
}
|
||||
|
||||
private fun bindStatus(status: CreatorChannelLiveReplayStatus) = with(binding) {
|
||||
ivCreatorChannelLiveReplayPlay.setImageResource(R.drawable.ic_new_player_play)
|
||||
when (status) {
|
||||
CreatorChannelLiveReplayStatus.Play -> {
|
||||
ivCreatorChannelLiveReplayPlay.isVisible = true
|
||||
ivCreatorChannelLiveReplayCan.isVisible = false
|
||||
layoutCreatorChannelLiveReplayActionText.isVisible = false
|
||||
}
|
||||
CreatorChannelLiveReplayStatus.Owned -> bindTextStatus(R.string.audio_content_badge_owned)
|
||||
CreatorChannelLiveReplayStatus.Rented -> bindTextStatus(R.string.audio_content_badge_rented)
|
||||
is CreatorChannelLiveReplayStatus.Price -> {
|
||||
ivCreatorChannelLiveReplayPlay.isVisible = false
|
||||
layoutCreatorChannelLiveReplayActionText.isVisible = true
|
||||
ivCreatorChannelLiveReplayCan.isVisible = true
|
||||
tvCreatorChannelLiveReplayActionText.text = status.price.moneyFormat()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindTextStatus(textResId: Int) = with(binding) {
|
||||
ivCreatorChannelLiveReplayPlay.isVisible = true
|
||||
layoutCreatorChannelLiveReplayActionText.isVisible = true
|
||||
ivCreatorChannelLiveReplayCan.isVisible = false
|
||||
tvCreatorChannelLiveReplayActionText.setText(textResId)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user