Skip to content

Commit 749e5a0

Browse files
committed
✨ 운동 기록 선택 페이지에서 운동 기록 선택 기능 및 선택하면 인증글 작성페이지로 넘어갈 수 있도록 설정
1 parent e9c72c9 commit 749e5a0

13 files changed

+310
-89
lines changed

presentation/src/main/java/com/whyranoid/presentation/community/SelectRunningHistoryFragment.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.whyranoid.presentation.community.runningpost
2+
3+
import android.graphics.Color
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.core.content.ContextCompat
8+
import androidx.recyclerview.widget.ListAdapter
9+
import androidx.recyclerview.widget.RecyclerView
10+
import com.whyranoid.presentation.R
11+
import com.whyranoid.presentation.databinding.ItemRunningHistoryBinding
12+
import com.whyranoid.presentation.model.RunningHistoryUiModel
13+
import com.whyranoid.presentation.myrun.MyRunningHistoryDiffCallback
14+
15+
class CommunityRunningHistoryAdapter(private val selectRunningHistoryListener: RunningHistoryItemListener) :
16+
ListAdapter<RunningHistoryUiModel, CommunityRunningHistoryViewHolder>(
17+
MyRunningHistoryDiffCallback()
18+
) {
19+
override fun onCreateViewHolder(
20+
parent: ViewGroup,
21+
viewType: Int
22+
): CommunityRunningHistoryViewHolder {
23+
val view = LayoutInflater.from(parent.context)
24+
.inflate(R.layout.item_running_history, parent, false)
25+
return CommunityRunningHistoryViewHolder(view, selectRunningHistoryListener)
26+
}
27+
28+
override fun onBindViewHolder(holder: CommunityRunningHistoryViewHolder, position: Int) {
29+
holder.bind(getItem(position))
30+
}
31+
}
32+
33+
class CommunityRunningHistoryViewHolder(
34+
view: View,
35+
private val listener: RunningHistoryItemListener
36+
) : RecyclerView.ViewHolder(view) {
37+
private val binding = ItemRunningHistoryBinding.bind(view)
38+
39+
fun bind(runningHistory: RunningHistoryUiModel) {
40+
// 아이템이 선택된 개체인지 확인
41+
42+
binding.root.setBackgroundColor(Color.TRANSPARENT)
43+
44+
binding.root.setOnClickListener {
45+
val isSelected = listener.checkRunningHistoryId(runningHistory)
46+
47+
// 이미 선택되어 있던 아이템이면
48+
if (isSelected) {
49+
listener.unSelectRunningHistory()
50+
it.setBackgroundColor(Color.TRANSPARENT)
51+
} else {
52+
// 선택되어 있지 않던 아이템이면
53+
listener.selectRunningHistory(runningHistory)
54+
it.setBackgroundColor(
55+
ContextCompat.getColor(
56+
binding.root.context,
57+
R.color.gray
58+
)
59+
)
60+
}
61+
}
62+
}
63+
}

presentation/src/main/java/com/whyranoid/presentation/community/CreateRunningPostFragment.kt renamed to presentation/src/main/java/com/whyranoid/presentation/community/runningpost/CreateRunningPostFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whyranoid.presentation.community
1+
package com.whyranoid.presentation.community.runningpost
22

33
import com.whyranoid.presentation.R
44
import com.whyranoid.presentation.base.BaseFragment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.whyranoid.presentation.community.runningpost
2+
3+
import com.whyranoid.presentation.model.RunningHistoryUiModel
4+
5+
interface RunningHistoryItemListener {
6+
fun checkRunningHistoryId(runningHistory: RunningHistoryUiModel): Boolean
7+
fun selectRunningHistory(runningHistory: RunningHistoryUiModel)
8+
fun unSelectRunningHistory()
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.whyranoid.presentation.community.runningpost
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.fragment.app.viewModels
6+
import androidx.navigation.fragment.findNavController
7+
import com.google.android.material.snackbar.Snackbar
8+
import com.whyranoid.presentation.R
9+
import com.whyranoid.presentation.base.BaseFragment
10+
import com.whyranoid.presentation.databinding.FragmentSelectRunningHistoryBinding
11+
import com.whyranoid.presentation.model.RunningHistoryUiModel
12+
import com.whyranoid.presentation.model.UiState
13+
import com.whyranoid.presentation.util.repeatWhenUiStarted
14+
import dagger.hilt.android.AndroidEntryPoint
15+
16+
@AndroidEntryPoint
17+
internal class SelectRunningHistoryFragment :
18+
BaseFragment<FragmentSelectRunningHistoryBinding>(R.layout.fragment_select_running_history),
19+
RunningHistoryItemListener {
20+
21+
private val viewModel: SelectRunningHistoryViewModel by viewModels()
22+
private val runningHistoryAdapter = CommunityRunningHistoryAdapter(this)
23+
24+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
25+
super.onViewCreated(view, savedInstanceState)
26+
27+
initViews()
28+
observeState()
29+
}
30+
31+
private fun initViews() {
32+
binding.rvSelectRunningHistory.adapter = runningHistoryAdapter
33+
setupMenu()
34+
}
35+
36+
private fun observeState() {
37+
viewLifecycleOwner.repeatWhenUiStarted {
38+
viewModel.runningHistoryListState.collect { runningHistoryState ->
39+
when (runningHistoryState) {
40+
is UiState.UnInitialized -> {
41+
// 초기화
42+
}
43+
is UiState.Loading -> {
44+
// 로딩중
45+
}
46+
47+
is UiState.Success<List<RunningHistoryUiModel>> -> handleRunningHistoryListSuccessState(
48+
runningHistoryState.value
49+
)
50+
51+
is UiState.Failure -> {
52+
// 실패
53+
}
54+
}
55+
}
56+
}
57+
58+
viewLifecycleOwner.repeatWhenUiStarted {
59+
viewModel.selectedRunningHistory.collect { selectedRunningHistory ->
60+
if (selectedRunningHistory != null) {
61+
binding.topAppBar.menu.setGroupVisible(R.id.ready_to_create_running_post, true)
62+
binding.topAppBar.menu.setGroupVisible(
63+
R.id.not_ready_to_create_running_post,
64+
false
65+
)
66+
} else {
67+
binding.topAppBar.menu.setGroupVisible(R.id.ready_to_create_running_post, false)
68+
binding.topAppBar.menu.setGroupVisible(
69+
R.id.not_ready_to_create_running_post,
70+
true
71+
)
72+
}
73+
}
74+
}
75+
}
76+
77+
private fun handleRunningHistoryListSuccessState(runningHistoryList: List<RunningHistoryUiModel>) {
78+
runningHistoryAdapter.submitList(runningHistoryList)
79+
}
80+
81+
private fun setupMenu() {
82+
with(binding.topAppBar) {
83+
inflateMenu(R.menu.community_select_running_history_menu)
84+
85+
setOnMenuItemClickListener { menuItem ->
86+
when (menuItem.itemId) {
87+
R.id.running_history_selected_button -> {
88+
val action = viewModel.getSelectedRunningHistory()?.let { runningHistory ->
89+
SelectRunningHistoryFragmentDirections.actionSelectRunningHistoryFragmentToCreateRunningPostFragment(
90+
runningHistory
91+
)
92+
}
93+
if (action != null) {
94+
findNavController().navigate(action)
95+
}
96+
true
97+
}
98+
R.id.warning_select_running_history_button -> {
99+
Snackbar.make(binding.root, getString(R.string.community_select_running_history_snack_bar), Snackbar.LENGTH_SHORT).show()
100+
true
101+
}
102+
else -> {
103+
false
104+
}
105+
}
106+
}
107+
}
108+
}
109+
110+
override fun checkRunningHistoryId(runningHistory: RunningHistoryUiModel): Boolean =
111+
viewModel.getSelectedRunningHistory()?.historyId == runningHistory.historyId
112+
113+
override fun selectRunningHistory(runningHistory: RunningHistoryUiModel) {
114+
viewModel.setSelectedRunningHistory(runningHistory)
115+
}
116+
117+
override fun unSelectRunningHistory() {
118+
viewModel.setSelectedRunningHistory(null)
119+
}
120+
}

presentation/src/main/java/com/whyranoid/presentation/community/SelectRunningHistoryViewModel.kt renamed to presentation/src/main/java/com/whyranoid/presentation/community/runningpost/SelectRunningHistoryViewModel.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whyranoid.presentation.community
1+
package com.whyranoid.presentation.community.runningpost
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
@@ -26,6 +26,11 @@ class SelectRunningHistoryViewModel @Inject constructor(private val getRunningHi
2626
val runningHistoryListState: StateFlow<UiState<List<RunningHistoryUiModel>>>
2727
get() = _runningHistoryListState.asStateFlow()
2828

29+
// TODO 인증글 작성 넘어가도록 하는 더미데이터 -> 인증글 작성 로직이 완료되면 삭제하겠습니다
30+
private val _selectedRunningHistory = MutableStateFlow<RunningHistoryUiModel?>(RunningHistoryUiModel("id", "asdf", "asdf", "asdf", "adsf", "asd", "zcx"))
31+
val selectedRunningHistory: StateFlow<RunningHistoryUiModel?>
32+
get() = _selectedRunningHistory.asStateFlow()
33+
2934
private fun getRunningHistoryList() {
3035
viewModelScope.launch {
3136
_runningHistoryListState.value = UiState.Loading
@@ -40,4 +45,12 @@ class SelectRunningHistoryViewModel @Inject constructor(private val getRunningHi
4045
}
4146
}
4247
}
48+
49+
fun getSelectedRunningHistory(): RunningHistoryUiModel? {
50+
return _selectedRunningHistory.value
51+
}
52+
53+
fun setSelectedRunningHistory(runningHistoryUiModel: RunningHistoryUiModel?) {
54+
_selectedRunningHistory.value = runningHistoryUiModel
55+
}
4356
}

presentation/src/main/java/com/whyranoid/presentation/myrun/CalendarDayBinder.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ class CalendarDayBinder(
3737
)
3838
// day.day와 day.date.monthValue를 지정해서 특정 월, 일에 달렸다는 콩 표시 가능
3939
} else if (day.day == 10 && day.date.monthValue == 11) {
40-
container.binding.root.background = (
40+
container.binding.root.background =
4141
ContextCompat.getDrawable(
4242
calendarView.context,
4343
R.drawable.calendar_kong
4444
)
45-
)
4645
} else {
4746
container.binding.tvCalendarDay.setTextColor(
4847
ContextCompat.getColor(
@@ -63,19 +62,17 @@ class CalendarDayBinder(
6362
}
6463

6564
if (startDate == day.date) {
66-
container.binding.root.background = (
65+
container.binding.root.background =
6766
ContextCompat.getDrawable(
6867
calendarView.context,
6968
R.drawable.thumbnail_src_small
7069
)
71-
)
7270
} else if (endDate == day.date) {
73-
container.binding.root.background = (
71+
container.binding.root.background =
7472
ContextCompat.getDrawable(
7573
calendarView.context,
7674
R.drawable.thumbnail_src_small
7775
)
78-
)
7976
}
8077
}
8178

presentation/src/main/java/com/whyranoid/presentation/myrun/MyRunningHistoryAdapter.kt

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,41 @@ import com.whyranoid.presentation.databinding.ItemRunningHistoryBinding
1111
import com.whyranoid.presentation.model.RunningHistoryUiModel
1212

1313
class MyRunningHistoryAdapter :
14-
ListAdapter<RunningHistoryUiModel, RunningHistoryViewHolder>(
14+
ListAdapter<RunningHistoryUiModel, MyRunningHistoryViewHolder>(
1515
MyRunningHistoryDiffCallback()
1616
) {
1717

18-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RunningHistoryViewHolder {
18+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyRunningHistoryViewHolder {
1919
val view = LayoutInflater.from(parent.context)
2020
.inflate(R.layout.item_running_history, parent, false)
21-
return RunningHistoryViewHolder(view)
21+
return MyRunningHistoryViewHolder(view)
2222
}
2323

24-
override fun onBindViewHolder(holder: RunningHistoryViewHolder, position: Int) {
24+
override fun onBindViewHolder(holder: MyRunningHistoryViewHolder, position: Int) {
2525
holder.bind(getItem(position))
2626
}
27-
28-
companion object {
29-
class MyRunningHistoryDiffCallback : DiffUtil.ItemCallback<RunningHistoryUiModel>() {
30-
override fun areItemsTheSame(
31-
oldItem: RunningHistoryUiModel,
32-
newItem: RunningHistoryUiModel
33-
): Boolean {
34-
return oldItem.historyId == newItem.historyId
35-
}
36-
37-
override fun areContentsTheSame(
38-
oldItem: RunningHistoryUiModel,
39-
newItem: RunningHistoryUiModel
40-
): Boolean {
41-
return oldItem == newItem
42-
}
43-
}
44-
}
4527
}
4628

47-
class RunningHistoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
29+
class MyRunningHistoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
4830
private val binding = ItemRunningHistoryBinding.bind(view)
4931

5032
fun bind(runningHistory: RunningHistoryUiModel) {
5133
binding.runningHistory = runningHistory
5234
}
5335
}
36+
37+
class MyRunningHistoryDiffCallback : DiffUtil.ItemCallback<RunningHistoryUiModel>() {
38+
override fun areItemsTheSame(
39+
oldItem: RunningHistoryUiModel,
40+
newItem: RunningHistoryUiModel
41+
): Boolean {
42+
return oldItem.historyId == newItem.historyId
43+
}
44+
45+
override fun areContentsTheSame(
46+
oldItem: RunningHistoryUiModel,
47+
newItem: RunningHistoryUiModel
48+
): Boolean {
49+
return oldItem == newItem
50+
}
51+
}

presentation/src/main/res/layout/fragment_create_running_post.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
style="@style/Widget.MaterialComponents.Toolbar.Primary"
2424
android:layout_width="match_parent"
2525
android:layout_height="?attr/actionBarSize"
26+
app:menu="@menu/community_create_running_post_menu"
2627
app:title="@string/community_create_running_post_title" />
2728

2829
</com.google.android.material.appbar.AppBarLayout>

0 commit comments

Comments
 (0)