11package me.alllexey123.itmowidgets.ui.schedule
22
3+ import android.os.Handler
4+ import android.os.Looper
35import android.view.LayoutInflater
46import android.view.View
57import android.view.ViewGroup
@@ -13,21 +15,21 @@ import me.alllexey123.itmowidgets.R
1315import me.alllexey123.itmowidgets.util.ScheduleUtils
1416import java.time.LocalTime
1517import java.time.format.DateTimeFormatter
16- import kotlin.math.max
1718
1819class LessonAdapter (private val scheduleList : List <ScheduleItem >) :
1920 RecyclerView .Adapter <RecyclerView .ViewHolder >() {
2021
2122 companion object {
2223 private const val VIEW_TYPE_LESSON = 1
2324 private const val VIEW_TYPE_BREAK = 2
25+ private const val VIEW_TYPE_NO_LESSONS = 3
2426 }
2527
2628 override fun getItemViewType (position : Int ): Int {
27- if (scheduleList.isEmpty()) return VIEW_TYPE_LESSON
2829 return when (scheduleList[position]) {
2930 is ScheduleItem .LessonItem -> VIEW_TYPE_LESSON
3031 is ScheduleItem .BreakItem -> VIEW_TYPE_BREAK
32+ is ScheduleItem .NoLessonsItem -> VIEW_TYPE_NO_LESSONS
3133 }
3234 }
3335
@@ -36,7 +38,7 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
3638 return when (viewType) {
3739 VIEW_TYPE_LESSON -> {
3840 val view = LayoutInflater .from(parent.context)
39- .inflate(R .layout.item_lesson_list_entry_dot , parent, false )
41+ .inflate(R .layout.item_schedule_lesson , parent, false )
4042 LessonViewHolder (view)
4143 }
4244
@@ -46,28 +48,52 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
4648 BreakViewHolder (view)
4749 }
4850
51+ VIEW_TYPE_NO_LESSONS -> {
52+ val view = LayoutInflater .from(parent.context)
53+ .inflate(R .layout.item_schedule_lesson, parent, false )
54+ LessonViewHolder (view)
55+ }
56+
4957 else -> throw IllegalArgumentException (" Invalid view type" )
5058 }
5159 }
5260
5361 override fun onBindViewHolder (holder : RecyclerView .ViewHolder , position : Int ) {
54- if (scheduleList.isEmpty()) {
55- bindEmptyDay(holder as LessonViewHolder )
56- return
57- }
5862 when (val item = scheduleList[position]) {
5963 is ScheduleItem .LessonItem -> {
60- bindLesson(holder as LessonViewHolder , item.lesson, position)
64+ bindLesson(holder as LessonViewHolder , item.lesson, item.lessonState, position)
6165 }
6266
6367 is ScheduleItem .BreakItem -> {
6468 bindBreak(holder as BreakViewHolder , item.from, item.to)
6569 }
70+
71+ is ScheduleItem .NoLessonsItem -> {
72+ bindEmptyDay(holder as LessonViewHolder , item.lessonState)
73+ }
6674 }
6775 }
6876
69- private fun bindEmptyDay (holder : LessonViewHolder ) {
77+ override fun onViewRecycled (holder : RecyclerView .ViewHolder ) {
78+ super .onViewRecycled(holder)
79+ if (holder is LessonViewHolder ) {
80+ holder.stopBlinking()
81+ }
82+ }
83+
84+ private fun bindEmptyDay (holder : LessonViewHolder , lessonState : ScheduleItem .LessonState ) {
85+ holder.stopBlinking()
7086 holder.lessonTitle.text = " В этот день нет пар"
87+ val typeIndicatorDrawable = when (lessonState) {
88+ ScheduleItem .LessonState .COMPLETED -> R .drawable.indicator_circle
89+ ScheduleItem .LessonState .CURRENT -> {
90+ holder.startBlinking()
91+ R .drawable.indicator_circle_hollow_dot
92+ }
93+ ScheduleItem .LessonState .UPCOMING -> R .drawable.indicator_circle_hollow
94+ }
95+
96+ holder.typeIndicator.setImageResource(typeIndicatorDrawable)
7197 holder.typeIndicator.setColorFilter(
7298 ContextCompat .getColor(holder.itemView.context, R .color.free_color)
7399 )
@@ -78,9 +104,26 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
78104 return
79105 }
80106
81- private fun bindLesson (holder : LessonViewHolder , lesson : Lesson , position : Int ) {
107+ private fun bindLesson (
108+ holder : LessonViewHolder ,
109+ lesson : Lesson ,
110+ lessonState : ScheduleItem .LessonState ,
111+ position : Int
112+ ) {
113+ holder.stopBlinking()
82114 holder.lessonTitle.text = lesson.subject ? : " Неизвестная дисциплина"
83115
116+ val typeIndicatorDrawable = when (lessonState) {
117+ ScheduleItem .LessonState .COMPLETED -> R .drawable.indicator_circle
118+ ScheduleItem .LessonState .CURRENT -> {
119+ holder.startBlinking()
120+ R .drawable.indicator_circle_hollow_dot
121+ }
122+ ScheduleItem .LessonState .UPCOMING -> R .drawable.indicator_circle_hollow
123+ }
124+
125+ holder.typeIndicator.setImageResource(typeIndicatorDrawable)
126+
84127 holder.typeIndicator.setColorFilter(
85128 ContextCompat .getColor(
86129 holder.itemView.context,
@@ -102,7 +145,9 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
102145 if (lesson.building == null ) " " else ScheduleUtils .shortenBuildingName(lesson.building!! )
103146 holder.locationLayout.visibility = View .VISIBLE
104147 } else {
105- holder.locationLayout.visibility = View .GONE
148+ holder.locationRoom.text = " нет кабинета"
149+ holder.locationBuilding.text = " "
150+ holder.locationLayout.visibility = View .VISIBLE
106151 }
107152
108153 if (lesson.timeStart != null ) {
@@ -112,7 +157,10 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
112157 holder.timeLayout.visibility = View .GONE
113158 }
114159
115- if (position == scheduleList.size - 1 || (scheduleList.getOrNull(position + 1 ) != null && scheduleList.getOrNull(position + 1 ) is ScheduleItem .BreakItem )) {
160+ if (position == scheduleList.size - 1 || (scheduleList.getOrNull(position + 1 ) != null && scheduleList.getOrNull(
161+ position + 1
162+ ) is ScheduleItem .BreakItem )
163+ ) {
116164 holder.divider.visibility = View .GONE
117165 } else {
118166 holder.divider.visibility = View .VISIBLE
@@ -124,35 +172,46 @@ class LessonAdapter(private val scheduleList: List<ScheduleItem>) :
124172 holder.breakText.text = " ⋯ можно отдохнуть с ${dtf.format(from)} до ${dtf.format(to)} ⋯"
125173 }
126174
127- override fun getItemCount () = max( 1 , scheduleList.size)
175+ override fun getItemCount () = scheduleList.size
128176
129177 inner class LessonViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView) {
130178
131179 val typeIndicator: ImageView = itemView.findViewById(R .id.type_indicator)
132-
133180 val lessonTitle: TextView = itemView.findViewById(R .id.title)
134-
135181 val teacherLayout: LinearLayout = itemView.findViewById(R .id.teacher_layout)
136182 val teacherName: TextView = itemView.findViewById(R .id.teacher)
137-
138183 val locationLayout: LinearLayout = itemView.findViewById(R .id.location_layout)
139-
140184 val locationRoom: TextView = itemView.findViewById(R .id.location_room)
141-
142185 val locationBuilding: TextView = itemView.findViewById(R .id.location_building)
143-
144186 val timeLayout: LinearLayout = itemView.findViewById(R .id.time_layout)
145-
146187 val time: TextView = itemView.findViewById(R .id.time)
147-
148188 val divider: ImageView = itemView.findViewById(R .id.divider)
149189
190+ private val handler = Handler (Looper .getMainLooper())
191+ private var isHollow = false
192+ private val blinkingRunnable = object : Runnable {
193+ override fun run () {
194+ if (isHollow) {
195+ typeIndicator.setImageResource(R .drawable.indicator_circle_hollow_dot)
196+ } else {
197+ typeIndicator.setImageResource(R .drawable.indicator_circle_hollow)
198+ }
199+ isHollow = ! isHollow
200+ handler.postDelayed(this , 1000 )
201+ }
202+ }
203+
204+ fun startBlinking () {
205+ isHollow = false
206+ handler.post(blinkingRunnable)
207+ }
150208
209+ fun stopBlinking () {
210+ handler.removeCallbacks(blinkingRunnable)
211+ }
151212 }
152213
153214 inner class BreakViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView) {
154-
155215 val breakText: TextView = itemView.findViewById(R .id.break_text)
156-
157216 }
158217}
0 commit comments