Use EventBus

This commit is contained in:
inorichi 2015-10-18 01:22:05 +02:00
parent 1802dd04e4
commit a52e33b628
6 changed files with 70 additions and 84 deletions

View file

@ -16,6 +16,7 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
@Inject SourceManager sourceManager; @Inject SourceManager sourceManager;
private Manga manga;
private Subscription chaptersSubscription; private Subscription chaptersSubscription;
private Subscription onlineChaptersSubscription; private Subscription onlineChaptersSubscription;
private boolean doingRequest = false; private boolean doingRequest = false;
@ -23,26 +24,34 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
@Override @Override
protected void onTakeView(MangaChaptersFragment view) { protected void onTakeView(MangaChaptersFragment view) {
super.onTakeView(view); super.onTakeView(view);
registerForStickyEvents();
getChapters(view.getMangaId());
} }
public void refreshChapters(Manga manga) { @Override
protected void onDropView() {
unregisterForEvents();
super.onDropView();
}
public void onEventMainThread(Manga manga) {
this.manga = manga;
getChapters();
}
public void refreshChapters() {
if (manga != null && !doingRequest) if (manga != null && !doingRequest)
getChaptersFromSource(manga); getChaptersFromSource(manga);
} }
private void getChapters(long manga_id) { public void getChapters() {
if (chaptersSubscription != null) if (chaptersSubscription != null)
remove(chaptersSubscription); return;
chaptersSubscription = db.getChapters(manga_id) add(chaptersSubscription = db.getChapters(manga.id)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.compose(deliverLatestCache()) .compose(deliverLatestCache())
.subscribe(this.split(MangaChaptersFragment::onNextChapters)); .subscribe(this.split(MangaChaptersFragment::onNextChapters)));
add(chaptersSubscription);
} }
public void getChaptersFromSource(Manga manga) { public void getChaptersFromSource(Manga manga) {

View file

@ -2,7 +2,9 @@ package eu.kanade.mangafeed.presenter;
import javax.inject.Inject; import javax.inject.Inject;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import rx.Observable; import rx.Observable;
import rx.Subscription; import rx.Subscription;
@ -13,25 +15,32 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
private Manga manga;
private Subscription mangaSubscription; private Subscription mangaSubscription;
@Override @Override
protected void onTakeView(MangaDetailActivity view) { protected void onTakeView(MangaDetailActivity view) {
super.onTakeView(view); super.onTakeView(view);
if (manga != null)
view.setManga(manga);
if (mangaSubscription == null) getManga(view);
initializeManga(view);
} }
private void initializeManga(MangaDetailActivity view) { private void getManga(MangaDetailActivity view) {
mangaSubscription = db.getManga(view.getMangaId()) if (mangaSubscription != null)
return;
add(mangaSubscription = db.getManga(view.getMangaId())
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.take(1) .take(1)
.flatMap(Observable::from) .flatMap(Observable::from)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(view::onMangaNext); .subscribe(manga -> {
this.manga = manga;
add(mangaSubscription); view.setManga(manga);
EventBus.getDefault().postSticky(manga);
}));
} }
} }

View file

@ -3,34 +3,47 @@ package eu.kanade.mangafeed.presenter;
import javax.inject.Inject; import javax.inject.Inject;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment; import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment;
import rx.Observable; import rx.Observable;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> { public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
private Manga manga;
private Subscription mangaInfoSubscription; private Subscription mangaInfoSubscription;
@Override @Override
protected void onTakeView(MangaInfoFragment view) { protected void onTakeView(MangaInfoFragment view) {
super.onTakeView(view); super.onTakeView(view);
registerForStickyEvents();
getMangaInfo(view);
} }
private void getMangaInfo(MangaInfoFragment view) { @Override
if (mangaInfoSubscription != null) protected void onDropView() {
remove(mangaInfoSubscription); unregisterForEvents();
super.onDropView();
}
mangaInfoSubscription = db.getManga(view.getMangaId()) public void onEventMainThread(Manga manga) {
.observeOn(AndroidSchedulers.mainThread()) this.manga = manga;
getMangaInfo();
}
private void getMangaInfo() {
if (mangaInfoSubscription != null)
return;
add(mangaInfoSubscription = db.getManga(manga.id)
.subscribeOn(Schedulers.io())
.take(1) .take(1)
.flatMap(Observable::from) .flatMap(Observable::from)
.subscribe(view::setMangaInfo); .observeOn(AndroidSchedulers.mainThread())
.compose(deliverLatestCache())
add(mangaInfoSubscription); .subscribe(split(MangaInfoFragment::setMangaInfo)));
} }
} }

View file

@ -23,27 +23,20 @@ import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaDetailPresenter.class) @RequiresPresenter(MangaDetailPresenter.class)
public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> { public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
@Bind(R.id.toolbar) @Bind(R.id.toolbar) Toolbar toolbar;
Toolbar toolbar; @Bind(R.id.tabs) TabLayout tabs;
@Bind(R.id.viewpager) ViewPager view_pager;
@Bind(R.id.tabs)
TabLayout tabs;
@Bind(R.id.viewpager)
ViewPager view_pager;
private MangaDetailAdapter adapter; private MangaDetailAdapter adapter;
private long manga_id; private long manga_id;
private boolean is_online = false; private boolean is_online;
public final static String MANGA_ID = "manga_id"; public final static String MANGA_ID = "manga_id";
public final static String MANGA_TITLE = "manga_title";
public final static String MANGA_ONLINE = "manga_online"; public final static String MANGA_ONLINE = "manga_online";
public static Intent newIntent(Context context, Manga manga) { public static Intent newIntent(Context context, Manga manga) {
Intent intent = new Intent(context, MangaDetailActivity.class); Intent intent = new Intent(context, MangaDetailActivity.class);
intent.putExtra(MANGA_ID, manga.id); intent.putExtra(MANGA_ID, manga.id);
intent.putExtra(MANGA_TITLE, manga.title);
return intent; return intent;
} }
@ -58,9 +51,6 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
Intent intent = getIntent(); Intent intent = getIntent();
String manga_title = intent.getStringExtra(MANGA_TITLE);
setToolbarTitle(manga_title);
manga_id = intent.getLongExtra(MANGA_ID, -1); manga_id = intent.getLongExtra(MANGA_ID, -1);
is_online = intent.getBooleanExtra(MANGA_ONLINE, false); is_online = intent.getBooleanExtra(MANGA_ONLINE, false);
@ -90,9 +80,8 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
return manga_id; return manga_id;
} }
public void onMangaNext(Manga manga) { public void setManga(Manga manga) {
((MangaChaptersFragment) adapter.getItem(MangaDetailAdapter.CHAPTERS_FRAGMENT)) setToolbarTitle(manga.title);
.onMangaNext(manga);
} }
class MangaDetailAdapter extends FragmentPagerAdapter { class MangaDetailAdapter extends FragmentPagerAdapter {
@ -124,9 +113,9 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
public Fragment getItem(int position) { public Fragment getItem(int position) {
switch (position) { switch (position) {
case INFO_FRAGMENT: case INFO_FRAGMENT:
return MangaInfoFragment.newInstance(manga_id); return MangaInfoFragment.newInstance();
case CHAPTERS_FRAGMENT: case CHAPTERS_FRAGMENT:
return MangaChaptersFragment.newInstance(manga_id); return MangaChaptersFragment.newInstance();
default: default:
return null; return null;

View file

@ -17,12 +17,9 @@ import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Chapter;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; import eu.kanade.mangafeed.ui.adapter.ChapterListHolder;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import timber.log.Timber;
import uk.co.ribot.easyadapter.EasyRecyclerAdapter; import uk.co.ribot.easyadapter.EasyRecyclerAdapter;
@RequiresPresenter(MangaChaptersPresenter.class) @RequiresPresenter(MangaChaptersPresenter.class)
@ -30,24 +27,16 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
@Bind(R.id.chapter_list) RecyclerView chapters; @Bind(R.id.chapter_list) RecyclerView chapters;
private long manga_id;
private Manga manga;
private EasyRecyclerAdapter<Chapter> adapter; private EasyRecyclerAdapter<Chapter> adapter;
public static Fragment newInstance(long manga_id) { public static Fragment newInstance() {
MangaChaptersFragment fragment = new MangaChaptersFragment(); return new MangaChaptersFragment();
Bundle args = new Bundle();
args.putLong(MangaDetailActivity.MANGA_ID, manga_id);
fragment.setArguments(args);
return fragment;
} }
@Override @Override
public void onCreate(Bundle savedState) { public void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
setHasOptionsMenu(true); setHasOptionsMenu(true);
manga_id = getArguments().getLong(MangaDetailActivity.MANGA_ID);
} }
@Override @Override
@ -73,7 +62,7 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_refresh: case R.id.action_refresh:
getPresenter().refreshChapters(manga); getPresenter().refreshChapters();
break; break;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -84,19 +73,8 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
chapters.setAdapter(adapter); chapters.setAdapter(adapter);
} }
public long getMangaId() {
return manga_id;
}
public Manga getManga() {
return manga;
}
public void onNextChapters(List<Chapter> chapters) { public void onNextChapters(List<Chapter> chapters) {
adapter.setItems(chapters); adapter.setItems(chapters);
} }
public void onMangaNext(Manga manga) {
this.manga = manga;
}
} }

View file

@ -15,7 +15,6 @@ 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.activity.MangaDetailActivity;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(MangaInfoPresenter.class) @RequiresPresenter(MangaInfoPresenter.class)
@ -29,20 +28,13 @@ public class MangaInfoFragment extends BaseFragment<MangaInfoPresenter> {
@Bind(R.id.manga_summary) TextView mDescription; @Bind(R.id.manga_summary) TextView mDescription;
@Bind(R.id.manga_cover) ImageView mCover; @Bind(R.id.manga_cover) ImageView mCover;
private long manga_id; public static MangaInfoFragment newInstance() {
return new MangaInfoFragment();
public static MangaInfoFragment newInstance(long manga_id) {
MangaInfoFragment fragment = new MangaInfoFragment();
Bundle args = new Bundle();
args.putLong(MangaDetailActivity.MANGA_ID, manga_id);
fragment.setArguments(args);
return fragment;
} }
@Override @Override
public void onCreate(Bundle savedState) { public void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
manga_id = getArguments().getLong(MangaDetailActivity.MANGA_ID);
} }
@Override @Override
@ -55,10 +47,6 @@ public class MangaInfoFragment extends BaseFragment<MangaInfoPresenter> {
return view; return view;
} }
public long getMangaId() {
return manga_id;
}
public void setMangaInfo(Manga manga) { public void setMangaInfo(Manga manga) {
mArtist.setText(manga.artist); mArtist.setText(manga.artist);
mAuthor.setText(manga.author); mAuthor.setText(manga.author);