오디션 탭 추가
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface AuditionApi {
|
||||
@GET("/audition")
|
||||
fun getAuditionList(
|
||||
@Query("page") page: Int,
|
||||
@Query("size") size: Int,
|
||||
@Header("Authorization") authHeader: String
|
||||
): Single<ApiResponse<GetAuditionListResponse>>
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import android.graphics.Rect
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kr.co.vividnext.sodalive.base.BaseFragment
|
||||
import kr.co.vividnext.sodalive.common.LoadingDialog
|
||||
import kr.co.vividnext.sodalive.databinding.FragmentAuditionBinding
|
||||
import kr.co.vividnext.sodalive.extensions.dpToPx
|
||||
import org.koin.android.ext.android.inject
|
||||
|
||||
class AuditionFragment : BaseFragment<FragmentAuditionBinding>(
|
||||
FragmentAuditionBinding::inflate
|
||||
) {
|
||||
private val viewModel: AuditionViewModel by inject()
|
||||
|
||||
private lateinit var adapter: AuditionListAdapter
|
||||
private lateinit var loadingDialog: LoadingDialog
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
setupView()
|
||||
bindData()
|
||||
|
||||
viewModel.getAuditionList()
|
||||
}
|
||||
|
||||
private fun setupView() {
|
||||
loadingDialog = LoadingDialog(requireActivity(), layoutInflater)
|
||||
|
||||
val recyclerView = binding.rvAudition
|
||||
adapter = AuditionListAdapter { }
|
||||
|
||||
recyclerView.layoutManager = LinearLayoutManager(
|
||||
requireContext(),
|
||||
LinearLayoutManager.VERTICAL,
|
||||
false
|
||||
)
|
||||
|
||||
recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
|
||||
override fun getItemOffsets(
|
||||
outRect: Rect,
|
||||
view: View,
|
||||
parent: RecyclerView,
|
||||
state: RecyclerView.State
|
||||
) {
|
||||
super.getItemOffsets(outRect, view, parent, state)
|
||||
|
||||
when (parent.getChildAdapterPosition(view)) {
|
||||
0 -> {
|
||||
outRect.top = 0
|
||||
outRect.bottom = 8.3f.dpToPx().toInt()
|
||||
}
|
||||
|
||||
adapter.itemCount - 1 -> {
|
||||
outRect.top = 8.3f.dpToPx().toInt()
|
||||
outRect.bottom = 0
|
||||
}
|
||||
|
||||
else -> {
|
||||
outRect.top = 8.3f.dpToPx().toInt()
|
||||
outRect.bottom = 8.3f.dpToPx().toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
recyclerView.adapter = adapter
|
||||
}
|
||||
|
||||
private fun bindData() {
|
||||
viewModel.toastLiveData.observe(viewLifecycleOwner) {
|
||||
it?.let { Toast.makeText(requireActivity(), it, Toast.LENGTH_LONG).show() }
|
||||
}
|
||||
|
||||
viewModel.isLoading.observe(viewLifecycleOwner) {
|
||||
if (it) {
|
||||
loadingDialog.show(screenWidth, "")
|
||||
} else {
|
||||
loadingDialog.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.auditionListLiveData.observe(viewLifecycleOwner) {
|
||||
adapter.addGroupedList(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import coil.load
|
||||
import coil.transform.RoundedCornersTransformation
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.databinding.ItemAuditionListBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemAuditionListCompletedHeaderBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemAuditionListInProgressHeaderBinding
|
||||
import kr.co.vividnext.sodalive.extensions.dpToPx
|
||||
|
||||
class AuditionListAdapter(
|
||||
private val onItemClick: (GetAuditionListItem) -> Unit
|
||||
) : ListAdapter<AuditionListAdapter.DisplayItem, RecyclerView.ViewHolder>(DiffCallback()) {
|
||||
companion object {
|
||||
private const val TYPE_IN_PROGRESS_HEADER = 0
|
||||
private const val TYPE_COMPLETED_HEADER = 1
|
||||
private const val TYPE_ITEM = 2
|
||||
}
|
||||
|
||||
sealed class DisplayItem {
|
||||
data class InProgressHeader(val count: Int) : DisplayItem()
|
||||
data class CompletedHeader(val count: Int) : DisplayItem()
|
||||
data class Data(val item: GetAuditionListItem) : DisplayItem()
|
||||
}
|
||||
|
||||
class DiffCallback : DiffUtil.ItemCallback<DisplayItem>() {
|
||||
override fun areItemsTheSame(oldItem: DisplayItem, newItem: DisplayItem): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: DisplayItem, newItem: DisplayItem): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
|
||||
inner class InProgressHeaderViewHolder(
|
||||
private val binding: ItemAuditionListInProgressHeaderBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
@SuppressLint("SetTextI18n")
|
||||
fun bind(totalCount: Int) {
|
||||
binding.tvTotalCount.text = "총 ${totalCount}개"
|
||||
}
|
||||
}
|
||||
|
||||
inner class CompletedHeaderViewHolder(
|
||||
private val binding: ItemAuditionListCompletedHeaderBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
@SuppressLint("SetTextI18n")
|
||||
fun bind(totalCount: Int) {
|
||||
binding.tvTotalCount.text = "총 ${totalCount}개"
|
||||
}
|
||||
}
|
||||
|
||||
inner class ViewHolder(
|
||||
private val binding: ItemAuditionListBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(data: DisplayItem.Data) {
|
||||
binding.tvTitle.text = data.item.title
|
||||
binding.ivCover.load(data.item.imageUrl) {
|
||||
crossfade(true)
|
||||
placeholder(R.drawable.ic_place_holder)
|
||||
transformations(RoundedCornersTransformation(5f.dpToPx()))
|
||||
}
|
||||
|
||||
if (data.item.isOff) {
|
||||
binding.blackCover.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.blackCover.visibility = View.GONE
|
||||
binding.root.setOnClickListener { onItemClick(data.item) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return when (getItem(position)) {
|
||||
is DisplayItem.InProgressHeader -> TYPE_IN_PROGRESS_HEADER
|
||||
is DisplayItem.CompletedHeader -> TYPE_COMPLETED_HEADER
|
||||
is DisplayItem.Data -> TYPE_ITEM
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||
return when (viewType) {
|
||||
TYPE_IN_PROGRESS_HEADER -> {
|
||||
InProgressHeaderViewHolder(
|
||||
ItemAuditionListInProgressHeaderBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
TYPE_COMPLETED_HEADER -> {
|
||||
CompletedHeaderViewHolder(
|
||||
ItemAuditionListCompletedHeaderBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
TYPE_ITEM -> {
|
||||
ViewHolder(
|
||||
ItemAuditionListBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException("Invalid view type")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
when (val item = getItem(position)) {
|
||||
is DisplayItem.InProgressHeader -> {
|
||||
(holder as InProgressHeaderViewHolder).bind(item.count)
|
||||
}
|
||||
|
||||
is DisplayItem.CompletedHeader -> (holder as CompletedHeaderViewHolder).bind(item.count)
|
||||
is DisplayItem.Data -> (holder as ViewHolder).bind(item)
|
||||
}
|
||||
}
|
||||
|
||||
private val currentItems = mutableListOf<DisplayItem>()
|
||||
|
||||
fun addGroupedList(newItem: GetAuditionListResponse) {
|
||||
val isOnItems = newItem.items.filter { !it.isOff }
|
||||
val isOffItems = newItem.items.filter { it.isOff }
|
||||
|
||||
if (isOnItems.isNotEmpty() && currentItems.none { it is DisplayItem.InProgressHeader }) {
|
||||
currentItems.add(DisplayItem.InProgressHeader(newItem.inProgressCount))
|
||||
}
|
||||
currentItems.addAll(isOnItems.map { DisplayItem.Data(it) })
|
||||
|
||||
if (isOffItems.isNotEmpty() && currentItems.none { it is DisplayItem.CompletedHeader }) {
|
||||
currentItems.add(DisplayItem.CompletedHeader(newItem.completedCount))
|
||||
}
|
||||
currentItems.addAll(isOffItems.map { DisplayItem.Data(it) })
|
||||
|
||||
submitList(currentItems.toList())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
|
||||
class AuditionRepository(
|
||||
private val api: AuditionApi
|
||||
) {
|
||||
fun getAuditionList(
|
||||
page: Int,
|
||||
size: Int,
|
||||
token: String
|
||||
): Single<ApiResponse<GetAuditionListResponse>> {
|
||||
return api.getAuditionList(
|
||||
page = page - 1,
|
||||
size = size,
|
||||
authHeader = token
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.orhanobut.logger.Logger
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
|
||||
class AuditionViewModel(private val repository: AuditionRepository) : BaseViewModel() {
|
||||
private val _toastLiveData = MutableLiveData<String?>()
|
||||
val toastLiveData: LiveData<String?>
|
||||
get() = _toastLiveData
|
||||
|
||||
private var _isLoading = MutableLiveData(false)
|
||||
val isLoading: LiveData<Boolean>
|
||||
get() = _isLoading
|
||||
|
||||
private val _auditionListLiveData = MutableLiveData<GetAuditionListResponse>()
|
||||
val auditionListLiveData: LiveData<GetAuditionListResponse>
|
||||
get() = _auditionListLiveData
|
||||
|
||||
var page = 1
|
||||
var isLast = false
|
||||
private val pageSize = 10
|
||||
|
||||
fun getAuditionList() {
|
||||
if (!isLast && !_isLoading.value!!) {
|
||||
_isLoading.value = true
|
||||
compositeDisposable.add(
|
||||
repository.getAuditionList(
|
||||
page = page,
|
||||
size = pageSize,
|
||||
token = "Bearer ${SharedPreferenceManager.token}"
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
_isLoading.value = false
|
||||
if (it.success && it.data != null) {
|
||||
_auditionListLiveData.value = it.data!!
|
||||
if (it.data.items.isNotEmpty()) {
|
||||
page += 1
|
||||
} else {
|
||||
isLast = true
|
||||
}
|
||||
} else {
|
||||
if (it.message != null) {
|
||||
_toastLiveData.postValue(it.message)
|
||||
} else {
|
||||
_toastLiveData.postValue(
|
||||
"알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
_isLoading.postValue(false)
|
||||
it.message?.let { message -> Logger.e(message) }
|
||||
_toastLiveData.postValue("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package kr.co.vividnext.sodalive.audition
|
||||
|
||||
import androidx.annotation.Keep
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
@Keep
|
||||
data class GetAuditionListResponse(
|
||||
@SerializedName("inProgressCount") val inProgressCount: Int,
|
||||
@SerializedName("completedCount") val completedCount: Int,
|
||||
@SerializedName("items") val items: List<GetAuditionListItem>
|
||||
)
|
||||
|
||||
@Keep
|
||||
data class GetAuditionListItem(
|
||||
@SerializedName("id") val id: Long,
|
||||
@SerializedName("title") val title: String,
|
||||
@SerializedName("imageUrl") val imageUrl: String,
|
||||
@SerializedName("isOff") val isOff: Boolean
|
||||
)
|
||||
Reference in New Issue
Block a user