-
Notifications
You must be signed in to change notification settings - Fork 0
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
#8 [ui] Daily Routine - 추가하기 #18
Changes from 30 commits
30917a4
411796f
9272708
13db25d
d3b843a
c5c057f
022f0a8
b81bf10
041f285
fdecc78
a859f4e
6ec660e
db96226
bf52dfe
47d5fdf
5527708
b6ca29f
66e7e17
82afb19
ccb3045
3b8a9f9
249f79d
9f2ea54
63d2a52
14c3e99
2d71560
7b7fd6d
7432bc5
4a4db0f
d51808e
85e1ad0
ce2ab8b
f3f751c
c490a1a
f0d7e14
fe5d383
0182c04
f608e2d
914c300
7d53ab7
1ce1e29
8e73d6c
2ba41c2
a3713b0
dea9844
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.sopetit.softie.domain.entity | ||
|
||
data class DailyCard( | ||
val backgroundImageUrl: String, | ||
val routineId: Int, | ||
val content: String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.sopetit.softie.ui.dailyroutine.dailyroutineadd | ||
|
||
import android.graphics.Rect | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.activity.viewModels | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.viewpager2.widget.CompositePageTransformer | ||
import androidx.viewpager2.widget.MarginPageTransformer | ||
import androidx.viewpager2.widget.ViewPager2 | ||
import com.sopetit.softie.R | ||
import com.sopetit.softie.databinding.ActivityDailyRoutineAddBinding | ||
import com.sopetit.softie.domain.entity.Theme | ||
import com.sopetit.softie.util.binding.BindingActivity | ||
import timber.log.Timber | ||
|
||
class DailyRoutineAddActivity : | ||
BindingActivity<ActivityDailyRoutineAddBinding>(R.layout.activity_daily_routine_add) { | ||
private lateinit var viewPager: ViewPager2 | ||
private lateinit var dailyRoutineAddCardPagerAdapter: DailyRoutineAddCardPagerAdapter | ||
private lateinit var dailyRoutineAddThemeAdapter: DailyRoutineAddThemeAdapter | ||
|
||
private val dailyRoutineAddViewModel by viewModels<DailyRoutineAddViewModel>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
viewPager = binding.vpDailyRoutineAddCard | ||
binding.viewModel = dailyRoutineAddViewModel | ||
|
||
setupAdapter() | ||
setViewPager() | ||
setIndicator() | ||
setItemDiv() | ||
initPagerDiv(0, 90) | ||
} | ||
|
||
private fun setupAdapter() { | ||
with(binding) { | ||
dailyRoutineAddCardPagerAdapter = DailyRoutineAddCardPagerAdapter() | ||
vpDailyRoutineAddCard.adapter = dailyRoutineAddCardPagerAdapter | ||
dailyRoutineAddThemeAdapter = DailyRoutineAddThemeAdapter().apply { | ||
setOnItemClickListener(object : DailyRoutineAddThemeAdapter.OnItemClickListener { | ||
override fun onItemClick(item: Theme, position: Int) { | ||
setRoutineList(item) | ||
} | ||
}) | ||
} | ||
rvDailyRoutineAddTheme.adapter = dailyRoutineAddThemeAdapter | ||
} | ||
dailyRoutineAddViewModel.mockThemeList.observe(this) { | ||
dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyFirstCardList.value) | ||
dailyRoutineAddThemeAdapter.submitList(dailyRoutineAddViewModel.mockThemeList.value) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수로 따로 빼주세요!~ setupAdapter와 맞지 않는 것 같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 넵! |
||
} | ||
|
||
private fun setRoutineList(item: Theme) { | ||
Timber.d("daily routine act -> ${item.themeId}") | ||
when (item.themeId) { | ||
1 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyFirstCardList.value) | ||
2 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailySecondCardList.value) | ||
3 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyThirdCardList.value) | ||
4 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyFourthCardList.value) | ||
5 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyFifthCardList.value) | ||
6 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailySixthCardList.value) | ||
7 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailySeventhCardList.value) | ||
8 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyEighthCardList.value) | ||
9 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyNinthCardList.value) | ||
10 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyTenthCardList.value) | ||
11 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyEleventhCardList.value) | ||
12 -> dailyRoutineAddCardPagerAdapter.submitList(dailyRoutineAddViewModel.mockDailyTwelfthCardList.value) | ||
|
||
else -> dailyRoutineAddCardPagerAdapter.submitList( | ||
dailyRoutineAddViewModel.mockDailyFirstCardList.value | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. val themeDailyRoutineList = listOf(startNewDayroutineList, healthyBodyRoutineList, ...) dailyRoutineAddCardPagerAdapter.submitList(themeDailyRoutineList[item.themeId].value) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 강희언니 리뷰처럼 하나의 리스트로 처리해도 될 것 같습니다! 어처피 저희 routineId가 각각 themeId와 상관없이 모두 다르니까요...!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 좋습니당!! 바꿔보겠슴당 |
||
} | ||
|
||
private fun initPagerDiv(previewWidth: Int, itemMargin: Int) { | ||
val decoMargin = previewWidth + itemMargin | ||
val pageTransX = decoMargin + previewWidth | ||
val decoration = PageDecoration(decoMargin) | ||
|
||
binding.vpDailyRoutineAddCard.also { | ||
it.offscreenPageLimit = 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3이 쓰인 곳이 많아서, ,companion object중에 의미가 맞는 게 있는지 한 번 살펴보는 것도 좋을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 한번 찾아보겠습니당 |
||
it.addItemDecoration(decoration) | ||
it.setPageTransformer { page, position -> | ||
page.translationX = position * -pageTransX | ||
} | ||
} | ||
} | ||
|
||
private class PageDecoration(private val margin: Int) : RecyclerView.ItemDecoration() { | ||
|
||
override fun getItemOffsets( | ||
outRect: Rect, | ||
view: View, | ||
parent: RecyclerView, | ||
state: RecyclerView.State | ||
) { | ||
outRect.left = margin | ||
outRect.right = margin | ||
} | ||
} | ||
|
||
private fun setViewPager() { | ||
with(binding.vpDailyRoutineAddCard) { | ||
adapter = dailyRoutineAddCardPagerAdapter | ||
offscreenPageLimit = 3 | ||
|
||
val dpValue = 40 | ||
val margin = (dpValue * resources.displayMetrics.density).toInt() | ||
setPadding(margin, 0, margin, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 친구들도 상수로 빼면 좋을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵넵! |
||
|
||
setPageTransformer(CompositePageTransformer().apply { | ||
addTransformer(MarginPageTransformer(resources.getDimensionPixelOffset(R.dimen.viewpager_margin))) | ||
}) | ||
} | ||
} | ||
|
||
private fun setIndicator() { | ||
binding.diDailyRoutineAddIndicator.attachTo(binding.vpDailyRoutineAddCard) | ||
} | ||
|
||
class HorizontalItemDecorator(private val divHeight: Int) : RecyclerView.ItemDecoration() { | ||
@Override | ||
override fun getItemOffsets( | ||
outRect: Rect, | ||
view: View, | ||
parent: RecyclerView, | ||
state: RecyclerView.State | ||
) { | ||
super.getItemOffsets(outRect, view, parent, state) | ||
outRect.left = divHeight | ||
outRect.right = divHeight | ||
} | ||
} | ||
|
||
private fun setItemDiv() { | ||
binding.rvDailyRoutineAddTheme.addItemDecoration(HorizontalItemDecorator(16)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||
package com.sopetit.softie.ui.dailyroutine.dailyroutineadd | ||||||
|
||||||
import android.view.LayoutInflater | ||||||
import android.view.ViewGroup | ||||||
import androidx.recyclerview.widget.ListAdapter | ||||||
import androidx.recyclerview.widget.RecyclerView | ||||||
import coil.load | ||||||
import com.sopetit.softie.databinding.ItemDailyRoutineAddCardBinding | ||||||
import com.sopetit.softie.domain.entity.DailyCard | ||||||
import com.sopetit.softie.util.ItemDiffCallback | ||||||
import timber.log.Timber | ||||||
|
||||||
class DailyRoutineAddCardPagerAdapter : | ||||||
ListAdapter<DailyCard, DailyRoutineAddCardPagerAdapter.DailyPagerViewHolder>( | ||||||
ItemDiffCallback<DailyCard>( | ||||||
onItemsTheSame = { oldItem, newItem -> oldItem == newItem }, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분에 item이 고유한 값으로 지닌 값으로 비교해주세요~
Suggested change
|
||||||
onContentsTheSame = { oldItem, newItem -> oldItem == newItem } | ||||||
) | ||||||
) { | ||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DailyPagerViewHolder { | ||||||
val binding = ItemDailyRoutineAddCardBinding.inflate( | ||||||
LayoutInflater.from(parent.context), parent, false | ||||||
) | ||||||
return DailyPagerViewHolder(binding) | ||||||
} | ||||||
|
||||||
override fun onBindViewHolder(holder: DailyPagerViewHolder, position: Int) { | ||||||
holder.onBind(currentList[position]) | ||||||
} | ||||||
|
||||||
class DailyPagerViewHolder(private val binding: ItemDailyRoutineAddCardBinding) : | ||||||
RecyclerView.ViewHolder(binding.root) { | ||||||
fun onBind(data: DailyCard) { | ||||||
binding.tvDailyRoutineAddCard.load(data.backgroundImageUrl) | ||||||
Timber.d("load Image -> ${data}") | ||||||
binding.tvDailyRoutineAddCardName.text = data.content | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.sopetit.softie.ui.dailyroutine.dailyroutineadd | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.core.content.ContextCompat | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import coil.load | ||
import com.sopetit.softie.R | ||
import com.sopetit.softie.databinding.ItemDailyRoutineAddThemeBinding | ||
import com.sopetit.softie.domain.entity.Theme | ||
import com.sopetit.softie.util.ItemDiffCallback | ||
|
||
class DailyRoutineAddThemeAdapter : | ||
ListAdapter<Theme, DailyRoutineAddThemeAdapter.DailyThemeViewHolder>( | ||
ItemDiffCallback<Theme>( | ||
onItemsTheSame = { old, new -> old.themeId == new.themeId }, | ||
onContentsTheSame = { old, new -> old == new } | ||
) | ||
) { | ||
private var onItemClickListener: OnItemClickListener? = null | ||
private var selectedPosition = 0 | ||
|
||
interface OnItemClickListener { | ||
fun onItemClick(item: Theme, position: Int) | ||
} | ||
|
||
fun setOnItemClickListener(listener: OnItemClickListener) { | ||
this.onItemClickListener = listener | ||
} | ||
|
||
inner class DailyThemeViewHolder(private val binding: ItemDailyRoutineAddThemeBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
fun onBind(data: Theme) { | ||
binding.tvDailyRoutineAddThemeName.text = data.name | ||
binding.ivDailyRoutineAddThemeIcon.load(data.iconImageUrl) | ||
if (selectedPosition == absoluteAdapterPosition) { | ||
changeThemeBackground(binding, true) | ||
} else { | ||
changeThemeBackground(binding, false) | ||
} | ||
|
||
if (onItemClickListener != null) { | ||
binding.root.setOnClickListener { | ||
onItemClickListener?.onItemClick(data, absoluteAdapterPosition) | ||
if (selectedPosition != absoluteAdapterPosition) { | ||
changeThemeBackground(binding, true) | ||
notifyItemChanged(selectedPosition) | ||
selectedPosition = absoluteAdapterPosition | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun changeThemeBackground( | ||
binding: ItemDailyRoutineAddThemeBinding, | ||
selected: Boolean | ||
) { | ||
when (selected) { | ||
true -> { | ||
binding.ivDailyRoutineAddThemeBackground.setBackgroundResource(R.drawable.ic_daily_theme_background_click) | ||
binding.tvDailyRoutineAddThemeName.setTextColor( | ||
ContextCompat.getColor( | ||
binding.root.context, | ||
R.color.gray700 | ||
) | ||
) | ||
} | ||
|
||
false -> { | ||
binding.ivDailyRoutineAddThemeBackground.setBackgroundResource(R.drawable.ic_daily_theme_background) | ||
binding.tvDailyRoutineAddThemeName.setTextColor( | ||
ContextCompat.getColor( | ||
binding.root.context, | ||
R.color.gray400 | ||
) | ||
) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 imageView background와 textView color 바꾸는 부분도 따로 함수로 빼주면 더 좋을 것 같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 넵!! 잘게잘게 쪼개볼게요 |
||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DailyThemeViewHolder { | ||
val binding = ItemDailyRoutineAddThemeBinding.inflate( | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
return DailyThemeViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: DailyThemeViewHolder, position: Int) { | ||
holder.onBind(currentList[position]) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 삭제해주세요~