From 667c4b6af7d010667e48b57809c7cd4d99a53fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 18 May 2019 23:32:37 +0200 Subject: [PATCH] Fix empty maybe when loading message content (#343) --- .../io/github/wulkanowy/data/db/dao/MessagesDao.kt | 4 ++-- .../data/repositories/message/MessageLocal.kt | 4 ++-- .../data/repositories/message/MessageRepository.kt | 8 ++++---- .../modules/message/preview/MessagePreviewFragment.kt | 10 ++++++---- .../modules/message/preview/MessagePreviewPresenter.kt | 6 +++--- .../ui/modules/message/tab/MessageTabFragment.kt | 6 +++--- .../ui/modules/message/tab/MessageTabPresenter.kt | 10 +++++----- .../wulkanowy/ui/modules/message/tab/MessageTabView.kt | 2 +- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt index 3ef5d6905..4f72c6c91 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt @@ -23,8 +23,8 @@ interface MessagesDao { @Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder AND removed = 0 ORDER BY date DESC") fun loadAll(studentId: Int, folder: Int): Maybe> - @Query("SELECT * FROM Messages WHERE student_id = :studentId AND real_id = :id") - fun load(studentId: Int, id: Int): Maybe + @Query("SELECT * FROM Messages WHERE id = :id") + fun load(id: Long): Maybe @Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC") fun loadDeleted(studentId: Int): Maybe> diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt index 411cca395..ce08d13ec 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt @@ -23,8 +23,8 @@ class MessageLocal @Inject constructor(private val messagesDb: MessagesDao) { messagesDb.deleteAll(messages) } - fun getMessage(student: Student, id: Int): Maybe { - return messagesDb.load(student.id.toInt(), id) + fun getMessage(id: Long): Maybe { + return messagesDb.load(id) } fun getMessages(student: Student, folder: MessageFolder): Maybe> { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt index b68f7f719..c10cd5181 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt @@ -46,14 +46,14 @@ class MessageRepository @Inject constructor( } } - fun getMessage(student: Student, messageId: Int, markAsRead: Boolean = false): Single { + fun getMessage(student: Student, messageDbId: Long, markAsRead: Boolean = false): Single { return Single.just(apiHelper.initApi(student)) .flatMap { _ -> - local.getMessage(student, messageId) + local.getMessage(messageDbId) .filter { !it.content.isNullOrEmpty() } .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) .flatMap { - if (it) local.getMessage(student, messageId).toSingle() + if (it) local.getMessage(messageDbId).toSingle() else Single.error(UnknownHostException()) } .flatMap { dbMessage -> @@ -64,7 +64,7 @@ class MessageRepository @Inject constructor( })) } }.flatMap { - local.getMessage(student, messageId).toSingle() + local.getMessage(messageDbId).toSingle() } ) } 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 12765546b..057d81dc6 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 @@ -27,7 +27,9 @@ class MessagePreviewFragment : BaseSessionFragment(), MessagePreviewView, MainVi lateinit var presenter: MessagePreviewPresenter private var menuReplyButton: MenuItem? = null + private var menuForwardButton: MenuItem? = null + private var menuDeleteButton: MenuItem? = null override val titleStringId: Int @@ -42,9 +44,9 @@ class MessagePreviewFragment : BaseSessionFragment(), MessagePreviewView, MainVi companion object { const val MESSAGE_ID_KEY = "message_id" - fun newInstance(messageId: Int?): MessagePreviewFragment { + fun newInstance(messageId: Long): MessagePreviewFragment { return MessagePreviewFragment().apply { - arguments = Bundle().apply { putInt(MESSAGE_ID_KEY, messageId ?: 0) } + arguments = Bundle().apply { putLong(MESSAGE_ID_KEY, messageId) } } } } @@ -61,7 +63,7 @@ class MessagePreviewFragment : BaseSessionFragment(), MessagePreviewView, MainVi override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) messageContainer = messagePreviewContainer - presenter.onAttachView(this, (savedInstanceState ?: arguments)?.getInt(MESSAGE_ID_KEY) ?: 0) + presenter.onAttachView(this, (savedInstanceState ?: arguments)?.getLong(MESSAGE_ID_KEY) ?: 0L) } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { @@ -145,7 +147,7 @@ class MessagePreviewFragment : BaseSessionFragment(), MessagePreviewView, MainVi override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putInt(MESSAGE_ID_KEY, presenter.messageId) + outState.putLong(MESSAGE_ID_KEY, presenter.messageId) } 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 397e103b2..31641d754 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 @@ -20,16 +20,16 @@ class MessagePreviewPresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BaseSessionPresenter(errorHandler) { - var messageId: Int = 0 + var messageId = 0L private var message: Message? = null - fun onAttachView(view: MessagePreviewView, id: Int) { + fun onAttachView(view: MessagePreviewView, id: Long) { super.onAttachView(view) loadData(id) } - private fun loadData(id: Int) { + private fun loadData(id: Long) { Timber.i("Loading message $id preview started") messageId = id disposable.apply { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt index e9d77e592..3859853fc 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabFragment.kt @@ -56,7 +56,7 @@ class MessageTabFragment : BaseSessionFragment(), MessageTabView { super.onActivityCreated(savedInstanceState) messageContainer = messageTabRecycler presenter.onAttachView(this, MessageFolder.valueOf( - (savedInstanceState ?: arguments)?.getString(MessageTabFragment.MESSAGE_TAB_FOLDER_ID).orEmpty() + (savedInstanceState ?: arguments)?.getString(MESSAGE_TAB_FOLDER_ID).orEmpty() )) } @@ -106,7 +106,7 @@ class MessageTabFragment : BaseSessionFragment(), MessageTabView { messageTabSwipe.isRefreshing = show } - override fun openMessage(messageId: Int?) { + override fun openMessage(messageId: Long) { (activity as? MainActivity)?.pushView(MessagePreviewFragment.newInstance(messageId)) } @@ -124,7 +124,7 @@ class MessageTabFragment : BaseSessionFragment(), MessageTabView { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putString(MessageTabFragment.MESSAGE_TAB_FOLDER_ID, presenter.folder.name) + outState.putString(MESSAGE_TAB_FOLDER_ID, presenter.folder.name) } override fun onDestroyView() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index 4a8415e87..2173c951d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -44,9 +44,9 @@ class MessageTabPresenter @Inject constructor( fun onMessageItemSelected(item: AbstractFlexibleItem<*>) { if (item is MessageItem) { - Timber.i("Select message ${item.message.realId} item") + Timber.i("Select message ${item.message.id} item") view?.run { - openMessage(item.message.realId) + openMessage(item.message.id) if (item.message.unread) { item.message.unread = false updateItem(item) @@ -90,13 +90,13 @@ class MessageTabPresenter @Inject constructor( } private fun updateMessage(message: Message) { - Timber.i("Attempt to update message ${message.realId}") + Timber.i("Attempt to update message ${message.id}") disposable.add(messageRepository.updateMessage(message) .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) - .subscribe({ Timber.d("Update message ${message.realId} result: Success") }) + .subscribe({ Timber.d("Update message ${message.id} result: Success") }) { error -> - Timber.i("Update message ${message.realId} result: An exception occurred") + Timber.i("Update message ${message.id} result: An exception occurred") errorHandler.dispatch(error) }) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabView.kt index bcda52b56..01a188e75 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabView.kt @@ -28,7 +28,7 @@ interface MessageTabView : BaseSessionView { fun showRefresh(show: Boolean) - fun openMessage(messageId: Int?) + fun openMessage(messageId: Long) fun notifyParentDataLoaded() }