소스 검색

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

inorichi 9 년 전
부모
커밋
af678a5b3c

+ 0 - 2
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);

+ 0 - 7
app/src/main/java/eu/kanade/mangafeed/presenter/MainPresenter.java

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

+ 2 - 3
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<MainPresenter> {
+public class MainActivity extends BaseActivity {
 
     @Bind(R.id.toolbar)
     Toolbar toolbar;

+ 2 - 1
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<MangaDetailPresenter> {
+public class MangaDetailActivity extends BaseRxActivity<MangaDetailPresenter> {
 
     @Bind(R.id.toolbar) Toolbar toolbar;
     @Bind(R.id.tabs) TabLayout tabs;

+ 2 - 1
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<ReaderPresenter> {
+public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
 
     @Bind(R.id.view_pager) ViewPager viewPager;
     @Bind(R.id.page_number) TextView pageNumber;

+ 3 - 13
app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java → 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<P extends Presenter> extends NucleusAppCompatActivity<P> {
-
-    @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);
-    }
+public class BaseActivity extends AppCompatActivity {
 
     protected void setupToolbar(Toolbar toolbar) {
         setSupportActionBar(toolbar);

+ 88 - 0
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 <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());
+    }
+}

+ 7 - 11
app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java

@@ -31,16 +31,12 @@ public class CatalogueHolder extends ItemViewHolder<Manga> {
     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);
+        }
     }
 }

+ 0 - 32
app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment.java

@@ -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));
-    }
-
-}

+ 2 - 1
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<CataloguePresenter> {
+public class CatalogueFragment extends BaseRxFragment<CataloguePresenter> {
 
     @Bind(R.id.gridView)
     GridView manga_list;

+ 2 - 1
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<LibraryPresenter> {
+public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
 
     @Bind(R.id.gridView) GridView grid;
     private MainActivity activity;

+ 2 - 1
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<MangaChaptersPresenter> {
+public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter> {
 
     @Bind(R.id.chapter_list) RecyclerView chapters;
     @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh;

+ 2 - 1
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<MangaInfoPresenter> {
+public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
 
     @Bind(R.id.manga_artist) TextView mArtist;
     @Bind(R.id.manga_author) TextView mAuthor;

+ 2 - 1
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<SourcePresenter> {
+public class SourceFragment extends BaseRxFragment<SourcePresenter> {
 
     @Bind(R.id.catalogue_list) ListView source_list;
 

+ 17 - 0
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));
+    }
+
+}

+ 86 - 0
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 <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());
+    }
+}