Rename project
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package eu.kanade.tachiyomi.ui.base.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import icepick.Icepick;
|
||||
|
||||
public class BaseActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
Icepick.saveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
protected void setupToolbar(Toolbar toolbar) {
|
||||
setSupportActionBar(toolbar);
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(String title) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setTitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(int titleResource) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setTitle(getString(titleResource));
|
||||
}
|
||||
|
||||
public void setToolbarSubtitle(String title) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setSubtitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarSubtitle(int titleResource) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setSubtitle(getString(titleResource));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
public void registerForStickyEvents() {
|
||||
registerForStickyEvents(0);
|
||||
}
|
||||
|
||||
public void registerForStickyEvents(int priority) {
|
||||
EventBus.getDefault().registerSticky(this, priority);
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
registerForEvents(0);
|
||||
}
|
||||
|
||||
public void registerForEvents(int priority) {
|
||||
EventBus.getDefault().register(this, priority);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package eu.kanade.tachiyomi.ui.base.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import eu.kanade.tachiyomi.App;
|
||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter;
|
||||
import nucleus.factory.PresenterFactory;
|
||||
import nucleus.factory.ReflectionPresenterFactory;
|
||||
import nucleus.presenter.Presenter;
|
||||
import nucleus.view.PresenterLifecycleDelegate;
|
||||
import nucleus.view.ViewWithPresenter;
|
||||
|
||||
|
||||
/**
|
||||
* This class is an example of how an activity could controls it's presenter.
|
||||
* You can inherit from this class or copy/paste this class's code to
|
||||
* create your own view implementation.
|
||||
*
|
||||
* @param <P> a type of presenter to return with {@link #getPresenter}.
|
||||
*/
|
||||
public abstract class BaseRxActivity<P extends Presenter> extends BaseActivity implements ViewWithPresenter<P> {
|
||||
|
||||
private static final String PRESENTER_STATE_KEY = "presenter_state";
|
||||
|
||||
private PresenterLifecycleDelegate<P> presenterDelegate =
|
||||
new PresenterLifecycleDelegate<>(ReflectionPresenterFactory.<P>fromViewClass(getClass()));
|
||||
|
||||
/**
|
||||
* Returns a current presenter factory.
|
||||
*/
|
||||
public PresenterFactory<P> getPresenterFactory() {
|
||||
return presenterDelegate.getPresenterFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a presenter factory.
|
||||
* Call this method before onCreate/onFinishInflate to override default {@link ReflectionPresenterFactory} presenter factory.
|
||||
* Use this method for presenter dependency injection.
|
||||
*/
|
||||
@Override
|
||||
public void setPresenterFactory(PresenterFactory<P> presenterFactory) {
|
||||
presenterDelegate.setPresenterFactory(presenterFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a current attached presenter.
|
||||
* This method is guaranteed to return a non-null value between
|
||||
* onResume/onPause and onAttachedToWindow/onDetachedFromWindow calls
|
||||
* if the presenter factory returns a non-null value.
|
||||
*
|
||||
* @return a currently attached presenter or null.
|
||||
*/
|
||||
public P getPresenter() {
|
||||
return presenterDelegate.getPresenter();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
final PresenterFactory<P> superFactory = getPresenterFactory();
|
||||
setPresenterFactory(() -> {
|
||||
P presenter = superFactory.createPresenter();
|
||||
App app = (App) getApplication();
|
||||
app.getComponentReflection().inject(presenter);
|
||||
((BasePresenter)presenter).setContext(app.getApplicationContext());
|
||||
return presenter;
|
||||
});
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState != null)
|
||||
presenterDelegate.onRestoreInstanceState(savedInstanceState.getBundle(PRESENTER_STATE_KEY));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putBundle(PRESENTER_STATE_KEY, presenterDelegate.onSaveInstanceState());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
presenterDelegate.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
presenterDelegate.onPause(isFinishing());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package eu.kanade.tachiyomi.ui.base.adapter;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
|
||||
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||
|
||||
public abstract class FlexibleViewHolder extends RecyclerView.ViewHolder
|
||||
implements View.OnClickListener, View.OnLongClickListener {
|
||||
|
||||
private final FlexibleAdapter adapter;
|
||||
private final OnListItemClickListener onListItemClickListener;
|
||||
|
||||
public FlexibleViewHolder(View itemView,FlexibleAdapter adapter,
|
||||
OnListItemClickListener onListItemClickListener) {
|
||||
super(itemView);
|
||||
this.adapter = adapter;
|
||||
|
||||
this.onListItemClickListener = onListItemClickListener;
|
||||
|
||||
this.itemView.setOnClickListener(this);
|
||||
this.itemView.setOnLongClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (onListItemClickListener.onListItemClick(getAdapterPosition())) {
|
||||
toggleActivation();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongClick(View view) {
|
||||
onListItemClickListener.onListItemLongClick(getAdapterPosition());
|
||||
toggleActivation();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void toggleActivation() {
|
||||
itemView.setActivated(adapter.isSelected(getAdapterPosition()));
|
||||
}
|
||||
|
||||
public interface OnListItemClickListener {
|
||||
boolean onListItemClick(int position);
|
||||
void onListItemLongClick(int position);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Paul Burke
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package eu.kanade.tachiyomi.ui.base.adapter;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.helper.ItemTouchHelper;
|
||||
|
||||
/**
|
||||
* Interface to listen for a move or dismissal event from a {@link ItemTouchHelper.Callback}.
|
||||
*
|
||||
* @author Paul Burke (ipaulpro)
|
||||
*/
|
||||
public interface ItemTouchHelperAdapter {
|
||||
|
||||
/**
|
||||
* Called when an item has been dragged far enough to trigger a move. This is called every time
|
||||
* an item is shifted, and <strong>not</strong> at the end of a "drop" event.<br/>
|
||||
* <br/>
|
||||
* Implementations should call {@link RecyclerView.Adapter#notifyItemMoved(int, int)} after
|
||||
* adjusting the underlying data to reflect this move.
|
||||
*
|
||||
* @param fromPosition The start position of the moved item.
|
||||
* @param toPosition Then resolved position of the moved item.
|
||||
*
|
||||
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
|
||||
* @see RecyclerView.ViewHolder#getAdapterPosition()
|
||||
*/
|
||||
void onItemMove(int fromPosition, int toPosition);
|
||||
|
||||
|
||||
/**
|
||||
* Called when an item has been dismissed by a swipe.<br/>
|
||||
* <br/>
|
||||
* Implementations should call {@link RecyclerView.Adapter#notifyItemRemoved(int)} after
|
||||
* adjusting the underlying data to reflect this removal.
|
||||
*
|
||||
* @param position The position of the item dismissed.
|
||||
*
|
||||
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
|
||||
* @see RecyclerView.ViewHolder#getAdapterPosition()
|
||||
*/
|
||||
void onItemDismiss(int position);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package eu.kanade.tachiyomi.ui.base.adapter;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
|
||||
public interface OnStartDragListener {
|
||||
|
||||
/**
|
||||
* Called when a view is requesting a start of a drag.
|
||||
*
|
||||
* @param viewHolder The holder of the view to drag.
|
||||
*/
|
||||
void onStartDrag(RecyclerView.ViewHolder viewHolder);
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package eu.kanade.tachiyomi.ui.base.adapter;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.helper.ItemTouchHelper;
|
||||
|
||||
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
|
||||
|
||||
private final ItemTouchHelperAdapter adapter;
|
||||
|
||||
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLongPressDragEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemViewSwipeEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
||||
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
|
||||
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
|
||||
return makeMovementFlags(dragFlags, swipeFlags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
|
||||
RecyclerView.ViewHolder target) {
|
||||
adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
|
||||
adapter.onItemDismiss(viewHolder.getAdapterPosition());
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package eu.kanade.tachiyomi.ui.base.adapter;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
import android.util.SparseArray;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
|
||||
// Sparse array to keep track of registered fragments in memory
|
||||
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
|
||||
|
||||
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
|
||||
super(fragmentManager);
|
||||
}
|
||||
|
||||
// Register the fragment when the item is instantiated
|
||||
@Override
|
||||
public Object instantiateItem(ViewGroup container, int position) {
|
||||
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
||||
registeredFragments.put(position, fragment);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
// Unregister when the item is inactive
|
||||
@Override
|
||||
public void destroyItem(ViewGroup container, int position, Object object) {
|
||||
registeredFragments.remove(position);
|
||||
super.destroyItem(container, position, object);
|
||||
}
|
||||
|
||||
// Returns the fragment for the position (if instantiated)
|
||||
public Fragment getRegisteredFragment(int position) {
|
||||
return registeredFragments.get(position);
|
||||
}
|
||||
|
||||
public List<Fragment> getRegisteredFragments() {
|
||||
ArrayList<Fragment> fragments = new ArrayList<>();
|
||||
for (int i = 0; i < registeredFragments.size(); i++) {
|
||||
fragments.add(registeredFragments.valueAt(i));
|
||||
}
|
||||
return fragments;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package eu.kanade.tachiyomi.ui.base.fab;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.design.widget.CoordinatorLayout;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v4.view.ViewCompat;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
|
||||
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
|
||||
final View directTargetChild, final View target, final int nestedScrollAxes) {
|
||||
// Ensure we react to vertical scrolling
|
||||
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|
||||
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
|
||||
final View target, final int dxConsumed, final int dyConsumed,
|
||||
final int dxUnconsumed, final int dyUnconsumed) {
|
||||
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
|
||||
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
|
||||
// User scrolled down and the FAB is currently visible -> hide the FAB
|
||||
child.hide();
|
||||
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
|
||||
// User scrolled up and the FAB is currently not visible -> show the FAB
|
||||
child.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package eu.kanade.tachiyomi.ui.base.fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity;
|
||||
import icepick.Icepick;
|
||||
|
||||
public class BaseFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
Icepick.saveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(String title) {
|
||||
getBaseActivity().setToolbarTitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(int resourceId) {
|
||||
getBaseActivity().setToolbarTitle(getString(resourceId));
|
||||
}
|
||||
|
||||
public BaseActivity getBaseActivity() {
|
||||
return (BaseActivity) getActivity();
|
||||
}
|
||||
|
||||
public void registerForStickyEvents() {
|
||||
registerForStickyEvents(0);
|
||||
}
|
||||
|
||||
public void registerForStickyEvents(int priority) {
|
||||
EventBus.getDefault().registerSticky(this, priority);
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
registerForEvents(0);
|
||||
}
|
||||
|
||||
public void registerForEvents(int priority) {
|
||||
EventBus.getDefault().register(this, priority);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package eu.kanade.tachiyomi.ui.base.fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import eu.kanade.tachiyomi.App;
|
||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter;
|
||||
import nucleus.factory.PresenterFactory;
|
||||
import nucleus.factory.ReflectionPresenterFactory;
|
||||
import nucleus.presenter.Presenter;
|
||||
import nucleus.view.PresenterLifecycleDelegate;
|
||||
import nucleus.view.ViewWithPresenter;
|
||||
|
||||
/**
|
||||
* This class is an example of how an activity could controls it's presenter.
|
||||
* You can inherit from this class or copy/paste this class's code to
|
||||
* create your own view implementation.
|
||||
*
|
||||
* @param <P> a type of presenter to return with {@link #getPresenter}.
|
||||
*/
|
||||
public abstract class BaseRxFragment<P extends Presenter> extends BaseFragment implements ViewWithPresenter<P> {
|
||||
|
||||
private static final String PRESENTER_STATE_KEY = "presenter_state";
|
||||
private PresenterLifecycleDelegate<P> presenterDelegate =
|
||||
new PresenterLifecycleDelegate<>(ReflectionPresenterFactory.<P>fromViewClass(getClass()));
|
||||
|
||||
/**
|
||||
* Returns a current presenter factory.
|
||||
*/
|
||||
public PresenterFactory<P> getPresenterFactory() {
|
||||
return presenterDelegate.getPresenterFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a presenter factory.
|
||||
* Call this method before onCreate/onFinishInflate to override default {@link ReflectionPresenterFactory} presenter factory.
|
||||
* Use this method for presenter dependency injection.
|
||||
*/
|
||||
@Override
|
||||
public void setPresenterFactory(PresenterFactory<P> presenterFactory) {
|
||||
presenterDelegate.setPresenterFactory(presenterFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a current attached presenter.
|
||||
* This method is guaranteed to return a non-null value between
|
||||
* onResume/onPause and onAttachedToWindow/onDetachedFromWindow calls
|
||||
* if the presenter factory returns a non-null value.
|
||||
*
|
||||
* @return a currently attached presenter or null.
|
||||
*/
|
||||
public P getPresenter() {
|
||||
return presenterDelegate.getPresenter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle bundle) {
|
||||
final PresenterFactory<P> superFactory = getPresenterFactory();
|
||||
setPresenterFactory(() -> {
|
||||
P presenter = superFactory.createPresenter();
|
||||
App app = (App) getActivity().getApplication();
|
||||
app.getComponentReflection().inject(presenter);
|
||||
((BasePresenter)presenter).setContext(app.getApplicationContext());
|
||||
return presenter;
|
||||
});
|
||||
|
||||
super.onCreate(bundle);
|
||||
if (bundle != null)
|
||||
presenterDelegate.onRestoreInstanceState(bundle.getBundle(PRESENTER_STATE_KEY));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle bundle) {
|
||||
super.onSaveInstanceState(bundle);
|
||||
bundle.putBundle(PRESENTER_STATE_KEY, presenterDelegate.onSaveInstanceState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
presenterDelegate.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
presenterDelegate.onPause(getActivity().isFinishing());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package eu.kanade.tachiyomi.ui.base.presenter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import icepick.Icepick;
|
||||
import nucleus.view.ViewWithPresenter;
|
||||
|
||||
public class BasePresenter<V extends ViewWithPresenter> extends RxPresenter<V> {
|
||||
|
||||
private Context context;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSave(@NonNull Bundle state) {
|
||||
super.onSave(state);
|
||||
Icepick.saveInstanceState(this, state);
|
||||
}
|
||||
|
||||
public void registerForStickyEvents() {
|
||||
EventBus.getDefault().registerSticky(this);
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
public void setContext(Context applicationContext) {
|
||||
context = applicationContext;
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
package eu.kanade.tachiyomi.ui.base.presenter;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import nucleus.presenter.Presenter;
|
||||
import nucleus.presenter.delivery.DeliverFirst;
|
||||
import nucleus.presenter.delivery.DeliverLatestCache;
|
||||
import nucleus.presenter.delivery.DeliverReplay;
|
||||
import nucleus.presenter.delivery.Delivery;
|
||||
import rx.Observable;
|
||||
import rx.Subscription;
|
||||
import rx.functions.Action1;
|
||||
import rx.functions.Action2;
|
||||
import rx.functions.Func0;
|
||||
import rx.internal.util.SubscriptionList;
|
||||
import rx.subjects.BehaviorSubject;
|
||||
|
||||
/**
|
||||
* This is an extension of {@link Presenter} which provides RxJava functionality.
|
||||
*
|
||||
* @param <View> a type of view.
|
||||
*/
|
||||
public class RxPresenter<View> extends Presenter<View> {
|
||||
|
||||
private static final String REQUESTED_KEY = RxPresenter.class.getName() + "#requested";
|
||||
|
||||
private final BehaviorSubject<View> views = BehaviorSubject.create();
|
||||
private final SubscriptionList subscriptions = new SubscriptionList();
|
||||
|
||||
private final HashMap<Integer, Func0<Subscription>> restartables = new HashMap<>();
|
||||
private final HashMap<Integer, Subscription> restartableSubscriptions = new HashMap<>();
|
||||
private final ArrayList<Integer> requested = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Returns an {@link rx.Observable} that emits the current attached view or null.
|
||||
* See {@link BehaviorSubject} for more information.
|
||||
*
|
||||
* @return an observable that emits the current attached view or null.
|
||||
*/
|
||||
public Observable<View> view() {
|
||||
return views;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a subscription to automatically unsubscribe it during onDestroy.
|
||||
* See {@link SubscriptionList#add(Subscription) for details.}
|
||||
*
|
||||
* @param subscription a subscription to add.
|
||||
*/
|
||||
public void add(Subscription subscription) {
|
||||
subscriptions.add(subscription);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and unsubscribes a subscription that has been registered with {@link #add} previously.
|
||||
* See {@link SubscriptionList#remove(Subscription)} for details.
|
||||
*
|
||||
* @param subscription a subscription to remove.
|
||||
*/
|
||||
public void remove(Subscription subscription) {
|
||||
subscriptions.remove(subscription);
|
||||
}
|
||||
|
||||
/**
|
||||
* A restartable is any RxJava observable that can be started (subscribed) and
|
||||
* should be automatically restarted (re-subscribed) after a process restart if
|
||||
* it was still subscribed at the moment of saving presenter's state.
|
||||
*
|
||||
* Registers a factory. Re-subscribes the restartable after the process restart.
|
||||
*
|
||||
* @param restartableId id of the restartable
|
||||
* @param factory factory of the restartable
|
||||
*/
|
||||
public void restartable(int restartableId, Func0<Subscription> factory) {
|
||||
restartables.put(restartableId, factory);
|
||||
if (requested.contains(restartableId))
|
||||
start(restartableId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the given restartable.
|
||||
*
|
||||
* @param restartableId id of the restartable
|
||||
*/
|
||||
public void start(int restartableId) {
|
||||
stop(restartableId);
|
||||
requested.add(restartableId);
|
||||
restartableSubscriptions.put(restartableId, restartables.get(restartableId).call());
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes a restartable
|
||||
*
|
||||
* @param restartableId id of a restartable.
|
||||
*/
|
||||
public void stop(int restartableId) {
|
||||
requested.remove((Integer) restartableId);
|
||||
Subscription subscription = restartableSubscriptions.get(restartableId);
|
||||
if (subscription != null)
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a restartable is subscribed.
|
||||
*
|
||||
* @param restartableId id of a restartable.
|
||||
* @return True if the restartable is subscribed, false otherwise.
|
||||
*/
|
||||
public boolean isSubscribed(int restartableId) {
|
||||
Subscription s = restartableSubscriptions.get(restartableId);
|
||||
return s != null && !s.isUnsubscribed();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut that can be used instead of combining together
|
||||
* {@link #restartable(int, Func0)},
|
||||
* {@link #deliverFirst()},
|
||||
* {@link #split(Action2, Action2)}.
|
||||
*
|
||||
* @param restartableId an id of the restartable.
|
||||
* @param observableFactory a factory that should return an Observable when the restartable should run.
|
||||
* @param onNext a callback that will be called when received data should be delivered to view.
|
||||
* @param onError a callback that will be called if the source observable emits onError.
|
||||
* @param <T> the type of the observable.
|
||||
*/
|
||||
public <T> void restartableFirst(int restartableId, final Func0<Observable<T>> observableFactory,
|
||||
final Action2<View, T> onNext, @Nullable final Action2<View, Throwable> onError) {
|
||||
|
||||
restartable(restartableId, new Func0<Subscription>() {
|
||||
@Override
|
||||
public Subscription call() {
|
||||
return observableFactory.call()
|
||||
.compose(RxPresenter.this.<T>deliverFirst())
|
||||
.subscribe(split(onNext, onError));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut for calling {@link #restartableFirst(int, Func0, Action2, Action2)} with the last parameter = null.
|
||||
*/
|
||||
public <T> void restartableFirst(int restartableId, final Func0<Observable<T>> observableFactory, final Action2<View, T> onNext) {
|
||||
restartableFirst(restartableId, observableFactory, onNext, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut that can be used instead of combining together
|
||||
* {@link #restartable(int, Func0)},
|
||||
* {@link #deliverLatestCache()},
|
||||
* {@link #split(Action2, Action2)}.
|
||||
*
|
||||
* @param restartableId an id of the restartable.
|
||||
* @param observableFactory a factory that should return an Observable when the restartable should run.
|
||||
* @param onNext a callback that will be called when received data should be delivered to view.
|
||||
* @param onError a callback that will be called if the source observable emits onError.
|
||||
* @param <T> the type of the observable.
|
||||
*/
|
||||
public <T> void restartableLatestCache(int restartableId, final Func0<Observable<T>> observableFactory,
|
||||
final Action2<View, T> onNext, @Nullable final Action2<View, Throwable> onError) {
|
||||
|
||||
restartable(restartableId, new Func0<Subscription>() {
|
||||
@Override
|
||||
public Subscription call() {
|
||||
return observableFactory.call()
|
||||
.compose(RxPresenter.this.<T>deliverLatestCache())
|
||||
.subscribe(split(onNext, onError));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut for calling {@link #restartableLatestCache(int, Func0, Action2, Action2)} with the last parameter = null.
|
||||
*/
|
||||
public <T> void restartableLatestCache(int restartableId, final Func0<Observable<T>> observableFactory, final Action2<View, T> onNext) {
|
||||
restartableLatestCache(restartableId, observableFactory, onNext, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut that can be used instead of combining together
|
||||
* {@link #restartable(int, Func0)},
|
||||
* {@link #deliverReplay()},
|
||||
* {@link #split(Action2, Action2)}.
|
||||
*
|
||||
* @param restartableId an id of the restartable.
|
||||
* @param observableFactory a factory that should return an Observable when the restartable should run.
|
||||
* @param onNext a callback that will be called when received data should be delivered to view.
|
||||
* @param onError a callback that will be called if the source observable emits onError.
|
||||
* @param <T> the type of the observable.
|
||||
*/
|
||||
public <T> void restartableReplay(int restartableId, final Func0<Observable<T>> observableFactory,
|
||||
final Action2<View, T> onNext, @Nullable final Action2<View, Throwable> onError) {
|
||||
|
||||
restartable(restartableId, new Func0<Subscription>() {
|
||||
@Override
|
||||
public Subscription call() {
|
||||
return observableFactory.call()
|
||||
.compose(RxPresenter.this.<T>deliverReplay())
|
||||
.subscribe(split(onNext, onError));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut for calling {@link #restartableReplay(int, Func0, Action2, Action2)} with the last parameter = null.
|
||||
*/
|
||||
public <T> void restartableReplay(int restartableId, final Func0<Observable<T>> observableFactory, final Action2<View, T> onNext) {
|
||||
restartableReplay(restartableId, observableFactory, onNext, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link rx.Observable.Transformer} that couples views with data that has been emitted by
|
||||
* the source {@link rx.Observable}.
|
||||
*
|
||||
* {@link #deliverLatestCache} keeps the latest onNext value and emits it each time a new view gets attached.
|
||||
* If a new onNext value appears while a view is attached, it will be delivered immediately.
|
||||
*
|
||||
* @param <T> the type of source observable emissions
|
||||
*/
|
||||
public <T> DeliverLatestCache<View, T> deliverLatestCache() {
|
||||
return new DeliverLatestCache<>(views);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link rx.Observable.Transformer} that couples views with data that has been emitted by
|
||||
* the source {@link rx.Observable}.
|
||||
*
|
||||
* {@link #deliverFirst} delivers only the first onNext value that has been emitted by the source observable.
|
||||
*
|
||||
* @param <T> the type of source observable emissions
|
||||
*/
|
||||
public <T> DeliverFirst<View, T> deliverFirst() {
|
||||
return new DeliverFirst<>(views);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link rx.Observable.Transformer} that couples views with data that has been emitted by
|
||||
* the source {@link rx.Observable}.
|
||||
*
|
||||
* {@link #deliverReplay} keeps all onNext values and emits them each time a new view gets attached.
|
||||
* If a new onNext value appears while a view is attached, it will be delivered immediately.
|
||||
*
|
||||
* @param <T> the type of source observable emissions
|
||||
*/
|
||||
public <T> DeliverReplay<View, T> deliverReplay() {
|
||||
return new DeliverReplay<>(views);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a method that can be used for manual restartable chain build. It returns an Action1 that splits
|
||||
* a received {@link Delivery} into two {@link Action2} onNext and onError calls.
|
||||
*
|
||||
* @param onNext a method that will be called if the delivery contains an emitted onNext value.
|
||||
* @param onError a method that will be called if the delivery contains an onError throwable.
|
||||
* @param <T> a type on onNext value.
|
||||
* @return an Action1 that splits a received {@link Delivery} into two {@link Action2} onNext and onError calls.
|
||||
*/
|
||||
public <T> Action1<Delivery<View, T>> split(final Action2<View, T> onNext, @Nullable final Action2<View, Throwable> onError) {
|
||||
return new Action1<Delivery<View, T>>() {
|
||||
@Override
|
||||
public void call(Delivery<View, T> delivery) {
|
||||
delivery.split(onNext, onError);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut for calling {@link #split(Action2, Action2)} when the second parameter is null.
|
||||
*/
|
||||
public <T> Action1<Delivery<View, T>> split(Action2<View, T> onNext) {
|
||||
return split(onNext, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
if (savedState != null)
|
||||
requested.addAll(savedState.getIntegerArrayList(REQUESTED_KEY));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
views.onCompleted();
|
||||
subscriptions.unsubscribe();
|
||||
for (Map.Entry<Integer, Subscription> entry : restartableSubscriptions.entrySet())
|
||||
entry.getValue().unsubscribe();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
protected void onSave(Bundle state) {
|
||||
for (int i = requested.size() - 1; i >= 0; i--) {
|
||||
int restartableId = requested.get(i);
|
||||
Subscription subscription = restartableSubscriptions.get(restartableId);
|
||||
if (subscription != null && subscription.isUnsubscribed())
|
||||
requested.remove(i);
|
||||
}
|
||||
state.putIntegerArrayList(REQUESTED_KEY, requested);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
protected void onTakeView(View view) {
|
||||
views.onNext(view);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
protected void onDropView() {
|
||||
views.onNext(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Please, use restartableXX and deliverXX methods for pushing data from RxPresenter into View.
|
||||
*/
|
||||
@Deprecated
|
||||
@Nullable
|
||||
@Override
|
||||
public View getView() {
|
||||
return super.getView();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user