From b94f86765d1299c1baed9b0c80772a95af9d04fe Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Thu, 18 Feb 2016 19:01:40 +0100 Subject: [PATCH] Code cleanup, implements #118 --- .../ui/recent/RecentChaptersAdapter.java | 34 +++- .../ui/recent/RecentChaptersFragment.java | 53 +++++- .../ui/recent/RecentChaptersHolder.java | 50 +++--- .../ui/recent/RecentChaptersPresenter.java | 157 +++++++++++++++--- 4 files changed, 244 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersAdapter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersAdapter.java index 3ff8d1c79..2f2fae299 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersAdapter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersAdapter.java @@ -16,13 +16,33 @@ import eu.davidea.flexibleadapter.FlexibleAdapter; import eu.kanade.tachiyomi.R; import eu.kanade.tachiyomi.data.database.models.MangaChapter; +/** + * Adapter of RecentChaptersHolder. + * Connection between Fragment and Holder + * Holder updates should be called from here. + */ public class RecentChaptersAdapter extends FlexibleAdapter { - private RecentChaptersFragment fragment; + /** + * Fragment of RecentChaptersFragment + */ + private final RecentChaptersFragment fragment; + /** + * The id of the view type + */ private static final int VIEW_TYPE_CHAPTER = 0; + + /** + * The id of the view type + */ private static final int VIEW_TYPE_SECTION = 1; + /** + * Constructor + * + * @param fragment fragment + */ public RecentChaptersAdapter(RecentChaptersFragment fragment) { this.fragment = fragment; setHasStableIds(true); @@ -37,6 +57,11 @@ public class RecentChaptersAdapter extends FlexibleAdapter items) { mItems = items; notifyDataSetChanged(); @@ -56,6 +81,8 @@ public class RecentChaptersAdapter extends FlexibleAdapter implements FlexibleViewHolder.OnListItemClickListener { @@ -60,14 +65,21 @@ public class RecentChaptersFragment extends BaseRxFragment chapters) { adapter.setItems(chapters); } @Override public boolean onListItemClick(int position) { + // Get item from position Object item = adapter.getItem(position); if (item instanceof MangaChapter) { + // Open chapter in reader openChapter((MangaChapter) item); } return false; @@ -75,14 +87,25 @@ public class RecentChaptersFragment extends BaseRxFragment chapters, Manga manga) { // Start the download service. DownloadService.start(getActivity()); @@ -107,6 +138,12 @@ public class RecentChaptersFragment extends BaseRxFragment chapters, Manga manga) { int size = adapter.getSelectedItemCount(); @@ -135,11 +172,25 @@ public class RecentChaptersFragment extends BaseRxFragment chapters) { getPresenter().markChaptersRead(chapters, true); return true; } + /** + * Mark chapter as unread + * + * @param chapters selected chapter + * @return true + */ + @SuppressWarnings("SameReturnValue") protected boolean onMarkAsUnread(Observable chapters) { getPresenter().markChaptersRead(chapters, false); return true; diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersHolder.java b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersHolder.java index 0db33f2ac..7aa99ae0b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersHolder.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersHolder.java @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.ui.recent; -import android.content.Context; import android.support.v4.content.ContextCompat; import android.view.Menu; import android.view.View; @@ -17,6 +16,11 @@ import eu.kanade.tachiyomi.data.download.model.Download; import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder; import rx.Observable; +/** + * Holder that contains chapter item + * Uses R.layout.item_recent_chapter. + * UI related actions should be called from here. + */ public class RecentChaptersHolder extends FlexibleViewHolder { /** @@ -24,11 +28,6 @@ public class RecentChaptersHolder extends FlexibleViewHolder { */ private final RecentChaptersAdapter adapter; - /** - * Interface to global information about an application environment. - */ - private Context context; - /** * TextView containing chapter title */ @@ -49,11 +48,6 @@ public class RecentChaptersHolder extends FlexibleViewHolder { */ @Bind(R.id.chapter_menu) RelativeLayout chapterMenu; - /** - * TextView containing read progress - */ -// @Bind(R.id.chapter_pages) TextView pages; - /** * Color of read chapter */ @@ -71,15 +65,13 @@ public class RecentChaptersHolder extends FlexibleViewHolder { /** * Constructor of RecentChaptersHolder - * - * @param view - * @param adapter - * @param onListItemClickListener + * @param view view of ChapterHolder + * @param adapter adapter of ChapterHolder + * @param onListItemClickListener ClickListener */ public RecentChaptersHolder(View view, RecentChaptersAdapter adapter, OnListItemClickListener onListItemClickListener) { super(view, adapter, onListItemClickListener); this.adapter = adapter; - context = view.getContext(); ButterKnife.bind(this, view); // Set colors. @@ -90,28 +82,38 @@ public class RecentChaptersHolder extends FlexibleViewHolder { chapterMenu.setOnClickListener(v -> v.post(() -> showPopupMenu(v))); } + /** + * Set values of view + * + * @param item item containing chapter information + */ public void onSetValues(MangaChapter item) { this.mangaChapter = item; + + // Set chapter title chapterTitle.setText(item.chapter.name); + + // Set manga title mangaTitle.setText(item.manga.title); + // Check if chapter is read and set correct color if (item.chapter.read) { chapterTitle.setTextColor(readColor); mangaTitle.setTextColor(readColor); } else { chapterTitle.setTextColor(unreadColor); mangaTitle.setTextColor(unreadColor); - -// if (item.chapter.last_page_read > 0) { -// pages.setText(context.getString(R.string.chapter_progress, item.chapter.last_page_read + 1)); -// } else { -// pages.setText(""); -// } } + // Set chapter status onStatusChange(item.chapter.status); } + /** + * Updates chapter status in view. + * + * @param status download status + */ public void onStatusChange(int status) { switch (status) { case Download.QUEUE: @@ -132,6 +134,10 @@ public class RecentChaptersHolder extends FlexibleViewHolder { } } + /** + * Show pop up menu + * @param view view containing popup menu. + */ private void showPopupMenu(View view) { // Create a PopupMenu, giving it the clicked view for an anchor PopupMenu popup = new PopupMenu(adapter.getFragment().getActivity(), view); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java index 9db61c121..898744cca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java @@ -30,58 +30,114 @@ import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import timber.log.Timber; +/** + * Presenter of RecentChaptersFragment. + * Contains information and data for fragment. + * Observable updates should be called from here. + */ public class RecentChaptersPresenter extends BasePresenter { + /** + * The id of the restartable. + */ + private static final int GET_RECENT_CHAPTERS = 1; + + /** + * The id of the restartable. + */ + private static final int CHAPTER_STATUS_CHANGES = 2; + + /** + * Used to connect to database + */ @Inject DatabaseHelper db; + + /** + * Used to get information from download manager + */ @Inject DownloadManager downloadManager; + + /** + * Used to get source from source id + */ @Inject SourceManager sourceManager; + /** + * List containing chapter and manga information + */ private List mangaChapters; - private static final int GET_RECENT_CHAPTERS = 1; - private static final int CHAPTER_STATUS_CHANGES = 2; - @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); + // Used to get recent chapters restartableLatestCache(GET_RECENT_CHAPTERS, this::getRecentChaptersObservable, (recentChaptersFragment, chapters) -> { + // Update adapter to show recent manga's recentChaptersFragment.onNextMangaChapters(chapters); - updateMangaInformation(convertToMangaChaptersList(chapters)); + // Update download status + updateChapterStatus(convertToMangaChaptersList(chapters)); }); + // Used to update download status startableLatestCache(CHAPTER_STATUS_CHANGES, this::getChapterStatusObs, RecentChaptersFragment::onChapterStatusChange, (view, error) -> Timber.e(error.getCause(), error.getMessage())); if (savedState == null) { + // Start fetching recent chapters start(GET_RECENT_CHAPTERS); } } - - private void updateMangaInformation(List mangaChapters) { - this.mangaChapters = mangaChapters; - - for (MangaChapter mangaChapter : mangaChapters) - setChapterStatus(mangaChapter); - - start(CHAPTER_STATUS_CHANGES); - } - - private List convertToMangaChaptersList(List chapters) { + /** + * Returns a list only containing MangaChapter objects. + * + * @param input the list that will be converted. + * @return list containing MangaChapters objects. + */ + private List convertToMangaChaptersList(List input) { + // Create temp list List tempMangaChapterList = new ArrayList<>(); - for (Object object : chapters) { + + // Only add MangaChapter objects + //noinspection Convert2streamapi + for (Object object : input) { if (object instanceof MangaChapter) { tempMangaChapterList.add((MangaChapter) object); } } + + // Return temp list return tempMangaChapterList; } + /** + * Update status of chapters + * + * @param mangaChapters list containing recent chapters + */ + private void updateChapterStatus(List mangaChapters) { + // Set global list of chapters. + this.mangaChapters = mangaChapters; + + // Update status. + //noinspection Convert2streamapi + for (MangaChapter mangaChapter : mangaChapters) + setChapterStatus(mangaChapter); + + // Start onChapterStatusChange restartable. + start(CHAPTER_STATUS_CHANGES); + } + + /** + * Returns observable containing chapter status. + * + * @return download object containing download progress. + */ private Observable getChapterStatusObs() { return downloadManager.getQueue().getStatusObservable() .observeOn(AndroidSchedulers.mainThread()) @@ -89,6 +145,11 @@ public class RecentChaptersPresenter extends BasePresenter> getRecentChaptersObservable() { + // Set date for recent chapters Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.MONTH, -1); + // Get recent chapters from database. return db.getRecentChapters(cal.getTime()).asRxObservable() - // group chapters by the date they were fetched on a ordered map + // Group chapters by the date they were fetched on a ordered map. .flatMap(recents -> Observable.from(recents) .toMultimap( recent -> getMapKey(recent.chapter.date_fetch), recent -> recent, () -> new TreeMap<>((d1, d2) -> d2.compareTo(d1)))) - // add every day and all its chapters to a single list + // Add every day and all its chapters to a single list. .map(recents -> { List items = new ArrayList<>(); for (Map.Entry> recent : recents.entrySet()) { @@ -135,7 +204,12 @@ public class RecentChaptersPresenter extends BasePresenter selectedChapter, Manga manga) { add(selectedChapter .toList() @@ -174,11 +265,20 @@ public class RecentChaptersPresenter extends BasePresenter selectedChapters) { add(selectedChapters .subscribe(chapter -> { @@ -188,6 +288,11 @@ public class RecentChaptersPresenter extends BasePresenter selectedChapters, boolean read) { add(selectedChapters .subscribeOn(Schedulers.io())