@@ -5,19 +5,21 @@ import android.graphics.Color
55import android.graphics.Typeface
66import android.view.LayoutInflater
77import android.view.View
8- import android.view.ViewDebug
98import android.view.ViewGroup
10- import android.widget.ArrayAdapter
9+ import android.widget.RelativeLayout
1110import android.widget.TextView
1211import androidx.core.content.ContextCompat
1312import androidx.core.content.res.ResourcesCompat
13+ import androidx.recyclerview.widget.RecyclerView
1414import java.util.*
1515
1616class CalendarAdapter (
17- context : Context ,
18- days : ArrayList <Calendar >, // days with events
19- private val eventDays : HashSet <Calendar >?
20- ) : ArrayAdapter<Calendar>(context, R .layout.control_calendar_day, days) {
17+ context : Context ,
18+ private val days : ArrayList <Calendar >, // days with events
19+ private val eventDays : HashSet <Calendar >? ,
20+ private var eventsHandler : CalenderViewInterface .EventHandler ? ,
21+ private var cellConfig : CellConfiguration ?
22+ ) : RecyclerView.Adapter<CalendarAdapter.MyViewHolder>() {
2123
2224 // for view inflation
2325 private val inflater: LayoutInflater
@@ -35,80 +37,108 @@ class CalendarAdapter(
3537 init {
3638 inflater = LayoutInflater .from(context)
3739 mContext = context
40+ cellConfig?.let {
41+ cell_font = it.cellFont
42+ cell_size = it.cellSize
43+ cell_bg = it.cellBg
44+ cell_txt_clr = it.cellTxtClr
45+ cell_txt_size = it.cellTxtSize
46+ cell_selected_txt_clr = it.cellSelectedClr
47+ cell_select_bg = it.cellSelectBg
48+ }
3849 }
3950
40- override fun getView (position : Int , view : View ? , parent : ViewGroup ): View {
41-
42- // day in question
43- var view = view
44- val date = getItem(position)
45-
46- val day = date!! .get(Calendar .DATE )
47- val month = date.get(Calendar .MONTH )
48- val year = date.get(Calendar .YEAR )
49-
50- // today
51- val today = Calendar .getInstance()
52-
53- // inflate item if it does not exist yet
54- if (view == null ) view = inflater.inflate(R .layout.control_calendar_day, parent, false )
55-
56- var textView = view as TextView
57- // clear styling
58- textView.setTypeface(null , Typeface .NORMAL )
59- textView.setTextColor(Color .BLACK )
60- if (month != today.get(Calendar .MONTH ) || year != today.get(Calendar .YEAR )) {
61- // if this day is outside current month, grey it out
62- textView.setTextColor(ContextCompat .getColor(mContext, R .color.greyed_out))
63- } else if (day == today.get(Calendar .DATE )) {
64- // if it is today, set it to blue/bold
65- textView.setTypeface(null , Typeface .BOLD )
66- textView.setTextColor(ContextCompat .getColor(mContext, R .color.today))
67- }
51+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): CalendarAdapter .MyViewHolder {
52+ val view = LayoutInflater .from(mContext).inflate(R .layout.row_calendar_day_layout, parent, false )
53+ return MyViewHolder (view)
54+ }
6855
69- cell_txt_size?.let {
70- textView.textSize = it.toFloat()
71- }
72- cell_txt_clr?.let {
73- textView.setTextColor(ContextCompat .getColor(mContext, it))
74- }
75- cell_font?.let {
76- textView.typeface = ResourcesCompat .getFont(context, it)
77- }
78- // set text
79- textView.text = date.get(Calendar .DATE ).toString()
56+ override fun onBindViewHolder (holder : CalendarAdapter .MyViewHolder , position : Int ) {
57+ holder.updateUi(holder,days[position])
58+ }
8059
81- // if this day has an event, specify event image
82- cell_bg?.let {
83- view.setBackgroundResource(it)
60+ override fun getItemCount (): Int {
61+ return days.size
62+ }
63+
64+ inner class MyViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {
65+
66+ var textView: TextView
67+ var rowLayout: RelativeLayout
68+
69+ init {
70+ textView = itemView.findViewById(R .id.control_calendar_day)
71+ rowLayout = itemView.findViewById(R .id.row_calendar_layout)
72+ rowLayout.setOnClickListener(this )
73+ rowLayout.setOnLongClickListener(this )
74+ cell_txt_size?.let {
75+ textView.textSize = it.toFloat()
76+ }
77+ cell_txt_clr?.let {
78+ textView.setTextColor(ContextCompat .getColor(mContext, it))
79+ }
80+ cell_font?.let {
81+ textView.typeface = ResourcesCompat .getFont(mContext, it)
82+ }
83+ cell_bg?.let {
84+ rowLayout.setBackgroundResource(it)
85+ }
8486 }
8587
86- if (eventDays != null ) {
87- for (eventDate in eventDays) {
88- if (eventDate.get(Calendar .DATE ) == day && eventDate.get(Calendar .MONTH ) == month && eventDate.get(Calendar .YEAR ) == year) {
89- // mark this day for event
90- cell_select_bg?.let {
91- view.setBackgroundResource(it)
92- }
93- cell_selected_txt_clr?.let {
94- textView.setTextColor(ContextCompat .getColor(mContext, it))
88+ fun updateUi (holder : MyViewHolder , date : Calendar ) {
89+
90+ val day = date.get(Calendar .DATE )
91+ val month = date.get(Calendar .MONTH )
92+ val year = date.get(Calendar .YEAR )
93+
94+ // today
95+ val today = Calendar .getInstance()
96+
97+ // set text
98+ holder.textView.text = date.get(Calendar .DATE ).toString()
99+
100+
101+ if (month != today.get(Calendar .MONTH ) || year != today.get(Calendar .YEAR )) {
102+ // if this day is outside current month, grey it out
103+ holder.textView.setTextColor(ContextCompat .getColor(mContext, R .color.greyed_out))
104+ } else if (day == today.get(Calendar .DATE )) {
105+ // if it is today, set it to blue/bold
106+ cell_selected_txt_clr?.let {
107+ holder.textView.setTextColor(ContextCompat .getColor(mContext, it))
108+ }
109+ cell_select_bg?.let {
110+ holder.rowLayout.setBackgroundResource(it)
111+ }
112+ }
113+
114+ if (eventDays != null ) {
115+ for (eventDate in eventDays) {
116+ if (eventDate.get(Calendar .DATE ) == day && eventDate.get(Calendar .MONTH ) == month && eventDate.get(Calendar .YEAR ) == year) {
117+ // mark this day for event
118+ cell_select_bg?.let {
119+ holder.rowLayout.setBackgroundResource(it)
120+ }
121+ cell_selected_txt_clr?.let {
122+ holder.textView.setTextColor(ContextCompat .getColor(mContext, it))
123+ }
124+ break
95125 }
96- break
97126 }
98127 }
99128 }
100129
101- return view
130+ override fun onClick (v : View ? ) {
131+ eventsHandler?.onCellClick(view = v,date = days[adapterPosition].time,adapterPosition)
132+ }
133+
134+ override fun onLongClick (v : View ? ): Boolean {
135+ eventsHandler?.onCellLongClick(view = v,date = days[adapterPosition].time,adapterPosition)
136+ return true
137+ }
102138 }
103139
104- fun setCellConfig (cellTxtClr : Int? , cellBg : Int? , cellSelectedClr : Int? , cellSelectBg : Int? , cellFont : Int? , cellSize : Int? , cellTxtSize : Int? ) {
105- cell_txt_clr = cellTxtClr
106- cell_bg = cellBg
107- cell_selected_txt_clr = cellSelectedClr
108- cell_select_bg = cellSelectBg
109- cell_font = cellFont
110- cell_size = cellSize
111- cell_txt_size = cellTxtSize
112- notifyDataSetChanged()
140+ fun setEventHandler (mEventsHandler : CalenderViewInterface .EventHandler ){
141+ this .eventsHandler = mEventsHandler
113142 }
114- }
143+
144+ }
0 commit comments