Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR/#185] OurTodo, MyTodo 뷰 / 코드 리팩토링만 #186

Merged
merged 8 commits into from
Jan 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import com.going.ui.extension.ItemDiffCallback
class TodoDetailNameAdapter : ListAdapter<TodoAllocatorModel, TodoDetailNameViewHolder>(diffUtil) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoDetailNameViewHolder {
val inflater by lazy { LayoutInflater.from(parent.context) }
val binding: ItemTodoCreateNameBinding =
ItemTodoCreateNameBinding.inflate(LayoutInflater.from(parent.context), parent, false)
ItemTodoCreateNameBinding.inflate(inflater, parent, false)
return TodoDetailNameViewHolder(binding)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.view.ViewTreeObserver
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.flowWithLifecycle
Expand All @@ -25,6 +24,7 @@ import com.going.presentation.todo.ourtodo.OurTodoFragment.Companion.debounceTim
import com.going.presentation.todo.ourtodo.create.OurTodoCreateActivity.Companion.EXTRA_PARTICIPANT_ID
import com.going.ui.base.BaseFragment
import com.going.ui.extension.UiState
import com.going.ui.extension.colorOf
import com.going.ui.extension.getWindowHeight
import com.going.ui.extension.setOnSingleClickListener
import com.going.ui.extension.setStatusBarColor
Expand Down Expand Up @@ -58,21 +58,20 @@ class MyTodoFragment() : BaseFragment<FragmentMyTodoBinding>(R.layout.fragment_m
setViewPagerChangeListener()
setViewPagerDebounce()
setTodoCountText()
setToolbarColor()
initEmptyViewHeight()
setEmptyViewHeight()
initOffsetChangedListener()
observeMyTripInfoState()
observeTotalUncompletedTodoCount()

}

private fun initAddTodoListener() {
binding.btnMyTodoAddTodo.setOnSingleClickListener {
Intent(activity, MyTodoCreateActivity::class.java).apply {
putExtra(EXTRA_TRIP_ID, viewModel.tripId)
putExtra(EXTRA_PARTICIPANT_ID, viewModel.participantId)
startActivity(this)
}
MyTodoCreateActivity.createIntent(
requireContext(),
viewModel.tripId,
viewModel.participantId
).apply { startActivity(this) }
Comment on lines +70 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이렇게 리팩해보겠습니당

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이 방법 알고는 감탄했는데 아주 굿이에요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...미쳤다!!>...저도 이거 이용해서 리팩해볼게요 역시 그는 김상호...

}
}

Expand Down Expand Up @@ -154,26 +153,6 @@ class MyTodoFragment() : BaseFragment<FragmentMyTodoBinding>(R.layout.fragment_m
setTodoCount(viewModel.totalUncompletedTodoCount.value)
}

private fun setToolbarColor() {
binding.appbarMyTodo.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
if (abs(verticalOffset) == appBarLayout.totalScrollRange) {
setStatusBarColor(R.color.white_000)
binding.toolbarMyTodo.setBackgroundColor(
ContextCompat.getColor(
requireContext(), R.color.white_000
)
)
} else {
setStatusBarColor(R.color.gray_50)
binding.toolbarMyTodo.setBackgroundColor(
ContextCompat.getColor(
requireContext(), R.color.gray_50
)
)
}
}
}

private fun initEmptyViewHeight() {
binding.appbarMyTodo.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
Expand All @@ -189,14 +168,22 @@ class MyTodoFragment() : BaseFragment<FragmentMyTodoBinding>(R.layout.fragment_m
})
}

private fun setEmptyViewHeight() {
private fun initOffsetChangedListener() {
binding.appbarMyTodo.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
val displayHeight = activity?.getWindowHeight() ?: return@addOnOffsetChangedListener
val toolbarHeight = binding.toolbarMyTodo.height
val appBarHeight = appBarLayout.totalScrollRange + verticalOffset
binding.layoutMyTodoEmpty.layoutParams = (binding.layoutMyTodoEmpty.layoutParams).also {
it.height = displayHeight - toolbarHeight - appBarHeight - 300
}

if (abs(verticalOffset) == appBarLayout.totalScrollRange) {
setStatusBarColor(R.color.white_000)
binding.toolbarMyTodo.setBackgroundColor(colorOf(R.color.white_000))
} else {
setStatusBarColor(R.color.gray_50)
binding.toolbarMyTodo.setBackgroundColor(colorOf(R.color.gray_50))
}
}
}

Expand Down Expand Up @@ -231,9 +218,7 @@ class MyTodoFragment() : BaseFragment<FragmentMyTodoBinding>(R.layout.fragment_m
).apply {
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
requireContext(), R.color.red_500
)
colorOf(R.color.red_500)
), length - 3, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MyTodoViewModel @Inject constructor(
_myTripInfoState.value = UiState.Success(response)
}
.onFailure {
_myTripInfoState.value = UiState.Failure(it.message.toString())
_myTripInfoState.value = UiState.Failure(it.message.orEmpty())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.going.presentation.todo.mytodo.create

import android.graphics.drawable.Drawable
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.going.domain.entity.NameState
Expand All @@ -14,6 +14,7 @@ import com.going.presentation.todo.TodoActivity.Companion.EXTRA_TRIP_ID
import com.going.presentation.todo.ourtodo.create.OurTodoCreateActivity.Companion.EXTRA_PARTICIPANT_ID
import com.going.ui.base.BaseActivity
import com.going.ui.extension.UiState
import com.going.ui.extension.drawableOf
import com.going.ui.extension.setOnSingleClickListener
import com.going.ui.extension.toast
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -93,7 +94,7 @@ class MyTodoCreateActivity :
state,
binding.tvMyTodoTodoCounter,
) { background ->
binding.etMyTodoCreateTodo.background = setBackgroundColor(background)
binding.etMyTodoCreateTodo.background = drawableOf(background)
}
}
}
Expand All @@ -104,7 +105,7 @@ class MyTodoCreateActivity :
state,
binding.tvMyTodoMemoCounter,
) { background ->
binding.etMyTodoCreateMemo.background = setBackgroundColor(background)
binding.etMyTodoCreateMemo.background = drawableOf(background)
}
}
}
Expand Down Expand Up @@ -139,20 +140,22 @@ class MyTodoCreateActivity :
counter.setTextColor(getColor(color))
}

private fun setBackgroundColor(background: Int): Drawable? {
return ResourcesCompat.getDrawable(
this.resources,
background,
theme,
)
}

override fun onDestroy() {
super.onDestroy()
if (myTodoCreateBottomSheet?.isAdded == true) myTodoCreateBottomSheet?.dismiss()
}

companion object {
private const val DATE_BOTTOM_SHEET = "DATE_BOTTOM_SHEET"

@JvmStatic
fun createIntent(
context: Context,
tripId: Long,
participantId: Long
): Intent = Intent(context, MyTodoCreateActivity::class.java).apply {
putExtra(EXTRA_TRIP_ID, tripId)
putExtra(EXTRA_PARTICIPANT_ID, participantId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.lifecycle.viewModelScope
import com.going.domain.entity.NameState
import com.going.domain.entity.request.TodoCreateRequestModel
import com.going.domain.repository.TodoRepository
import com.going.presentation.todo.ourtodo.create.OurTodoCreateViewModel
import com.going.ui.extension.UiState
import com.going.ui.extension.getGraphemeLength
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -65,8 +64,8 @@ class MyTodoCreateViewModel @Inject constructor(
todoRepository.postToCreateTodo(
tripId = tripId,
request = TodoCreateRequestModel(
title = todo.value ?: "",
endDate = endDate.value ?: "",
title = todo.value.orEmpty(),
endDate = endDate.value.orEmpty(),
allocators = listOf(participantId),
memo = memo.value,
secret = true,
Expand All @@ -76,7 +75,7 @@ class MyTodoCreateViewModel @Inject constructor(
_todoCreateState.value = UiState.Success(response)
}
.onFailure {
_todoCreateState.value = UiState.Failure(it.message.toString())
_todoCreateState.value = UiState.Failure(it.message.orEmpty())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.going.presentation.todo.mytodo.todolist

import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.going.domain.entity.response.TodoModel
import com.going.presentation.R
import com.going.presentation.databinding.ItemMyTodoBinding
import com.going.presentation.todo.name.TodoNameAdapter
import com.going.ui.extension.colorOf
import com.going.ui.extension.setOnSingleClickListener

class MyTodoListViewHolder(
Expand All @@ -20,7 +20,7 @@ class MyTodoListViewHolder(
fun onBind(item: TodoModel) {
binding.run {
tvMyTodoItemTitle.text = item.title
tvMyTodoItemDate.text = item.endDate.replace("-", ".") + "까지"
tvMyTodoItemDate.text = item.endDate.replace("-", ".") + "까지"

layoutMyTodoLock.isVisible = item.secret
rvMyTodoName.isVisible = !item.secret
Expand All @@ -33,17 +33,17 @@ class MyTodoListViewHolder(
}

if (isCompleted) {
tvMyTodoItemTitle.setTextColor(ContextCompat.getColor(binding.root.context, R.color.gray_300))
tvMyTodoItemDate.setTextColor(ContextCompat.getColor(binding.root.context, R.color.gray_200))
tvMyTodoItemTitle.setTextColor(binding.root.context.colorOf(R.color.gray_300))
tvMyTodoItemDate.setTextColor(binding.root.context.colorOf(R.color.gray_200))
Comment on lines +36 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확장함수 야무지다!!

layoutMyTodoLock.setBackgroundResource(R.drawable.shape_rect_2_gray300_line)
ivMyTodoLock.setImageResource(R.drawable.ic_lock_complete)
tvMyTodoLock.setTextColor(ContextCompat.getColor(binding.root.context, R.color.gray_300))
tvMyTodoLock.setTextColor(binding.root.context.colorOf(R.color.gray_300))
} else {
tvMyTodoItemTitle.setTextColor(ContextCompat.getColor(binding.root.context, R.color.black_000))
tvMyTodoItemDate.setTextColor(ContextCompat.getColor(binding.root.context, R.color.gray_300))
tvMyTodoItemTitle.setTextColor(binding.root.context.colorOf(R.color.black_000))
tvMyTodoItemDate.setTextColor(binding.root.context.colorOf(R.color.gray_300))
layoutMyTodoLock.setBackgroundResource(R.drawable.shape_rect_2_gray400_line)
ivMyTodoLock.setImageResource(R.drawable.ic_lock_uncomplete)
tvMyTodoLock.setTextColor(ContextCompat.getColor(binding.root.context, R.color.gray_400))
tvMyTodoLock.setTextColor(binding.root.context.colorOf(R.color.gray_400))
}

cbMyTodoUnselected.setOnSingleClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class TodoNameAdapter(
) : ListAdapter<TodoAllocatorModel, TodoNameViewHolder>(diffUtil) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoNameViewHolder {
val inflater by lazy { LayoutInflater.from(parent.context) }
val binding: ItemTodoNameBinding =
ItemTodoNameBinding.inflate(LayoutInflater.from(parent.context), parent, false)
ItemTodoNameBinding.inflate(inflater, parent, false)
return TodoNameViewHolder(binding, isCompleted)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package com.going.presentation.todo.name

import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.going.domain.entity.response.TodoAllocatorModel
import com.going.presentation.R
import com.going.presentation.databinding.ItemTodoNameBinding
import com.going.ui.extension.colorOf

class TodoNameViewHolder(
val binding: ItemTodoNameBinding, private val isCompleted: Boolean
val binding: ItemTodoNameBinding,
private val isCompleted: Boolean
) : RecyclerView.ViewHolder(binding.root) {

private val redColor = ContextCompat.getColor(binding.root.context, R.color.red_500)
private val grayColor = ContextCompat.getColor(binding.root.context, R.color.gray_400)
private val completedColor = ContextCompat.getColor(binding.root.context, R.color.gray_300)

fun onBind(item: TodoAllocatorModel) {
binding.run {
tvTodoName.text = item.name
if (isCompleted) {
tvTodoName.setTextColor(completedColor)
tvTodoName.setBackgroundResource(R.drawable.shape_rect_2_gray300_line)
} else {
if (item.isOwner) {
tvTodoName.setTextColor(redColor)
tvTodoName.isSelected = true
} else {
tvTodoName.setTextColor(grayColor)
with(binding.tvTodoName) {
text = item.name
when {
isCompleted -> {
setTextColor(binding.root.context.colorOf(R.color.gray_300))
setBackgroundResource(R.drawable.shape_rect_2_gray300_line)
}

item.isOwner -> {
setTextColor(binding.root.context.colorOf(R.color.red_500))
isSelected = true
}
Comment on lines +17 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문 말고 이렇게 바꿀 수도 있군용


else -> {
setTextColor(binding.root.context.colorOf(R.color.gray_400))
}
}
}
Expand Down
Loading