/* * Copyright (c) 2010-2019 Belledonne Communications SARL. * * This file is part of linphone-android * (see https://www.linphone.org). * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package org.linphone.chat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; abstract class ChatScrollListener extends RecyclerView.OnScrollListener { // The minimum amount of items to have below your current scroll position // before mLoading more. private static final int mVisibleThreshold = 5; // The total number of items in the dataset after the last load private int mPreviousTotalItemCount = 0; // True if we are still waiting for the last set of data to load. private boolean mLoading = true; private final LinearLayoutManager mLayoutManager; public ChatScrollListener(LinearLayoutManager layoutManager) { mLayoutManager = layoutManager; } // This happens many times a second during a scroll, so be wary of the code you place here. // We are given a few useful parameters to help us work out if we need to load some more data, // but first we check if we are waiting for the previous load to finish. @Override public void onScrolled(RecyclerView view, int dx, int dy) { int lastVisibleItemPosition; int totalItemCount = mLayoutManager.getItemCount(); lastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition(); // If the total item count is zero and the previous isn't, assume the // list is invalidated and should be reset back to initial state if (totalItemCount < mPreviousTotalItemCount) { this.mPreviousTotalItemCount = totalItemCount; if (totalItemCount == 0) { this.mLoading = true; } } // If it’s still mLoading, we check to see if the dataset count has // changed, if so we conclude it has finished mLoading and update the current page // number and total item count. if (mLoading && (totalItemCount > mPreviousTotalItemCount)) { mLoading = false; mPreviousTotalItemCount = totalItemCount; } // If it isn’t currently mLoading, we check to see if we have breached // the mVisibleThreshold and need to reload more data. // If we do need to reload some more data, we execute onLoadMore to fetch the data. // threshold should reflect how many total columns there are too if (!mLoading && (lastVisibleItemPosition + mVisibleThreshold) > totalItemCount) { onLoadMore(totalItemCount); mLoading = true; } } // Defines the process for actually mLoading more data based on page protected abstract void onLoadMore(int totalItemsCount); }