Skip to content

Commit

Permalink
[REFACTOR] : follow, setting 함수화 / #55
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyun127 committed Oct 31, 2023
1 parent d164ca0 commit 2f7f461
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,10 @@ class AddFollowerFragment :
initClickEventListeners()
initEditText()
addFollower()
removeText()
collectInputResult()
collectEventData()
collectDeleteFollower()
removeText()
}

private fun collectInputResult() {
addFollowerViewModel.getInputFollower.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Loading -> {
}

is UiState.Success -> {
with(binding) {
addFollower = it.data
val list: ArrayList<String>? =
requireArguments().getStringArrayList(FOLLOWER_LIST)

layoutAddFollower.visibility =
if (it.data.validate) View.VISIBLE else View.GONE

if (list != null) {
val isFollowing = it.data.userName in list
binding.tvAddFollowerLabel.text = if (isFollowing) "팔로우 취소" else "팔로우"
}

layoutAddFollowerEmpty.visibility =
if (!it.data.validate) View.VISIBLE else View.GONE
}
}

else -> {
binding.layoutAddFollowerEmpty.visibility = View.VISIBLE
}
}
}.launchIn(lifecycleScope)
}

private fun initClickEventListeners() {
Expand All @@ -96,7 +64,7 @@ class AddFollowerFragment :

private fun addFollower() {
binding.tvAddFollowerLabel.setOnClickListener {
if (binding.tvAddFollowerLabel.text == "팔로우") {
if (binding.tvAddFollowerLabel.text == getString(R.string.tv_add_follower_button)) {
addFollowerViewModel.addFollower(binding.tvAddFollowerName.text.toString())
} else {
val dialog = DeleteFollowerDialogFragment(deleteFollower = {
Expand All @@ -107,44 +75,71 @@ class AddFollowerFragment :
}
}

private fun collectEventData() {
addFollowerViewModel.eventData.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Loading -> {
private fun removeText() {
with(binding) {
etAddFollowerSearch.doAfterTextChanged { text ->
if (text?.length == 0) {
ivAddFollowerCancel.visibility = View.INVISIBLE
} else {
ivAddFollowerCancel.visibility = View.VISIBLE
ivAddFollowerCancel.setOnClickListener { etAddFollowerSearch.text.clear() }
}
}
}
}

private fun collectInputResult() {
addFollowerViewModel.getInputFollower.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Loading -> Unit

is UiState.Success -> {
binding.tvAddFollowerLabel.text = "팔로우 취소"
with(binding) {
addFollower = it.data
val list: ArrayList<String>? =
requireArguments().getStringArrayList(FOLLOWER_LIST)

layoutAddFollower.visibility =
if (it.data.validate) View.VISIBLE else View.GONE

if (list != null) {
val isFollowing = it.data.userName in list
binding.tvAddFollowerLabel.text =
if (isFollowing) getString(R.string.tv_item_rv_follow_cancel) else getString(
R.string.tv_add_follower_button
)
}

layoutAddFollowerEmpty.visibility =
if (!it.data.validate) View.VISIBLE else View.GONE
}
}

else -> {}
else -> binding.layoutAddFollowerEmpty.visibility = View.VISIBLE
}
}.launchIn(lifecycleScope)
}

private fun collectDeleteFollower() {
followViewModel.deleteFollower.flowWithLifecycle(lifecycle).onEach {
private fun collectEventData() {
addFollowerViewModel.eventData.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> {
binding.tvAddFollowerLabel.text = "팔로우"
}
is UiState.Success -> binding.tvAddFollowerLabel.text =
getString(R.string.tv_item_rv_follow_cancel)

else -> {}
else -> Unit
}
}.launchIn(lifecycleScope)
}

private fun removeText() {
with(binding) {
etAddFollowerSearch.doAfterTextChanged { text ->
if (text?.length == 0) {
ivAddFollowerCancel.visibility = View.INVISIBLE
} else {
ivAddFollowerCancel.visibility = View.VISIBLE
ivAddFollowerCancel.setOnClickListener { etAddFollowerSearch.text.clear() }
}
private fun collectDeleteFollower() {
followViewModel.deleteFollower.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> binding.tvAddFollowerLabel.text =
getString(R.string.tv_add_follower_button)

else -> Unit
}
}
}.launchIn(lifecycleScope)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.velogm.core_ui.base.BindingFragment
import com.velogm.core_ui.fragment.toast
import com.velogm.core_ui.view.UiState
import com.velogm.presentation.R
import com.velogm.presentation.databinding.FragmentFollowBinding
Expand Down Expand Up @@ -40,21 +39,23 @@ class FollowFragment : BindingFragment<FragmentFollowBinding>(R.layout.fragment_
private fun collectFollowerNameList() {
viewModel.getFollowerNameList.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> {
openAddFollower(it.data.name)
}

else -> {}
is UiState.Success -> openAddFollower(it.data.name)
else -> Unit
}
}.launchIn(lifecycleScope)
}

private fun openAddFollower(list: ArrayList<String>) {
binding.tvFollowAddFollower.setOnClickListener {
findNavController().navigate(
R.id.action_follow_to_addFollowerFragment, bundleOf(FOLLOWER_LIST to list)
)
}
}

private fun collectFollower() {
viewModel.getFollower.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Loading -> {
}

is UiState.Success -> {
binding.rvFollow.adapter = FollowerAdapter(deleteFollowerClick = {
val dialog = DeleteFollowerDialogFragment(deleteFollower = {
Expand All @@ -68,28 +69,17 @@ class FollowFragment : BindingFragment<FragmentFollowBinding>(R.layout.fragment_
}
}

else -> {}
else -> Unit
}
}.launchIn(lifecycleScope)
}

private fun collectDeleteFollower() {
viewModel.deleteFollower.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> {
viewModel.getFollower()
}

else -> {}
is UiState.Success -> viewModel.getFollower()
else -> Unit
}
}.launchIn(lifecycleScope)
}

private fun openAddFollower(list: ArrayList<String>) {
binding.tvFollowAddFollower.setOnClickListener {
findNavController().navigate(
R.id.action_follow_to_addFollowerFragment, bundleOf(FOLLOWER_LIST to list)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ class FollowViewModel @Inject constructor(
}
_deleteFollower.value = UiState.Loading
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ class SettingFragment : BindingFragment<FragmentSettingBinding>(R.layout.fragmen
dialog.show(childFragmentManager, "withdrawal")
}
}

}

0 comments on commit 2f7f461

Please sign in to comment.