2017-01-20 19:09:10 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-02-13 16:18:17 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import com.android.volley.AuthFailureError;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
|
|
|
import com.android.volley.toolbox.JsonArrayRequest;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-01-23 10:42:05 +11:00
|
|
|
public class NotificationsFragment extends SFragment implements
|
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener, FooterActionListener {
|
2017-02-07 18:05:50 +11:00
|
|
|
private static final String TAG = "Notifications"; // logging tag
|
2017-02-08 08:47:05 +11:00
|
|
|
private static final int EXPECTED_NOTIFICATIONS_FETCHED = 10;
|
2017-02-07 18:05:50 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-01-23 10:42:05 +11:00
|
|
|
private RecyclerView recyclerView;
|
2017-02-13 16:18:17 +11:00
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private EndlessOnScrollListener scrollListener;
|
2017-01-08 09:24:02 +11:00
|
|
|
private NotificationsAdapter adapter;
|
2017-02-13 16:18:17 +11:00
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
public static NotificationsFragment newInstance() {
|
|
|
|
NotificationsFragment fragment = new NotificationsFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
|
|
|
@Nullable Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
|
|
|
|
|
|
|
|
// Setup the SwipeRefreshLayout.
|
2017-01-23 10:42:05 +11:00
|
|
|
Context context = getContext();
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
// Setup the RecyclerView.
|
2017-01-23 10:42:05 +11:00
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-02-13 16:18:17 +11:00
|
|
|
layoutManager = new LinearLayoutManager(context);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
|
|
|
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.status_divider);
|
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-02-13 16:18:17 +11:00
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
2017-01-08 09:24:02 +11:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter();
|
2017-01-23 10:42:05 +11:00
|
|
|
Notification notification = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (notification != null) {
|
|
|
|
sendFetchNotificationsRequest(notification.getId());
|
|
|
|
} else {
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
2017-01-23 10:42:05 +11:00
|
|
|
adapter = new NotificationsAdapter(this, this);
|
2017-01-08 09:24:02 +11:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-02-13 16:18:17 +11:00
|
|
|
TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onTabSelected(TabLayout.Tab tab) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:18:17 +11:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void jumpToTop() {
|
|
|
|
layoutManager.scrollToPosition(0);
|
|
|
|
scrollListener.reset();
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
private void sendFetchNotificationsRequest(final String fromId) {
|
|
|
|
String endpoint = getString(R.string.endpoint_notifications);
|
|
|
|
String url = "https://" + domain + endpoint;
|
|
|
|
if (fromId != null) {
|
|
|
|
url += "?max_id=" + fromId;
|
|
|
|
}
|
|
|
|
JsonArrayRequest request = new JsonArrayRequest(url,
|
|
|
|
new Response.Listener<JSONArray>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONArray response) {
|
|
|
|
try {
|
2017-01-25 15:35:54 +11:00
|
|
|
List<Notification> notifications = Notification.parse(response);
|
2017-01-08 09:24:02 +11:00
|
|
|
onFetchNotificationsSuccess(notifications, fromId != null);
|
|
|
|
} catch (JSONException e) {
|
2017-02-07 18:05:50 +11:00
|
|
|
onFetchNotificationsFailure(e);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2017-02-07 18:05:50 +11:00
|
|
|
onFetchNotificationsFailure(error);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
}) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Authorization", "Bearer " + accessToken);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
VolleySingleton.getInstance(getContext()).addToRequestQueue(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchNotificationsRequest() {
|
|
|
|
sendFetchNotificationsRequest(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchNotificationsSuccess(List<Notification> notifications, boolean added) {
|
|
|
|
if (added) {
|
|
|
|
adapter.addItems(notifications);
|
|
|
|
} else {
|
|
|
|
adapter.update(notifications);
|
|
|
|
}
|
2017-02-08 08:47:05 +11:00
|
|
|
if (notifications.size() >= EXPECTED_NOTIFICATIONS_FETCHED) {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.LOADING);
|
|
|
|
} else {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.END_OF_TIMELINE);
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:05:50 +11:00
|
|
|
private void onFetchNotificationsFailure(Exception exception) {
|
2017-02-08 08:47:05 +11:00
|
|
|
setFetchTimelineState(FooterViewHolder.State.RETRY);
|
2017-01-08 09:24:02 +11:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-02-07 18:05:50 +11:00
|
|
|
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-08 08:47:05 +11:00
|
|
|
private void setFetchTimelineState(FooterViewHolder.State state) {
|
2017-01-23 10:42:05 +11:00
|
|
|
RecyclerView.ViewHolder viewHolder =
|
|
|
|
recyclerView.findViewHolderForAdapterPosition(adapter.getItemCount() - 1);
|
|
|
|
if (viewHolder != null) {
|
|
|
|
FooterViewHolder holder = (FooterViewHolder) viewHolder;
|
2017-02-08 08:47:05 +11:00
|
|
|
holder.setState(state);
|
2017-01-23 10:42:05 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void onRefresh() {
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
2017-01-23 10:42:05 +11:00
|
|
|
|
|
|
|
public void onLoadMore() {
|
|
|
|
Notification notification = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (notification != null) {
|
|
|
|
sendFetchNotificationsRequest(notification.getId());
|
|
|
|
} else {
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReply(int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
|
|
|
super.reply(notification.getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReblog(boolean reblog, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
|
|
|
super.reblog(notification.getStatus(), reblog, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onFavourite(boolean favourite, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
|
|
|
super.favourite(notification.getStatus(), favourite, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onMore(View view, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
|
|
|
super.more(notification.getStatus(), view, adapter, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewMedia(String url, Status.MediaAttachment.Type type) {
|
|
|
|
super.viewMedia(url, type);
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
|
|
|
|
public void onViewThread(int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
|
|
|
super.viewThread(notification.getStatus());
|
|
|
|
}
|
2017-01-27 11:34:32 +11:00
|
|
|
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
|
|
|
public void onViewAccount(String id, String username) {
|
|
|
|
super.viewAccount(id, username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewAccount(int position) {
|
|
|
|
Status status = adapter.getItem(position).getStatus();
|
|
|
|
String id = status.getAccountId();
|
|
|
|
String username = status.getUsername();
|
|
|
|
super.viewAccount(id, username);
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|