mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-12-23 02:51:47 +01:00
Code cleanup, implements #118
This commit is contained in:
parent
ba0f3778ce
commit
b94f86765d
@ -16,13 +16,33 @@ import eu.davidea.flexibleadapter.FlexibleAdapter;
|
|||||||
import eu.kanade.tachiyomi.R;
|
import eu.kanade.tachiyomi.R;
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaChapter;
|
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> {
|
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;
|
private static final int VIEW_TYPE_CHAPTER = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The id of the view type
|
||||||
|
*/
|
||||||
private static final int VIEW_TYPE_SECTION = 1;
|
private static final int VIEW_TYPE_SECTION = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param fragment fragment
|
||||||
|
*/
|
||||||
public RecentChaptersAdapter(RecentChaptersFragment fragment) {
|
public RecentChaptersAdapter(RecentChaptersFragment fragment) {
|
||||||
this.fragment = fragment;
|
this.fragment = fragment;
|
||||||
setHasStableIds(true);
|
setHasStableIds(true);
|
||||||
@ -37,6 +57,11 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
|
|||||||
return item.hashCode();
|
return item.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update items
|
||||||
|
*
|
||||||
|
* @param items items
|
||||||
|
*/
|
||||||
public void setItems(List<Object> items) {
|
public void setItems(List<Object> items) {
|
||||||
mItems = items;
|
mItems = items;
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
@ -56,6 +81,8 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
|
|||||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||||
View v;
|
View v;
|
||||||
|
|
||||||
|
// Check which view type and set correct values.
|
||||||
switch (viewType) {
|
switch (viewType) {
|
||||||
case VIEW_TYPE_CHAPTER:
|
case VIEW_TYPE_CHAPTER:
|
||||||
v = inflater.inflate(R.layout.item_recent_chapter, parent, false);
|
v = inflater.inflate(R.layout.item_recent_chapter, parent, false);
|
||||||
@ -69,6 +96,7 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||||
|
// Check which view type and set correct values.
|
||||||
switch (holder.getItemViewType()) {
|
switch (holder.getItemViewType()) {
|
||||||
case VIEW_TYPE_CHAPTER:
|
case VIEW_TYPE_CHAPTER:
|
||||||
final MangaChapter chapter = (MangaChapter) getItem(position);
|
final MangaChapter chapter = (MangaChapter) getItem(position);
|
||||||
@ -84,6 +112,10 @@ public class RecentChaptersAdapter extends FlexibleAdapter<RecyclerView.ViewHold
|
|||||||
holder.itemView.setActivated(isSelected(position));
|
holder.itemView.setActivated(isSelected(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns fragment
|
||||||
|
* @return RecentChaptersFragment
|
||||||
|
*/
|
||||||
public RecentChaptersFragment getFragment() {
|
public RecentChaptersFragment getFragment() {
|
||||||
return fragment;
|
return fragment;
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,11 @@ import rx.Observable;
|
|||||||
import rx.android.schedulers.AndroidSchedulers;
|
import rx.android.schedulers.AndroidSchedulers;
|
||||||
import rx.schedulers.Schedulers;
|
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)
|
@RequiresPresenter(RecentChaptersPresenter.class)
|
||||||
public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresenter> implements FlexibleViewHolder.OnListItemClickListener {
|
public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresenter> implements FlexibleViewHolder.OnListItemClickListener {
|
||||||
|
|
||||||
@ -60,14 +65,21 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate adapter with chapters
|
||||||
|
*
|
||||||
|
* @param chapters list of chapters
|
||||||
|
*/
|
||||||
public void onNextMangaChapters(List<Object> chapters) {
|
public void onNextMangaChapters(List<Object> chapters) {
|
||||||
adapter.setItems(chapters);
|
adapter.setItems(chapters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onListItemClick(int position) {
|
public boolean onListItemClick(int position) {
|
||||||
|
// Get item from position
|
||||||
Object item = adapter.getItem(position);
|
Object item = adapter.getItem(position);
|
||||||
if (item instanceof MangaChapter) {
|
if (item instanceof MangaChapter) {
|
||||||
|
// Open chapter in reader
|
||||||
openChapter((MangaChapter) item);
|
openChapter((MangaChapter) item);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -75,14 +87,25 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onListItemLongClick(int position) {
|
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);
|
getPresenter().onOpenChapter(chapter);
|
||||||
Intent intent = ReaderActivity.newIntent(getActivity());
|
Intent intent = ReaderActivity.newIntent(getActivity());
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update download status of chapter
|
||||||
|
*
|
||||||
|
* @param download download object containing download progress.
|
||||||
|
*/
|
||||||
public void onChapterStatusChange(Download download) {
|
public void onChapterStatusChange(Download download) {
|
||||||
RecentChaptersHolder holder = getHolder(download.chapter);
|
RecentChaptersHolder holder = getHolder(download.chapter);
|
||||||
if (holder != null)
|
if (holder != null)
|
||||||
@ -94,6 +117,14 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
|
|||||||
return (RecentChaptersHolder) recyclerView.findViewHolderForItemId(chapter.id);
|
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) {
|
protected boolean onDownload(Observable<Chapter> chapters, Manga manga) {
|
||||||
// Start the download service.
|
// Start the download service.
|
||||||
DownloadService.start(getActivity());
|
DownloadService.start(getActivity());
|
||||||
@ -107,6 +138,12 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
|
|||||||
return true;
|
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) {
|
protected boolean onDelete(Observable<Chapter> chapters, Manga manga) {
|
||||||
int size = adapter.getSelectedItemCount();
|
int size = adapter.getSelectedItemCount();
|
||||||
|
|
||||||
@ -135,11 +172,25 @@ public class RecentChaptersFragment extends BaseRxFragment<RecentChaptersPresent
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark chapter as read
|
||||||
|
*
|
||||||
|
* @param chapters selected chapter
|
||||||
|
* @return true
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("SameReturnValue")
|
||||||
protected boolean onMarkAsRead(Observable<Chapter> chapters) {
|
protected boolean onMarkAsRead(Observable<Chapter> chapters) {
|
||||||
getPresenter().markChaptersRead(chapters, true);
|
getPresenter().markChaptersRead(chapters, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark chapter as unread
|
||||||
|
*
|
||||||
|
* @param chapters selected chapter
|
||||||
|
* @return true
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("SameReturnValue")
|
||||||
protected boolean onMarkAsUnread(Observable<Chapter> chapters) {
|
protected boolean onMarkAsUnread(Observable<Chapter> chapters) {
|
||||||
getPresenter().markChaptersRead(chapters, false);
|
getPresenter().markChaptersRead(chapters, false);
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package eu.kanade.tachiyomi.ui.recent;
|
package eu.kanade.tachiyomi.ui.recent;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.View;
|
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 eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder;
|
||||||
import rx.Observable;
|
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 {
|
public class RecentChaptersHolder extends FlexibleViewHolder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,11 +28,6 @@ public class RecentChaptersHolder extends FlexibleViewHolder {
|
|||||||
*/
|
*/
|
||||||
private final RecentChaptersAdapter adapter;
|
private final RecentChaptersAdapter adapter;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface to global information about an application environment.
|
|
||||||
*/
|
|
||||||
private Context context;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TextView containing chapter title
|
* TextView containing chapter title
|
||||||
*/
|
*/
|
||||||
@ -49,11 +48,6 @@ public class RecentChaptersHolder extends FlexibleViewHolder {
|
|||||||
*/
|
*/
|
||||||
@Bind(R.id.chapter_menu) RelativeLayout chapterMenu;
|
@Bind(R.id.chapter_menu) RelativeLayout chapterMenu;
|
||||||
|
|
||||||
/**
|
|
||||||
* TextView containing read progress
|
|
||||||
*/
|
|
||||||
// @Bind(R.id.chapter_pages) TextView pages;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Color of read chapter
|
* Color of read chapter
|
||||||
*/
|
*/
|
||||||
@ -71,15 +65,13 @@ public class RecentChaptersHolder extends FlexibleViewHolder {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of RecentChaptersHolder
|
* Constructor of RecentChaptersHolder
|
||||||
*
|
* @param view view of ChapterHolder
|
||||||
* @param view
|
* @param adapter adapter of ChapterHolder
|
||||||
* @param adapter
|
* @param onListItemClickListener ClickListener
|
||||||
* @param onListItemClickListener
|
|
||||||
*/
|
*/
|
||||||
public RecentChaptersHolder(View view, RecentChaptersAdapter adapter, OnListItemClickListener onListItemClickListener) {
|
public RecentChaptersHolder(View view, RecentChaptersAdapter adapter, OnListItemClickListener onListItemClickListener) {
|
||||||
super(view, adapter, onListItemClickListener);
|
super(view, adapter, onListItemClickListener);
|
||||||
this.adapter = adapter;
|
this.adapter = adapter;
|
||||||
context = view.getContext();
|
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
|
|
||||||
// Set colors.
|
// Set colors.
|
||||||
@ -90,28 +82,38 @@ public class RecentChaptersHolder extends FlexibleViewHolder {
|
|||||||
chapterMenu.setOnClickListener(v -> v.post(() -> showPopupMenu(v)));
|
chapterMenu.setOnClickListener(v -> v.post(() -> showPopupMenu(v)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set values of view
|
||||||
|
*
|
||||||
|
* @param item item containing chapter information
|
||||||
|
*/
|
||||||
public void onSetValues(MangaChapter item) {
|
public void onSetValues(MangaChapter item) {
|
||||||
this.mangaChapter = item;
|
this.mangaChapter = item;
|
||||||
|
|
||||||
|
// Set chapter title
|
||||||
chapterTitle.setText(item.chapter.name);
|
chapterTitle.setText(item.chapter.name);
|
||||||
|
|
||||||
|
// Set manga title
|
||||||
mangaTitle.setText(item.manga.title);
|
mangaTitle.setText(item.manga.title);
|
||||||
|
|
||||||
|
// Check if chapter is read and set correct color
|
||||||
if (item.chapter.read) {
|
if (item.chapter.read) {
|
||||||
chapterTitle.setTextColor(readColor);
|
chapterTitle.setTextColor(readColor);
|
||||||
mangaTitle.setTextColor(readColor);
|
mangaTitle.setTextColor(readColor);
|
||||||
} else {
|
} else {
|
||||||
chapterTitle.setTextColor(unreadColor);
|
chapterTitle.setTextColor(unreadColor);
|
||||||
mangaTitle.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);
|
onStatusChange(item.chapter.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates chapter status in view.
|
||||||
|
*
|
||||||
|
* @param status download status
|
||||||
|
*/
|
||||||
public void onStatusChange(int status) {
|
public void onStatusChange(int status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case Download.QUEUE:
|
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) {
|
private void showPopupMenu(View view) {
|
||||||
// Create a PopupMenu, giving it the clicked view for an anchor
|
// Create a PopupMenu, giving it the clicked view for an anchor
|
||||||
PopupMenu popup = new PopupMenu(adapter.getFragment().getActivity(), view);
|
PopupMenu popup = new PopupMenu(adapter.getFragment().getActivity(), view);
|
||||||
|
@ -30,58 +30,114 @@ import rx.android.schedulers.AndroidSchedulers;
|
|||||||
import rx.schedulers.Schedulers;
|
import rx.schedulers.Schedulers;
|
||||||
import timber.log.Timber;
|
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> {
|
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;
|
@Inject DatabaseHelper db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to get information from download manager
|
||||||
|
*/
|
||||||
@Inject DownloadManager downloadManager;
|
@Inject DownloadManager downloadManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to get source from source id
|
||||||
|
*/
|
||||||
@Inject SourceManager sourceManager;
|
@Inject SourceManager sourceManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List containing chapter and manga information
|
||||||
|
*/
|
||||||
private List<MangaChapter> mangaChapters;
|
private List<MangaChapter> mangaChapters;
|
||||||
|
|
||||||
private static final int GET_RECENT_CHAPTERS = 1;
|
|
||||||
private static final int CHAPTER_STATUS_CHANGES = 2;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedState) {
|
protected void onCreate(Bundle savedState) {
|
||||||
super.onCreate(savedState);
|
super.onCreate(savedState);
|
||||||
|
|
||||||
|
// Used to get recent chapters
|
||||||
restartableLatestCache(GET_RECENT_CHAPTERS,
|
restartableLatestCache(GET_RECENT_CHAPTERS,
|
||||||
this::getRecentChaptersObservable,
|
this::getRecentChaptersObservable,
|
||||||
(recentChaptersFragment, chapters) -> {
|
(recentChaptersFragment, chapters) -> {
|
||||||
|
// Update adapter to show recent manga's
|
||||||
recentChaptersFragment.onNextMangaChapters(chapters);
|
recentChaptersFragment.onNextMangaChapters(chapters);
|
||||||
updateMangaInformation(convertToMangaChaptersList(chapters));
|
// Update download status
|
||||||
|
updateChapterStatus(convertToMangaChaptersList(chapters));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Used to update download status
|
||||||
startableLatestCache(CHAPTER_STATUS_CHANGES,
|
startableLatestCache(CHAPTER_STATUS_CHANGES,
|
||||||
this::getChapterStatusObs,
|
this::getChapterStatusObs,
|
||||||
RecentChaptersFragment::onChapterStatusChange,
|
RecentChaptersFragment::onChapterStatusChange,
|
||||||
(view, error) -> Timber.e(error.getCause(), error.getMessage()));
|
(view, error) -> Timber.e(error.getCause(), error.getMessage()));
|
||||||
|
|
||||||
if (savedState == null) {
|
if (savedState == null) {
|
||||||
|
// Start fetching recent chapters
|
||||||
start(GET_RECENT_CHAPTERS);
|
start(GET_RECENT_CHAPTERS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
private void updateMangaInformation(List<MangaChapter> mangaChapters) {
|
* Returns a list only containing MangaChapter objects.
|
||||||
this.mangaChapters = mangaChapters;
|
*
|
||||||
|
* @param input the list that will be converted.
|
||||||
for (MangaChapter mangaChapter : mangaChapters)
|
* @return list containing MangaChapters objects.
|
||||||
setChapterStatus(mangaChapter);
|
*/
|
||||||
|
private List<MangaChapter> convertToMangaChaptersList(List<Object> input) {
|
||||||
start(CHAPTER_STATUS_CHANGES);
|
// Create temp list
|
||||||
}
|
|
||||||
|
|
||||||
private List<MangaChapter> convertToMangaChaptersList(List<Object> chapters) {
|
|
||||||
List<MangaChapter> tempMangaChapterList = new ArrayList<>();
|
List<MangaChapter> tempMangaChapterList = new ArrayList<>();
|
||||||
for (Object object : chapters) {
|
|
||||||
|
// Only add MangaChapter objects
|
||||||
|
//noinspection Convert2streamapi
|
||||||
|
for (Object object : input) {
|
||||||
if (object instanceof MangaChapter) {
|
if (object instanceof MangaChapter) {
|
||||||
tempMangaChapterList.add((MangaChapter) object);
|
tempMangaChapterList.add((MangaChapter) object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return temp list
|
||||||
return tempMangaChapterList;
|
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() {
|
private Observable<Download> getChapterStatusObs() {
|
||||||
return downloadManager.getQueue().getStatusObservable()
|
return downloadManager.getQueue().getStatusObservable()
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
@ -89,6 +145,11 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
|
|||||||
.doOnNext(this::updateChapterStatus);
|
.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) {
|
private boolean chapterIdEquals(Long chaptersId) {
|
||||||
for (MangaChapter mangaChapter : mangaChapters) {
|
for (MangaChapter mangaChapter : mangaChapters) {
|
||||||
if (chaptersId.equals(mangaChapter.chapter.id)) {
|
if (chaptersId.equals(mangaChapter.chapter.id)) {
|
||||||
@ -98,32 +159,40 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateChapterStatus(Download download) {
|
/**
|
||||||
for (Object item : mangaChapters) {
|
* Update status of chapters.
|
||||||
if (item instanceof MangaChapter) {
|
*
|
||||||
if (download.chapter.id.equals(((MangaChapter) item).chapter.id)) {
|
* @param download download object containing progress.
|
||||||
((MangaChapter) item).chapter.status = download.getStatus();
|
*/
|
||||||
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get observable containing recent chapters and date
|
||||||
|
* @return observable containing recent chapters and date
|
||||||
|
*/
|
||||||
private Observable<List<Object>> getRecentChaptersObservable() {
|
private Observable<List<Object>> getRecentChaptersObservable() {
|
||||||
|
// Set date for recent chapters
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(new Date());
|
cal.setTime(new Date());
|
||||||
cal.add(Calendar.MONTH, -1);
|
cal.add(Calendar.MONTH, -1);
|
||||||
|
|
||||||
|
// Get recent chapters from database.
|
||||||
return db.getRecentChapters(cal.getTime()).asRxObservable()
|
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)
|
.flatMap(recents -> Observable.from(recents)
|
||||||
.toMultimap(
|
.toMultimap(
|
||||||
recent -> getMapKey(recent.chapter.date_fetch),
|
recent -> getMapKey(recent.chapter.date_fetch),
|
||||||
recent -> recent,
|
recent -> recent,
|
||||||
() -> new TreeMap<>((d1, d2) -> d2.compareTo(d1))))
|
() -> 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 -> {
|
.map(recents -> {
|
||||||
List<Object> items = new ArrayList<>();
|
List<Object> items = new ArrayList<>();
|
||||||
for (Map.Entry<Date, Collection<MangaChapter>> recent : recents.entrySet()) {
|
for (Map.Entry<Date, Collection<MangaChapter>> recent : recents.entrySet()) {
|
||||||
@ -135,7 +204,12 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
|
|||||||
.observeOn(AndroidSchedulers.mainThread());
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the chapter status
|
||||||
|
* @param mangaChapter MangaChapter which status gets updated
|
||||||
|
*/
|
||||||
private void setChapterStatus(MangaChapter mangaChapter) {
|
private void setChapterStatus(MangaChapter mangaChapter) {
|
||||||
|
// Check if chapter in queue
|
||||||
for (Download download : downloadManager.getQueue()) {
|
for (Download download : downloadManager.getQueue()) {
|
||||||
if (mangaChapter.chapter.id.equals(download.chapter.id)) {
|
if (mangaChapter.chapter.id.equals(download.chapter.id)) {
|
||||||
mangaChapter.chapter.status = download.getStatus();
|
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);
|
Source source = sourceManager.get(mangaChapter.manga.source);
|
||||||
|
|
||||||
|
// Check if chapter is downloaded
|
||||||
if (downloadManager.isChapterDownloaded(source, mangaChapter.manga, mangaChapter.chapter)) {
|
if (downloadManager.isChapterDownloaded(source, mangaChapter.manga, mangaChapter.chapter)) {
|
||||||
mangaChapter.chapter.status = Download.DOWNLOADED;
|
mangaChapter.chapter.status = Download.DOWNLOADED;
|
||||||
} else {
|
} 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) {
|
private Date getMapKey(long date) {
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(new Date(date));
|
cal.setTime(new Date(date));
|
||||||
@ -161,11 +243,20 @@ public class RecentChaptersPresenter extends BasePresenter<RecentChaptersFragmen
|
|||||||
return cal.getTime();
|
return cal.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open chapter in reader
|
||||||
|
* @param item chapter that is opened
|
||||||
|
*/
|
||||||
public void onOpenChapter(MangaChapter item) {
|
public void onOpenChapter(MangaChapter item) {
|
||||||
Source source = sourceManager.get(item.manga.source);
|
Source source = sourceManager.get(item.manga.source);
|
||||||
EventBus.getDefault().postSticky(new ReaderEvent(source, item.manga, item.chapter));
|
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) {
|
public void downloadChapter(Observable<Chapter> selectedChapter, Manga manga) {
|
||||||
add(selectedChapter
|
add(selectedChapter
|
||||||
.toList()
|
.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) {
|
public void deleteChapter(Chapter chapter, Manga manga) {
|
||||||
Source source = sourceManager.get(manga.source);
|
Source source = sourceManager.get(manga.source);
|
||||||
downloadManager.deleteChapter(source, manga, chapter);
|
downloadManager.deleteChapter(source, manga, chapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete selected chapter observable
|
||||||
|
* @param selectedChapters chapter that are selected
|
||||||
|
*/
|
||||||
public void deleteChapters(Observable<Chapter> selectedChapters) {
|
public void deleteChapters(Observable<Chapter> selectedChapters) {
|
||||||
add(selectedChapters
|
add(selectedChapters
|
||||||
.subscribe(chapter -> {
|
.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) {
|
public void markChaptersRead(Observable<Chapter> selectedChapters, boolean read) {
|
||||||
add(selectedChapters
|
add(selectedChapters
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
|
Loading…
Reference in New Issue
Block a user