From 5526691cb6ece26c021979e2703f3ab1b1409083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sun, 17 Feb 2019 17:32:23 +0100 Subject: [PATCH] Fix timetable widget crash on update (#250) --- .../widgets/TimetableWidgetService.kt | 6 +++- .../timetable/TimetableWidgetFactory.kt | 34 +++++++++---------- .../res/xml/provider_widget_timetable.xml | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt index da009f7fe..0432ee146 100644 --- a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt +++ b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt @@ -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) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/widgets/timetable/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/widgets/timetable/TimetableWidgetFactory.kt index 88d6fd8d9..7210b0539 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/widgets/timetable/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/widgets/timetable/TimetableWidgetFactory.kt @@ -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() 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() - } } diff --git a/app/src/main/res/xml/provider_widget_timetable.xml b/app/src/main/res/xml/provider_widget_timetable.xml index 184b3f399..48c6ba395 100644 --- a/app/src/main/res/xml/provider_widget_timetable.xml +++ b/app/src/main/res/xml/provider_widget_timetable.xml @@ -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" />