.
This commit is contained in:
@@ -71,6 +71,7 @@ public class ArticlesActivity extends AppCompatActivity
|
||||
articlesAdapter = new ArticlesAdapter(this);
|
||||
binding.articlesRv.setAdapter(articlesAdapter);
|
||||
|
||||
// pagination
|
||||
binding.articlesRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
|
||||
@@ -21,13 +21,13 @@ import com.app.simplitend.databinding.NotificationViewholderBinding;
|
||||
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Header;
|
||||
import retrofit2.http.Query;
|
||||
import retrofit2.http.Url;
|
||||
|
||||
public class NotificationsActivity extends AppCompatActivity implements Callback<CallResponse<ArrayList<NotificationsActivity.Notification>>> {
|
||||
@@ -36,6 +36,12 @@ public class NotificationsActivity extends AppCompatActivity implements Callback
|
||||
|
||||
protected NotificationsApiService apiService;
|
||||
|
||||
private static final String NOTIFICATION_BASE_URL = "api/get-notifications";
|
||||
|
||||
private static final int PER_PAGE_COUNT = 10;
|
||||
|
||||
private int page_no;
|
||||
|
||||
protected String user_id;
|
||||
public static final String USER_ID = "user_id";
|
||||
|
||||
@@ -45,6 +51,8 @@ public class NotificationsActivity extends AppCompatActivity implements Callback
|
||||
protected boolean isCaregiver;
|
||||
|
||||
private NotificationsAdapter notificationsAdapter;
|
||||
private boolean isLoadingNotifications;
|
||||
private boolean lastPageHit;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -62,25 +70,65 @@ public class NotificationsActivity extends AppCompatActivity implements Callback
|
||||
notificationsAdapter = new NotificationsAdapter();
|
||||
binding.notificationRv.setAdapter(notificationsAdapter);
|
||||
|
||||
// pagination
|
||||
binding.notificationRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
||||
int visibleItemCount = layoutManager.getChildCount();
|
||||
int totalItemCount = layoutManager.getItemCount();
|
||||
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
|
||||
|
||||
if (!isLoadingNotifications && !lastPageHit) {
|
||||
if ((visibleItemCount + firstVisibleItemPosition) >= (totalItemCount)
|
||||
&& firstVisibleItemPosition >= 0) {
|
||||
page_no++; // loading next page
|
||||
loadNotifications(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
page_no = 1; // loading first page
|
||||
loadNotifications(false);
|
||||
|
||||
}
|
||||
|
||||
private void loadNotifications(boolean show_bottom_progress) {
|
||||
if (user_id != null){
|
||||
binding.progress.setVisibility(View.VISIBLE);
|
||||
|
||||
if (show_bottom_progress){
|
||||
binding.pageProgress.setVisibility(View.VISIBLE);
|
||||
}else{
|
||||
binding.progress.setVisibility(View.VISIBLE);
|
||||
}
|
||||
isLoadingNotifications = true;
|
||||
|
||||
if (isCaregiver){
|
||||
apiService.getNotifications("api/get-notifications?caregiverId=" + user_id,
|
||||
|
||||
String url = NOTIFICATION_BASE_URL + "?caregiverId=" + user_id;
|
||||
url += "&per_page=" + PER_PAGE_COUNT + "&page=" + page_no;
|
||||
apiService.getNotifications(url,
|
||||
"Bearer " + token).enqueue(this);
|
||||
}else{
|
||||
apiService.getNotifications("api/get-notifications?patientId=" + user_id,
|
||||
|
||||
String url = NOTIFICATION_BASE_URL + "?patientId=" + user_id;
|
||||
url += "&per_page=" + PER_PAGE_COUNT + "&page=" + page_no;
|
||||
apiService.getNotifications(url,
|
||||
"Bearer " + token).enqueue(this);
|
||||
}
|
||||
}else{
|
||||
binding.errorMsg.setText(R.string.couldn_t_load_notifications);
|
||||
binding.errorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call<CallResponse<ArrayList<Notification>>> call, Response<CallResponse<ArrayList<Notification>>> response) {
|
||||
binding.progress.setVisibility(View.GONE);
|
||||
binding.pageProgress.setVisibility(View.GONE);
|
||||
|
||||
if (response.body() != null){
|
||||
if (response.code() != 200){
|
||||
binding.errorMsg.setText(response.body().message);
|
||||
@@ -88,15 +136,28 @@ public class NotificationsActivity extends AppCompatActivity implements Callback
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.body().result == null || response.body().result.isEmpty()){
|
||||
binding.errorMsg.setText(R.string.no_notifications);
|
||||
if (response.body().result == null){
|
||||
binding.errorMsg.setText(R.string.couldn_t_load_notifications);
|
||||
binding.errorView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
binding.errorView.setVisibility(View.GONE);
|
||||
binding.notificationRv.setVisibility(View.VISIBLE);
|
||||
notificationsAdapter.submitList(response.body().result);
|
||||
|
||||
isLoadingNotifications = false;
|
||||
lastPageHit = response.body().result.size() < 10; // no more data to load
|
||||
|
||||
List<Notification> current_list = notificationsAdapter.getCurrentList();
|
||||
ArrayList<Notification> total_list = new ArrayList<>(current_list);
|
||||
total_list.addAll(response.body().result);
|
||||
|
||||
if (total_list.isEmpty()){
|
||||
binding.errorMsg.setText(R.string.no_notifications);
|
||||
binding.errorView.setVisibility(View.VISIBLE);
|
||||
}else{
|
||||
notificationsAdapter.submitList(total_list);
|
||||
}
|
||||
|
||||
}else{
|
||||
binding.errorMsg.setText(R.string.couldn_t_load_notifications);
|
||||
@@ -107,6 +168,7 @@ public class NotificationsActivity extends AppCompatActivity implements Callback
|
||||
@Override
|
||||
public void onFailure(Call<CallResponse<ArrayList<Notification>>> call, Throwable t) {
|
||||
binding.progress.setVisibility(View.GONE);
|
||||
binding.pageProgress.setVisibility(View.GONE);
|
||||
binding.errorMsg.setText(R.string.couldn_t_load_notifications);
|
||||
binding.errorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user