All fragments using Nucleus

This commit is contained in:
inorichi 2015-10-17 16:28:35 +02:00
parent 8c0994747f
commit b36aa69a67
11 changed files with 74 additions and 85 deletions

View file

@ -1,45 +1,24 @@
package eu.kanade.mangafeed.presenter;
import android.content.Intent;
import javax.inject.Inject;
import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.data.helpers.SourceManager;
import eu.kanade.mangafeed.sources.Source;
import eu.kanade.mangafeed.ui.activity.CatalogueActivity;
import eu.kanade.mangafeed.ui.adapter.SourceHolder;
import eu.kanade.mangafeed.view.SourceView;
import uk.co.ribot.easyadapter.EasyAdapter;
import eu.kanade.mangafeed.ui.fragment.SourceFragment;
public class SourcePresenter {
private SourceView view;
public class SourcePresenter extends BasePresenter2<SourceFragment> {
@Inject SourceManager sourceManager;
EasyAdapter<Source> adapter;
@Override
protected void onTakeView(SourceFragment view) {
super.onTakeView(view);
public SourcePresenter(SourceView view) {
this.view = view;
App.getComponent(view.getActivity()).inject(this);
view.setItems(sourceManager.getSources());
}
public void initializeSources() {
adapter = new EasyAdapter<>(
view.getActivity(),
SourceHolder.class,
sourceManager.getSources());
view.setAdapter(adapter);
view.setSourceClickListener();
}
public void onSourceClick(int position) {
sourceManager.setSelectedSource(adapter.getItem(position).getSource());
Intent intent = new Intent(view.getActivity(), CatalogueActivity.class);
intent.putExtra(Intent.EXTRA_UID, adapter.getItem(position).getSource());
view.getActivity().startActivity(intent);
public void setSelectedSource(Source source) {
sourceManager.setSelectedSource(source.getSourceId());
}
}

View file

@ -92,7 +92,7 @@ public class Batoto extends Source {
}
@Override
public int getSource() {
public int getSourceId() {
return SourceManager.BATOTO;
}
@ -148,7 +148,7 @@ public class Batoto extends Source {
Element nameElement = urlElement;
Element updateElement = htmlBlock.select("td").get(5);
mangaFromHtmlBlock.source = getSource();
mangaFromHtmlBlock.source = getSourceId();
if (urlElement != null) {
String fieldUrl = urlElement.attr("href");

View file

@ -37,7 +37,7 @@ public class MangaHere extends Source {
return NAME;
}
public int getSource() {
public int getSourceId() {
return SourceManager.MANGAHERE;
}
@ -112,7 +112,7 @@ public class MangaHere extends Source {
private Manga constructMangaFromHtmlBlock(Element htmlBlock) {
Manga mangaFromHtmlBlock = new Manga();
mangaFromHtmlBlock.source = getSource();
mangaFromHtmlBlock.source = getSourceId();
Element urlElement = htmlBlock.select("a.manga_info").first();
Element nameElement = htmlBlock.select("a.manga_info").first();

View file

@ -102,7 +102,7 @@ public abstract class Source {
}
public abstract String getName();
public abstract int getSource();
public abstract int getSourceId();
protected abstract String getUrlFromPageNumber(int page);
protected abstract String getSearchUrl(String query, int page);

View file

@ -1,5 +1,7 @@
package eu.kanade.mangafeed.ui.activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
@ -39,6 +41,14 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
private EasyAdapter<Manga> adapter;
private EndlessScrollListener scroll_listener;
public final static String SOURCE_ID = "source_id";
public static Intent newIntent(Context context, int source_id) {
Intent intent = new Intent(context, CatalogueActivity.class);
intent.putExtra(SOURCE_ID, source_id);
return intent;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View file

@ -1,6 +1,23 @@
package eu.kanade.mangafeed.ui.fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import eu.kanade.mangafeed.App;
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 class BaseFragment extends Fragment {
}

View file

@ -1,23 +0,0 @@
package eu.kanade.mangafeed.ui.fragment;
import android.os.Bundle;
import eu.kanade.mangafeed.App;
import nucleus.factory.PresenterFactory;
import nucleus.presenter.Presenter;
import nucleus.view.NucleusSupportFragment;
public class BaseFragment2<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);
}
}

View file

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

View file

@ -13,7 +13,7 @@ import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaChaptersPresenter.class)
public class MangaChaptersFragment extends BaseFragment2<MangaChaptersPresenter> {
public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter> {
private long manga_id;

View file

@ -19,7 +19,7 @@ import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaInfoPresenter.class)
public class MangaInfoFragment extends BaseFragment2<MangaInfoPresenter> {
public class MangaInfoFragment extends BaseFragment<MangaInfoPresenter> {
@Bind(R.id.manga_artist) TextView mArtist;
@Bind(R.id.manga_author) TextView mAuthor;

View file

@ -1,40 +1,41 @@
package eu.kanade.mangafeed.ui.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.presenter.SourcePresenter;
import eu.kanade.mangafeed.sources.Source;
import eu.kanade.mangafeed.ui.activity.CatalogueActivity;
import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.view.SourceView;
import eu.kanade.mangafeed.ui.adapter.SourceHolder;
import nucleus.factory.RequiresPresenter;
import uk.co.ribot.easyadapter.EasyAdapter;
@RequiresPresenter(SourcePresenter.class)
public class SourceFragment extends BaseFragment<SourcePresenter> {
public class SourceFragment extends BaseFragment implements SourceView {
@Bind(R.id.catalogue_list) ListView source_list;
private SourcePresenter presenter;
private MainActivity activity;
@Bind(R.id.catalogue_list)
ListView source_list;
private EasyAdapter<Source> adapter;
public static SourceFragment newInstance() {
SourceFragment fragment = new SourceFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
return new SourceFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new SourcePresenter(this);
activity = (MainActivity)getActivity();
}
@ -46,22 +47,27 @@ public class SourceFragment extends BaseFragment implements SourceView {
activity.setToolbarTitle(R.string.catalogues_title);
ButterKnife.bind(this, view);
presenter.initializeSources();
createAdapter();
return view;
}
// SourceView
@OnItemClick(R.id.catalogue_list)
public void onSourceClick(int position) {
Source source = adapter.getItem(position);
getPresenter().setSelectedSource(source);
@Override
public void setAdapter(EasyAdapter adapter) {
Intent intent = CatalogueActivity.newIntent(activity, source.getSourceId());
startActivity(intent);
}
private void createAdapter() {
adapter = new EasyAdapter<>(activity, SourceHolder.class);
source_list.setAdapter(adapter);
}
@Override
public void setSourceClickListener() {
source_list.setOnItemClickListener(
(parent, view, position, id) ->
presenter.onSourceClick(position)
);
public void setItems(List<Source> items) {
adapter.setItems(items);
}
}