Allow to remove a favorite manga from info tab

This commit is contained in:
inorichi 2015-11-02 21:22:31 +01:00
parent 04dfdba0b7
commit b3f12ae333
12 changed files with 65 additions and 54 deletions

View File

@ -30,7 +30,6 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
.doOnNext(manga -> this.manga = manga), .doOnNext(manga -> this.manga = manga),
(view, manga) -> { (view, manga) -> {
view.setManga(manga); view.setManga(manga);
view.setFavoriteBtnVisible(!manga.favorite);
EventBus.getDefault().postSticky(manga); EventBus.getDefault().postSticky(manga);
}); });
} }
@ -54,14 +53,4 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
start(DB_MANGA); start(DB_MANGA);
} }
public void setFavoriteVisibility() {
if (getView() != null) {
getView().setFavoriteBtnVisible(!manga.favorite);
}
}
public boolean addToFavorites() {
manga.favorite = true;
return db.insertMangaBlock(manga).numberOfRowsUpdated() == 1;
}
} }

View File

@ -60,4 +60,14 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
} }
} }
public void initFavoriteIcon() {
if (getView() != null)
getView().setFavoriteIcon(manga.favorite);
}
public void toggleFavorite() {
manga.favorite = !manga.favorite;
db.insertMangaBlock(manga);
}
} }

View File

@ -10,9 +10,7 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -34,7 +32,6 @@ public class MangaDetailActivity extends BaseRxActivity<MangaDetailPresenter> {
private MangaDetailAdapter adapter; private MangaDetailAdapter adapter;
private long manga_id; private long manga_id;
private boolean is_online; private boolean is_online;
private MenuItem favoriteBtn;
public final static String MANGA_ID = "manga_id"; public final static String MANGA_ID = "manga_id";
public final static String MANGA_ONLINE = "manga_online"; public final static String MANGA_ONLINE = "manga_online";
@ -65,23 +62,12 @@ public class MangaDetailActivity extends BaseRxActivity<MangaDetailPresenter> {
getPresenter().queryManga(manga_id); getPresenter().queryManga(manga_id);
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manga, menu);
favoriteBtn = menu.findItem(R.id.action_favorite);
getPresenter().setFavoriteVisibility();
return true;
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;
case R.id.action_favorite:
onFavoriteClick();
return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@ -104,30 +90,14 @@ public class MangaDetailActivity extends BaseRxActivity<MangaDetailPresenter> {
view_pager.setCurrentItem(MangaDetailAdapter.CHAPTERS_FRAGMENT); view_pager.setCurrentItem(MangaDetailAdapter.CHAPTERS_FRAGMENT);
} }
public long getMangaId() {
return manga_id;
}
public void setManga(Manga manga) { public void setManga(Manga manga) {
setToolbarTitle(manga.title); setToolbarTitle(manga.title);
} }
public void setFavoriteBtnVisible(boolean visible) {
if (favoriteBtn != null)
favoriteBtn.setVisible(visible);
}
public boolean isOnlineManga() { public boolean isOnlineManga() {
return is_online; return is_online;
} }
private void onFavoriteClick() {
if (getPresenter().addToFavorites()) {
Toast.makeText(this, getString(R.string.toast_added_favorites), Toast.LENGTH_SHORT)
.show();
}
}
class MangaDetailAdapter extends FragmentPagerAdapter { class MangaDetailAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2; final int PAGE_COUNT = 2;

View File

@ -2,6 +2,9 @@ package eu.kanade.mangafeed.ui.fragment;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
@ -29,6 +32,9 @@ public class MangaInfoFragment extends BaseRxFragment<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 MenuItem favoriteBtn;
private MenuItem removeFavoriteBtn;
public static MangaInfoFragment newInstance() { public static MangaInfoFragment newInstance() {
return new MangaInfoFragment(); return new MangaInfoFragment();
} }
@ -36,6 +42,7 @@ public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
@Override @Override
public void onCreate(Bundle savedState) { public void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
setHasOptionsMenu(true);
} }
@Override @Override
@ -48,6 +55,26 @@ public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
return view; return view;
} }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.manga_info, menu);
favoriteBtn = menu.findItem(R.id.action_favorite);
removeFavoriteBtn = menu.findItem(R.id.action_remove_favorite);
getPresenter().initFavoriteIcon();
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorite:
case R.id.action_remove_favorite:
getPresenter().toggleFavorite();
break;
}
return super.onOptionsItemSelected(item);
}
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);
@ -55,6 +82,8 @@ public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
mStatus.setText("Ongoing"); //TODO mStatus.setText("Ongoing"); //TODO
mDescription.setText(manga.description); mDescription.setText(manga.description);
setFavoriteIcon(manga.favorite);
Glide.with(getActivity()) Glide.with(getActivity())
.load(manga.thumbnail_url) .load(manga.thumbnail_url)
.diskCacheStrategy(DiskCacheStrategy.RESULT) .diskCacheStrategy(DiskCacheStrategy.RESULT)
@ -65,4 +94,10 @@ public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
public void setChapterCount(int count) { public void setChapterCount(int count) {
mChapters.setText(String.valueOf(count)); mChapters.setText(String.valueOf(count));
} }
public void setFavoriteIcon(boolean isFavorite) {
if (favoriteBtn != null) favoriteBtn.setVisible(!isFavorite);
if (removeFavoriteBtn != null) removeFavoriteBtn.setVisible(isFavorite);
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 584 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 732 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_favorite"
android:title="@string/action_favorite"
android:icon="@drawable/ic_action_favorite"
app:showAsAction="always"
android:visible="false"/>
</menu>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_favorite"
android:title="@string/action_favorite"
android:icon="@drawable/ic_action_favorite"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_remove_favorite"
android:title="@string/action_remove_favorite"
android:icon="@drawable/ic_action_favorite_border"
android:visible="false"
app:showAsAction="ifRoom" />
</menu>

View File

@ -70,8 +70,8 @@
<string name="success">Success</string> <string name="success">Success</string>
<string name="invalid_login">Login error</string> <string name="invalid_login">Login error</string>
<string name="loading">Loading…</string> <string name="loading">Loading…</string>
<string name="toast_added_favorites">Added to favorites</string> <string name="action_favorite">Add to favorites</string>
<string name="action_favorite">Favorite</string> <string name="action_remove_favorite">Remove from favorites</string>
<string name="downloading">Downloading…</string> <string name="downloading">Downloading…</string>
<string name="download_progress">Downloaded %1$d%%</string> <string name="download_progress">Downloaded %1$d%%</string>
<string name="chapter_progress">Page: %1$d</string> <string name="chapter_progress">Page: %1$d</string>