diff --git a/data/src/main/java/com/depromeet/threedays/data/mapper/GoalMapper.kt b/data/src/main/java/com/depromeet/threedays/data/mapper/GoalMapper.kt index 53b1e731..b5adf67c 100644 --- a/data/src/main/java/com/depromeet/threedays/data/mapper/GoalMapper.kt +++ b/data/src/main/java/com/depromeet/threedays/data/mapper/GoalMapper.kt @@ -8,32 +8,25 @@ fun Goal.toGoalEntity(): GoalEntity { return GoalEntity( goalId = this.goalId, title = this.title, - startDate = this.startDate.toString(), - endDate = this.endDate.toString(), - startTime = this.startTime.toString(), - notificationTime = this.notificationTime.toString(), + startDate = this.startDate?.toString(), + endDate = this.endDate?.toString(), + startTime = this.startTime?.toString(), + notificationTime = this.notificationTime?.toString(), notificationContent = this.notificationContent, - status = this.status.toString(), - createDate = this.createDate.toString(), + status = this.status?.toString(), + createDate = this.createDate?.toString(), sequence = this.sequence, clapIndex = this.clapIndex, clapChecked = this.clapChecked, - lastAchievementDate = this.lastAchievementDate.toString(), + lastAchievementDate = this.lastAchievementDate?.toString(), ) } fun GoalEntity.toGoal() = Goal( goalId = this.goalId, title = this.title, - startDate = ZonedDateTime.parse(this.startDate), - endDate = ZonedDateTime.parse(this.endDate), - startTime = ZonedDateTime.parse(this.startTime), - notificationTime = ZonedDateTime.parse(this.notificationTime), notificationContent = this.notificationContent, - status = ZonedDateTime.parse(this.status), - createDate = ZonedDateTime.parse(this.createDate), sequence = this.sequence, clapIndex = this.clapIndex, clapChecked = this.clapChecked, - lastAchievementDate = ZonedDateTime.parse(this.lastAchievementDate), ) diff --git a/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalAdapter.kt b/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalAdapter.kt index 811ab359..5a9ee924 100644 --- a/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalAdapter.kt +++ b/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalAdapter.kt @@ -4,15 +4,16 @@ import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import com.depromeet.threedays.domain.entity.Goal +import kotlin.reflect.KFunction1 -class GoalAdapter(val viewModel: HomeViewModel) : ListAdapter(DIFF_UTIL) { +class GoalAdapter(private val onGoalClick: KFunction1) : ListAdapter(DIFF_UTIL) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoalViewHolder { return GoalViewHolder.create(parent, false) } override fun onBindViewHolder(holder: GoalViewHolder, position: Int) { - holder.onBind(getItem(position), viewModel) + holder.onBind(getItem(position), onGoalClick) } override fun getItemCount(): Int = currentList.size @@ -24,7 +25,7 @@ class GoalAdapter(val viewModel: HomeViewModel) : ListAdapter() { override fun areItemsTheSame(oldItem: Goal, newItem: Goal): Boolean { - return oldItem.goalId == newItem.goalId + return oldItem == newItem } override fun areContentsTheSame(oldItem: Goal, newItem: Goal): Boolean { diff --git a/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalViewHolder.kt b/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalViewHolder.kt index 41d53630..87ab58ba 100644 --- a/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalViewHolder.kt +++ b/presentation/home/src/main/java/com/depromeet/threedays/home/home/GoalViewHolder.kt @@ -2,14 +2,46 @@ package com.depromeet.threedays.home.home import android.view.LayoutInflater import android.view.ViewGroup +import android.widget.ImageButton import androidx.recyclerview.widget.RecyclerView import com.depromeet.threedays.domain.entity.Goal +import com.depromeet.threedays.home.R import com.depromeet.threedays.home.databinding.ItemGoalBinding +import kotlin.reflect.KFunction1 class GoalViewHolder(private val view: ItemGoalBinding) : RecyclerView.ViewHolder(view.root) { - fun onBind(goal: Goal, viewModel: HomeViewModel) { + + fun onBind(goal: Goal, onGoalClick: KFunction1) { view.goal = goal - view.viewModel = viewModel + view.ivFirstDay.setOnClickListener { + switchGoalState(0, goal, onGoalClick, it as ImageButton) + } + view.ivSecondDay.setOnClickListener { + switchGoalState(1, goal, onGoalClick, it as ImageButton) + } + view.ivThirdDay.setOnClickListener { + switchGoalState(2, goal, onGoalClick, it as ImageButton) + } + } + + private fun switchGoalState( + clickedIndex: Int, + goal: Goal, + onGoalClick: KFunction1, + view: ImageButton + ) { + if (goal.clapIndex == clickedIndex) { + goal.clapChecked = !goal.clapChecked + onGoalClick(goal) + + if (goal.clapChecked) { + view.background = itemView.resources.getDrawable(R.drawable.bg_oval_white, null) + view.setImageResource(com.depromeet.threedays.core_design_system.R.drawable.ic_hand_done) + } else { + view.background = itemView.resources.getDrawable(R.drawable.bg_oval_gray, null) + view.setImageResource(com.depromeet.threedays.core_design_system.R.drawable.ic_hand_normal) + } + } } companion object { diff --git a/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeFragment.kt b/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeFragment.kt index 7c4e90c4..67251777 100644 --- a/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeFragment.kt +++ b/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeFragment.kt @@ -4,7 +4,6 @@ import android.graphics.Rect import android.os.Bundle import android.view.View import androidx.fragment.app.viewModels -import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.depromeet.threedays.core.BaseFragment @@ -12,9 +11,6 @@ import com.depromeet.threedays.domain.entity.Goal import com.depromeet.threedays.home.R import com.depromeet.threedays.home.databinding.FragmentHomeBinding import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.flow.filter -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach @AndroidEntryPoint class HomeFragment : BaseFragment(R.layout.fragment_home) { @@ -24,12 +20,20 @@ class HomeFragment : BaseFragment(R.layout.f override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - goalAdapter = GoalAdapter(viewModel) + initAdapter() viewModel.fetchGoals() viewModel.setObserve() initView() } + private fun onGoalClick(goal: Goal) { + viewModel.updateGoals(goal) + } + + private fun initAdapter() { + goalAdapter = GoalAdapter(::onGoalClick) + } + private fun initView() { // 우선 임시로 setOnClickListener를 달아놨습니다. // 혜인님 레포에 있는 것처럼 setOnSingleClickListener를 만들어서 사용할 지 여부와 @@ -59,12 +63,10 @@ class HomeFragment : BaseFragment(R.layout.f } private fun HomeViewModel.setObserve() { - goals - .filter { it.isNotEmpty() } - .onEach { - goalAdapter.submitList(it) - } - .launchIn(viewLifecycleOwner.lifecycleScope) + goals.observe(viewLifecycleOwner) { + goalAdapter.submitList(it) + goalAdapter.notifyDataSetChanged() + } } private fun tempCreateData() { diff --git a/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeViewModel.kt b/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeViewModel.kt index fe067ca9..bb80a28c 100644 --- a/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeViewModel.kt +++ b/presentation/home/src/main/java/com/depromeet/threedays/home/home/HomeViewModel.kt @@ -1,5 +1,7 @@ package com.depromeet.threedays.home.home +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.depromeet.threedays.core.BaseViewModel import com.depromeet.threedays.domain.entity.Goal @@ -7,8 +9,6 @@ import com.depromeet.threedays.domain.usecase.CreateGoalUseCase import com.depromeet.threedays.domain.usecase.GetAllGoalsUseCase import com.depromeet.threedays.domain.usecase.UpdateGoalUseCase import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import javax.inject.Inject @@ -19,16 +19,15 @@ class HomeViewModel @Inject constructor( private val createGoalUseCase: CreateGoalUseCase ) : BaseViewModel() { - private val _goals: MutableStateFlow> = MutableStateFlow(emptyList()) - val goals: StateFlow> + private val _goals: MutableLiveData> = MutableLiveData(emptyList()) + val goals: LiveData> get() = _goals fun fetchGoals() { viewModelScope.launch { - try { - _goals.value = getAllGoalsUseCase() - } catch (exception: Exception) { - exception.printStackTrace() + val response = getAllGoalsUseCase() + if (response != null) { + _goals.postValue(response) } } } @@ -43,15 +42,12 @@ class HomeViewModel @Inject constructor( } } - fun onGoalClick(goal: Goal, clickedIndex: Int) { - if (goal.clapIndex == clickedIndex) { - viewModelScope.launch { - try { - goal.apply { clapChecked = !clapChecked } - updateGoalUseCase(goal) - } catch (exception: Exception) { - exception.printStackTrace() - } + fun updateGoals(goal: Goal) { + viewModelScope.launch { + try { + updateGoalUseCase(goal) + } catch (exception: Exception) { + exception.printStackTrace() } } } diff --git a/presentation/home/src/main/res/layout/item_goal.xml b/presentation/home/src/main/res/layout/item_goal.xml index 5e22623e..385f430d 100644 --- a/presentation/home/src/main/res/layout/item_goal.xml +++ b/presentation/home/src/main/res/layout/item_goal.xml @@ -8,10 +8,6 @@ - -