1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-18 21:16:48 -06:00

Fix timetable widget crash on update (#250)

This commit is contained in:
Rafał Borcz 2019-02-17 17:32:23 +01:00 committed by Mikołaj Pich
parent 1d7585071d
commit 5526691cb6
3 changed files with 22 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.data.repositories.semester.SemesterRepository
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.data.repositories.timetable.TimetableRepository
import io.github.wulkanowy.ui.widgets.timetable.TimetableWidgetFactory
import io.github.wulkanowy.utils.SchedulersProvider
import javax.inject.Inject
class TimetableWidgetService : RemoteViewsService() {
@ -24,8 +25,11 @@ class TimetableWidgetService : RemoteViewsService() {
@Inject
lateinit var sharedPref: SharedPrefHelper
@Inject
lateinit var schedulers: SchedulersProvider
override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory {
AndroidInjection.inject(this)
return TimetableWidgetFactory(timetableRepo, studentRepo, semesterRepo, sharedPref, applicationContext, intent)
return TimetableWidgetFactory(timetableRepo, studentRepo, semesterRepo, sharedPref, schedulers, applicationContext, intent)
}
}

View File

@ -15,9 +15,8 @@ import io.github.wulkanowy.data.db.entities.Timetable
import io.github.wulkanowy.data.repositories.semester.SemesterRepository
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.data.repositories.timetable.TimetableRepository
import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.toFormattedString
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import org.threeten.bp.LocalDate
import timber.log.Timber
@ -26,12 +25,11 @@ class TimetableWidgetFactory(
private val studentRepository: StudentRepository,
private val semesterRepository: SemesterRepository,
private val sharedPref: SharedPrefHelper,
private val schedulers: SchedulersProvider,
private val context: Context,
private val intent: Intent?
) : RemoteViewsService.RemoteViewsFactory {
private val disposable: CompositeDisposable = CompositeDisposable()
private var lessons = emptyList<Timetable>()
override fun getLoadingView() = null
@ -46,23 +44,27 @@ class TimetableWidgetFactory(
override fun onCreate() {}
override fun onDestroy() {}
override fun onDataSetChanged() {
intent?.action?.let { LocalDate.ofEpochDay(sharedPref.getLong(it, 0)) }
?.let { date ->
disposable.add(studentRepository.isStudentSaved()
.flatMap {
if (it) studentRepository.getCurrentStudent()
else Single.error(IllegalArgumentException("No saved students"))
}.flatMap { semesterRepository.getCurrentSemester(it) }
.flatMap { timetableRepository.getTimetable(it, date, date) }
.map { item -> item.sortedBy { it.number } }
.subscribe({ lessons = it })
{ Timber.e(it, "An error has occurred while downloading data for the widget") })
try {
lessons = studentRepository.isStudentSaved()
.flatMap { studentRepository.getCurrentStudent() }
.flatMap { semesterRepository.getCurrentSemester(it) }
.flatMap { timetableRepository.getTimetable(it, date, date) }
.map { item -> item.sortedBy { it.number } }
.subscribeOn(schedulers.backgroundThread)
.blockingGet()
} catch (e: Exception) {
Timber.e(e, "An error has occurred while downloading data for the widget")
}
}
}
override fun getViewAt(position: Int): RemoteViews? {
if (position == INVALID_POSITION) return null
if (position == INVALID_POSITION || lessons.getOrNull(position) == null) return null
return RemoteViews(context.packageName, R.layout.item_widget_timetable).apply {
lessons[position].let {
@ -90,8 +92,4 @@ class TimetableWidgetFactory(
setOnClickFillInIntent(R.id.timetableWidgetItemContainer, Intent())
}
}
override fun onDestroy() {
disposable.clear()
}
}

View File

@ -5,5 +5,5 @@
android:minHeight="100dp"
android:previewImage="@drawable/img_timetable_widget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="7200000"
android:updatePeriodMillis="3600000"
android:widgetCategory="home_screen" />