Skip to content

Commit

Permalink
DEP-68 feat: 목표 활성 비활성화 구현 및 버그 수정 (#27)
Browse files Browse the repository at this point in the history
* refactor: Goal 자료형 변경 및 그로 인한 변경사항들

* feat: 목표 DB 연동

* fix: 해성님 파일과 conflict 해결

* chore: 자료형 변경으로 혜인님 코드 잠시 주석

* feat: 목표 활성/비활성화 구현

* chore: 불 필요한 코드 삭제 및 nullable 추가
  • Loading branch information
juhwankim-dev authored Oct 23, 2022
1 parent a34812c commit 357fb89
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<Goal, GoalViewHolder>(DIFF_UTIL) {
class GoalAdapter(private val onGoalClick: KFunction1<Goal, Unit>) : ListAdapter<Goal, GoalViewHolder>(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
Expand All @@ -24,7 +25,7 @@ class GoalAdapter(val viewModel: HomeViewModel) : ListAdapter<Goal, GoalViewHold
companion object {
private val DIFF_UTIL = object : DiffUtil.ItemCallback<Goal>() {
override fun areItemsTheSame(oldItem: Goal, newItem: Goal): Boolean {
return oldItem.goalId == newItem.goalId
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: Goal, newItem: Goal): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Goal, Unit>) {
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<Goal, Unit>,
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ 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
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<FragmentHomeBinding, HomeViewModel>(R.layout.fragment_home) {
Expand All @@ -24,12 +20,20 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(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를 만들어서 사용할 지 여부와
Expand Down Expand Up @@ -59,12 +63,10 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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
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

Expand All @@ -19,16 +19,15 @@ class HomeViewModel @Inject constructor(
private val createGoalUseCase: CreateGoalUseCase
) : BaseViewModel() {

private val _goals: MutableStateFlow<List<Goal>> = MutableStateFlow(emptyList())
val goals: StateFlow<List<Goal>>
private val _goals: MutableLiveData<List<Goal>> = MutableLiveData(emptyList())
val goals: LiveData<List<Goal>>
get() = _goals

fun fetchGoals() {
viewModelScope.launch {
try {
_goals.value = getAllGoalsUseCase()
} catch (exception: Exception) {
exception.printStackTrace()
val response = getAllGoalsUseCase()
if (response != null) {
_goals.postValue(response)
}
}
}
Expand All @@ -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()
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions presentation/home/src/main/res/layout/item_goal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<variable
name="goal"
type="com.depromeet.threedays.domain.entity.Goal" />

<variable
name="viewModel"
type="com.depromeet.threedays.home.home.HomeViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -60,7 +56,6 @@
android:layout_gravity="center"
android:layout_marginTop="19dp"
android:background="@{goal.isChecked(0) ? @drawable/bg_oval_white : @drawable/bg_oval_gray}"
android:onClick="@{()->viewModel.onGoalClick(goal, 0)}"
android:src="@{goal.isChecked(0) ? @drawable/ic_hand_done : @drawable/ic_hand_normal}"
app:layout_constraintEnd_toStartOf="@+id/iv_second_day"
app:layout_constraintHorizontal_bias="0.5"
Expand All @@ -74,7 +69,6 @@
android:layout_height="84dp"
android:layout_gravity="center"
android:background="@{goal.isChecked(1) ? @drawable/bg_oval_white : @drawable/bg_oval_gray}"
android:onClick="@{()->viewModel.onGoalClick(goal, 1)}"
android:src="@{goal.isChecked(1) ? @drawable/ic_hand_done : @drawable/ic_hand_normal}"
app:layout_constraintBottom_toBottomOf="@+id/iv_first_day"
app:layout_constraintEnd_toStartOf="@+id/iv_third_day"
Expand All @@ -88,7 +82,6 @@
android:layout_height="84dp"
android:layout_gravity="center"
android:background="@{goal.isChecked(2) ? @drawable/bg_oval_white : @drawable/bg_oval_gray}"
android:onClick="@{()->viewModel.onGoalClick(goal, 2)}"
android:src="@{goal.isChecked(2) ? @drawable/ic_hand_done : @drawable/ic_hand_normal}"
app:layout_constraintBottom_toBottomOf="@+id/iv_second_day"
app:layout_constraintEnd_toEndOf="parent"
Expand Down

0 comments on commit 357fb89

Please sign in to comment.