Copy nucleus activity and fragment to create a base activity and fragment

This commit is contained in:
inorichi 2015-10-21 18:36:01 +02:00
parent 4966f1e15f
commit af678a5b3c
16 changed files with 217 additions and 75 deletions

View File

@ -8,7 +8,6 @@ import dagger.Component;
import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.data.DataModule;
import eu.kanade.mangafeed.presenter.CataloguePresenter; import eu.kanade.mangafeed.presenter.CataloguePresenter;
import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.presenter.MainPresenter;
import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
import eu.kanade.mangafeed.presenter.MangaDetailPresenter; import eu.kanade.mangafeed.presenter.MangaDetailPresenter;
import eu.kanade.mangafeed.presenter.MangaInfoPresenter; import eu.kanade.mangafeed.presenter.MangaInfoPresenter;
@ -24,7 +23,6 @@ import eu.kanade.mangafeed.presenter.SourcePresenter;
) )
public interface AppComponent { public interface AppComponent {
void inject(MainPresenter mainPresenter);
void inject(LibraryPresenter libraryPresenter); void inject(LibraryPresenter libraryPresenter);
void inject(MangaDetailPresenter mangaDetailPresenter); void inject(MangaDetailPresenter mangaDetailPresenter);
void inject(SourcePresenter sourcePresenter); void inject(SourcePresenter sourcePresenter);

View File

@ -1,7 +0,0 @@
package eu.kanade.mangafeed.presenter;
import eu.kanade.mangafeed.ui.activity.MainActivity;
public class MainPresenter extends BasePresenter<MainActivity> {
}

View File

@ -13,13 +13,12 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.presenter.MainPresenter; import eu.kanade.mangafeed.ui.activity.base.BaseActivity;
import eu.kanade.mangafeed.ui.fragment.LibraryFragment; import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
import eu.kanade.mangafeed.ui.fragment.SourceFragment; import eu.kanade.mangafeed.ui.fragment.SourceFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MainPresenter.class) public class MainActivity extends BaseActivity {
public class MainActivity extends BaseActivity<MainPresenter> {
@Bind(R.id.toolbar) @Bind(R.id.toolbar)
Toolbar toolbar; Toolbar toolbar;

View File

@ -17,12 +17,13 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.MangaDetailPresenter; import eu.kanade.mangafeed.presenter.MangaDetailPresenter;
import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity;
import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment; import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment;
import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment; import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaDetailPresenter.class) @RequiresPresenter(MangaDetailPresenter.class)
public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> { public class MangaDetailActivity extends BaseRxActivity<MangaDetailPresenter> {
@Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.tabs) TabLayout tabs; @Bind(R.id.tabs) TabLayout tabs;

View File

@ -13,11 +13,12 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.presenter.ReaderPresenter; import eu.kanade.mangafeed.presenter.ReaderPresenter;
import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity;
import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(ReaderPresenter.class) @RequiresPresenter(ReaderPresenter.class)
public class ReaderActivity extends BaseActivity<ReaderPresenter> { public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
@Bind(R.id.view_pager) ViewPager viewPager; @Bind(R.id.view_pager) ViewPager viewPager;
@Bind(R.id.page_number) TextView pageNumber; @Bind(R.id.page_number) TextView pageNumber;

View File

@ -1,7 +1,8 @@
package eu.kanade.mangafeed.ui.activity; package eu.kanade.mangafeed.ui.activity.base;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.App;
@ -9,18 +10,7 @@ import nucleus.factory.PresenterFactory;
import nucleus.presenter.Presenter; import nucleus.presenter.Presenter;
import nucleus.view.NucleusAppCompatActivity; import nucleus.view.NucleusAppCompatActivity;
public class BaseActivity<P extends Presenter> extends NucleusAppCompatActivity<P> { public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final PresenterFactory<P> superFactory = super.getPresenterFactory();
setPresenterFactory(() -> {
P presenter = superFactory.createPresenter();
App.getComponentReflection(getActivity()).inject(presenter);
return presenter;
});
super.onCreate(savedInstanceState);
}
protected void setupToolbar(Toolbar toolbar) { protected void setupToolbar(Toolbar toolbar) {
setSupportActionBar(toolbar); setSupportActionBar(toolbar);

View File

@ -0,0 +1,88 @@
package eu.kanade.mangafeed.ui.activity.base;
import android.os.Bundle;
import android.support.annotation.NonNull;
import eu.kanade.mangafeed.App;
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.getComponentReflection(getActivity()).inject(presenter);
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());
}
}

View File

@ -31,16 +31,12 @@ public class CatalogueHolder extends ItemViewHolder<Manga> {
public void onSetValues(Manga manga, PositionInfo positionInfo) { public void onSetValues(Manga manga, PositionInfo positionInfo) {
title.setText(manga.title); title.setText(manga.title);
String thumbnail; if (manga.thumbnail_url != null) {
if (manga.thumbnail_url != null)
thumbnail = manga.thumbnail_url;
else
thumbnail = "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg";
Glide.with(getContext()) Glide.with(getContext())
.load(thumbnail) .load(manga.thumbnail_url)
.diskCacheStrategy(DiskCacheStrategy.RESULT) .diskCacheStrategy(DiskCacheStrategy.RESULT)
.centerCrop() .centerCrop()
.into(image); .into(image);
} }
} }
}

View File

@ -1,32 +0,0 @@
package eu.kanade.mangafeed.ui.fragment;
import android.os.Bundle;
import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.ui.activity.BaseActivity;
import nucleus.factory.PresenterFactory;
import nucleus.presenter.Presenter;
import nucleus.view.NucleusSupportFragment;
public class BaseFragment<P extends Presenter> extends NucleusSupportFragment<P> {
@Override
public void onCreate(Bundle savedInstanceState) {
final PresenterFactory<P> superFactory = super.getPresenterFactory();
setPresenterFactory(() -> {
P presenter = superFactory.createPresenter();
App.getComponentReflection(getActivity()).inject(presenter);
return presenter;
});
super.onCreate(savedInstanceState);
}
public void setToolbarTitle(String title) {
((BaseActivity)getActivity()).setToolbarTitle(title);
}
public void setToolbarTitle(int resourceId) {
((BaseActivity)getActivity()).setToolbarTitle(getString(resourceId));
}
}

View File

@ -25,13 +25,14 @@ import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.CataloguePresenter; import eu.kanade.mangafeed.presenter.CataloguePresenter;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.CatalogueHolder; import eu.kanade.mangafeed.ui.adapter.CatalogueHolder;
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
import eu.kanade.mangafeed.util.PageBundle; import eu.kanade.mangafeed.util.PageBundle;
import eu.kanade.mangafeed.widget.EndlessScrollListener; import eu.kanade.mangafeed.widget.EndlessScrollListener;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import uk.co.ribot.easyadapter.EasyAdapter; import uk.co.ribot.easyadapter.EasyAdapter;
@RequiresPresenter(CataloguePresenter.class) @RequiresPresenter(CataloguePresenter.class)
public class CatalogueFragment extends BaseFragment<CataloguePresenter> { public class CatalogueFragment extends BaseRxFragment<CataloguePresenter> {
@Bind(R.id.gridView) @Bind(R.id.gridView)
GridView manga_list; GridView manga_list;

View File

@ -23,10 +23,11 @@ import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(LibraryPresenter.class) @RequiresPresenter(LibraryPresenter.class)
public class LibraryFragment extends BaseFragment<LibraryPresenter> { public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
@Bind(R.id.gridView) GridView grid; @Bind(R.id.gridView) GridView grid;
private MainActivity activity; private MainActivity activity;

View File

@ -23,11 +23,12 @@ import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.activity.ReaderActivity;
import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; import eu.kanade.mangafeed.ui.adapter.ChapterListHolder;
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import uk.co.ribot.easyadapter.EasyRecyclerAdapter; import uk.co.ribot.easyadapter.EasyRecyclerAdapter;
@RequiresPresenter(MangaChaptersPresenter.class) @RequiresPresenter(MangaChaptersPresenter.class)
public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter> { public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter> {
@Bind(R.id.chapter_list) RecyclerView chapters; @Bind(R.id.chapter_list) RecyclerView chapters;
@Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh; @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh;

View File

@ -15,10 +15,11 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.MangaInfoPresenter; import eu.kanade.mangafeed.presenter.MangaInfoPresenter;
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaInfoPresenter.class) @RequiresPresenter(MangaInfoPresenter.class)
public class MangaInfoFragment extends BaseFragment<MangaInfoPresenter> { public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
@Bind(R.id.manga_artist) TextView mArtist; @Bind(R.id.manga_artist) TextView mArtist;
@Bind(R.id.manga_author) TextView mAuthor; @Bind(R.id.manga_author) TextView mAuthor;

View File

@ -16,11 +16,12 @@ import eu.kanade.mangafeed.presenter.SourcePresenter;
import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.sources.Source;
import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.ui.adapter.SourceHolder; import eu.kanade.mangafeed.ui.adapter.SourceHolder;
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import uk.co.ribot.easyadapter.EasyAdapter; import uk.co.ribot.easyadapter.EasyAdapter;
@RequiresPresenter(SourcePresenter.class) @RequiresPresenter(SourcePresenter.class)
public class SourceFragment extends BaseFragment<SourcePresenter> { public class SourceFragment extends BaseRxFragment<SourcePresenter> {
@Bind(R.id.catalogue_list) ListView source_list; @Bind(R.id.catalogue_list) ListView source_list;

View File

@ -0,0 +1,17 @@
package eu.kanade.mangafeed.ui.fragment.base;
import android.support.v4.app.Fragment;
import eu.kanade.mangafeed.ui.activity.base.BaseActivity;
public class BaseFragment extends Fragment {
public void setToolbarTitle(String title) {
((BaseActivity)getActivity()).setToolbarTitle(title);
}
public void setToolbarTitle(int resourceId) {
((BaseActivity)getActivity()).setToolbarTitle(getString(resourceId));
}
}

View File

@ -0,0 +1,86 @@
package eu.kanade.mangafeed.ui.fragment.base;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import eu.kanade.mangafeed.App;
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.getComponentReflection(getActivity()).inject(presenter);
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());
}
}