From af678a5b3ca5698cacb29d321f31b85e799be966 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 21 Oct 2015 18:36:01 +0200 Subject: [PATCH] Copy nucleus activity and fragment to create a base activity and fragment --- .../eu/kanade/mangafeed/AppComponent.java | 2 - .../mangafeed/presenter/MainPresenter.java | 7 -- .../mangafeed/ui/activity/MainActivity.java | 5 +- .../ui/activity/MangaDetailActivity.java | 3 +- .../mangafeed/ui/activity/ReaderActivity.java | 3 +- .../ui/activity/{ => base}/BaseActivity.java | 16 +--- .../ui/activity/base/BaseRxActivity.java | 88 +++++++++++++++++++ .../mangafeed/ui/adapter/CatalogueHolder.java | 18 ++-- .../mangafeed/ui/fragment/BaseFragment.java | 32 ------- .../ui/fragment/CatalogueFragment.java | 3 +- .../ui/fragment/LibraryFragment.java | 3 +- .../ui/fragment/MangaChaptersFragment.java | 3 +- .../ui/fragment/MangaInfoFragment.java | 3 +- .../mangafeed/ui/fragment/SourceFragment.java | 3 +- .../ui/fragment/base/BaseFragment.java | 17 ++++ .../ui/fragment/base/BaseRxFragment.java | 86 ++++++++++++++++++ 16 files changed, 217 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/mangafeed/presenter/MainPresenter.java rename app/src/main/java/eu/kanade/mangafeed/ui/activity/{ => base}/BaseActivity.java (61%) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseRxActivity.java delete mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseFragment.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseRxFragment.java diff --git a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java index 4557c3550b..f058920ae4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java @@ -8,7 +8,6 @@ import dagger.Component; import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.presenter.CataloguePresenter; import eu.kanade.mangafeed.presenter.LibraryPresenter; -import eu.kanade.mangafeed.presenter.MainPresenter; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.presenter.MangaDetailPresenter; import eu.kanade.mangafeed.presenter.MangaInfoPresenter; @@ -24,7 +23,6 @@ import eu.kanade.mangafeed.presenter.SourcePresenter; ) public interface AppComponent { - void inject(MainPresenter mainPresenter); void inject(LibraryPresenter libraryPresenter); void inject(MangaDetailPresenter mangaDetailPresenter); void inject(SourcePresenter sourcePresenter); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MainPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MainPresenter.java deleted file mode 100644 index 792d8a12a8..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MainPresenter.java +++ /dev/null @@ -1,7 +0,0 @@ -package eu.kanade.mangafeed.presenter; - -import eu.kanade.mangafeed.ui.activity.MainActivity; - -public class MainPresenter extends BasePresenter { - -} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java index 208fe513c3..aec529b60a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java @@ -13,13 +13,12 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; import butterknife.Bind; import butterknife.ButterKnife; 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.SourceFragment; import nucleus.factory.RequiresPresenter; -@RequiresPresenter(MainPresenter.class) -public class MainActivity extends BaseActivity { +public class MainActivity extends BaseActivity { @Bind(R.id.toolbar) Toolbar toolbar; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java index 97333cc7a7..19f90cb8eb 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java @@ -17,12 +17,13 @@ import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; 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.MangaInfoFragment; import nucleus.factory.RequiresPresenter; @RequiresPresenter(MangaDetailPresenter.class) -public class MangaDetailActivity extends BaseActivity { +public class MangaDetailActivity extends BaseRxActivity { @Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.tabs) TabLayout tabs; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java index 0e6eff9b3f..ed101b0e6c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java @@ -13,11 +13,12 @@ import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.presenter.ReaderPresenter; +import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity; import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; import nucleus.factory.RequiresPresenter; @RequiresPresenter(ReaderPresenter.class) -public class ReaderActivity extends BaseActivity { +public class ReaderActivity extends BaseRxActivity { @Bind(R.id.view_pager) ViewPager viewPager; @Bind(R.id.page_number) TextView pageNumber; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseActivity.java similarity index 61% rename from app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java rename to app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseActivity.java index 97ea61110e..b9bab9229f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseActivity.java @@ -1,7 +1,8 @@ -package eu.kanade.mangafeed.ui.activity; +package eu.kanade.mangafeed.ui.activity.base; import android.content.Context; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import eu.kanade.mangafeed.App; @@ -9,18 +10,7 @@ import nucleus.factory.PresenterFactory; import nucleus.presenter.Presenter; import nucleus.view.NucleusAppCompatActivity; -public class BaseActivity

extends NucleusAppCompatActivity

{ - - @Override - protected void onCreate(Bundle savedInstanceState) { - final PresenterFactory

superFactory = super.getPresenterFactory(); - setPresenterFactory(() -> { - P presenter = superFactory.createPresenter(); - App.getComponentReflection(getActivity()).inject(presenter); - return presenter; - }); - super.onCreate(savedInstanceState); - } +public class BaseActivity extends AppCompatActivity { protected void setupToolbar(Toolbar toolbar) { setSupportActionBar(toolbar); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseRxActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseRxActivity.java new file mode 100644 index 0000000000..e8ef7bc577 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/base/BaseRxActivity.java @@ -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

a type of presenter to return with {@link #getPresenter}. + */ +public abstract class BaseRxActivity

extends BaseActivity implements ViewWithPresenter

{ + + private static final String PRESENTER_STATE_KEY = "presenter_state"; + + private PresenterLifecycleDelegate

presenterDelegate = + new PresenterLifecycleDelegate<>(ReflectionPresenterFactory.

fromViewClass(getClass())); + + /** + * Returns a current presenter factory. + */ + public PresenterFactory

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

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

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()); + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java index 461e92b88f..a95a201167 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java @@ -31,16 +31,12 @@ public class CatalogueHolder extends ItemViewHolder { public void onSetValues(Manga manga, PositionInfo positionInfo) { title.setText(manga.title); - String thumbnail; - 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()) - .load(thumbnail) - .diskCacheStrategy(DiskCacheStrategy.RESULT) - .centerCrop() - .into(image); + if (manga.thumbnail_url != null) { + Glide.with(getContext()) + .load(manga.thumbnail_url) + .diskCacheStrategy(DiskCacheStrategy.RESULT) + .centerCrop() + .into(image); + } } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment.java deleted file mode 100644 index 25c91f457f..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment.java +++ /dev/null @@ -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

extends NucleusSupportFragment

{ - - @Override - public void onCreate(Bundle savedInstanceState) { - final PresenterFactory

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)); - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java index 348a388ab7..fbfbc37308 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java @@ -25,13 +25,14 @@ import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.CataloguePresenter; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; 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.widget.EndlessScrollListener; import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyAdapter; @RequiresPresenter(CataloguePresenter.class) -public class CatalogueFragment extends BaseFragment { +public class CatalogueFragment extends BaseRxFragment { @Bind(R.id.gridView) GridView manga_list; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java index e2e4d19bba..6d6ee81c6e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java @@ -23,10 +23,11 @@ import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; +import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; @RequiresPresenter(LibraryPresenter.class) -public class LibraryFragment extends BaseFragment { +public class LibraryFragment extends BaseRxFragment { @Bind(R.id.gridView) GridView grid; private MainActivity activity; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java index 8878568b94..c18f7fcb57 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java @@ -23,11 +23,12 @@ import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; +import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyRecyclerAdapter; @RequiresPresenter(MangaChaptersPresenter.class) -public class MangaChaptersFragment extends BaseFragment { +public class MangaChaptersFragment extends BaseRxFragment { @Bind(R.id.chapter_list) RecyclerView chapters; @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java index 698eaaa223..dfa02f30cd 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java @@ -15,10 +15,11 @@ import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.MangaInfoPresenter; +import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; @RequiresPresenter(MangaInfoPresenter.class) -public class MangaInfoFragment extends BaseFragment { +public class MangaInfoFragment extends BaseRxFragment { @Bind(R.id.manga_artist) TextView mArtist; @Bind(R.id.manga_author) TextView mAuthor; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java index 3cce7a2dc2..fab6b3d6bf 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java @@ -16,11 +16,12 @@ import eu.kanade.mangafeed.presenter.SourcePresenter; import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.adapter.SourceHolder; +import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyAdapter; @RequiresPresenter(SourcePresenter.class) -public class SourceFragment extends BaseFragment { +public class SourceFragment extends BaseRxFragment { @Bind(R.id.catalogue_list) ListView source_list; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseFragment.java new file mode 100644 index 0000000000..91d82ef943 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseFragment.java @@ -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)); + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseRxFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseRxFragment.java new file mode 100644 index 0000000000..b7f416385c --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/base/BaseRxFragment.java @@ -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

a type of presenter to return with {@link #getPresenter}. + */ +public abstract class BaseRxFragment

extends BaseFragment implements ViewWithPresenter

{ + + private static final String PRESENTER_STATE_KEY = "presenter_state"; + private PresenterLifecycleDelegate

presenterDelegate = + new PresenterLifecycleDelegate<>(ReflectionPresenterFactory.

fromViewClass(getClass())); + + /** + * Returns a current presenter factory. + */ + public PresenterFactory

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

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

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()); + } +} \ No newline at end of file