From 02df06a80990cca491d29f525970ec3f6242a20b Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 25 Jan 2023 16:32:34 +0600 Subject: [PATCH 1/3] Hide `load more` footer onAllItemLoad in unifiedInbox page --- .../k9/ui/messagelist/MessageListFragment.kt | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 5de5586899..039da479d0 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 @@ -939,7 +939,7 @@ class MessageListFragment : } if(isUnifiedInbox && adapter.messages.isNotEmpty()) { - updateFooterText(getString(R.string.message_list_load_more_messages_action)) + updateFooterTextForUnifiedInbox() return } @@ -961,6 +961,29 @@ class MessageListFragment : updateFooterText(footerText) } + private fun updateFooterTextForUnifiedInbox() { + var shouldShowFooter = false + + preferences.accounts + .filter { account -> account.isFinishedSetup && account.inboxFolderId != null } + .forEach { + val localFolder = MlfUtils.getOpenFolder(it.inboxFolderId!!, it) + + if (localFolder.hasMoreMessages()) { + shouldShowFooter = true + return@forEach + } + } + + val footerText = if (shouldShowFooter) { + getString(R.string.message_list_load_more_messages_action) + } else { + null + } + + updateFooterText(footerText) + } + fun updateFooterText(text: String?) { adapter.footerText = text } -- GitLab From 286773b989adc66016adf46b361b02223f17ad50 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 26 Jan 2023 18:03:04 +0600 Subject: [PATCH 2/3] Show toast when all messages loaded but user wants to load more items If all messages are loaded, but user swipe to load more messages from the end of the list, we want to show toast that all message is already loaded. --- .../k9/ui/messagelist/MessageListFragment.kt | 62 ++++++++++++++----- app/ui/legacy/src/main/res/values/strings.xml | 3 +- 2 files changed, 50 insertions(+), 15 deletions(-) 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 039da479d0..c0815d8dd6 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,51 @@ 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) + + if (newState == SCROLL_STATE_IDLE && recyclerView.layoutManager is LinearLayoutManager) { + val currentPosition = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() + + recyclerView.adapter?.let { adapter -> + if (adapter.itemCount > 0 && 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 -> @@ -962,20 +1009,7 @@ class MessageListFragment : } private fun updateFooterTextForUnifiedInbox() { - var shouldShowFooter = false - - preferences.accounts - .filter { account -> account.isFinishedSetup && account.inboxFolderId != null } - .forEach { - val localFolder = MlfUtils.getOpenFolder(it.inboxFolderId!!, it) - - if (localFolder.hasMoreMessages()) { - shouldShowFooter = true - return@forEach - } - } - - val footerText = if (shouldShowFooter) { + val footerText = if (canLoadMoreForAllAccountsInbox()) { getString(R.string.message_list_load_more_messages_action) } else { null diff --git a/app/ui/legacy/src/main/res/values/strings.xml b/app/ui/legacy/src/main/res/values/strings.xml index 32a4fb6ba4..62b9c56e9e 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 + -- GitLab From 3970fd8bf1cc2dfac964c9200d69340ae201adda Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 27 Jan 2023 15:17:17 +0600 Subject: [PATCH 3/3] refactor on scroll to end, show toast code --- .../k9/ui/messagelist/MessageListFragment.kt | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) 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 c0815d8dd6..e0f6abdc21 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 @@ -385,18 +385,27 @@ class MessageListFragment : recyclerView?.addOnScrollListener(object : OnScrollListener() { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { super.onScrollStateChanged(recyclerView, newState) + showToastOnAllItemLoaded(recyclerView, newState) + } + }) + } - if (newState == SCROLL_STATE_IDLE && recyclerView.layoutManager is LinearLayoutManager) { - val currentPosition = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() + 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 + } - recyclerView.adapter?.let { adapter -> - if (adapter.itemCount > 0 && currentPosition == (adapter.itemCount - 1) && !canLoadMoreItems()) { - Toast.makeText(context, R.string.all_messges_loaded, Toast.LENGTH_SHORT).show() - } - } - } + 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 { -- GitLab