Code cleanup, implements #118

This commit is contained in:
NoodleMage 2016-02-18 19:01:40 +01:00
parent ba0f3778ce
commit b94f86765d
4 changed files with 244 additions and 50 deletions

View File

@ -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<RecyclerView.ViewHolder, Object> {
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<RecyclerView.ViewHold
return item.hashCode();
}
/**
* Update items
*
* @param items items
*/
public void setItems(List<Object> items) {
mItems = items;
notifyDataSetChanged();
@ -56,6 +81,8 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v;
// Check which view type and set correct values.
switch (viewType) {
case VIEW_TYPE_CHAPTER:
v = inflater.inflate(R.layout.item_recent_chapter, parent, false);
@ -69,6 +96,7 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Check which view type and set correct values.
switch (holder.getItemViewType()) {
case VIEW_TYPE_CHAPTER:
final MangaChapter chapter = (MangaChapter) getItem(position);
@ -84,6 +112,10 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
holder.itemView.setActivated(isSelected(position));
}
/**
* Returns fragment
* @return RecentChaptersFragment
*/
public RecentChaptersFragment getFragment() {
return fragment;
}

View File

@ -31,6 +31,11 @@ import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Fragment that shows recent chapters.
* Uses R.layout.fragment_recent_chapters.
* UI related actions should be called from here.
*/
@RequiresPresenter(RecentChaptersPresenter.class)
public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresenter> implements FlexibleViewHolder.OnListItemClickListener {
@ -60,14 +65,21 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
return view;
}
/**
* Populate adapter with chapters
*
* @param chapters list of chapters
*/
public void onNextMangaChapters(List<Object> 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<RecentChaptersPresent
@Override
public void onListItemLongClick(int position) {
// Empty function
}
protected void openChapter(MangaChapter chapter) {
/**
* Open chapter in reader
*
* @param chapter selected chapter
*/
private void openChapter(MangaChapter chapter) {
getPresenter().onOpenChapter(chapter);
Intent intent = ReaderActivity.newIntent(getActivity());
startActivity(intent);
}
/**
* Update download status of chapter
*
* @param download download object containing download progress.
*/
public void onChapterStatusChange(Download download) {
RecentChaptersHolder holder = getHolder(download.chapter);
if (holder != null)
@ -94,6 +117,14 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
return (RecentChaptersHolder) recyclerView.findViewHolderForItemId(chapter.id);
}
/**
* Start downloading chapter
*
* @param chapters selected chapters
* @param manga manga that belongs to chapter
* @return true
*/
@SuppressWarnings("SameReturnValue")
protected boolean onDownload(Observable<Chapter> chapters, Manga manga) {
// Start the download service.
DownloadService.start(getActivity());
@ -107,6 +138,12 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
return true;
}
/**
* Start deleting chapter
* @param chapters selected chapters
* @param manga manga that belongs to chapter
* @return success of deletion.
*/
protected boolean onDelete(Observable<Chapter> chapters, Manga manga) {
int size = adapter.getSelectedItemCount();
@ -135,11 +172,25 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
return true;
}
/**
* Mark chapter as read
*
* @param chapters selected chapter
* @return true
*/
@SuppressWarnings("SameReturnValue")
protected boolean onMarkAsRead(Observable<Chapter> chapters) {
getPresenter().markChaptersRead(chapters, true);
return true;
}
/**
* Mark chapter as unread
*
* @param chapters selected chapter
* @return true
*/
@SuppressWarnings("SameReturnValue")
protected boolean onMarkAsUnread(Observable<Chapter> chapters) {
getPresenter().markChaptersRead(chapters, false);
return true;

View File

@ -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);

View File

@ -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<RecentChaptersFragment> {
/**
* 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<MangaChapter> 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<MangaChapter> mangaChapters) {
this.mangaChapters = mangaChapters;
for (MangaChapter mangaChapter : mangaChapters)
setChapterStatus(mangaChapter);
start(CHAPTER_STATUS_CHANGES);
}
private List<MangaChapter> convertToMangaChaptersList(List<Object> chapters) {
/**
* Returns a list only containing MangaChapter objects.
*
* @param input the list that will be converted.
* @return list containing MangaChapters objects.
*/
private List<MangaChapter> convertToMangaChaptersList(List<Object> input) {
// Create temp list
List<MangaChapter> 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<MangaChapter> 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<Download> getChapterStatusObs() {
return downloadManager.getQueue().getStatusObservable()
.observeOn(AndroidSchedulers.mainThread())
@ -89,6 +145,11 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
.doOnNext(this::updateChapterStatus);
}
/**
* Function to check if chapter is in recent list
* @param chaptersId id of chapter
* @return exist in recent list
*/
private boolean chapterIdEquals(Long chaptersId) {
for (MangaChapter mangaChapter : mangaChapters) {
if (chaptersId.equals(mangaChapter.chapter.id)) {
@ -98,32 +159,40 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
return false;
}
public void updateChapterStatus(Download download) {
for (Object item : mangaChapters) {
if (item instanceof MangaChapter) {
if (download.chapter.id.equals(((MangaChapter) item).chapter.id)) {
((MangaChapter) item).chapter.status = download.getStatus();
/**
* Update status of chapters.
*
* @param download download object containing progress.
*/
private void updateChapterStatus(Download download) {
// Loop through list
for (MangaChapter item : mangaChapters) {
if (download.chapter.id.equals(item.chapter.id)) {
item.chapter.status = download.getStatus();
break;
}
}
}
}
/**
* Get observable containing recent chapters and date
* @return observable containing recent chapters and date
*/
private Observable<List<Object>> 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<Object> items = new ArrayList<>();
for (Map.Entry<Date, Collection<MangaChapter>> recent : recents.entrySet()) {
@ -135,7 +204,12 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
.observeOn(AndroidSchedulers.mainThread());
}
/**
* Set the chapter status
* @param mangaChapter MangaChapter which status gets updated
*/
private void setChapterStatus(MangaChapter mangaChapter) {
// Check if chapter in queue
for (Download download : downloadManager.getQueue()) {
if (mangaChapter.chapter.id.equals(download.chapter.id)) {
mangaChapter.chapter.status = download.getStatus();
@ -143,7 +217,10 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
}
}
// Get source of chapter
Source source = sourceManager.get(mangaChapter.manga.source);
// Check if chapter is downloaded
if (downloadManager.isChapterDownloaded(source, mangaChapter.manga, mangaChapter.chapter)) {
mangaChapter.chapter.status = Download.DOWNLOADED;
} else {
@ -151,6 +228,11 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
}
}
/**
* Get date as time key
* @param date desired date
* @return date as time key
*/
private Date getMapKey(long date) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(date));
@ -161,11 +243,20 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
return cal.getTime();
}
/**
* Open chapter in reader
* @param item chapter that is opened
*/
public void onOpenChapter(MangaChapter item) {
Source source = sourceManager.get(item.manga.source);
EventBus.getDefault().postSticky(new ReaderEvent(source, item.manga, item.chapter));
}
/**
* Download selected chapter
* @param selectedChapter chapter that is selected
* @param manga manga that belongs to chapter
*/
public void downloadChapter(Observable<Chapter> selectedChapter, Manga manga) {
add(selectedChapter
.toList()
@ -174,11 +265,20 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
}));
}
/**
* Delete selected chapter
* @param chapter chapter that is selected
* @param manga manga that belongs to chapter
*/
public void deleteChapter(Chapter chapter, Manga manga) {
Source source = sourceManager.get(manga.source);
downloadManager.deleteChapter(source, manga, chapter);
}
/**
* Delete selected chapter observable
* @param selectedChapters chapter that are selected
*/
public void deleteChapters(Observable<Chapter> selectedChapters) {
add(selectedChapters
.subscribe(chapter -> {
@ -188,6 +288,11 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
}));
}
/**
* Mark selected chapter as read
* @param selectedChapters chapter that is selected
* @param read read status
*/
public void markChaptersRead(Observable<Chapter> selectedChapters, boolean read) {
add(selectedChapters
.subscribeOn(Schedulers.io())