2017-01-20 09:09:10 +01: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-07 23:24:02 +01:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-03-11 19:20:10 +01:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-02-13 06:18:17 +01:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-01-07 23:24:02 +01:00
|
|
|
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;
|
|
|
|
|
2017-03-09 00:27:37 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-09 00:27:37 +01:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-03-20 08:03:03 +01:00
|
|
|
import retrofit2.Response;
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-01-23 00:42:05 +01:00
|
|
|
public class NotificationsFragment extends SFragment implements
|
2017-03-10 21:12:40 +01:00
|
|
|
SwipeRefreshLayout.OnRefreshListener, StatusActionListener,
|
2017-02-27 02:14:13 +01:00
|
|
|
NotificationsAdapter.FollowListener {
|
2017-03-10 00:20:08 +01:00
|
|
|
private static final String TAG = "Notifications"; // logging tag
|
2017-02-07 08:05:50 +01:00
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2017-02-13 06:18:17 +01:00
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private EndlessOnScrollListener scrollListener;
|
2017-01-07 23:24:02 +01:00
|
|
|
private NotificationsAdapter adapter;
|
2017-02-13 06:18:17 +01:00
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-03-14 12:45:19 +01:00
|
|
|
private Call<List<Notification>> listCall;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
|
|
|
public static NotificationsFragment newInstance() {
|
|
|
|
NotificationsFragment fragment = new NotificationsFragment();
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:03:13 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
@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 00:42:05 +01:00
|
|
|
Context context = getContext();
|
2017-01-07 23:24:02 +01:00
|
|
|
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
// Setup the RecyclerView.
|
2017-03-20 08:03:03 +01:00
|
|
|
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setHasFixedSize(true);
|
2017-02-13 06:18:17 +01:00
|
|
|
layoutManager = new LinearLayoutManager(context);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-02-21 23:55:37 +01:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-07 23:24:02 +01:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-02-13 06:18:17 +01:00
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
2017-01-07 23:24:02 +01:00
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter();
|
2017-01-23 00:42:05 +01:00
|
|
|
Notification notification = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (notification != null) {
|
2017-03-10 21:12:40 +01:00
|
|
|
sendFetchNotificationsRequest(notification.id, null);
|
2017-01-23 00:42:05 +01:00
|
|
|
} else {
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
2017-03-10 21:12:40 +01:00
|
|
|
adapter = new NotificationsAdapter(this, this);
|
2017-01-07 23:24:02 +01:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-02-13 06:18:17 +01: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-07 23:24:02 +01:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-03-12 08:49:38 +01:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:45:19 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
if (listCall != null) listCall.cancel();
|
|
|
|
}
|
|
|
|
|
2017-02-13 06:18:17 +01: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-03-10 21:12:40 +01:00
|
|
|
private void sendFetchNotificationsRequest(final String fromId, String uptoId) {
|
2017-03-09 00:27:37 +01:00
|
|
|
MastodonAPI api = ((BaseActivity) getActivity()).mastodonAPI;
|
|
|
|
|
2017-03-14 12:45:19 +01:00
|
|
|
listCall = api.notifications(fromId, uptoId, null);
|
|
|
|
|
|
|
|
listCall.enqueue(new Callback<List<Notification>>() {
|
2017-01-07 23:24:02 +01:00
|
|
|
@Override
|
2017-03-20 08:03:03 +01:00
|
|
|
public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
onFetchNotificationsSuccess(response.body(), fromId);
|
|
|
|
} else {
|
|
|
|
onFetchNotificationsFailure(new Exception(response.message()));
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<List<Notification>> call, Throwable t) {
|
|
|
|
onFetchNotificationsFailure((Exception) t);
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 01:34:27 +01:00
|
|
|
callList.add(listCall);
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFetchNotificationsRequest() {
|
2017-03-10 21:12:40 +01:00
|
|
|
sendFetchNotificationsRequest(null, null);
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 23:55:37 +01:00
|
|
|
private static boolean findNotification(List<Notification> notifications, String id) {
|
|
|
|
for (Notification notification : notifications) {
|
2017-03-09 00:27:37 +01:00
|
|
|
if (notification.id.equals(id)) {
|
2017-02-21 23:55:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-02-21 23:55:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchNotificationsSuccess(List<Notification> notifications, String fromId) {
|
|
|
|
if (fromId != null) {
|
|
|
|
if (notifications.size() > 0 && !findNotification(notifications, fromId)) {
|
|
|
|
adapter.addItems(notifications);
|
2017-03-11 19:20:10 +01:00
|
|
|
|
|
|
|
// Set last update id for pull notifications so that we don't get notified
|
|
|
|
// about things we already loaded here
|
|
|
|
SharedPreferences preferences = getActivity().getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = preferences.edit();
|
|
|
|
editor.putString("lastUpdateId", notifications.get(0).id);
|
|
|
|
editor.apply();
|
2017-02-21 23:55:37 +01:00
|
|
|
}
|
2017-02-07 22:47:05 +01:00
|
|
|
} else {
|
2017-02-21 23:55:37 +01:00
|
|
|
adapter.update(notifications);
|
2017-02-07 22:47:05 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
2017-02-07 08:05:50 +01:00
|
|
|
private void onFetchNotificationsFailure(Exception exception) {
|
2017-01-07 23:24:02 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2017-02-07 08:05:50 +01:00
|
|
|
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onRefresh() {
|
2017-03-10 21:12:40 +01:00
|
|
|
Notification notification = adapter.getItem(0);
|
2017-01-23 00:42:05 +01:00
|
|
|
if (notification != null) {
|
2017-03-10 21:12:40 +01:00
|
|
|
sendFetchNotificationsRequest(null, notification.id);
|
2017-01-23 00:42:05 +01:00
|
|
|
} else {
|
|
|
|
sendFetchNotificationsRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReply(int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
2017-03-09 00:27:37 +01:00
|
|
|
super.reply(notification.status);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onReblog(boolean reblog, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
2017-03-09 00:27:37 +01:00
|
|
|
super.reblog(notification.status, reblog, adapter, position);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onFavourite(boolean favourite, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
2017-03-09 00:27:37 +01:00
|
|
|
super.favourite(notification.status, favourite, adapter, position);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onMore(View view, int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
2017-03-09 00:27:37 +01:00
|
|
|
super.more(notification.status, view, adapter, position);
|
2017-01-23 00:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewMedia(String url, Status.MediaAttachment.Type type) {
|
|
|
|
super.viewMedia(url, type);
|
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
|
|
|
|
public void onViewThread(int position) {
|
|
|
|
Notification notification = adapter.getItem(position);
|
2017-03-09 00:27:37 +01:00
|
|
|
super.viewThread(notification.status);
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
2017-01-27 01:34:32 +01:00
|
|
|
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
super.viewTag(tag);
|
|
|
|
}
|
2017-01-28 04:33:43 +01:00
|
|
|
|
2017-03-03 01:25:35 +01:00
|
|
|
public void onViewAccount(String id) {
|
|
|
|
super.viewAccount(id);
|
2017-01-28 04:33:43 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|