finishes the Notifications timeline

This commit is contained in:
Vavassor 2017-01-22 18:42:05 -05:00
parent 98085bab6a
commit 32fecabd7f
1 changed files with 70 additions and 24 deletions

View File

@ -16,11 +16,9 @@
package com.keylesspalace.tusky; package com.keylesspalace.tusky;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
@ -29,7 +27,6 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import com.android.volley.AuthFailureError; import com.android.volley.AuthFailureError;
import com.android.volley.Response; import com.android.volley.Response;
@ -45,11 +42,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class NotificationsFragment extends Fragment implements public class NotificationsFragment extends SFragment implements
SwipeRefreshLayout.OnRefreshListener { SwipeRefreshLayout.OnRefreshListener, StatusActionListener, FooterActionListener {
private String domain = null;
private String accessToken = null;
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private NotificationsAdapter adapter; private NotificationsAdapter adapter;
public static NotificationsFragment newInstance() { public static NotificationsFragment newInstance() {
@ -65,19 +61,12 @@ public class NotificationsFragment extends Fragment implements
@Nullable Bundle savedInstanceState) { @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false); View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
Context context = getContext();
SharedPreferences preferences = context.getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
domain = preferences.getString("domain", null);
accessToken = preferences.getString("accessToken", null);
assert(domain != null);
assert(accessToken != null);
// Setup the SwipeRefreshLayout. // Setup the SwipeRefreshLayout.
Context context = getContext();
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setOnRefreshListener(this);
// Setup the RecyclerView. // Setup the RecyclerView.
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true); recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context); LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
@ -90,12 +79,16 @@ public class NotificationsFragment extends Fragment implements
@Override @Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter(); NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter();
String fromId = adapter.getItem(adapter.getItemCount() - 1).getId(); Notification notification = adapter.getItem(adapter.getItemCount() - 2);
sendFetchNotificationsRequest(fromId); if (notification != null) {
sendFetchNotificationsRequest(notification.getId());
} else {
sendFetchNotificationsRequest();
}
} }
}; };
recyclerView.addOnScrollListener(scrollListener); recyclerView.addOnScrollListener(scrollListener);
adapter = new NotificationsAdapter(); adapter = new NotificationsAdapter(this, this);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
sendFetchNotificationsRequest(); sendFetchNotificationsRequest();
@ -123,17 +116,22 @@ public class NotificationsFragment extends Fragment implements
JSONObject account = object.getJSONObject("account"); JSONObject account = object.getJSONObject("account");
String displayName = account.getString("display_name"); String displayName = account.getString("display_name");
Notification notification = new Notification(type, id, displayName); Notification notification = new Notification(type, id, displayName);
if (notification.hasStatusType()) {
JSONObject statusObject = object.getJSONObject("status");
Status status = Status.parse(statusObject, false);
notification.setStatus(status);
}
notifications.add(notification); notifications.add(notification);
} }
onFetchNotificationsSuccess(notifications, fromId != null); onFetchNotificationsSuccess(notifications, fromId != null);
} catch (JSONException e) { } catch (JSONException e) {
onFetchNotificationsFailure(e); onFetchNotificationsFailure();
} }
} }
}, new Response.ErrorListener() { }, new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
onFetchNotificationsFailure(error); onFetchNotificationsFailure();
} }
}) { }) {
@Override @Override
@ -156,17 +154,65 @@ public class NotificationsFragment extends Fragment implements
} else { } else {
adapter.update(notifications); adapter.update(notifications);
} }
showFetchTimelineRetry(false);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }
private void onFetchNotificationsFailure(Exception exception) { private void onFetchNotificationsFailure() {
Toast.makeText(getContext(), R.string.error_fetching_notifications, Toast.LENGTH_SHORT) showFetchTimelineRetry(true);
.show();
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }
private void showFetchTimelineRetry(boolean show) {
RecyclerView.ViewHolder viewHolder =
recyclerView.findViewHolderForAdapterPosition(adapter.getItemCount() - 1);
if (viewHolder != null) {
FooterViewHolder holder = (FooterViewHolder) viewHolder;
holder.showRetry(show);
}
}
@Override @Override
public void onRefresh() { public void onRefresh() {
sendFetchNotificationsRequest(); sendFetchNotificationsRequest();
} }
@Override
public void onLoadMore() {
Notification notification = adapter.getItem(adapter.getItemCount() - 2);
if (notification != null) {
sendFetchNotificationsRequest(notification.getId());
} else {
sendFetchNotificationsRequest();
}
}
@Override
public void onReply(int position) {
Notification notification = adapter.getItem(position);
super.reply(notification.getStatus());
}
@Override
public void onReblog(boolean reblog, int position) {
Notification notification = adapter.getItem(position);
super.reblog(notification.getStatus(), reblog, adapter, position);
}
@Override
public void onFavourite(boolean favourite, int position) {
Notification notification = adapter.getItem(position);
super.favourite(notification.getStatus(), favourite, adapter, position);
}
@Override
public void onMore(View view, int position) {
Notification notification = adapter.getItem(position);
super.more(notification.getStatus(), view, adapter, position);
}
@Override
public void onViewMedia(String url, Status.MediaAttachment.Type type) {
super.viewMedia(url, type);
}
} }