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

Fix crash in message preview (#762)

This commit is contained in:
Mikołaj Pich 2020-04-09 23:30:56 +02:00 committed by GitHub
parent 0a18fefb1f
commit 299345b864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 6 deletions

View File

@ -22,6 +22,9 @@ interface StudentDao {
@Query("SELECT * FROM Students WHERE is_current = 1")
fun loadCurrent(): Maybe<Student>
@Query("SELECT * FROM Students WHERE id = :id")
fun loadById(id: Int): Maybe<Student>
@Query("SELECT * FROM Students")
fun loadAll(): Maybe<List<Student>>

View File

@ -33,6 +33,14 @@ class StudentLocal @Inject constructor(
.filter { it.isNotEmpty() }
}
fun getStudentById(id: Int): Maybe<Student> {
return studentDb.loadById(id).map {
it.apply {
if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
}
}
}
fun getCurrentStudent(decryptPass: Boolean): Maybe<Student> {
return studentDb.loadCurrent().map {
it.apply {

View File

@ -47,6 +47,12 @@ class StudentRepository @Inject constructor(
return local.getStudents(decryptPass).toSingle(emptyList())
}
fun getStudentById(id: Int): Single<Student> {
return local.getStudentById(id)
.switchIfEmpty(Maybe.error(NoCurrentStudentException()))
.toSingle()
}
fun getCurrentStudent(decryptPass: Boolean = true): Single<Student> {
return local.getCurrentStudent(decryptPass)
.switchIfEmpty(Maybe.error(NoCurrentStudentException()))

View File

@ -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() {

View File

@ -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)

View File

@ -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 {