-
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
#10 [ui] 행복루틴 추가하기(목록) 뷰 #20
Changes from all commits
7784374
bc79862
6b4c0a5
6ae9da1
558f7d0
a34b25d
6af7775
e362db2
c4de93d
c27de5a
fa6b5d9
9745584
b346cdf
51981c5
0e8eedd
ffd448a
b880f82
0cb86c6
40f307d
57e9fdb
f230357
35bb19a
f139f92
4ec8e14
1dc9f10
258ed07
7723c76
bba8d31
3a8bfe4
35410b4
b95a3e5
b39c071
61d2e5d
64df98d
03a66fe
5377e86
ea1c32e
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,6 @@ | ||
package com.sopetit.softie.domain.entity | ||
|
||
data class HappyChip( | ||
val themeId: Int, | ||
val name: String, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.sopetit.softie.domain.entity | ||
|
||
import androidx.annotation.DrawableRes | ||
|
||
data class HappyContent( | ||
val routineId: Int, | ||
val title: String, | ||
val content: String, | ||
@DrawableRes val imageUrl: Int, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.sopetit.softie.ui.main.happy.addlist | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.sopetit.softie.R | ||
import com.sopetit.softie.databinding.ActivityHappyAddListBinding | ||
import com.sopetit.softie.domain.entity.HappyContent | ||
import com.sopetit.softie.ui.main.MainActivity | ||
import com.sopetit.softie.util.HorizontalChipItemDecoration | ||
import com.sopetit.softie.util.VerticalItemDecoration | ||
import com.sopetit.softie.util.binding.BindingActivity | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class HappyAddListActivity : | ||
BindingActivity<ActivityHappyAddListBinding>(R.layout.activity_happy_add_list) { | ||
|
||
private val viewModel by viewModels<HappyAddListViewModel>() | ||
private lateinit var itemDeco: RecyclerView.ItemDecoration | ||
private lateinit var chipDeco: RecyclerView.ItemDecoration | ||
|
||
private var happyAddListChipContentAdapter: HappyAddListChipContentAdapter? = null | ||
private var happyAddListContentAdapter: HappyAddListContentAdapter? = null | ||
|
||
private val themeId = 0 | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityHappyAddListBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
setChipAdapters() | ||
setBackEnter() | ||
setItemDeco() | ||
setupAdapter(themeId) | ||
} | ||
|
||
private fun setChipAdapters() { | ||
happyAddListChipContentAdapter = HappyAddListChipContentAdapter() | ||
happyAddListContentAdapter = HappyAddListContentAdapter(::moveToDetail) | ||
Comment on lines
+41
to
+42
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. setChipAdapter 처럼 함수 분리 해주세요! |
||
} | ||
|
||
private fun setBackEnter() { | ||
binding.ivHappyAddBackArrow.setOnClickListener { | ||
finish() | ||
} | ||
Comment on lines
+46
to
+48
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. 역시나 함수 분리 부탁드립니다! setBackBtnClickListener처럼 (꼭 변수명 따라가지 않아도 됨) 부탁드립니다. |
||
} | ||
|
||
private fun setItemDeco() { | ||
itemDeco = VerticalItemDecoration(applicationContext) | ||
binding.rvHappyAddList.addItemDecoration(itemDeco) | ||
chipDeco = HorizontalChipItemDecoration(applicationContext) | ||
binding.rvHappyAddListChip.addItemDecoration(chipDeco) | ||
} | ||
|
||
private fun setupAdapter(themeId: Int) { | ||
with(binding) { | ||
rvHappyAddListChip.adapter = happyAddListChipContentAdapter | ||
rvHappyAddList.adapter = happyAddListContentAdapter | ||
} | ||
happyAddListChipContentAdapter?.submitList(viewModel.mockHappyChipList.value) | ||
happyAddListContentAdapter?.submitList(viewModel.mockHappyContentList.value) | ||
|
||
happyAddListChipContentAdapter?.setOnChipClickListener { handleChipClick(it.themeId) } | ||
} | ||
|
||
private fun handleChipClick(themeId: Int) { | ||
val listToSubmit = when (themeId) { | ||
1 -> viewModel.mockHappyContentList.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. 넘 멋져요 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. 고맙습니다 허허 |
||
2 -> viewModel.mockHappyContentListOne.value | ||
3 -> viewModel.mockHappyContentListTwo.value | ||
4 -> viewModel.mockHappyContentListThree.value | ||
5 -> viewModel.mockHappyContentListFour.value | ||
6 -> viewModel.mockHappyContentListFive.value | ||
else -> viewModel.mockHappyContentList.value | ||
Comment on lines
+71
to
+77
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. 나중에 enum class 한 번 써보세요! when을 사용할 때 else를 쓰지 않아도 됩니다! 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. 좋은 충고 감사함다 |
||
} | ||
|
||
listToSubmit?.let { makeSubmitList(it) } | ||
} | ||
|
||
private fun makeSubmitList(list: List<HappyContent>) { | ||
happyAddListContentAdapter?.submitList(list) | ||
} | ||
|
||
private fun moveToDetail(id: Int) { | ||
Intent(this, MainActivity::class.java).apply { | ||
putExtra(ID, id) | ||
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. 좋아요 |
||
startActivity(this) | ||
} | ||
} | ||
|
||
companion object { | ||
const val ID = "id" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.sopetit.softie.ui.main.happy.addlist | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.sopetit.softie.databinding.ItemHappyAddListChipBinding | ||
import com.sopetit.softie.domain.entity.HappyChip | ||
import com.sopetit.softie.util.ItemDiffCallback | ||
|
||
class HappyAddListChipContentAdapter : | ||
ListAdapter<HappyChip, HappyAddListChipContentAdapter.HappyAddListChipContentViewHolder>( | ||
ItemDiffCallback<HappyChip>( | ||
onItemsTheSame = { oldItem, newItem -> oldItem.themeId == newItem.themeId }, | ||
onContentsTheSame = { oldItem, newItem -> oldItem == newItem } | ||
) | ||
) { | ||
private val selectedPositions = HashSet<Int>() | ||
private var onItemClickListener: ((HappyChip) -> Unit)? = null | ||
|
||
inner class HappyAddListChipContentViewHolder( | ||
private val binding: ItemHappyAddListChipBinding, | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
fun onBind(data: HappyChip) { | ||
binding.itemHappyAddChipComponent.text = data.name | ||
binding.itemHappyAddChipComponent.isChecked = isSelected(adapterPosition) | ||
|
||
binding.root.setOnClickListener { | ||
chipCurrentSelection(adapterPosition) | ||
notifyDataSetChanged() | ||
onItemClickListener?.let { it(data) } | ||
} | ||
} | ||
} | ||
|
||
private fun isSelected(position: Int) = selectedPositions.contains(position) | ||
|
||
private fun chipCurrentSelection(position: Int) { | ||
if (isSelected(position)) { | ||
selectedPositions.remove(position) | ||
} else { | ||
selectedPositions.clear() | ||
selectedPositions.add(position) | ||
} | ||
} | ||
|
||
fun setOnChipClickListener(listener: (HappyChip) -> Unit) { | ||
onItemClickListener = listener | ||
} | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): HappyAddListChipContentViewHolder { | ||
val binding = ItemHappyAddListChipBinding.inflate( | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
return HappyAddListChipContentViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: HappyAddListChipContentViewHolder, position: Int) { | ||
holder.onBind(getItem(position)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.sopetit.softie.ui.main.happy.addlist | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.sopetit.softie.databinding.ItemHappyAddListBinding | ||
import com.sopetit.softie.domain.entity.HappyContent | ||
import com.sopetit.softie.util.ItemDiffCallback | ||
|
||
class HappyAddListContentAdapter(private val moveToDetail: (Int) -> Unit) : | ||
ListAdapter<HappyContent, HappyAddListContentAdapter.HappyAddListContentViewHolder>( | ||
ItemDiffCallback<HappyContent>( | ||
onItemsTheSame = { oldItem, newItem -> oldItem == newItem }, | ||
onContentsTheSame = { oldItem, newItem -> oldItem == newItem } | ||
) | ||
) { | ||
|
||
inner class HappyAddListContentViewHolder( | ||
private val binding: ItemHappyAddListBinding, | ||
private val moveToDetail: (Int) -> Unit, | ||
) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
fun onBind(data: HappyContent) { | ||
binding.tvHappyListItemTitle.text = data.title | ||
binding.tvHappyListItemContent.text = data.content | ||
binding.ivHappyListItemIcon.setImageResource(data.imageUrl) | ||
|
||
binding.root.setOnClickListener { | ||
moveToDetail(data.routineId) | ||
} | ||
Comment on lines
+29
to
+31
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. 이야!!!! 이제 리사이클러뷰 클릭리스너 마스터했네요!!! |
||
} | ||
} | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): HappyAddListContentViewHolder { | ||
val binding = ItemHappyAddListBinding.inflate( | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
return HappyAddListContentViewHolder(binding, moveToDetail) | ||
} | ||
|
||
override fun onBindViewHolder(holder: HappyAddListContentViewHolder, 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.
좋아요~