diff --git a/app/src/main/java/com/ssjm/sw_hackathon/goal/GoalFragment.kt b/app/src/main/java/com/ssjm/sw_hackathon/goal/GoalFragment.kt index 82e1a24..6cbf983 100644 --- a/app/src/main/java/com/ssjm/sw_hackathon/goal/GoalFragment.kt +++ b/app/src/main/java/com/ssjm/sw_hackathon/goal/GoalFragment.kt @@ -1,11 +1,22 @@ package com.ssjm.sw_hackathon.goal +import android.os.Build import android.os.Bundle -import androidx.fragment.app.Fragment +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.annotation.RequiresApi +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager import com.ssjm.sw_hackathon.databinding.FragmentGoalBinding +import com.ssjm.sw_hackathon.goal.recycler.DayOfWeekAdapter +import com.ssjm.sw_hackathon.goal.recycler.DayOfWeekItem +import java.time.DayOfWeek +import java.time.LocalDate +import java.time.format.TextStyle +import java.util.Locale + // 목표 탭 class GoalFragment : Fragment() { @@ -13,6 +24,14 @@ class GoalFragment : Fragment() { private var _binding: FragmentGoalBinding? = null private val binding get() = _binding!! + // 실천 리스트 recyclerview adapter + private var dayOfWeekItems: MutableList? = null + private lateinit var dayOfWeekAdapter: DayOfWeekAdapter + + private lateinit var today: LocalDate + private lateinit var startDay: LocalDate + var progressValues = mutableListOf("done", "fail", "some", "fail", "done", "some", "done") + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -22,8 +41,104 @@ class GoalFragment : Fragment() { return binding.root } + @RequiresApi(Build.VERSION_CODES.O) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + + initRecycler() + + // 오늘 날짜 + today = LocalDate.now() + + // 요일 + val dayOfWeek: DayOfWeek = today.getDayOfWeek() + //val todayOfWeek = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.KOREAN) + val todayOfWeekNum = dayOfWeek.value // 요일 1~7 + startDay = today.minusDays((todayOfWeekNum - 1).toLong()) + + // 이번주 + setDate(startDay) + + // 저번주 + binding.btnMoveWeekLeft.setOnClickListener { + startDay = startDay.minusDays(7L) + setDate(startDay) + } + // 다음주 + binding.btnMoveWeekRight.setOnClickListener { + startDay = startDay.plusDays(7L) + setDate(startDay) + } + } + private fun initRecycler() { + dayOfWeekItems = mutableListOf() + + // 링크 리스트 recyclerview 세팅 + dayOfWeekAdapter = DayOfWeekAdapter( + requireContext() + ) + binding.recyclerviewGoalDate.layoutManager = + LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + binding.recyclerviewGoalDate.adapter = dayOfWeekAdapter + binding.recyclerviewGoalDate.isNestedScrollingEnabled = false + dayOfWeekAdapter.items = dayOfWeekItems!! + } + + @RequiresApi(Build.VERSION_CODES.O) + private fun setDate(startDay: LocalDate) { + dayOfWeekItems = mutableListOf() + dayOfWeekAdapter.items = dayOfWeekItems!! + dayOfWeekAdapter.notifyDataSetChanged() + + // 주차 표시 + val weekOfMonth: String = startDay.month.value.toString() + "월 " + getWeekOfMonth(startDay).toString() + "주차" + binding.textWeekOfMonth.text = weekOfMonth + + for(i: Long in 0L..6L) { + val date = startDay.plusDays(i) + if(date < today) { + addDay( + DayOfWeekItem( + date, // 날짜 + date.dayOfMonth, // 일 + date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.KOREAN), // 요일 + progressValues[i.toInt()], + ) + ) + } + else if(date == today) { + addDay( + DayOfWeekItem( + date, // 날짜 + date.dayOfMonth, // 일 + date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.KOREAN), // 요일 + progressValues[i.toInt()], + true + ) + ) + } + else { + addDay( + DayOfWeekItem( + date, // 날짜 + date.dayOfMonth, // 일 + date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.KOREAN), // 요일 + "none" + ) + ) + } + } + } + + // 주차 계산 + @RequiresApi(Build.VERSION_CODES.O) + private fun getWeekOfMonth(startDay: LocalDate): Int { + return ((startDay.dayOfMonth) / 7).toInt() + 1 + } + + private fun addDay(day: DayOfWeekItem) { + dayOfWeekItems!!.add(day) + dayOfWeekAdapter.notifyDataSetChanged() } override fun onDestroy() { diff --git a/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekAdapter.kt b/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekAdapter.kt new file mode 100644 index 0000000..b1e0e8c --- /dev/null +++ b/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekAdapter.kt @@ -0,0 +1,79 @@ +package com.ssjm.sw_hackathon.goal.recycler + +import android.content.Context +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.ssjm.sw_hackathon.R +import com.ssjm.sw_hackathon.databinding.ItemDayOfTodoBinding + +class DayOfWeekAdapter (private val context: Context, +) : RecyclerView.Adapter() { + + var items = mutableListOf() + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : + DayOfWeekViewHolder { + + val view = LayoutInflater.from(context).inflate(R.layout.item_day_of_todo, parent, false) + + return DayOfWeekViewHolder(ItemDayOfTodoBinding.bind(view)) + } + + override fun getItemCount(): Int = items.size + + override fun onBindViewHolder(holder: DayOfWeekViewHolder, position: Int) { + holder.bind(items[position]) + + // 날짜 아이템 클릭 시 + holder.itemView.setOnClickListener { + for(i: Int in 0..(items.size - 1)) { + if(i == position) + items[i].selected = true + else + items[i].selected = false + } + notifyDataSetChanged() + } + } + + inner class DayOfWeekViewHolder(private val binding: ItemDayOfTodoBinding) : + RecyclerView.ViewHolder(binding.root) { + + fun bind(item: DayOfWeekItem) { + // 요일 + binding.textTodoListDay.text = item.dayOfWeek + + // 일 + binding.textTodoListDate.text = item.day.toString() + + // 달성도 + if(item.progress == "done") { + binding.textTodoListDate.setTextColor(context.resources.getColor(R.color.white)) + binding.textTodoListDate.setBackgroundResource(R.drawable.shape_todo_round_done) + } + else if(item.progress == "fail") { + binding.textTodoListDate.setTextColor(context.resources.getColor(R.color.white)) + binding.textTodoListDate.setBackgroundResource(R.drawable.shape_todo_round_fail) + } + else if(item.progress == "some") { + binding.textTodoListDate.setTextColor(context.resources.getColor(R.color.white)) + binding.textTodoListDate.setBackgroundResource(R.drawable.shape_todo_round_some) + } + else if(item.progress == "none") { + binding.textTodoListDate.setTextColor(context.resources.getColor(R.color.gray04)) + binding.textTodoListDate.setBackgroundResource(R.drawable.shape_todo_round_none) + } + + // 선택 유무 + if(item.selected) { + binding.linearTodoBox.setBackgroundResource(R.drawable.shape_todo_today) + binding.textTodoListDay.setTextColor(context.resources.getColor(R.color.white)) + } + else { + binding.linearTodoBox.setBackgroundResource(R.drawable.shape_todo_today_not) + binding.textTodoListDay.setTextColor(context.resources.getColor(R.color.black03)) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekItem.kt b/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekItem.kt new file mode 100644 index 0000000..aa11ccb --- /dev/null +++ b/app/src/main/java/com/ssjm/sw_hackathon/goal/recycler/DayOfWeekItem.kt @@ -0,0 +1,11 @@ +package com.ssjm.sw_hackathon.goal.recycler + +import java.time.LocalDate + +data class DayOfWeekItem( + var date: LocalDate, // 날짜 + var day: Int, // 일 + var dayOfWeek: String, // 요일 + var progress: String, // 진행도: done, some, fail, none + var selected: Boolean = false, // 선택 유무 +) diff --git a/app/src/main/res/layouts/goal/drawable/shape_todo_round_none.xml b/app/src/main/res/layouts/goal/drawable/shape_todo_round_none.xml new file mode 100644 index 0000000..d1823c2 --- /dev/null +++ b/app/src/main/res/layouts/goal/drawable/shape_todo_round_none.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layouts/goal/layout/fragment_goal.xml b/app/src/main/res/layouts/goal/layout/fragment_goal.xml index 7143f83..a0c4043 100644 --- a/app/src/main/res/layouts/goal/layout/fragment_goal.xml +++ b/app/src/main/res/layouts/goal/layout/fragment_goal.xml @@ -112,6 +112,7 @@ android:scaleType="fitXY"/> - - + android:layout_centerHorizontal="true"> + + + + +