|
| 1 | +package com.shrikanthravi.collapsiblecalendarview.data |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.PorterDuff |
| 5 | +import android.view.LayoutInflater |
| 6 | +import android.view.View |
| 7 | +import android.widget.ImageView |
| 8 | +import android.widget.TextView |
| 9 | + |
| 10 | +import com.shrikanthravi.collapsiblecalendarview.R |
| 11 | + |
| 12 | +import java.util.ArrayList |
| 13 | +import java.util.Calendar |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by shrikanthravi on 06/03/18. |
| 17 | + */ |
| 18 | + |
| 19 | +class CalendarAdapter(context: Context, cal: Calendar) { |
| 20 | + private var mFirstDayOfWeek = 0 |
| 21 | + var calendar: Calendar |
| 22 | + private val mInflater: LayoutInflater |
| 23 | + |
| 24 | + private val mItemList = ArrayList<Day>() |
| 25 | + private val mViewList = ArrayList<View>() |
| 26 | + var mEventList = ArrayList<Event>() |
| 27 | + |
| 28 | + // public methods |
| 29 | + val count: Int |
| 30 | + get() = mItemList.size |
| 31 | + |
| 32 | + init { |
| 33 | + this.calendar = cal.clone() as Calendar |
| 34 | + this.calendar.set(Calendar.DAY_OF_MONTH, 1) |
| 35 | + |
| 36 | + mInflater = LayoutInflater.from(context) |
| 37 | + |
| 38 | + refresh() |
| 39 | + } |
| 40 | + |
| 41 | + fun getItem(position: Int): Day { |
| 42 | + return mItemList[position] |
| 43 | + } |
| 44 | + |
| 45 | + fun getView(position: Int): View { |
| 46 | + return mViewList[position] |
| 47 | + } |
| 48 | + |
| 49 | + fun setFirstDayOfWeek(firstDayOfWeek: Int) { |
| 50 | + mFirstDayOfWeek = firstDayOfWeek |
| 51 | + } |
| 52 | + |
| 53 | + fun addEvent(event: Event) { |
| 54 | + mEventList.add(event) |
| 55 | + } |
| 56 | + |
| 57 | + fun refresh() { |
| 58 | + // clear data |
| 59 | + mItemList.clear() |
| 60 | + mViewList.clear() |
| 61 | + |
| 62 | + // set calendar |
| 63 | + val year = calendar.get(Calendar.YEAR) |
| 64 | + val month = calendar.get(Calendar.MONTH) |
| 65 | + |
| 66 | + calendar.set(year, month, 1) |
| 67 | + |
| 68 | + val lastDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) |
| 69 | + val firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1 |
| 70 | + |
| 71 | + // generate day list |
| 72 | + val offset = 0 - (firstDayOfWeek - mFirstDayOfWeek) + 1 |
| 73 | + val length = Math.ceil(((lastDayOfMonth - offset + 1).toFloat() / 7).toDouble()).toInt() * 7 |
| 74 | + for (i in offset until length + offset) { |
| 75 | + val numYear: Int |
| 76 | + val numMonth: Int |
| 77 | + val numDay: Int |
| 78 | + |
| 79 | + val tempCal = Calendar.getInstance() |
| 80 | + if (i <= 0) { // prev month |
| 81 | + if (month == 0) { |
| 82 | + numYear = year - 1 |
| 83 | + numMonth = 11 |
| 84 | + } else { |
| 85 | + numYear = year |
| 86 | + numMonth = month - 1 |
| 87 | + } |
| 88 | + tempCal.set(numYear, numMonth, 1) |
| 89 | + numDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH) + i |
| 90 | + } else if (i > lastDayOfMonth) { // next month |
| 91 | + if (month == 11) { |
| 92 | + numYear = year + 1 |
| 93 | + numMonth = 0 |
| 94 | + } else { |
| 95 | + numYear = year |
| 96 | + numMonth = month + 1 |
| 97 | + } |
| 98 | + tempCal.set(numYear, numMonth, 1) |
| 99 | + numDay = i - lastDayOfMonth |
| 100 | + } else { |
| 101 | + numYear = year |
| 102 | + numMonth = month |
| 103 | + numDay = i |
| 104 | + } |
| 105 | + |
| 106 | + val day = Day(numYear, numMonth, numDay) |
| 107 | + |
| 108 | + val view = mInflater.inflate(R.layout.day_layout, null) |
| 109 | + val txtDay = view.findViewById<View>(R.id.txt_day) as TextView |
| 110 | + val imgEventTag = view.findViewById<View>(R.id.img_event_tag) as ImageView |
| 111 | + |
| 112 | + txtDay.text = day.day.toString() |
| 113 | + if (day.month != calendar.get(Calendar.MONTH)) { |
| 114 | + txtDay.alpha = 0.3f |
| 115 | + } |
| 116 | + |
| 117 | + for (j in mEventList.indices) { |
| 118 | + val event = mEventList[j] |
| 119 | + if (day.year == event.year |
| 120 | + && day.month == event.month |
| 121 | + && day.day == event.day) { |
| 122 | + imgEventTag.visibility = View.VISIBLE |
| 123 | + imgEventTag.setColorFilter(event.color, PorterDuff.Mode.SRC_ATOP) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + mItemList.add(day) |
| 128 | + mViewList.add(view) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments