2020-07-22 21:32:42 +02:00
|
|
|
package org.mian.gitnex.fragments;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
2020-08-06 17:16:51 +02:00
|
|
|
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.NotificationThread;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.actions.NotificationsActions;
|
|
|
|
import org.mian.gitnex.activities.IssueDetailActivity;
|
|
|
|
import org.mian.gitnex.adapters.NotificationsAdapter;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.FragmentNotificationsBinding;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2021-03-21 16:56:54 +01:00
|
|
|
import org.mian.gitnex.helpers.Constants;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.mian.gitnex.helpers.InfiniteScrollListener;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2020-08-06 17:16:51 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
2020-07-22 21:32:42 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author opyale
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class NotificationsFragment extends Fragment implements NotificationsAdapter.OnNotificationClickedListener, NotificationsAdapter.OnMoreClickedListener, BottomSheetNotificationsFragment.OnOptionSelectedListener {
|
|
|
|
|
|
|
|
private List<NotificationThread> notificationThreads;
|
|
|
|
private NotificationsAdapter notificationsAdapter;
|
|
|
|
private NotificationsActions notificationsActions;
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
private ExtendedFloatingActionButton markAllAsRead;
|
2020-07-22 21:32:42 +02:00
|
|
|
private ProgressBar progressBar;
|
|
|
|
private ProgressBar loadingMoreView;
|
|
|
|
private TextView noDataNotifications;
|
|
|
|
private SwipeRefreshLayout pullToRefresh;
|
|
|
|
|
|
|
|
private Activity activity;
|
|
|
|
private Context context;
|
|
|
|
private TinyDB tinyDB;
|
|
|
|
private Menu menu;
|
|
|
|
|
|
|
|
private int pageCurrentIndex = 1;
|
|
|
|
private int pageResultLimit;
|
|
|
|
private String currentFilterMode = "unread";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
FragmentNotificationsBinding fragmentNotificationsBinding = FragmentNotificationsBinding.inflate(inflater, container, false);
|
2020-07-22 21:32:42 +02:00
|
|
|
setHasOptionsMenu(true);
|
|
|
|
|
2020-09-18 06:51:41 +02:00
|
|
|
activity = requireActivity();
|
2020-07-22 21:32:42 +02:00
|
|
|
context = getContext();
|
2020-11-02 16:17:00 +01:00
|
|
|
tinyDB = TinyDB.getInstance(context);
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
pageResultLimit = Constants.getCurrentResultLimit(context);
|
2020-07-22 21:32:42 +02:00
|
|
|
tinyDB.putString("notificationsFilterState", currentFilterMode);
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
markAllAsRead = fragmentNotificationsBinding.markAllAsRead;
|
|
|
|
noDataNotifications = fragmentNotificationsBinding.noDataNotifications;
|
|
|
|
loadingMoreView = fragmentNotificationsBinding.loadingMoreView;
|
|
|
|
progressBar = fragmentNotificationsBinding.progressBar;
|
2020-07-22 21:32:42 +02:00
|
|
|
|
|
|
|
notificationThreads = new ArrayList<>();
|
|
|
|
notificationsActions = new NotificationsActions(context);
|
|
|
|
notificationsAdapter = new NotificationsAdapter(context, notificationThreads, this, this);
|
|
|
|
|
|
|
|
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
RecyclerView recyclerView = fragmentNotificationsBinding.notifications;
|
2020-07-22 21:32:42 +02:00
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
recyclerView.setLayoutManager(linearLayoutManager);
|
|
|
|
recyclerView.setAdapter(notificationsAdapter);
|
|
|
|
recyclerView.addOnScrollListener(new InfiniteScrollListener(pageResultLimit, linearLayoutManager) {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScrolledToEnd(int firstVisibleItemPosition) {
|
|
|
|
|
|
|
|
pageCurrentIndex++;
|
|
|
|
loadNotifications(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
|
|
|
|
if(currentFilterMode.equalsIgnoreCase("unread")) {
|
|
|
|
|
|
|
|
if(dy > 0 && markAllAsRead.isShown()) {
|
|
|
|
|
|
|
|
markAllAsRead.setVisibility(View.GONE);
|
|
|
|
} else if(dy < 0) {
|
|
|
|
|
|
|
|
markAllAsRead.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
|
|
|
|
|
|
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
markAllAsRead.setOnClickListener(v1 -> {
|
|
|
|
|
|
|
|
Thread thread = new Thread(() -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(notificationsActions.setAllNotificationsRead(new Date())) {
|
|
|
|
|
|
|
|
activity.runOnUiThread(() -> {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.success(context, getString(R.string.markedNotificationsAsRead));
|
2020-07-22 21:32:42 +02:00
|
|
|
loadNotifications(true);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(IOException e) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
activity.runOnUiThread(() -> Toasty.error(context, getString(R.string.genericError)));
|
2020-07-22 21:32:42 +02:00
|
|
|
Log.e("onError", e.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
pullToRefresh = fragmentNotificationsBinding.pullToRefresh;
|
2020-07-22 21:32:42 +02:00
|
|
|
pullToRefresh.setOnRefreshListener(() -> {
|
|
|
|
|
|
|
|
pageCurrentIndex = 1;
|
|
|
|
loadNotifications(false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
loadNotifications(false);
|
2021-01-26 16:10:31 +01:00
|
|
|
return fragmentNotificationsBinding.getRoot();
|
2020-07-22 21:32:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadNotifications(boolean append) {
|
|
|
|
|
|
|
|
noDataNotifications.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
if(pageCurrentIndex == 1 || !append) {
|
|
|
|
|
|
|
|
notificationThreads.clear();
|
|
|
|
notificationsAdapter.notifyDataSetChanged();
|
|
|
|
pullToRefresh.setRefreshing(false);
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
loadingMoreView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
String loginUid = tinyDB.getString("loginUid");
|
|
|
|
String instanceToken = "token " + tinyDB.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
String[] filter = tinyDB.getString("notificationsFilterState").equals("read") ?
|
|
|
|
new String[]{"pinned", "read"} :
|
|
|
|
new String[]{"pinned", "unread"};
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
Call<List<NotificationThread>> call = RetrofitClient
|
|
|
|
.getApiInterface(context)
|
2020-07-22 21:32:42 +02:00
|
|
|
.getNotificationThreads(instanceToken, false, filter,
|
2021-03-21 16:56:54 +01:00
|
|
|
Constants.defaultOldestTimestamp, "",
|
2020-07-22 21:32:42 +02:00
|
|
|
pageCurrentIndex, pageResultLimit);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<List<NotificationThread>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<NotificationThread>> call, @NonNull Response<List<NotificationThread>> response) {
|
|
|
|
|
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
assert response.body() != null;
|
|
|
|
|
|
|
|
if(!append) {
|
|
|
|
|
|
|
|
notificationThreads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationThreads.addAll(response.body());
|
|
|
|
notificationsAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.e("onError", String.valueOf(response.code()));
|
|
|
|
}
|
|
|
|
|
|
|
|
onCleanup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<NotificationThread>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e("onError", t.toString());
|
|
|
|
onCleanup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onCleanup() {
|
|
|
|
|
|
|
|
AppUtil.setMultiVisibility(View.GONE, loadingMoreView, progressBar);
|
|
|
|
pullToRefresh.setRefreshing(false);
|
|
|
|
|
|
|
|
if(notificationThreads.isEmpty()) {
|
|
|
|
|
|
|
|
noDataNotifications.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void changeFilterMode() {
|
|
|
|
|
|
|
|
int filterIcon = currentFilterMode.equalsIgnoreCase("read") ?
|
|
|
|
R.drawable.ic_filter_closed :
|
|
|
|
R.drawable.ic_filter;
|
|
|
|
|
|
|
|
menu.getItem(0).setIcon(filterIcon);
|
|
|
|
|
|
|
|
if(currentFilterMode.equalsIgnoreCase("read")) {
|
|
|
|
|
|
|
|
markAllAsRead.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
markAllAsRead.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
|
|
|
|
|
|
|
this.menu = menu;
|
|
|
|
|
|
|
|
inflater.inflate(R.menu.filter_menu_notifications, menu);
|
|
|
|
|
|
|
|
currentFilterMode = tinyDB.getString("notificationsFilterState");
|
|
|
|
changeFilterMode();
|
|
|
|
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
|
|
|
|
|
|
|
if(item.getItemId() == R.id.filterNotifications) {
|
|
|
|
|
|
|
|
BottomSheetNotificationsFilterFragment bottomSheetNotificationsFilterFragment = new BottomSheetNotificationsFilterFragment();
|
|
|
|
bottomSheetNotificationsFilterFragment.show(getChildFragmentManager(), "notificationsFilterBottomSheet");
|
|
|
|
bottomSheetNotificationsFilterFragment.setOnDismissedListener(() -> {
|
|
|
|
|
|
|
|
pageCurrentIndex = 1;
|
|
|
|
currentFilterMode = tinyDB.getString("notificationsFilterState");
|
|
|
|
|
|
|
|
changeFilterMode();
|
|
|
|
loadNotifications(false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationClicked(NotificationThread notificationThread) {
|
|
|
|
|
|
|
|
Thread thread = new Thread(() -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(notificationThread.isUnread()) {
|
|
|
|
|
|
|
|
notificationsActions.setNotificationStatus(notificationThread, NotificationsActions.NotificationStatus.READ);
|
|
|
|
activity.runOnUiThread(() -> loadNotifications(false));
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch(IOException ignored) {}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
|
|
|
|
if(StringUtils.containsAny(notificationThread.getSubject().getType().toLowerCase(), "pull", "issue")) {
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, IssueDetailActivity.class);
|
|
|
|
String issueUrl = notificationThread.getSubject().getUrl();
|
|
|
|
|
|
|
|
tinyDB.putString("issueNumber", issueUrl.substring(issueUrl.lastIndexOf("/") + 1));
|
|
|
|
tinyDB.putString("issueType", notificationThread.getSubject().getType());
|
2020-09-13 06:54:05 +02:00
|
|
|
tinyDB.putString("repoFullName", notificationThread.getRepository().getFullName());
|
2020-07-22 21:32:42 +02:00
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoreClicked(NotificationThread notificationThread) {
|
|
|
|
|
|
|
|
BottomSheetNotificationsFragment bottomSheetNotificationsFragment = new BottomSheetNotificationsFragment();
|
|
|
|
bottomSheetNotificationsFragment.onAttach(context, notificationThread, this);
|
|
|
|
bottomSheetNotificationsFragment.show(getChildFragmentManager(), "notificationsBottomSheet");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSelected() {
|
|
|
|
|
|
|
|
pageCurrentIndex = 1;
|
|
|
|
loadNotifications(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|