팔로잉 채널 전체보기 페이지 추가

This commit is contained in:
2023-08-09 07:53:38 +09:00
parent f455aa81a2
commit 6d93da2136
10 changed files with 432 additions and 2 deletions

View File

@@ -0,0 +1,109 @@
package kr.co.vividnext.sodalive.following
import android.annotation.SuppressLint
import android.content.Intent
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.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityFollowingCreatorBinding
import kr.co.vividnext.sodalive.explorer.profile.UserProfileActivity
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject
class FollowingCreatorActivity : BaseActivity<ActivityFollowingCreatorBinding>(
ActivityFollowingCreatorBinding::inflate
) {
private val viewModel: FollowingCreatorViewModel by inject()
private lateinit var loadingDialog: LoadingDialog
private lateinit var adapter: FollowingCreatorAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bindData()
viewModel.getFollowedCreatorAllList()
}
override fun setupView() {
loadingDialog = LoadingDialog(this, layoutInflater)
binding.toolbar.tvBack.text = "팔로잉 채널 리스트"
binding.toolbar.tvBack.setOnClickListener { finish() }
adapter = FollowingCreatorAdapter(
onClickItem = { creatorId ->
startActivity(
Intent(applicationContext, UserProfileActivity::class.java).apply {
putExtra(Constants.EXTRA_USER_ID, creatorId)
}
)
},
onClickRegisterNotification = { viewModel.registerNotification(it) },
onClickUnRegisterNotification = { viewModel.unRegisterNotification(it) }
)
binding.rvFollowingCreator.layoutManager = LinearLayoutManager(
applicationContext,
LinearLayoutManager.VERTICAL,
false
)
binding.rvFollowingCreator.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val layoutManager = recyclerView.layoutManager as? LinearLayoutManager
if (
layoutManager != null &&
layoutManager.findLastCompletelyVisibleItemPosition() == adapter.itemCount - 1
) {
viewModel.getFollowedCreatorAllList()
}
}
})
binding.rvFollowingCreator.addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
super.getItemOffsets(outRect, view, parent, state)
outRect.left = 13.3f.dpToPx().toInt()
outRect.right = 13.3f.dpToPx().toInt()
}
})
binding.rvFollowingCreator.adapter = adapter
}
@SuppressLint("SetTextI18n")
private fun bindData() {
viewModel.toastLiveData.observe(this) {
it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
}
viewModel.isLoading.observe(this) {
if (it) {
loadingDialog.show(screenWidth, "")
} else {
loadingDialog.dismiss()
}
}
viewModel.creatorListLiveData.observe(this) {
adapter.addAll(it)
}
viewModel.creatorListTotalCountLiveData.observe(this) {
binding.tvTotalCount.text = " $it "
}
}
}

View File

@@ -0,0 +1,83 @@
package kr.co.vividnext.sodalive.following
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemFollowerListBinding
class FollowingCreatorAdapter(
private val onClickItem: (Long) -> Unit,
private val onClickRegisterNotification: (Long) -> Unit,
private val onClickUnRegisterNotification: (Long) -> Unit,
) : RecyclerView.Adapter<FollowingCreatorAdapter.ViewHolder>() {
private val items = mutableListOf<GetCreatorFollowingAllListItem>()
inner class ViewHolder(
private val binding: ItemFollowerListBinding
) : RecyclerView.ViewHolder(binding.root) {
@SuppressLint("NotifyDataSetChanged")
fun bind(item: GetCreatorFollowingAllListItem) {
binding.tvNickname.text = item.nickname
binding.ivProfile.load(item.profileImageUrl) {
transformations(CircleCropTransformation())
placeholder(R.drawable.bg_placeholder)
crossfade(true)
}
binding.ivNotification.visibility = View.VISIBLE
if (item.isFollow) {
binding.ivNotification.setImageResource(R.drawable.btn_notification_selected)
binding.ivNotification.setOnClickListener {
val index = items.indexOf(item)
val copyItem = item.copy(isFollow = false)
items.add(index, copyItem)
items.removeAt(index + 1)
notifyDataSetChanged()
onClickUnRegisterNotification(item.creatorId)
}
} else {
binding.ivNotification.setImageResource(R.drawable.btn_notification)
binding.ivNotification.setOnClickListener {
val index = items.indexOf(item)
val copyItem = item.copy(isFollow = true)
items.add(index, copyItem)
items.removeAt(index + 1)
notifyDataSetChanged()
onClickRegisterNotification(item.creatorId)
}
}
binding.root.setOnClickListener { onClickItem(item.creatorId) }
}
}
@SuppressLint("NotifyDataSetChanged")
fun addAll(items: List<GetCreatorFollowingAllListItem>) {
this.items.addAll(items)
notifyDataSetChanged()
}
fun clear() {
this.items.clear()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
ItemFollowerListBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position])
}
override fun getItemCount() = items.size
}

View File

@@ -0,0 +1,32 @@
package kr.co.vividnext.sodalive.following
import kr.co.vividnext.sodalive.live.recommend.LiveRecommendApi
import kr.co.vividnext.sodalive.user.CreatorFollowRequestRequest
import kr.co.vividnext.sodalive.user.UserApi
class FollowingCreatorRepository(
private val api: LiveRecommendApi,
private val userApi: UserApi
) {
fun getFollowedCreatorAllList(
page: Int,
size: Int,
token: String
) = api.getCreatorFollowingAllList(page = page - 1, size = size, authHeader = token)
fun registerNotification(
creatorId: Long,
token: String
) = userApi.creatorFollow(
request = CreatorFollowRequestRequest(creatorId = creatorId),
authHeader = token
)
fun unRegisterNotification(
creatorId: Long,
token: String
) = userApi.creatorUnFollow(
request = CreatorFollowRequestRequest(creatorId = creatorId),
authHeader = token
)
}

View File

@@ -0,0 +1,106 @@
package kr.co.vividnext.sodalive.following
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 FollowingCreatorViewModel(
private val repository: FollowingCreatorRepository
) : BaseViewModel() {
private val _creatorListLiveData = MutableLiveData<List<GetCreatorFollowingAllListItem>>()
val creatorListLiveData: LiveData<List<GetCreatorFollowingAllListItem>>
get() = _creatorListLiveData
private val _creatorListTotalCountLiveData = MutableLiveData<Int>()
val creatorListTotalCountLiveData: LiveData<Int>
get() = _creatorListTotalCountLiveData
private var _isLoading = MutableLiveData(false)
val isLoading: LiveData<Boolean>
get() = _isLoading
private val _toastLiveData = MutableLiveData<String?>()
val toastLiveData: LiveData<String?>
get() = _toastLiveData
var page = 1
var isLast = false
private val pageSize = 10
fun getFollowedCreatorAllList() {
if (!isLast && !_isLoading.value!!) {
_isLoading.value = true
compositeDisposable.add(
repository.getFollowedCreatorAllList(
page = page,
size = pageSize,
token = "Bearer ${SharedPreferenceManager.token}"
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
_isLoading.value = false
if (it.success && it.data != null) {
val data = it.data
if (data.items.isEmpty()) {
isLast = true
} else {
page += 1
_creatorListTotalCountLiveData.value = data.totalCount
_creatorListLiveData.value = data.items
}
} else {
if (it.message != null) {
_toastLiveData.postValue(it.message)
} else {
_toastLiveData.postValue(
"알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
)
}
}
},
{
_isLoading.value = false
it.message?.let { message -> Logger.e(message) }
_toastLiveData.postValue(
"알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
)
}
)
)
}
}
fun registerNotification(creatorId: Long) {
_creatorListTotalCountLiveData.value = _creatorListTotalCountLiveData.value!! + 1
compositeDisposable.add(
repository.registerNotification(
creatorId,
"Bearer ${SharedPreferenceManager.token}"
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({}, {})
)
}
fun unRegisterNotification(creatorId: Long) {
_creatorListTotalCountLiveData.value = _creatorListTotalCountLiveData.value!! - 1
compositeDisposable.add(
repository.unRegisterNotification(
creatorId,
"Bearer ${SharedPreferenceManager.token}"
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({}, {})
)
}
}

View File

@@ -0,0 +1,15 @@
package kr.co.vividnext.sodalive.following
import com.google.gson.annotations.SerializedName
data class GetCreatorFollowingAllListResponse(
@SerializedName("totalCount") val totalCount: Int,
@SerializedName("items") val items: List<GetCreatorFollowingAllListItem>
)
data class GetCreatorFollowingAllListItem(
@SerializedName("creatorId") val creatorId: Long,
@SerializedName("nickname") val nickname: String,
@SerializedName("profileImageUrl") val profileImageUrl: String,
@SerializedName("isFollow") val isFollow: Boolean
)