2014-12-03 11:27:11 +01:00
|
|
|
package org.mariotaku.twidere.fragment.support;
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
2014-12-03 11:27:11 +01:00
|
|
|
import android.content.Context;
|
2014-12-04 15:31:29 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2014-12-03 11:27:11 +01:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-12-07 16:13:07 +01:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
2014-12-03 11:27:11 +01:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.Loader;
|
2014-12-07 16:13:07 +01:00
|
|
|
import android.support.v4.util.Pair;
|
2014-12-03 11:27:11 +01:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.support.v7.widget.RecyclerView.OnScrollListener;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import org.mariotaku.twidere.R;
|
|
|
|
import org.mariotaku.twidere.adapter.AbsStatusesAdapter;
|
2014-12-04 15:31:29 +01:00
|
|
|
import org.mariotaku.twidere.adapter.AbsStatusesAdapter.EventListener;
|
2014-12-03 11:27:11 +01:00
|
|
|
import org.mariotaku.twidere.adapter.decorator.DividerItemDecoration;
|
2014-12-04 15:31:29 +01:00
|
|
|
import org.mariotaku.twidere.constant.IntentConstants;
|
|
|
|
import org.mariotaku.twidere.fragment.iface.RefreshScrollTopInterface;
|
|
|
|
import org.mariotaku.twidere.model.ParcelableStatus;
|
|
|
|
import org.mariotaku.twidere.util.AsyncTwitterWrapper;
|
2014-12-03 11:27:11 +01:00
|
|
|
import org.mariotaku.twidere.util.SimpleDrawerCallback;
|
|
|
|
import org.mariotaku.twidere.util.ThemeUtils;
|
|
|
|
import org.mariotaku.twidere.util.Utils;
|
|
|
|
import org.mariotaku.twidere.view.HeaderDrawerLayout.DrawerCallback;
|
2014-12-04 15:31:29 +01:00
|
|
|
import org.mariotaku.twidere.view.holder.GapViewHolder;
|
|
|
|
import org.mariotaku.twidere.view.holder.StatusViewHolder;
|
2014-12-03 11:27:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 14/11/5.
|
|
|
|
*/
|
2014-12-04 15:31:29 +01:00
|
|
|
public abstract class AbsStatusesFragment<Data> extends BaseSupportFragment implements LoaderCallbacks<Data>,
|
|
|
|
OnRefreshListener, DrawerCallback, RefreshScrollTopInterface, EventListener {
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
|
|
|
|
private final BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
|
|
|
if (getActivity() == null || !isAdded() || isDetached()) return;
|
|
|
|
onReceivedBroadcast(intent, intent.getAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2014-12-03 11:27:11 +01:00
|
|
|
private View mContentView;
|
|
|
|
private SharedPreferences mPreferences;
|
2014-12-04 15:31:29 +01:00
|
|
|
private View mProgressContainer;
|
|
|
|
private SwipeRefreshLayout mSwipeRefreshLayout;
|
|
|
|
private RecyclerView mRecyclerView;
|
|
|
|
private AbsStatusesAdapter<Data> mAdapter;
|
|
|
|
private SimpleDrawerCallback mDrawerCallback;
|
|
|
|
private OnScrollListener mOnScrollListener = new OnScrollListener() {
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
@Override
|
|
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
|
|
|
if (isRefreshing()) return;
|
|
|
|
if (layoutManager.findLastVisibleItemPosition() == mAdapter.getItemCount() - 1) {
|
|
|
|
onLoadMoreStatuses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-12-03 11:27:11 +01:00
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
public boolean canScroll(float dy) {
|
|
|
|
return mDrawerCallback.canScroll(dy);
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
public void cancelTouch() {
|
|
|
|
mDrawerCallback.cancelTouch();
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
public void fling(float velocity) {
|
|
|
|
mDrawerCallback.fling(velocity);
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isScrollContent(float x, float y) {
|
|
|
|
return mDrawerCallback.isScrollContent(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
public void scrollBy(float dy) {
|
|
|
|
mDrawerCallback.scrollBy(dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldLayoutHeaderBottom() {
|
|
|
|
return mDrawerCallback.shouldLayoutHeaderBottom();
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void topChanged(int offset) {
|
|
|
|
mDrawerCallback.topChanged(offset);
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public AbsStatusesAdapter<Data> getAdapter() {
|
|
|
|
return mAdapter;
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public SharedPreferences getSharedPreferences() {
|
|
|
|
if (mPreferences != null) return mPreferences;
|
|
|
|
return mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public abstract int getStatuses(long[] accountIds, long[] maxIds, long[] sinceIds);
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public boolean isRefreshing() {
|
|
|
|
return mSwipeRefreshLayout.isRefreshing();
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public void setRefreshing(boolean refreshing) {
|
|
|
|
mSwipeRefreshLayout.setRefreshing(refreshing);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
return inflater.inflate(R.layout.fragment_recycler_view, container, false);
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
final View view = getView();
|
|
|
|
if (view == null) throw new AssertionError();
|
|
|
|
final Context context = view.getContext();
|
|
|
|
final boolean compact = Utils.isCompactCards(context);
|
|
|
|
mDrawerCallback = new SimpleDrawerCallback(mRecyclerView);
|
|
|
|
mSwipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
mSwipeRefreshLayout.setColorSchemeColors(ThemeUtils.getUserAccentColor(context));
|
|
|
|
mAdapter = onCreateAdapter(context, compact);
|
|
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
|
|
|
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
|
|
|
mRecyclerView.setLayoutManager(layoutManager);
|
|
|
|
if (compact) {
|
|
|
|
mRecyclerView.addItemDecoration(new DividerItemDecoration(context, layoutManager.getOrientation()));
|
|
|
|
}
|
|
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
|
|
mRecyclerView.setOnScrollListener(mOnScrollListener);
|
2014-12-04 15:31:29 +01:00
|
|
|
mAdapter.setEventListener(this);
|
2014-12-03 11:27:11 +01:00
|
|
|
getLoaderManager().initLoader(0, getArguments(), this);
|
|
|
|
setListShown(false);
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
final IntentFilter filter = new IntentFilter();
|
|
|
|
onSetIntentFilter(filter);
|
|
|
|
registerReceiver(mStateReceiver, filter);
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
unregisterReceiver(mStateReceiver);
|
|
|
|
super.onStop();
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Data> loader, Data data) {
|
|
|
|
setRefreshing(false);
|
|
|
|
mAdapter.setData(data);
|
|
|
|
setListShown(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Data> loader) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
triggerRefresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStatusActionClick(StatusViewHolder holder, int id, int position) {
|
|
|
|
switch (id) {
|
|
|
|
case R.id.reply_count: {
|
|
|
|
final Context context = getActivity();
|
|
|
|
final Intent intent = new Intent(IntentConstants.INTENT_ACTION_REPLY);
|
|
|
|
intent.setPackage(context.getPackageName());
|
|
|
|
intent.putExtra(IntentConstants.EXTRA_STATUS, mAdapter.getStatus(position));
|
|
|
|
context.startActivity(intent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStatusClick(StatusViewHolder holder, int position) {
|
2014-12-07 16:13:07 +01:00
|
|
|
final FragmentActivity activity = getActivity();
|
|
|
|
final Bundle activityOption = Utils.makeSceneTransitionOption(activity,
|
|
|
|
new Pair<View, String>(holder.getProfileImageView(), StatusFragment.TRANSITION_NAME_PROFILE_IMAGE),
|
|
|
|
new Pair<View, String>(holder.getProfileTypeView(), StatusFragment.TRANSITION_NAME_PROFILE_TYPE));
|
|
|
|
Utils.openStatus(getActivity(), mAdapter.getStatus(position), activityOption);
|
2014-12-04 15:31:29 +01:00
|
|
|
// final View cardView = holder.getCardView();
|
|
|
|
// if (cardView != null && context instanceof FragmentActivity) {
|
|
|
|
// final Bundle options = Utils.makeSceneTransitionOption((FragmentActivity) context,
|
|
|
|
// new Pair<>(cardView, StatusFragment.TRANSITION_NAME_CARD));
|
|
|
|
// Utils.openStatus(context, getStatus(position), options);
|
|
|
|
// } else {
|
|
|
|
// Utils.openStatus(context, getStatus(position), null);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGapClick(GapViewHolder holder, int position) {
|
|
|
|
final ParcelableStatus status = mAdapter.getStatus(position);
|
|
|
|
final long sinceId = position + 1 < mAdapter.getStatusCount() ? mAdapter.getStatus(position + 1).id : -1;
|
|
|
|
final long[] accountIds = {status.account_id};
|
|
|
|
final long[] maxIds = {status.id};
|
|
|
|
final long[] sinceIds = {sinceId};
|
|
|
|
getStatuses(accountIds, maxIds, sinceIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStatusMenuClick(StatusViewHolder holder, int position) {
|
|
|
|
final Bundle args = new Bundle();
|
|
|
|
args.putParcelable(EXTRA_STATUS, mAdapter.getStatus(position));
|
|
|
|
final StatusMenuDialogFragment f = new StatusMenuDialogFragment();
|
|
|
|
f.setArguments(args);
|
|
|
|
f.show(getActivity().getSupportFragmentManager(), "status_menu");
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
|
|
|
mContentView = view.findViewById(R.id.fragment_content);
|
|
|
|
mProgressContainer = view.findViewById(R.id.progress_container);
|
|
|
|
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);
|
|
|
|
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void fitSystemWindows(Rect insets) {
|
|
|
|
super.fitSystemWindows(insets);
|
|
|
|
mContentView.setPadding(insets.left, insets.top, insets.right, insets.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
public boolean scrollToStart() {
|
|
|
|
final AsyncTwitterWrapper twitter = getTwitterWrapper();
|
|
|
|
final int tabPosition = getTabPosition();
|
|
|
|
if (twitter != null && tabPosition != -1) {
|
|
|
|
twitter.clearUnreadCountAsync(tabPosition);
|
|
|
|
}
|
|
|
|
mRecyclerView.smoothScrollToPosition(0);
|
|
|
|
return true;
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract long[] getAccountIds();
|
|
|
|
|
2014-12-03 11:27:11 +01:00
|
|
|
protected Data getAdapterData() {
|
|
|
|
return mAdapter.getData();
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected void setAdapterData(Data data) {
|
2014-12-03 11:27:11 +01:00
|
|
|
mAdapter.setData(data);
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract AbsStatusesAdapter<Data> onCreateAdapter(Context context, boolean compact);
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract void onLoadMoreStatuses();
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract void onReceivedBroadcast(Intent intent, String action);
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract void onSetIntentFilter(IntentFilter filter);
|
2014-12-03 11:27:11 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
private void setListShown(boolean shown) {
|
|
|
|
mProgressContainer.setVisibility(shown ? View.GONE : View.VISIBLE);
|
|
|
|
mSwipeRefreshLayout.setVisibility(shown ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
2014-12-03 11:27:11 +01:00
|
|
|
}
|