Skip to content

Commit 95649e3

Browse files
author
alllexey
committed
Use ListAdapter for ScheduleActivity
1 parent 3f65654 commit 95649e3

File tree

2 files changed

+46
-82
lines changed

2 files changed

+46
-82
lines changed

app/src/main/java/me/alllexey123/itmowidgets/ui/schedule/DayScheduleAdapter.kt

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import android.view.LayoutInflater
44
import android.view.View
55
import android.view.ViewGroup
66
import android.widget.TextView
7+
import androidx.recyclerview.widget.DiffUtil
78
import androidx.recyclerview.widget.LinearLayoutManager
9+
import androidx.recyclerview.widget.ListAdapter
810
import androidx.recyclerview.widget.RecyclerView
911
import api.myitmo.model.Lesson
1012
import api.myitmo.model.Schedule
@@ -16,8 +18,8 @@ import java.time.LocalDateTime
1618
import java.time.LocalTime
1719
import java.time.format.DateTimeFormatter
1820

19-
class DayScheduleAdapter(private var schedules: List<Schedule>) :
20-
RecyclerView.Adapter<DayScheduleAdapter.DayViewHolder>() {
21+
class DayScheduleAdapter :
22+
ListAdapter<Schedule, DayScheduleAdapter.DayViewHolder>(ScheduleDiffCallback) {
2123

2224
private val viewPool = RecyclerView.RecycledViewPool()
2325

@@ -28,7 +30,7 @@ class DayScheduleAdapter(private var schedules: List<Schedule>) :
2830
}
2931

3032
override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
31-
val daySchedule = schedules[position]
33+
val daySchedule = getItem(position)
3234
val date = daySchedule.date
3335
val lessons = daySchedule.lessons
3436

@@ -57,63 +59,42 @@ class DayScheduleAdapter(private var schedules: List<Schedule>) :
5759
holder.innerRecyclerView.setRecycledViewPool(viewPool)
5860
}
5961

60-
fun updateData(newDaySchedules: List<Schedule>) {
61-
val oldDaySchedules = this.schedules
62-
this.schedules = newDaySchedules
63-
if (oldDaySchedules != newDaySchedules) notifyDataSetChanged()
64-
}
65-
6662
fun updateLessonStates() {
67-
notifyDataSetChanged()
63+
submitList(currentList.toList())
6864
}
6965

70-
override fun getItemCount(): Int = schedules.size
71-
7266
inner class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
7367
val dayTitle: TextView = itemView.findViewById(R.id.day_title)
74-
7568
val numberOfLessons: TextView = itemView.findViewById(R.id.number_of_lessons)
76-
7769
val dayDate: TextView = itemView.findViewById(R.id.day_date)
7870
val innerRecyclerView: RecyclerView = itemView.findViewById(R.id.inner_recycler_view)
7971
}
8072

81-
fun processLessonsWithBreaks(lessons: List<Lesson>, date: LocalDate): List<ScheduleItem> {
73+
private fun processLessonsWithBreaks(lessons: List<Lesson>, date: LocalDate): List<ScheduleItem> {
8274
val BIG_BREAK_THRESHOLD = Duration.ofMinutes(60)
83-
8475
val now = LocalDateTime.now()
85-
8676
val processedList = mutableListOf<ScheduleItem>()
8777
val sortedLessons = lessons.sortedBy { it.timeStart }
8878

8979
sortedLessons.forEachIndexed { index, currentLesson ->
9080
val lessonStartTime = ScheduleUtils.parseTime(date, currentLesson.timeStart)
9181
val lessonEndTime = ScheduleUtils.parseTime(date, currentLesson.timeEnd)
92-
val lessonState = if (lessonEndTime < now) {
93-
ScheduleItem.LessonState.COMPLETED
94-
} else {
95-
if (lessonStartTime < now) {
96-
ScheduleItem.LessonState.CURRENT
97-
} else {
98-
ScheduleItem.LessonState.UPCOMING
99-
}
82+
val lessonState = when {
83+
lessonEndTime < now -> ScheduleItem.LessonState.COMPLETED
84+
lessonStartTime < now -> ScheduleItem.LessonState.CURRENT
85+
else -> ScheduleItem.LessonState.UPCOMING
10086
}
101-
10287
processedList.add(ScheduleItem.LessonItem(currentLesson, lessonState))
10388

10489
if (index < sortedLessons.size - 1) {
10590
val nextLesson = sortedLessons[index + 1]
106-
10791
try {
10892
val currentEndTime = LocalTime.parse(currentLesson.timeEnd, DateTimeFormatter.ofPattern("HH:mm"))
10993
val nextStartTime = LocalTime.parse(nextLesson.timeStart, DateTimeFormatter.ofPattern("HH:mm"))
110-
11194
val breakDuration = Duration.between(currentEndTime, nextStartTime)
112-
11395
if (breakDuration.abs() > BIG_BREAK_THRESHOLD) {
11496
processedList.add(ScheduleItem.BreakItem(currentEndTime, nextStartTime))
11597
}
116-
11798
} catch (e: Exception) {
11899
e.printStackTrace()
119100
}
@@ -122,22 +103,26 @@ class DayScheduleAdapter(private var schedules: List<Schedule>) :
122103

123104
if (processedList.isEmpty()) {
124105
val nowDate = now.toLocalDate()
125-
val lessonState = if (date < nowDate) {
126-
ScheduleItem.LessonState.COMPLETED
127-
} else {
128-
if (date > nowDate) {
129-
ScheduleItem.LessonState.UPCOMING
130-
} else {
131-
ScheduleItem.LessonState.CURRENT
132-
}
106+
val lessonState = when {
107+
date < nowDate -> ScheduleItem.LessonState.COMPLETED
108+
date > nowDate -> ScheduleItem.LessonState.UPCOMING
109+
else -> ScheduleItem.LessonState.CURRENT
133110
}
134111
return listOf(ScheduleItem.NoLessonsItem(lessonState))
135112
}
136113

137114
return processedList
138115
}
139116

140-
fun getItemAt(position: Int): Schedule? {
141-
return schedules.getOrNull(position)
117+
companion object {
118+
private val ScheduleDiffCallback = object : DiffUtil.ItemCallback<Schedule>() {
119+
override fun areItemsTheSame(oldItem: Schedule, newItem: Schedule): Boolean {
120+
return oldItem.date == newItem.date
121+
}
122+
123+
override fun areContentsTheSame(oldItem: Schedule, newItem: Schedule): Boolean {
124+
return oldItem == newItem
125+
}
126+
}
142127
}
143128
}

app/src/main/java/me/alllexey123/itmowidgets/ui/schedule/ScheduleActivity.kt

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class ScheduleActivity : AppCompatActivity() {
5252

5353
observeUiState()
5454

55-
5655
swipeRefreshLayout.setOnRefreshListener {
5756
scheduleViewModel.fetchScheduleData(forceRefresh = true)
5857
}
@@ -88,7 +87,7 @@ class ScheduleActivity : AppCompatActivity() {
8887
outerRecyclerView.layoutManager =
8988
LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
9089

91-
dayScheduleAdapter = DayScheduleAdapter(listOf())
90+
dayScheduleAdapter = DayScheduleAdapter()
9291
outerRecyclerView.adapter = dayScheduleAdapter
9392

9493
snapHelper.attachToRecyclerView(outerRecyclerView)
@@ -127,37 +126,17 @@ class ScheduleActivity : AppCompatActivity() {
127126
val scheduleList = state.schedule
128127
val layoutManager = outerRecyclerView.layoutManager as LinearLayoutManager
129128

130-
var dateToRestore: LocalDate? = null
131-
var offsetToRestore = 0
132-
133-
if (!isInitialLoad) {
134-
val snapView = snapHelper.findSnapView(layoutManager)
135-
val snapPosition = snapView?.let { layoutManager.getPosition(it) }
136-
137-
if (snapPosition != null && snapPosition != RecyclerView.NO_POSITION) {
138-
dateToRestore = dayScheduleAdapter.getItemAt(snapPosition)?.date
139-
offsetToRestore =
140-
layoutManager.getDecoratedLeft(snapView) - outerRecyclerView.paddingLeft * 2
141-
}
142-
}
143-
144-
dayScheduleAdapter.updateData(scheduleList)
145-
146-
if (dateToRestore != null) {
147-
val newIndex = scheduleList.indexOfFirst { it.date == dateToRestore }
148-
if (newIndex != -1) {
149-
layoutManager.scrollToPositionWithOffset(newIndex, offsetToRestore)
129+
if (isInitialLoad && scheduleList.isNotEmpty()) {
130+
dayScheduleAdapter.submitList(scheduleList) {
131+
val todayIndex = scheduleList.indexOfFirst { it.date == LocalDate.now() }
132+
if (todayIndex != -1) {
133+
layoutManager.scrollToPosition(todayIndex)
134+
}
150135
}
136+
isInitialLoad = false
151137
} else {
152-
val today = LocalDate.now()
153-
val todayIndex = scheduleList.indexOfFirst { it.date == today }
154-
if (todayIndex != -1) {
155-
layoutManager.scrollToPosition(todayIndex)
156-
}
157-
}
158138

159-
if (scheduleList.isNotEmpty()) {
160-
isInitialLoad = false
139+
dayScheduleAdapter.submitList(scheduleList)
161140
}
162141

163142
swipeRefreshLayout.isRefreshing = state.isStillUpdating
@@ -172,18 +151,6 @@ class ScheduleActivity : AppCompatActivity() {
172151
}
173152
}
174153

175-
companion object {
176-
fun getOnClickPendingIntent(context: Context): PendingIntent? {
177-
val clickIntent = Intent(context, ScheduleActivity::class.java)
178-
return PendingIntent.getActivity(
179-
context,
180-
0,
181-
clickIntent,
182-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
183-
)
184-
}
185-
}
186-
187154
private fun startLessonStateUpdater() {
188155
updateTimeRunnable = object : Runnable {
189156
override fun run() {
@@ -198,4 +165,16 @@ class ScheduleActivity : AppCompatActivity() {
198165
handler.removeCallbacks(updateTimeRunnable)
199166
}
200167

168+
companion object {
169+
fun getOnClickPendingIntent(context: Context): PendingIntent? {
170+
val clickIntent = Intent(context, ScheduleActivity::class.java)
171+
return PendingIntent.getActivity(
172+
context,
173+
0,
174+
clickIntent,
175+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
176+
)
177+
}
178+
}
179+
201180
}

0 commit comments

Comments
 (0)