From 299345b864b6ab07c6413981f2ad1a08f7a43264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 9 Apr 2020 23:30:56 +0200 Subject: [PATCH] Fix crash in message preview (#762) --- .../java/io/github/wulkanowy/data/db/dao/StudentDao.kt | 3 +++ .../wulkanowy/data/repositories/student/StudentLocal.kt | 8 ++++++++ .../data/repositories/student/StudentRepository.kt | 6 ++++++ .../ui/modules/message/preview/MessagePreviewFragment.kt | 4 ++-- .../ui/modules/message/preview/MessagePreviewPresenter.kt | 7 ++++--- .../main/java/io/github/wulkanowy/utils/LoggerUtils.kt | 2 +- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt index 57bf25fb8..901ddc73f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt @@ -22,6 +22,9 @@ interface StudentDao { @Query("SELECT * FROM Students WHERE is_current = 1") fun loadCurrent(): Maybe + @Query("SELECT * FROM Students WHERE id = :id") + fun loadById(id: Int): Maybe + @Query("SELECT * FROM Students") fun loadAll(): Maybe> diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt index a3576ca22..c7a46e9d6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt @@ -33,6 +33,14 @@ class StudentLocal @Inject constructor( .filter { it.isNotEmpty() } } + fun getStudentById(id: Int): Maybe { + return studentDb.loadById(id).map { + it.apply { + if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) + } + } + } + fun getCurrentStudent(decryptPass: Boolean): Maybe { return studentDb.loadCurrent().map { it.apply { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt index 270d080e1..bebd1eb9c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt @@ -47,6 +47,12 @@ class StudentRepository @Inject constructor( return local.getStudents(decryptPass).toSingle(emptyList()) } + fun getStudentById(id: Int): Single { + return local.getStudentById(id) + .switchIfEmpty(Maybe.error(NoCurrentStudentException())) + .toSingle() + } + fun getCurrentStudent(decryptPass: Boolean = true): Single { return local.getCurrentStudent(decryptPass) .switchIfEmpty(Maybe.error(NoCurrentStudentException())) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt index 3f52be895..953238c08 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt @@ -63,7 +63,7 @@ class MessagePreviewFragment : BaseFragment(), MessagePreviewView, MainView.Titl override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) messageContainer = messagePreviewContainer - presenter.onAttachView(this, (savedInstanceState ?: arguments)?.getSerializable(MESSAGE_ID_KEY) as Message) + presenter.onAttachView(this, (savedInstanceState ?: arguments)?.getSerializable(MESSAGE_ID_KEY) as? Message) } override fun initView() { @@ -150,8 +150,8 @@ class MessagePreviewFragment : BaseFragment(), MessagePreviewView, MainView.Titl } override fun onSaveInstanceState(outState: Bundle) { - super.onSaveInstanceState(outState) outState.putSerializable(MESSAGE_ID_KEY, presenter.message) + super.onSaveInstanceState(outState) } override fun onDestroyView() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index 7bf9c05e7..5a19d9fab 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -24,11 +24,12 @@ class MessagePreviewPresenter @Inject constructor( private var retryCallback: () -> Unit = {} - fun onAttachView(view: MessagePreviewView, message: Message) { + fun onAttachView(view: MessagePreviewView, message: Message?) { super.onAttachView(view) view.initView() errorHandler.showErrorMessage = ::showErrorViewOnError - loadData(message) + this.message = message + loadData(requireNotNull(message)) } private fun onMessageLoadRetry(message: Message) { @@ -47,7 +48,7 @@ class MessagePreviewPresenter @Inject constructor( Timber.i("Loading message ${message.messageId} preview started") disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(studentRepository.getStudentById(message.studentId) .flatMap { messageRepository.getMessage(it, message, true) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) diff --git a/app/src/main/java/io/github/wulkanowy/utils/LoggerUtils.kt b/app/src/main/java/io/github/wulkanowy/utils/LoggerUtils.kt index 15de40d37..fc3528495 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/LoggerUtils.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/LoggerUtils.kt @@ -18,7 +18,7 @@ class DebugLogTree : Timber.DebugTree() { } } -private fun Bundle?.checkSavedState() = if (this == null) "(STATE IS NULL)" else "" +private fun Bundle?.checkSavedState() = if (this == null) "(STATE IS NULL)" else "(STATE IS NOT NULL)" class ActivityLifecycleLogger : Application.ActivityLifecycleCallbacks {