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

View File

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