This commit is contained in:
2023-10-09 20:54:56 +05:30
parent 5b0f7fdc4c
commit 8a485d58e3
4 changed files with 107 additions and 12 deletions

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -75,11 +75,31 @@
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notification_rv"
android:visibility="gone"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"/>
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notification_rv"
android:visibility="gone"
android:layout_above="@id/page_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"/>
<ProgressBar
android:id="@+id/page_progress"
android:layout_width="30dp"
android:layout_height="30dp"
android:indeterminateTint="@android:color/darker_gray"
android:indeterminate="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginVertical="5dp"
android:visibility="gone"
/>
</RelativeLayout>
</LinearLayout>