2015-01-03 16:16:50 +01:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.fragment.support;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
2015-05-12 06:27:54 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-03 16:16:50 +01:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
|
2015-05-12 06:27:54 +02:00
|
|
|
import android.view.KeyEvent;
|
2015-01-03 16:16:50 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import com.squareup.otto.Bus;
|
|
|
|
|
|
|
|
import org.mariotaku.twidere.R;
|
|
|
|
import org.mariotaku.twidere.adapter.AbsActivitiesAdapter;
|
2015-01-15 06:43:12 +01:00
|
|
|
import org.mariotaku.twidere.adapter.AbsActivitiesAdapter.ActivityAdapterListener;
|
2015-01-03 16:16:50 +01:00
|
|
|
import org.mariotaku.twidere.app.TwidereApplication;
|
|
|
|
import org.mariotaku.twidere.fragment.iface.RefreshScrollTopInterface;
|
2015-01-15 06:43:12 +01:00
|
|
|
import org.mariotaku.twidere.model.ParcelableActivity;
|
2015-01-03 16:16:50 +01:00
|
|
|
import org.mariotaku.twidere.util.AsyncTwitterWrapper;
|
2015-05-12 06:27:54 +02:00
|
|
|
import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
|
|
|
|
import org.mariotaku.twidere.util.RecyclerViewNavigationHelper;
|
2015-01-03 16:16:50 +01:00
|
|
|
import org.mariotaku.twidere.view.HeaderDrawerLayout.DrawerCallback;
|
2015-01-15 06:43:12 +01:00
|
|
|
import org.mariotaku.twidere.view.holder.GapViewHolder;
|
2015-01-03 16:16:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 14/11/5.
|
|
|
|
*/
|
2015-04-27 14:25:26 +02:00
|
|
|
public abstract class AbsActivitiesFragment<Data> extends AbsContentRecyclerViewFragment<AbsActivitiesAdapter<Data>>
|
|
|
|
implements LoaderCallbacks<Data>, OnRefreshListener, DrawerCallback, RefreshScrollTopInterface,
|
2015-05-12 06:27:54 +02:00
|
|
|
ActivityAdapterListener, KeyboardShortcutsHandler.KeyboardShortcutCallback {
|
2015-01-03 16:16:50 +01:00
|
|
|
|
|
|
|
private final Object mStatusesBusCallback;
|
2015-05-12 06:27:54 +02:00
|
|
|
private SharedPreferences mPreferences;
|
|
|
|
private RecyclerViewNavigationHelper mNavigationHelper;
|
2015-01-03 16:16:50 +01:00
|
|
|
|
|
|
|
protected AbsActivitiesFragment() {
|
|
|
|
mStatusesBusCallback = createMessageBusCallback();
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:43:12 +01:00
|
|
|
@Override
|
|
|
|
public void onGapClick(GapViewHolder holder, int position) {
|
2015-04-27 14:25:26 +02:00
|
|
|
final ParcelableActivity activity = getAdapter().getActivity(position);
|
2015-01-15 06:43:12 +01:00
|
|
|
final long[] accountIds = {activity.account_id};
|
|
|
|
final long[] maxIds = {activity.min_position};
|
|
|
|
getActivities(accountIds, maxIds, null);
|
|
|
|
}
|
|
|
|
|
2015-05-12 06:27:54 +02:00
|
|
|
@Override
|
|
|
|
public boolean handleKeyboardShortcutSingle(@NonNull KeyboardShortcutsHandler handler, int keyCode, @NonNull KeyEvent event) {
|
2015-05-20 10:42:57 +02:00
|
|
|
return mNavigationHelper.handleKeyboardShortcutSingle(handler, keyCode, event);
|
2015-05-12 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handleKeyboardShortcutRepeat(@NonNull KeyboardShortcutsHandler handler, final int keyCode, final int repeatCount,
|
|
|
|
@NonNull final KeyEvent event) {
|
|
|
|
return mNavigationHelper.handleKeyboardShortcutRepeat(handler, keyCode, repeatCount, event);
|
|
|
|
}
|
2015-01-03 16:16:50 +01:00
|
|
|
|
|
|
|
public SharedPreferences getSharedPreferences() {
|
|
|
|
if (mPreferences != null) return mPreferences;
|
|
|
|
return mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:43:12 +01:00
|
|
|
public abstract int getActivities(long[] accountIds, long[] maxIds, long[] sinceIds);
|
2015-01-03 16:16:50 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2015-04-26 12:55:41 +02:00
|
|
|
return inflater.inflate(R.layout.fragment_content_recyclerview, container, false);
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
2015-05-12 06:27:54 +02:00
|
|
|
final AbsActivitiesAdapter<Data> adapter = getAdapter();
|
2015-05-20 10:42:57 +02:00
|
|
|
mNavigationHelper = new RecyclerViewNavigationHelper(getRecyclerView(), getLayoutManager(),
|
|
|
|
adapter, this);
|
2015-01-03 16:16:50 +01:00
|
|
|
final View view = getView();
|
|
|
|
if (view == null) throw new AssertionError();
|
2015-05-12 06:27:54 +02:00
|
|
|
adapter.setListener(this);
|
2015-01-03 16:16:50 +01:00
|
|
|
getLoaderManager().initLoader(0, getArguments(), this);
|
2015-04-27 14:25:26 +02:00
|
|
|
showProgress();
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
final Bus bus = TwidereApplication.getInstance(getActivity()).getMessageBus();
|
2015-05-20 10:42:57 +02:00
|
|
|
assert bus != null;
|
2015-01-03 16:16:50 +01:00
|
|
|
bus.register(mStatusesBusCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
final Bus bus = TwidereApplication.getInstance(getActivity()).getMessageBus();
|
2015-05-20 10:42:57 +02:00
|
|
|
assert bus != null;
|
2015-01-03 16:16:50 +01:00
|
|
|
bus.unregister(mStatusesBusCallback);
|
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Data> loader, Data data) {
|
|
|
|
setRefreshing(false);
|
2015-04-27 14:25:26 +02:00
|
|
|
getAdapter().setData(data);
|
|
|
|
showContent();
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Data> loader) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
triggerRefresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean scrollToStart() {
|
2015-04-27 14:25:26 +02:00
|
|
|
final boolean result = super.scrollToStart();
|
|
|
|
if (result) {
|
|
|
|
final AsyncTwitterWrapper twitter = getTwitterWrapper();
|
|
|
|
final int tabPosition = getTabPosition();
|
|
|
|
if (twitter != null && tabPosition != -1) {
|
|
|
|
twitter.clearUnreadCountAsync(tabPosition);
|
|
|
|
}
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract long[] getAccountIds();
|
|
|
|
|
|
|
|
protected Data getAdapterData() {
|
2015-04-27 14:25:26 +02:00
|
|
|
return getAdapter().getData();
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void setAdapterData(Data data) {
|
2015-04-27 14:25:26 +02:00
|
|
|
getAdapter().setData(data);
|
2015-01-03 16:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Object createMessageBusCallback() {
|
|
|
|
return new StatusesBusCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final class StatusesBusCallback {
|
|
|
|
|
|
|
|
protected StatusesBusCallback() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|