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

집안일 카드 상태 저장되도록 변경 #180

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.housekeeper.ui.addHousework.selectSpace

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.view.View
import androidx.fragment.app.viewModels
Expand All @@ -11,7 +12,6 @@ import androidx.recyclerview.widget.GridLayoutManager
import com.depromeet.housekeeper.R
import com.depromeet.housekeeper.base.BaseFragment
import com.depromeet.housekeeper.databinding.FragmentSelectSpaceBinding
import com.depromeet.housekeeper.model.FeedbackHouseworkResponse
import com.depromeet.housekeeper.model.SpaceChores
import com.depromeet.housekeeper.model.enums.ViewType
import com.depromeet.housekeeper.model.request.RepeatCycle
Expand Down Expand Up @@ -108,28 +108,34 @@ class SelectSpaceFragment :
val gridLayoutManager = GridLayoutManager(context, 3)
binding.selectSpaceRecyclerview.layoutManager = gridLayoutManager
binding.selectSpaceRecyclerview.addItemDecoration(VerticalItemDecorator(12))
myAdapter = SelectSpaceChoreAdapter(emptyList<String>())
myAdapter = SelectSpaceChoreAdapter(emptyList(), emptyList())
binding.selectSpaceRecyclerview.adapter = myAdapter
viewModel.setChoreAdapter(myAdapter)
}

@SuppressLint("NotifyDataSetChanged")
private fun bindingVm() {
viewModel.clearChore()
lifecycleScope.launchWhenStarted {
viewModel.choreList.collect {
myAdapter = SelectSpaceChoreAdapter(viewModel.choreList.value)
if(viewModel.selectedChorePos.value.isEmpty()){
viewModel.setIsSelectedChore(false)
}else{
viewModel.setIsSelectedChore(true)
}
myAdapter = SelectSpaceChoreAdapter(viewModel.choreList.value,viewModel.selectedChorePos.value)
myAdapter.notifyDataSetChanged()
binding.selectSpaceRecyclerview.adapter = myAdapter

myAdapter.setItemClickListener(object :
SelectSpaceChoreAdapter.OnItemClickListener {
override fun onClick(v: View, chore: String, position: Int) {
v.isSelected = !v.isSelected
Timber.d("item click $position")
viewModel.updateSelectedChorePos(position)
viewModel.updateChores(chore, v.isSelected)
viewModel.setIsSelectedChore(true)
if (viewModel.getChoreCount() == 0) {
if(viewModel.selectedChorePos.value.isEmpty()){
viewModel.setIsSelectedChore(false)
}else{
viewModel.setIsSelectedChore(true)
}
}
})
Expand Down Expand Up @@ -222,10 +228,10 @@ class SelectSpaceFragment :
dialog.showDialog()
dialog.onItemClickListener = object : FairerDialog.OnItemClickListener {
override fun onItemClick() {
setSelected()
onClick(space)
viewModel.setIsSelectedChore(false)
viewModel.clearChore()
setSelected()
onClick(space)
}
}
}
Expand Down Expand Up @@ -298,4 +304,4 @@ class SelectSpaceFragment :
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar
import java.util.Locale
import javax.inject.Inject

@HiltViewModel
Expand All @@ -23,7 +25,7 @@ class SelectSpaceViewModel @Inject constructor(
getChoreList()
}
private val _choreAdapter : MutableStateFlow<SelectSpaceChoreAdapter?> = MutableStateFlow(null)
val choreAdapter : StateFlow<SelectSpaceChoreAdapter?>
val choreAdapter: StateFlow<SelectSpaceChoreAdapter?>
get() = _choreAdapter

private val _chorePreset: MutableStateFlow<List<ChoreList>> = MutableStateFlow(listOf())
Expand All @@ -34,6 +36,10 @@ class SelectSpaceViewModel @Inject constructor(
val choreList: StateFlow<List<String>>
get() = _choreList

private val _selectedChorePos: MutableStateFlow<List<Int>> = MutableStateFlow(listOf())
val selectedChorePos: StateFlow<List<Int>>
get() = _selectedChorePos

private val _selectSpace: MutableStateFlow<String> = MutableStateFlow("")
val selectSpace: StateFlow<String>
get() = _selectSpace
Expand Down Expand Up @@ -67,6 +73,7 @@ class SelectSpaceViewModel @Inject constructor(
}

fun clearChore() {
_selectedChorePos.value = emptyList()
_chores.value = emptyList()
}

Expand All @@ -86,12 +93,18 @@ class SelectSpaceViewModel @Inject constructor(
_isSelectedSpace.value = boolean
}

fun setSpace(space: String) {
_selectSpace.value = space
fun updateSelectedChorePos(pos: Int) {
if (_selectedChorePos.value.contains(pos)) {
_selectedChorePos.value = _selectedChorePos.value.minus(pos)
Timber.d("${_selectedChorePos.value}")
} else {
_selectedChorePos.value = _selectedChorePos.value.plus(pos)
Timber.d("${_selectedChorePos.value}")
}
}

fun getChoreCount(): Int {
return _chores.value.size
fun setSpace(space: String) {
_selectSpace.value = space
}

fun updateChores(chores: String, isSelect: Boolean) {
Expand All @@ -105,10 +118,6 @@ class SelectSpaceViewModel @Inject constructor(
}
}

fun updateSelectDate(date: String) {
_selectCalendar.value = DayOfWeek(date)
}

fun updateCalendarView(year: Int, month: Int, dayOfMonth: Int) {
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
Expand Down Expand Up @@ -145,4 +154,4 @@ class SelectSpaceViewModel @Inject constructor(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.depromeet.housekeeper.databinding.ItemSelectSpaceTaskBinding

class SelectSpaceChoreAdapter(private val chores: List<String>) :
class SelectSpaceChoreAdapter(private val chores: List<String>,private val selectedPos : List<Int>) :
RecyclerView.Adapter<SelectSpaceChoreAdapter.ViewHolder>() {

interface OnItemClickListener {
Expand Down Expand Up @@ -40,6 +40,9 @@ class SelectSpaceChoreAdapter(private val chores: List<String>) :
binding.selectSpaceBtnTask.text = chore
val pos = adapterPosition
if (pos != RecyclerView.NO_POSITION) {
if(selectedPos.contains(pos)){
itemView.isSelected = true
}
binding.selectSpaceBtnTask.setOnClickListener {
itemClickListener.onClick(itemView, chore, pos)
}
Expand All @@ -48,4 +51,4 @@ class SelectSpaceChoreAdapter(private val chores: List<String>) :
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,4 @@ class SettingFragment : BaseFragment<FragmentSettingBinding>(R.layout.fragment_s
}
}
}


}