mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
Improved comments
This commit is contained in:
parent
1cd77a97a7
commit
1dd49a2ab1
2 changed files with 173 additions and 40 deletions
|
@ -21,114 +21,223 @@ import eu.kanade.tachiyomi.data.source.base.Source;
|
|||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment;
|
||||
import nucleus.factory.RequiresPresenter;
|
||||
|
||||
/**
|
||||
* Fragment that shows manga information.
|
||||
* Uses R.layout.fragment_manga_info.
|
||||
* UI related actions should be called from here.
|
||||
*/
|
||||
@RequiresPresenter(MangaInfoPresenter.class)
|
||||
public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
|
||||
/**
|
||||
* SwipeRefreshLayout showing refresh status
|
||||
*/
|
||||
@Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh;
|
||||
|
||||
/**
|
||||
* TextView containing artist information.
|
||||
*/
|
||||
@Bind(R.id.manga_artist) TextView artist;
|
||||
|
||||
/**
|
||||
* TextView containing author information.
|
||||
*/
|
||||
@Bind(R.id.manga_author) TextView author;
|
||||
|
||||
/**
|
||||
* TextView containing chapter count.
|
||||
*/
|
||||
@Bind(R.id.manga_chapters) TextView chapterCount;
|
||||
|
||||
/**
|
||||
* TextView containing genres.
|
||||
*/
|
||||
@Bind(R.id.manga_genres) TextView genres;
|
||||
|
||||
/**
|
||||
* TextView containing status (ongoing, finished).
|
||||
*/
|
||||
@Bind(R.id.manga_status) TextView status;
|
||||
|
||||
/**
|
||||
* TextView containing source.
|
||||
*/
|
||||
@Bind(R.id.manga_source) TextView source;
|
||||
|
||||
/**
|
||||
* TextView containing manga summary.
|
||||
*/
|
||||
@Bind(R.id.manga_summary) TextView description;
|
||||
|
||||
/**
|
||||
* ImageView of cover.
|
||||
*/
|
||||
@Bind(R.id.manga_cover) ImageView cover;
|
||||
|
||||
/**
|
||||
* ImageView containing manga cover shown as blurred backdrop.
|
||||
*/
|
||||
@Bind(R.id.backdrop) ImageView backdrop;
|
||||
|
||||
/**
|
||||
* FAB anchored to bottom of top view used to (add / remove) manga (to / from) library.
|
||||
*/
|
||||
@Bind(R.id.fab_favorite) FloatingActionButton fabFavorite;
|
||||
|
||||
/**
|
||||
* Create new instance of MangaInfoFragment.
|
||||
*
|
||||
* @return MangaInfoFragment.
|
||||
*/
|
||||
public static MangaInfoFragment newInstance() {
|
||||
return new MangaInfoFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
// Inflate the layout for this fragment.
|
||||
View view = inflater.inflate(R.layout.fragment_manga_info, container, false);
|
||||
|
||||
// Bind layout objects.
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
// Set onclickListener to toggle favorite when FAB clicked.
|
||||
fabFavorite.setOnClickListener(v -> getPresenter().toggleFavorite());
|
||||
|
||||
// Set SwipeRefresh to refresh manga data.
|
||||
swipeRefresh.setOnRefreshListener(this::fetchMangaFromSource);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if manga is initialized.
|
||||
* If true update view with manga information,
|
||||
* if false fetch manga information
|
||||
*
|
||||
* @param manga manga object containing information about manga.
|
||||
* @param source the source of the manga.
|
||||
*/
|
||||
public void onNextManga(Manga manga, Source source) {
|
||||
if (manga.initialized) {
|
||||
// Update view.
|
||||
setMangaInfo(manga, source);
|
||||
} else {
|
||||
// Initialize manga
|
||||
// Initialize manga.
|
||||
fetchMangaFromSource();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the info of the manga
|
||||
* Update the view with manga information.
|
||||
*
|
||||
* @param manga manga object containing information about manga
|
||||
* @param mangaSource the source of the manga
|
||||
* @param manga manga object containing information about manga.
|
||||
* @param mangaSource the source of the manga.
|
||||
*/
|
||||
private void setMangaInfo(Manga manga, Source mangaSource) {
|
||||
// Update artist TextView.
|
||||
artist.setText(manga.artist);
|
||||
|
||||
// Update author TextView.
|
||||
author.setText(manga.author);
|
||||
|
||||
// If manga source is known update source TextView.
|
||||
if (mangaSource != null) {
|
||||
source.setText(mangaSource.getName());
|
||||
}
|
||||
|
||||
// Update genres TextView.
|
||||
genres.setText(manga.genre);
|
||||
|
||||
// Update status TextView.
|
||||
status.setText(manga.getStatus(getActivity()));
|
||||
|
||||
// Update description TextView.
|
||||
description.setText(manga.description);
|
||||
|
||||
// Set the favorite drawable to the correct one.
|
||||
setFavoriteDrawable(manga.favorite);
|
||||
|
||||
// Initialize CoverCache and Glide headers to retrieve cover information.
|
||||
CoverCache coverCache = getPresenter().coverCache;
|
||||
LazyHeaders headers = getPresenter().source.getGlideHeaders();
|
||||
if (manga.thumbnail_url != null && cover.getDrawable() == null) {
|
||||
if (manga.favorite) {
|
||||
coverCache.saveOrLoadFromCache(cover, manga.thumbnail_url, headers);
|
||||
} else {
|
||||
coverCache.loadFromNetwork(cover, manga.thumbnail_url, headers);
|
||||
|
||||
// Check if thumbnail_url is given.
|
||||
if (manga.thumbnail_url != null) {
|
||||
// Check if cover is already drawn.
|
||||
if (cover.getDrawable() == null) {
|
||||
// If manga is in library then (download / save) (from / to) local cache if available,
|
||||
// else download from network.
|
||||
if (manga.favorite) {
|
||||
coverCache.saveOrLoadFromCache(cover, manga.thumbnail_url, headers);
|
||||
} else {
|
||||
coverCache.loadFromNetwork(cover, manga.thumbnail_url, headers);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (manga.thumbnail_url != null && backdrop.getDrawable() == null) {
|
||||
if (manga.favorite) {
|
||||
coverCache.saveOrLoadFromCache(backdrop, manga.thumbnail_url, headers);
|
||||
} else {
|
||||
coverCache.loadFromNetwork(backdrop, manga.thumbnail_url, headers);
|
||||
// Check if backdrop is already drawn.
|
||||
if (backdrop.getDrawable() == null) {
|
||||
// If manga is in library then (download / save) (from / to) local cache if available,
|
||||
// else download from network.
|
||||
if (manga.favorite) {
|
||||
coverCache.saveOrLoadFromCache(backdrop, manga.thumbnail_url, headers);
|
||||
} else {
|
||||
coverCache.loadFromNetwork(backdrop, manga.thumbnail_url, headers);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update chapter count TextView.
|
||||
*
|
||||
* @param count number of chapters.
|
||||
*/
|
||||
public void setChapterCount(int count) {
|
||||
chapterCount.setText(String.valueOf(count));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update FAB with correct drawable.
|
||||
*
|
||||
* @param isFavorite determines if manga is favorite or not.
|
||||
*/
|
||||
private void setFavoriteDrawable(boolean isFavorite) {
|
||||
// Set the Favorite drawable to the correct one.
|
||||
// Border drawable if false, filled drawable if true.
|
||||
fabFavorite.setImageDrawable(ContextCompat.getDrawable(getContext(), isFavorite ?
|
||||
R.drawable.ic_bookmark_white_24dp :
|
||||
R.drawable.ic_bookmark_border_white_24dp));
|
||||
}
|
||||
|
||||
/**
|
||||
* Start fetching manga information from source.
|
||||
*/
|
||||
private void fetchMangaFromSource() {
|
||||
setRefreshing(true);
|
||||
// Call presenter and start fetching manga information
|
||||
getPresenter().fetchMangaFromSource();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update swipeRefresh to stop showing refresh in progress spinner.
|
||||
*/
|
||||
public void onFetchMangaDone() {
|
||||
setRefreshing(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update swipeRefresh to start showing refresh in progress spinner.
|
||||
*/
|
||||
public void onFetchMangaError() {
|
||||
setRefreshing(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set swipeRefresh status.
|
||||
*
|
||||
* @param value status of manga fetch.
|
||||
*/
|
||||
private void setRefreshing(boolean value) {
|
||||
swipeRefresh.setRefreshing(value);
|
||||
}
|
||||
|
|
|
@ -19,42 +19,55 @@ import rx.Observable;
|
|||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Presenter of MangaInfoFragment.
|
||||
* Contains information and data for fragment.
|
||||
* Observable updates should be called from here.
|
||||
*/
|
||||
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
||||
|
||||
/**
|
||||
* The id of the restartable.
|
||||
*/
|
||||
private static final int GET_MANGA = 1;
|
||||
|
||||
/**
|
||||
* The id of the restartable.
|
||||
*/
|
||||
private static final int GET_CHAPTER_COUNT = 2;
|
||||
|
||||
/**
|
||||
* The id of the restartable.
|
||||
*/
|
||||
private static final int FETCH_MANGA_INFO = 3;
|
||||
|
||||
/**
|
||||
* Source information
|
||||
* Source information.
|
||||
*/
|
||||
protected Source source;
|
||||
|
||||
/**
|
||||
* Used to connect to database
|
||||
* Used to connect to database.
|
||||
*/
|
||||
@Inject DatabaseHelper db;
|
||||
|
||||
/**
|
||||
* Used to connect to different manga sources
|
||||
* Used to connect to different manga sources.
|
||||
*/
|
||||
@Inject SourceManager sourceManager;
|
||||
|
||||
/**
|
||||
* Used to connect to cache
|
||||
* Used to connect to cache.
|
||||
*/
|
||||
@Inject CoverCache coverCache;
|
||||
|
||||
/**
|
||||
* Selected manga information
|
||||
* Selected manga information.
|
||||
*/
|
||||
private Manga manga;
|
||||
|
||||
/**
|
||||
* Count of chapters
|
||||
* Count of chapters.
|
||||
*/
|
||||
private int count = -1;
|
||||
|
||||
|
@ -62,23 +75,23 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
|
||||
// Notify the view a manga is available or has changed
|
||||
// Notify the view a manga is available or has changed.
|
||||
startableLatestCache(GET_MANGA,
|
||||
() -> Observable.just(manga),
|
||||
(view, manga) -> view.onNextManga(manga, source));
|
||||
|
||||
// Update chapter count
|
||||
// Update chapter count.
|
||||
startableLatestCache(GET_CHAPTER_COUNT,
|
||||
() -> Observable.just(count),
|
||||
MangaInfoFragment::setChapterCount);
|
||||
|
||||
// Fetch manga info from source
|
||||
// Fetch manga info from source.
|
||||
startableFirst(FETCH_MANGA_INFO,
|
||||
this::fetchMangaObs,
|
||||
(view, manga) -> view.onFetchMangaDone(),
|
||||
(view, error) -> view.onFetchMangaError());
|
||||
|
||||
// Listen for events
|
||||
// Listen for events.
|
||||
registerForEvents();
|
||||
}
|
||||
|
||||
|
@ -99,12 +112,13 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
public void onEvent(ChapterCountEvent event) {
|
||||
if (count != event.getCount()) {
|
||||
count = event.getCount();
|
||||
// Update chapter count
|
||||
start(GET_CHAPTER_COUNT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch manga info from source
|
||||
* Fetch manga information from source.
|
||||
*/
|
||||
public void fetchMangaFromSource() {
|
||||
if (isUnsubscribed(FETCH_MANGA_INFO)) {
|
||||
|
@ -112,6 +126,11 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch manga information from source.
|
||||
*
|
||||
* @return manga information.
|
||||
*/
|
||||
private Observable<Manga> fetchMangaObs() {
|
||||
return source.pullMangaFromNetwork(manga.url)
|
||||
.flatMap(networkManga -> {
|
||||
|
@ -124,6 +143,9 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
.doOnNext(manga -> refreshManga());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update favorite status of manga, (removes / adds) manga (to / from) library.
|
||||
*/
|
||||
public void toggleFavorite() {
|
||||
manga.favorite = !manga.favorite;
|
||||
onMangaFavoriteChange(manga.favorite);
|
||||
|
@ -132,7 +154,11 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* (Removes / Saves) cover depending on favorite status.
|
||||
*
|
||||
* @param isFavorite determines if manga is favorite or not.
|
||||
*/
|
||||
private void onMangaFavoriteChange(boolean isFavorite) {
|
||||
if (isFavorite) {
|
||||
coverCache.save(manga.thumbnail_url, source.getGlideHeaders());
|
||||
|
@ -141,12 +167,10 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
|
|||
}
|
||||
}
|
||||
|
||||
public Manga getManga() {
|
||||
return manga;
|
||||
}
|
||||
|
||||
// Used to refresh the view
|
||||
protected void refreshManga() {
|
||||
/**
|
||||
* Refresh MangaInfo view.
|
||||
*/
|
||||
private void refreshManga() {
|
||||
start(GET_MANGA);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue