Skip to content

Commit

Permalink
[MERGE] #54 -> develop
Browse files Browse the repository at this point in the history
[FEAT/#54] Onboarding / 친구추가 리스트 구현
  • Loading branch information
Chaeyeon committed Jul 20, 2023
2 parents df5bc15 + dc75a3f commit 8166d0e
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class RecommendInviteDialog :
// TODO: 추천인 아이디 설정 & 링크 생기면 넣기
private val templateId = 95890.toLong()
private val myYelloId: String = "sangho.kk"
private val linkText: String = "여기다 링크 넣어주세요"
private val linkText: String = "추천인코드: {$myYelloId}\n" +
"우리 같이 YELL:O 해요!\n" +
"(여기에는 다운로드 링크)"

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class YelloViewModel @Inject constructor(

init {
getVoteState()
dataStore.isLogin = true
dataStore.userToken =
"eyJ0eXBlIjoiYWNjZXNzVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyOTExNzI0MDAyIiwianRpIjoiMTQ4IiwiaWF0IjoxNjg5NjE5NzkzLCJleHAiOjE2ODk3MDYxOTN9.pBvnzLuwAg2wDZZ77HUBbdZvE0-Xvp6XRhqF9ZDA1xc"
}

fun decreaseTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class UnlockDialogFragment :
private val templateId = 95890.toLong()
private val url = "http://naver.com"
private val myYelloId: String = "sangho.kk"
private val linkText: String = "여기다 링크 넣어주세요"
private val linkText: String = "추천인코드: {$myYelloId}\n" +
"우리 같이 YELL:O 해요!\n" +
"(여기에는 다운로드 링크)"

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import com.example.domain.entity.onboarding.Friend
import com.example.domain.entity.RequestOnboardingListModel
import com.example.domain.entity.onboarding.FriendGroup
import com.example.domain.entity.onboarding.FriendList
import com.example.domain.entity.onboarding.GroupList
Expand All @@ -15,11 +15,12 @@ import com.example.domain.entity.onboarding.UserInfo
import com.example.domain.repository.OnboardingRepository
import com.example.ui.view.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import java.util.regex.Pattern
import javax.inject.Inject
import kotlinx.coroutines.launch
import retrofit2.HttpException
import timber.log.Timber
import java.util.regex.Pattern
import javax.inject.Inject
import kotlin.math.ceil

@HiltViewModel
class OnBoardingViewModel @Inject constructor(
Expand All @@ -39,9 +40,6 @@ class OnBoardingViewModel @Inject constructor(
private val _departmentData = MutableLiveData<UiState<GroupList>>()
val departmentData: MutableLiveData<UiState<GroupList>> = _departmentData

private val _friendData = MutableLiveData<UiState<FriendList>>()
val friendData: MutableLiveData<UiState<FriendList>> = _friendData

private val _currentPage = MutableLiveData(0)
val currentPage: LiveData<Int> = _currentPage

Expand All @@ -53,10 +51,6 @@ class OnBoardingViewModel @Inject constructor(
private var isDepartmentPagingFinish = false
private var totalDepartmentPage = Integer.MAX_VALUE

var friendPage = -1L
private var isFriendPagingFinish = false
private var totalFriendPage = Long.MAX_VALUE

val _school = MutableLiveData("")
val school: String
get() = _school.value?.trim() ?: ""
Expand Down Expand Up @@ -85,10 +79,6 @@ class OnBoardingViewModel @Inject constructor(
private val id: String
get() = _id.value?.trim() ?: ""

private val _friendList = MutableLiveData<FriendList>()
val friendList: FriendList
get() = _friendList.value ?: FriendList(0, emptyList())

val _gender = MutableLiveData("")
val gender: String
get() = _gender.value ?: ""
Expand Down Expand Up @@ -118,8 +108,18 @@ class OnBoardingViewModel @Inject constructor(
private val _studentIdResult: MutableLiveData<List<Int>> = MutableLiveData()
val studentIdResult: LiveData<List<Int>> = _studentIdResult

private val _friendResult: MutableLiveData<List<Friend>> = MutableLiveData()
val friendResult: LiveData<List<Friend>> = _friendResult

private val _friendState = MutableLiveData<FriendList>()
val friendState: LiveData<FriendList> = _friendState

var kakaoFriendList: List<String> = listOf()
var selectedFriendIdList : List<Long> = listOf()
var selectedFriendCount: MutableLiveData<Int> = MutableLiveData(0)

private var currentFriendPage: Int = -1
private var isFriendPagingFinish = false
private var totalFriendPage = Int.MAX_VALUE


// TODO: throttle 및 페이징 처리
fun getSchoolList(search: String) {
Expand Down Expand Up @@ -185,25 +185,21 @@ class OnBoardingViewModel @Inject constructor(
}
}

fun addListFriend(friendGroup: FriendGroup) {
if (isFriendPagingFinish) return
fun addFriendList(friendGroup: FriendGroup) {
viewModelScope.launch {
_friendData.value = UiState.Loading
onboardingRepository.postFriendService(
friendGroup,
++friendPage,
).onSuccess { friend ->
if (friend == null) {
_friendData.value = UiState.Empty
return@launch
}
totalFriendPage = kotlin.math.ceil((friend.totalCount * 0.1)).toLong()
if (totalFriendPage == friendPage) isFriendPagingFinish = true
_friendData.value =
when {
friend.friendList.isEmpty() -> UiState.Empty
else -> UiState.Success(friend)
}
if (isFriendPagingFinish) return@launch
runCatching {
onboardingRepository.postToGetFriendList(
RequestOnboardingListModel(friendGroup.friendIdList, friendGroup.groupId),
++currentFriendPage
)
}.onSuccess { friendList ->
friendList ?: return@launch
totalFriendPage = ceil((friendList.totalCount * 0.1)).toInt() - 1
if (totalFriendPage == currentFriendPage) isFriendPagingFinish = true
_friendState.value = friendList
}.onFailure {
Timber.e(it.message)
}
}
}
Expand All @@ -219,7 +215,7 @@ class OnBoardingViewModel @Inject constructor(
name = name,
yelloId = id,
gender = gender,
friendList = friendList.toIdList(),
friendList = selectedFriendIdList,
recommendId = recommendId,
)
onboardingRepository.postSignup(signupInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.yello.databinding.ItemAddfriendBinding

class AddFriendAdapter(private val itemClick: (Friend, Int) -> (Unit)) :
ListAdapter<Friend, AddFriendAdapter.AddFriendViewHolder>(diffUtil) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddFriendViewHolder {
return AddFriendViewHolder(
ItemAddfriendBinding.inflate(
Expand All @@ -27,34 +28,37 @@ class AddFriendAdapter(private val itemClick: (Friend, Int) -> (Unit)) :
}

override fun onBindViewHolder(holder: AddFriendViewHolder, position: Int) {
holder.setFriend(getItem(position), position)
holder.onBind(getItem(position), position)
}

class AddFriendViewHolder(
private val binding: ItemAddfriendBinding,
private val itemClick: (Friend, Int) -> Unit,
) : RecyclerView.ViewHolder(binding.root) {
fun setFriend(friend: Friend, position: Int) {

fun onBind(friend: Friend, position: Int) {
binding.ivFriendProfile.load(friend.profileImage) {
transformations(CircleCropTransformation())
}
binding.tvFriendName.text = friend.name
binding.tvFriendDepartment.text = friend.groupName
binding.ivFreindCheck.isSelected = friend.isSelected

binding.tvFriendName.setTextColor(
ContextCompat.getColor(
itemView.context,
if (friend.isSelected) R.color.white else R.color.grayscales_onbarding_light,
),
)

binding.tvFriendDepartment.setTextColor(
ContextCompat.getColor(
itemView.context,
if (friend.isSelected) R.color.grayscales_500 else R.color.grayscales_onbarding_dark,
),
)

binding.root.setOnSingleClickListener {
binding.ivFreindCheck.setOnSingleClickListener {
itemClick(friend, position)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
package com.yello.presentation.onboarding.fragment.addfriend

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.domain.entity.onboarding.Friend
import com.example.domain.entity.onboarding.FriendGroup
import com.example.ui.base.BindingFragment
import com.example.ui.view.setOnSingleClickListener
import com.kakao.sdk.talk.TalkApiClient
import com.yello.R
import com.yello.databinding.FragmentAddfreindBinding
import com.yello.presentation.onboarding.activity.OnBoardingViewModel
import com.yello.util.context.yelloSnackbar
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AddFriendFragment : BindingFragment<FragmentAddfreindBinding>(R.layout.fragment_addfreind) {

private lateinit var friendList: List<Friend>
private var adapter: AddFriendAdapter? = null

private val viewModel by activityViewModels<OnBoardingViewModel>()
private lateinit var friendsList: List<Friend>

private var selectedItemIdList = mutableListOf<Long>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.vm = viewModel

initFriendAdapter()
setConfirmBtnClickListener()
setBackBtnClickListener()
observe()
setListWithInfinityScroll()
observeAddListState()
getFriendIdList()
}

private fun initFriendAdapter() {
friendList = viewModel.friendResult.value ?: emptyList()
adapter = AddFriendAdapter { friend, position ->
friend.isSelected = !friend.isSelected
if (friend.isSelected && friend.id !in selectedItemIdList) {
selectedItemIdList.add(friend.id)
viewModel.selectedFriendCount.value = viewModel.selectedFriendCount.value?.plus(1)
} else {
selectedItemIdList.remove(friend.id)
viewModel.selectedFriendCount.value = viewModel.selectedFriendCount.value?.minus(1)
}
adapter?.notifyItemChanged(position)
}
binding.rvFreindList.adapter = adapter
adapter?.submitList(friendList)
}

private fun setConfirmBtnClickListener() {
binding.btnAddfriendNext.setOnSingleClickListener {
viewModel.selectedFriendIdList = selectedItemIdList
viewModel.navigateToNextPage()
}
}
Expand All @@ -47,9 +68,50 @@ class AddFriendFragment : BindingFragment<FragmentAddfreindBinding>(R.layout.fra
}
}

private fun observe() {
viewModel.friendData.observe(viewLifecycleOwner) {
adapter?.submitList(friendList)
private fun getFriendIdList() {
TalkApiClient.instance.friends { friends, error ->
if (error == null) {
viewModel.kakaoFriendList = friends?.elements?.map { it.id.toString() } ?: listOf()
addFriendListFromServer()
addFriendListFromServer()
} else {
yelloSnackbar(binding.root, getString(R.string.msg_error))
}
}
}

private fun addFriendListFromServer() {
viewModel.addFriendList(
FriendGroup(
viewModel.kakaoFriendList, viewModel.groupId
)
)
}

// 무한 스크롤 구현
private fun setListWithInfinityScroll() {
binding.rvFreindList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
recyclerView.layoutManager?.let { layoutManager ->
if (!binding.rvFreindList.canScrollVertically(1) && layoutManager is LinearLayoutManager && layoutManager.findLastVisibleItemPosition() == adapter!!.itemCount - 1) {
addFriendListFromServer()
}
}
}
}
})
}

// 리스트 추가 서버 통신 성공 시 어댑터에 리스트 추가
private fun observeAddListState() {
viewModel.friendState.observe(viewLifecycleOwner) {
friendsList = it.friendList
adapter?.submitList(friendsList)
selectedItemIdList.addAll(friendsList.map { friend -> friend.id })
viewModel.selectedFriendCount.value = viewModel.selectedFriendCount.value?.plus(friendsList.size)
adapter?.notifyDataSetChanged()
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_addfreind.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
android:layout_marginStart="4dp"
android:layout_marginTop="78dp"
android:fontFamily="@font/font_pretendard_bold"
tools:text="40"
tools:text="@{@string/to_string(vm.selectedFriendCount)}"
android:textColor="@color/purple_sub_400"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/tv_friend_howmany"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.data.datasource

import com.example.data.model.request.onboarding.RequestPostSignupDto
import com.example.data.model.request.onboarding.RequestServiceTokenDto
import com.example.data.model.request.onboarding.RequestSignFriendDto
import com.example.data.model.request.onboarding.RequestOnboardingListDto
import com.example.data.model.response.BaseResponse
import com.example.data.model.response.onboarding.ResponseDepartmentDto
import com.example.data.model.response.onboarding.ResponseFriendDto
Expand Down Expand Up @@ -31,8 +31,8 @@ interface OnboardingDataSource {
): BaseResponse<Boolean>

suspend fun postFriendData(
requestSignFriendDto: RequestSignFriendDto,
page: Long,
requestOnboardingListDto: RequestOnboardingListDto,
page: Int,
): BaseResponse<ResponseFriendDto>

suspend fun postSignup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.data.datasource.remote
import com.example.data.datasource.OnboardingDataSource
import com.example.data.model.request.onboarding.RequestPostSignupDto
import com.example.data.model.request.onboarding.RequestServiceTokenDto
import com.example.data.model.request.onboarding.RequestSignFriendDto
import com.example.data.model.request.onboarding.RequestOnboardingListDto
import com.example.data.model.response.BaseResponse
import com.example.data.model.response.onboarding.ResponseDepartmentDto
import com.example.data.model.response.onboarding.ResponseFriendDto
Expand Down Expand Up @@ -43,10 +43,10 @@ class OnboardingDataSourceImpl @Inject constructor(
}

override suspend fun postFriendData(
requestSignFriendDto: RequestSignFriendDto,
page: Long,
requestOnboardingListDto: RequestOnboardingListDto,
page: Int,
): BaseResponse<ResponseFriendDto> {
return onboardingService.postFriend(requestSignFriendDto, page)
return onboardingService.postFriend(requestOnboardingListDto, page)
}

override suspend fun postSignup(requestPostSignupDto: RequestPostSignupDto): BaseResponse<ResponsePostSignupDto> =
Expand Down
Loading

0 comments on commit 8166d0e

Please sign in to comment.