Fix lifecycle of timer tasks in timetable lessons (#958)

Co-authored-by: Faierbel <RafalBO99@outlook.com>
This commit is contained in:
Mikołaj Pich 2020-09-13 16:27:53 +02:00 committed by GitHub
parent 792b123598
commit 47150364d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 12 deletions

View File

@ -1,6 +1,6 @@
package io.github.wulkanowy.data
data class Resource<out T>(val status: Status, val data: T?, val error: Throwable?) {
data class Resource<T>(val status: Status, val data: T?, val error: Throwable?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(Status.SUCCESS, data, null)

View File

@ -6,6 +6,7 @@ import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import io.github.wulkanowy.R
import io.github.wulkanowy.data.db.entities.Timetable
@ -44,8 +45,8 @@ class TimetableAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerView
private val timers = mutableMapOf<Int, Timer>()
private fun resetTimers() {
Timber.d("Timetable timers reset")
fun resetTimers() {
Timber.d("Timetable timers (${timers.size}) reset")
with(timers) {
forEach { (_, timer) -> timer.cancel() }
clear()
@ -69,11 +70,6 @@ class TimetableAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerView
}
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
resetTimers()
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val lesson = items[position]
@ -112,8 +108,12 @@ class TimetableAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerView
bindNormalDescription(binding, lesson)
bindNormalColors(binding, lesson)
if (lesson.isStudentPlan && showTimers) timers[position] = timer(period = 1000) {
root.post { updateTimeLeft(binding, lesson, position) }
if (lesson.isStudentPlan && showTimers) {
timers[position] = timer(period = 1000) {
if (ViewCompat.isAttachedToWindow(root)) {
root.post { updateTimeLeft(binding, lesson, position) }
}
}
} else {
// reset item on set changed
timetableItemTimeUntil.visibility = GONE

View File

@ -185,6 +185,7 @@ class TimetableFragment : BaseFragment<FragmentTimetableBinding>(R.layout.fragme
}
override fun onDestroyView() {
timetableAdapter.resetTimers()
presenter.onDetachView()
super.onDestroyView()
}

View File

@ -89,7 +89,7 @@ fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
}
}
} catch (e: Throwable) {
emit(Resource.error(e))
emit(Resource.error<T>(e))
}
}

View File

@ -1,6 +1,6 @@
buildscript {
ext {
kotlin_version = '1.4.0'
kotlin_version = '1.4.10'
about_libraries = '8.3.0'
hilt_version = "2.29.1-alpha"
}