Skip to content

Commit

Permalink
refactor - 캘린더 데코레이터 클래스의 가독성 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyahozz committed Jun 30, 2022
1 parent ffb80b0 commit f797009
Showing 1 changed file with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ import java.util.*
class CalendarDecorator {
class TodayDecorator(context: Context) : DayViewDecorator {
private var date = CalendarDay.today()
private val boldSpan: StyleSpan = StyleSpan(Typeface.BOLD)
private val boldSpan = StyleSpan(Typeface.BOLD)

@SuppressLint("UseCompatLoadingForDrawables")
val drawable = ContextCompat.getDrawable(context, R.drawable.bg_calendar_today)
override fun shouldDecorate(day: CalendarDay?): Boolean {
return day?.equals(date)!!
override fun shouldDecorate(day: CalendarDay): Boolean {
return day == date
}

override fun decorate(view: DayViewFacade?) {
override fun decorate(view: DayViewFacade) {
if (drawable != null) {
view?.addSpan(object : ForegroundColorSpan(Color.White.toArgb()) {})
view?.setSelectionDrawable(drawable)
view?.addSpan(boldSpan)
view.apply {
this.addSpan(object : ForegroundColorSpan(Color.White.toArgb()) {})
this.setSelectionDrawable(drawable)
this.addSpan(boldSpan)
}
}
}
}
Expand All @@ -46,18 +48,20 @@ class CalendarDecorator {
private val mCalendar = mCalendar.currentDate.calendar
private val drawable = ContextCompat.getDrawable(context, R.drawable.bg_calendar_selection)

override fun shouldDecorate(day: CalendarDay?): Boolean {
val calendar = day!!.calendar
override fun shouldDecorate(day: CalendarDay): Boolean {
val calendar = day.calendar

return calendar.get(Calendar.ERA) == mCalendar.get(Calendar.ERA)
&& calendar.get(Calendar.YEAR) == mCalendar.get(Calendar.YEAR)
&& calendar.get(Calendar.MONTH) == mCalendar.get(Calendar.MONTH)
}

override fun decorate(view: DayViewFacade?) {
override fun decorate(view: DayViewFacade) {
if (drawable != null) {
view?.addSpan(object : ForegroundColorSpan(Gray900.toArgb()) {})
view?.setSelectionDrawable(drawable)
view.apply {
this.addSpan(object : ForegroundColorSpan(Gray900.toArgb()) {})
this.setSelectionDrawable(drawable)
}
}
}
}
Expand All @@ -66,45 +70,47 @@ class CalendarDecorator {
@SuppressLint("UseCompatLoadingForDrawables")
private val mCalendar = mCalendar.currentDate.calendar
private val drawable = ContextCompat.getDrawable(context, R.drawable.bg_calendar_selection)
override fun shouldDecorate(day: CalendarDay?): Boolean {
val calendar = day!!.calendar
override fun shouldDecorate(day: CalendarDay): Boolean {
val calendar = day.calendar

return calendar.get(Calendar.ERA) == mCalendar.get(Calendar.ERA)
&& calendar.get(Calendar.YEAR) == mCalendar.get(Calendar.YEAR)
&& calendar.get(Calendar.MONTH) != mCalendar.get(Calendar.MONTH)
}

override fun decorate(view: DayViewFacade?) {
override fun decorate(view: DayViewFacade) {
if (drawable != null) {
view?.addSpan(object : ForegroundColorSpan(Gray300.toArgb()) {})
view?.setSelectionDrawable(drawable)
view.apply {
this.addSpan(object : ForegroundColorSpan(Gray300.toArgb()) {})
this.setSelectionDrawable(drawable)
}
}
}
}

class SundayDecorator : DayViewDecorator {
private val calendar = Calendar.getInstance()
override fun shouldDecorate(day: CalendarDay?): Boolean {
day?.copyTo(calendar)
override fun shouldDecorate(day: CalendarDay): Boolean {
day.copyTo(calendar)
val weekDay = calendar.get(Calendar.DAY_OF_WEEK)
return (weekDay == Calendar.SUNDAY)
}

override fun decorate(view: DayViewFacade?) {
view?.addSpan(object : ForegroundColorSpan(SubCoral.toArgb()) {})
override fun decorate(view: DayViewFacade) {
view.addSpan(object : ForegroundColorSpan(SubCoral.toArgb()) {})
}
}

// TODO : 일정이 있는 날만 점 데코레이터 찍기
class DotDecorator : DayViewDecorator {
private var date = CalendarDay.from(2022, 5, 21)

override fun shouldDecorate(day: CalendarDay?): Boolean {
return day?.equals(date)!!
override fun shouldDecorate(day: CalendarDay): Boolean {
return day == date
}

override fun decorate(view: DayViewFacade?) {
view?.addSpan(CustomMultipleDotSpan(4F, color = intArrayOf(SubCoral.toArgb(), SubYellow.toArgb(), MainPurple900.toArgb())))
override fun decorate(view: DayViewFacade) {
view.addSpan(CustomMultipleDotSpan(4F, color = intArrayOf(SubCoral.toArgb(), SubYellow.toArgb(), MainPurple900.toArgb())))
}
}

Expand Down Expand Up @@ -133,10 +139,17 @@ class CalendarDecorator {
}

override fun drawBackground(
canvas: Canvas, paint: Paint,
left: Int, right: Int, top: Int, baseline: Int, bottom: Int,
canvas: Canvas,
paint: Paint,
left: Int,
right: Int,
top: Int,
baseline: Int,
bottom: Int,
charSequence: CharSequence,
start: Int, end: Int, lineNum: Int
start: Int,
end: Int,
lineNum: Int
) {
val total = if (color.size > 5) 5 else color.size
var leftMost = (total - 1) * - 10
Expand Down

0 comments on commit f797009

Please sign in to comment.