From dbbc8069b1cf20438bf5321f1003d86a55d4f6ec Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Mon, 29 Apr 2019 19:56:23 +0200 Subject: [PATCH] Add showing proper fragment in notifications (#331) --- .../services/sync/works/GradeWork.kt | 5 ++- .../services/sync/works/LuckyNumberWork.kt | 8 ++-- .../services/sync/works/MessageWork.kt | 7 ++-- .../wulkanowy/services/sync/works/NoteWork.kt | 8 ++-- .../LuckyNumberWidgetConfigurePresenter.kt | 6 +-- .../LuckyNumberWidgetProvider.kt | 37 ++++++++++--------- .../wulkanowy/ui/modules/main/MainActivity.kt | 23 ++++++++++-- .../ui/modules/main/MainPresenter.kt | 27 ++++++++------ .../wulkanowy/ui/modules/main/MainView.kt | 13 +++++++ .../TimetableWidgetProvider.kt | 23 +++++++----- .../ui/modules/main/MainPresenterTest.kt | 2 +- 11 files changed, 103 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt index 66316934..d0b8a6bb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt @@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.grade.GradeRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.NewEntriesChannel import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import javax.inject.Inject @@ -48,7 +49,7 @@ class GradeWork @Inject constructor( .setColor(context.getCompatColor(R.color.colorPrimary)) .setContentIntent( PendingIntent.getActivity(context, 0, - MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 0), FLAG_UPDATE_CURRENT)) + MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.GRADE), FLAG_UPDATE_CURRENT)) .setStyle(NotificationCompat.InboxStyle().run { setSummaryText(context.resources.getQuantityString(R.plurals.grade_number_item, grades.size, grades.size)) grades.forEach { addLine("${it.subject}: ${it.entry}") } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index 6f57063f..c40f95cb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.NewEntriesChannel import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import javax.inject.Inject @@ -47,8 +48,9 @@ class LuckyNumberWork @Inject constructor( .setPriority(PRIORITY_HIGH) .setColor(context.getCompatColor(R.color.colorPrimary)) .setContentIntent( - PendingIntent.getActivity(context, 0, - MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT)) + PendingIntent.getActivity(context, MainView.MenuView.MESSAGE.id, + MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.LUCKY_NUMBER) + , FLAG_UPDATE_CURRENT)) .build()) } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt index 7250544b..9d39d106 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt @@ -16,7 +16,8 @@ import io.github.wulkanowy.data.repositories.message.MessageRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.NewEntriesChannel import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import javax.inject.Inject @@ -48,8 +49,8 @@ class MessageWork @Inject constructor( .setPriority(PRIORITY_HIGH) .setColor(context.getCompatColor(R.color.colorPrimary)) .setContentIntent( - PendingIntent.getActivity(context, 0, MainActivity.getStartIntent(context) - .putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT) + PendingIntent.getActivity(context, MainView.MenuView.MESSAGE.id, MainActivity.getStartIntent(context) + .putExtra(EXTRA_START_MENU, MainView.MenuView.MESSAGE), FLAG_UPDATE_CURRENT) ) .setStyle(NotificationCompat.InboxStyle().run { setSummaryText(context.resources.getQuantityString(R.plurals.message_number_item, messages.size, messages.size)) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt index a9567f5a..69e7b379 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt @@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.note.NoteRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.NewEntriesChannel import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import javax.inject.Inject @@ -47,8 +48,9 @@ class NoteWork @Inject constructor( .setPriority(PRIORITY_HIGH) .setColor(context.getCompatColor(R.color.colorPrimary)) .setContentIntent( - PendingIntent.getActivity(context, 0, - MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT)) + PendingIntent.getActivity(context, MainView.MenuView.NOTE.id, + MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.NOTE) + , FLAG_UPDATE_CURRENT)) .setStyle(NotificationCompat.InboxStyle().run { setSummaryText(context.resources.getQuantityString(R.plurals.note_number_item, notes.size, notes.size)) notes.forEach { addLine("${it.teacher}: ${it.category}") } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt index 3f808f09..4fd81e96 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt @@ -6,7 +6,7 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.createWidgetKey +import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.utils.SchedulersProvider import javax.inject.Inject @@ -34,7 +34,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor( private fun loadData() { disposable.add(studentRepository.getSavedStudents(false) - .map { it to appWidgetId?.let { id -> sharedPref.getLong(createWidgetKey(id), 0) } } + .map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } } .map { (students, currentStudentId) -> students.map { student -> LuckyNumberWidgetConfigureItem(student, student.id == currentStudentId) } } @@ -51,7 +51,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor( private fun registerStudent(student: Student) { appWidgetId?.also { - sharedPref.putLong(createWidgetKey(it), student.id) + sharedPref.putLong(getStudentWidgetKey(it), student.id) view?.apply { updateLuckyNumberWidget(it) setSuccessResult(it) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index 40352279..2d965af6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -26,7 +26,8 @@ import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.SchedulersProvider import io.reactivex.Maybe import timber.log.Timber @@ -53,7 +54,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { lateinit var sharedPref: SharedPrefHelper companion object { - fun createWidgetKey(appWidgetId: Int) = "lucky_number_widget_$appWidgetId" + fun getStudentWidgetKey(appWidgetId: Int) = "lucky_number_widget_student_$appWidgetId" } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @@ -70,11 +71,11 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS).forEach { appWidgetId -> RemoteViews(context.packageName, R.layout.widget_luckynumber).apply { setTextViewText(R.id.luckyNumberWidgetNumber, - getLuckyNumber(sharedPref.getLong(createWidgetKey(appWidgetId), 0), appWidgetId)?.luckyNumber?.toString() ?: "#" + getLuckyNumber(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId)?.luckyNumber?.toString() ?: "#" ) setOnClickPendingIntent(R.id.luckyNumberWidgetContainer, - PendingIntent.getActivity(context, 2, MainActivity.getStartIntent(context).apply { - putExtra(EXTRA_START_MENU_INDEX, 4) + PendingIntent.getActivity(context, MainView.MenuView.LUCKY_NUMBER.id, MainActivity.getStartIntent(context).apply { + putExtra(EXTRA_START_MENU, MainView.MenuView.LUCKY_NUMBER) }, PendingIntent.FLAG_UPDATE_CURRENT)) }.also { setStyles(it, intent) @@ -85,7 +86,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { private fun onDelete(intent: Intent) { intent.getIntExtra(EXTRA_APPWIDGET_ID, 0).let { - if (it != 0) sharedPref.delete(createWidgetKey(it)) + if (it != 0) sharedPref.delete(getStudentWidgetKey(it)) } } @@ -97,12 +98,14 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { .flatMap { students -> students.singleOrNull { student -> student.id == studentId } .let { student -> - if (student != null) { - Maybe.just(student) - } else { - studentRepository.getCurrentStudent(false) - .toMaybe() - .doOnSuccess { sharedPref.putLong(createWidgetKey(appWidgetId), it.id) } + when { + student != null -> Maybe.just(student) + studentId != 0L -> { + studentRepository.getCurrentStudent(false) + .toMaybe() + .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } + } + else -> null } } } @@ -132,12 +135,12 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { val maxWidth = options?.getInt(OPTION_APPWIDGET_MAX_WIDTH) ?: 150 val maxHeight = options?.getInt(OPTION_APPWIDGET_MAX_HEIGHT) ?: 40 - Timber.d("New luckynumber widget measurement: %dx%d", maxWidth, maxHeight) + Timber.d("New lucky number widget measurement: %dx%d", maxWidth, maxHeight) when { // 1x1 maxWidth < 150 && maxHeight < 110 -> { - Timber.d("Luckynumber widget size: 1x1") + Timber.d("Lucky number widget size: 1x1") views.run { setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE) setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE) @@ -147,7 +150,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { } // 1x2 maxWidth < 150 && maxHeight > 110 -> { - Timber.d("Luckynumber widget size: 1x2") + Timber.d("Lucky number widget size: 1x2") views.run { setViewVisibility(R.id.luckyNumberWidgetImageTop, VISIBLE) setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE) @@ -157,7 +160,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { } // 2x1 maxWidth >= 150 && maxHeight <= 110 -> { - Timber.d("Luckynumber widget size: 2x1") + Timber.d("Lucky number widget size: 2x1") views.run { setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE) setViewVisibility(R.id.luckyNumberWidgetImageLeft, VISIBLE) @@ -167,7 +170,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() { } // 2x2 and bigger else -> { - Timber.d("Luckynumber widget size: 2x2 and bigger") + Timber.d("Lucky number widget size: 2x2 and bigger") views.run { setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE) setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt index 9b49e51c..c44836c5 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt @@ -22,8 +22,12 @@ import io.github.wulkanowy.ui.modules.account.AccountDialog import io.github.wulkanowy.ui.modules.attendance.AttendanceFragment import io.github.wulkanowy.ui.modules.exam.ExamFragment import io.github.wulkanowy.ui.modules.grade.GradeFragment +import io.github.wulkanowy.ui.modules.homework.HomeworkFragment import io.github.wulkanowy.ui.modules.login.LoginActivity +import io.github.wulkanowy.ui.modules.luckynumber.LuckyNumberFragment +import io.github.wulkanowy.ui.modules.message.MessageFragment import io.github.wulkanowy.ui.modules.more.MoreFragment +import io.github.wulkanowy.ui.modules.note.NoteFragment import io.github.wulkanowy.ui.modules.timetable.TimetableFragment import io.github.wulkanowy.utils.getThemeAttrColor import io.github.wulkanowy.utils.safelyPopFragment @@ -40,7 +44,7 @@ class MainActivity : BaseActivity(), MainView { lateinit var navController: FragNavController companion object { - const val EXTRA_START_MENU_INDEX = "extraStartMenuIndex" + const val EXTRA_START_MENU = "extraStartMenu" fun getStartIntent(context: Context) = Intent(context, MainActivity::class.java) } @@ -56,14 +60,27 @@ class MainActivity : BaseActivity(), MainView { override var startMenuIndex = 0 + override var startMenuMoreIndex = -1 + + private val moreMenuFragments = listOf( + MessageFragment.newInstance(), + HomeworkFragment.newInstance(), + NoteFragment.newInstance(), + LuckyNumberFragment.newInstance() + ) + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(mainToolbar) messageContainer = mainFragmentContainer - presenter.onAttachView(this, intent.getIntExtra(EXTRA_START_MENU_INDEX, -1)) - navController.initialize(startMenuIndex, savedInstanceState) + presenter.onAttachView(this, intent.getSerializableExtra(EXTRA_START_MENU) as? MainView.MenuView) + + navController.run { + initialize(startMenuIndex, savedInstanceState) + pushFragment(moreMenuFragments.getOrNull(startMenuMoreIndex)) + } } override fun onCreateOptionsMenu(menu: Menu?): Boolean { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt index e8d9402c..12f2bb04 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt @@ -22,22 +22,19 @@ class MainPresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(errorHandler) { - fun onAttachView(view: MainView, initMenuIndex: Int) { + fun onAttachView(view: MainView, initMenu: MainView.MenuView?) { super.onAttachView(view) - view.run { - startMenuIndex = if (initMenuIndex != -1) initMenuIndex else prefRepository.startMenuIndex + view.apply { + getProperViewIndexes(initMenu).let { (main, more) -> + startMenuIndex = main + startMenuMoreIndex = more + } initView() - Timber.i("Main view was initialized with $startMenuIndex menu index") + Timber.i("Main view was initialized with $startMenuIndex menu index and $startMenuMoreIndex more index") } syncManager.startSyncWorker() - - analytics.logEvent(APP_OPEN, DESTINATION to when (initMenuIndex) { - 1 -> "Grades" - 3 -> "Timetable" - 4 -> "More" - else -> "User action" - }) + analytics.logEvent(APP_OPEN, DESTINATION to initMenu?.name) } fun onViewChange() { @@ -104,4 +101,12 @@ class MainPresenter @Inject constructor( errorHandler.dispatch(it) })) } + + private fun getProperViewIndexes(initMenu: MainView.MenuView?): Pair { + return when { + initMenu?.id in 0..3 -> initMenu!!.id to -1 + (initMenu?.id ?: 0) > 3 -> 4 to initMenu!!.id - 4 + else -> prefRepository.startMenuIndex to -1 + } + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainView.kt index d3d7e342..a786a3c3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainView.kt @@ -6,6 +6,8 @@ interface MainView : BaseView { var startMenuIndex: Int + var startMenuMoreIndex: Int + val isRootView: Boolean val currentViewTitle: String? @@ -37,4 +39,15 @@ interface MainView : BaseView { val titleStringId: Int } + + enum class MenuView(val id: Int) { + GRADE(0), + ATTENDANCE(1), + EXAM(2), + TIMETABLE(3), + MESSAGE(4), + HOMEWORK(5), + NOTE(6), + LUCKY_NUMBER(7), + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 83334c78..be051a41 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -20,7 +20,8 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.widgets.TimetableWidgetService import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX +import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU +import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.nextOrSameSchoolDay @@ -131,7 +132,7 @@ class TimetableWidgetProvider : BroadcastReceiver() { }, FLAG_UPDATE_CURRENT)) setPendingIntentTemplate(R.id.timetableWidgetList, PendingIntent.getActivity(context, 1, MainActivity.getStartIntent(context).apply { - putExtra(EXTRA_START_MENU_INDEX, 3) + putExtra(EXTRA_START_MENU, MainView.MenuView.TIMETABLE) }, FLAG_UPDATE_CURRENT)) }.also { sharedPref.putLong(getDateWidgetKey(appWidgetId), date.toEpochDay(), true) @@ -151,20 +152,22 @@ class TimetableWidgetProvider : BroadcastReceiver() { }, FLAG_UPDATE_CURRENT) } - private fun getStudent(id: Long, appWidgetId: Int): Student? { + private fun getStudent(studentId: Long, appWidgetId: Int): Student? { return try { studentRepository.isStudentSaved() .filter { true } .flatMap { studentRepository.getSavedStudents(false).toMaybe() } .flatMap { students -> - students.singleOrNull { student -> student.id == id } + students.singleOrNull { student -> student.id == studentId } .let { student -> - if (student != null) { - Maybe.just(student) - } else { - studentRepository.getCurrentStudent(false) - .toMaybe() - .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } + when { + student != null -> Maybe.just(student) + studentId != 0L -> { + studentRepository.getCurrentStudent(false) + .toMaybe() + .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } + } + else -> null } } } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt index f7c5559d..1f5dc8de 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt @@ -41,7 +41,7 @@ class MainPresenterTest { clearInvocations(mainView) presenter = MainPresenter(errorHandler, studentRepository, prefRepository, syncManager, TestSchedulersProvider(), analytics) - presenter.onAttachView(mainView, -1) + presenter.onAttachView(mainView, null) } @Test