Skip to content

Commit

Permalink
[FEAT] #9 - 주차 리스트, 주차 이동, 날짜 선택
Browse files Browse the repository at this point in the history
  • Loading branch information
jung0115 committed Aug 5, 2023
1 parent c792143 commit 555fe14
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 13 deletions.
117 changes: 116 additions & 1 deletion app/src/main/java/com/ssjm/sw_hackathon/goal/GoalFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
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() {
// ViewBinding Setting
private var _binding: FragmentGoalBinding? = null
private val binding get() = _binding!!

// 실천 리스트 recyclerview adapter
private var dayOfWeekItems: MutableList<DayOfWeekItem>? = null
private lateinit var dayOfWeekAdapter: DayOfWeekAdapter

private lateinit var today: LocalDate
private lateinit var startDay: LocalDate
var progressValues = mutableListOf<String>("done", "fail", "some", "fail", "done", "some", "done")

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -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<DayOfWeekItem>()

// 링크 리스트 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<DayOfWeekItem>()
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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DayOfWeekAdapter.DayOfWeekViewHolder>() {

var items = mutableListOf<DayOfWeekItem>()

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))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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, // 선택 유무
)
10 changes: 10 additions & 0 deletions app/src/main/res/layouts/goal/drawable/shape_todo_round_none.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">

<solid android:color="@color/gray06"/>
<corners
android:radius="20dp" />

</shape>
32 changes: 20 additions & 12 deletions app/src/main/res/layouts/goal/layout/fragment_goal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
android:scaleType="fitXY"/>

<TextView
android:id="@+id/text_week_of_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
Expand All @@ -131,20 +132,27 @@

</LinearLayout>

<!-- 일자 표시 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_goal_date"
android:layout_width="match_parent"
<LinearLayout
android:id="@+id/linear_gaol_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="5dp"
android:layout_below="@id/linear_move_week"
android:layout_alignParentStart="true"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_day_of_todo"
android:gravity="center"/>
android:layout_centerHorizontal="true">

<!-- 일자 표시 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_goal_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="5dp"
android:layout_alignParentStart="true"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_day_of_todo" />

</LinearLayout>

<!-- 실천사항 내용 -->

Expand Down

0 comments on commit 555fe14

Please sign in to comment.