diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListFragment.kt index 5de5586899ae401e3f823c2d26399df6e0694fa1..e0f6abdc21706ecba6bd3f965d1871c8ce88b8a9 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListFragment.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListFragment.kt @@ -23,6 +23,8 @@ import androidx.fragment.app.Fragment import androidx.recyclerview.widget.DefaultItemAnimator import androidx.lifecycle.Observer import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.OnScrollListener +import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import app.k9mail.ui.utils.itemtouchhelper.ItemTouchHelper import app.k9mail.ui.utils.linearlayoutmanager.LinearLayoutManager @@ -375,6 +377,60 @@ class MessageListFragment : this.recyclerView = recyclerView this.itemTouchHelper = itemTouchHelper + + addRecyclerViewScrollListener() + } + + private fun addRecyclerViewScrollListener() { + recyclerView?.addOnScrollListener(object : OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + showToastOnAllItemLoaded(recyclerView, newState) + } + }) + } + + private fun showToastOnAllItemLoaded(recyclerView: RecyclerView, scrollState: Int) { + recyclerView.adapter?.let { adapter -> + // If no item is loaded already & scroll is not done, + // then ignore the the statements + if ((adapter.itemCount == 0) || (scrollState != SCROLL_STATE_IDLE) || (recyclerView.layoutManager !is LinearLayoutManager)) { + return@let + } + + val currentPosition = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() + + // if the last visible item is the last item of the list & can't load any more item, + // then show toast user that all items are already loaded + if (currentPosition == (adapter.itemCount - 1) && !canLoadMoreItems()) { + Toast.makeText(context, R.string.all_messges_loaded, Toast.LENGTH_SHORT).show() + } + } + } + + private fun canLoadMoreItems(): Boolean { + if (isUnifiedInbox) { + return canLoadMoreForAllAccountsInbox() + } + + return currentFolder?.moreMessages ?: false + } + + private fun canLoadMoreForAllAccountsInbox(): Boolean { + var canLoadMore = false + + preferences.accounts + .filter { account -> account.isFinishedSetup && account.inboxFolderId != null } + .forEach { + val localFolder = MlfUtils.getOpenFolder(it.inboxFolderId!!, it) + + if (localFolder.hasMoreMessages()) { + canLoadMore = true + return@forEach + } + } + + return canLoadMore } private val shouldShowRecentChangesHintObserver = Observer { showRecentChangesHint -> @@ -939,7 +995,7 @@ class MessageListFragment : } if(isUnifiedInbox && adapter.messages.isNotEmpty()) { - updateFooterText(getString(R.string.message_list_load_more_messages_action)) + updateFooterTextForUnifiedInbox() return } @@ -961,6 +1017,16 @@ class MessageListFragment : updateFooterText(footerText) } + private fun updateFooterTextForUnifiedInbox() { + val footerText = if (canLoadMoreForAllAccountsInbox()) { + getString(R.string.message_list_load_more_messages_action) + } else { + null + } + + updateFooterText(footerText) + } + fun updateFooterText(text: String?) { adapter.footerText = text } diff --git a/app/ui/legacy/src/main/res/values/strings.xml b/app/ui/legacy/src/main/res/values/strings.xml index 32a4fb6ba48b1524a9730e3ef8091fb0125f2e8a..62b9c56e9ee00a0c37067757f6fe1ba6098ac2a1 100644 --- a/app/ui/legacy/src/main/res/values/strings.xml +++ b/app/ui/legacy/src/main/res/values/strings.xml @@ -1084,4 +1084,5 @@ You can keep this message and use it as a backup for your secret key. If you wan Error Folder not found - \ No newline at end of file + All messages have been loaded +