From aa6dfddffaf8d7f1a4d1c39b7b8a0699af701320 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 2 Dec 2015 10:17:08 +0100 Subject: [PATCH] Show download status in chapters fragment --- .../data/download/DownloadManager.java | 2 +- .../data/download/model/Download.java | 5 +++- .../mangafeed/event/DownloadStatusEvent.java | 21 +++++++++++++++ .../ui/base/activity/BaseActivity.java | 14 ++++++++++ .../ui/base/fragment/BaseFragment.java | 13 +++++++++ .../ui/manga/chapter/ChaptersFragment.java | 27 +++++++++++++++++++ .../ui/manga/chapter/ChaptersHolder.java | 15 ++++++++--- app/src/main/res/layout/item_chapter.xml | 2 +- app/src/main/res/values/strings.xml | 5 +++- 9 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java diff --git a/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java b/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java index 5c06bbba13..e8c7092536 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java @@ -119,7 +119,7 @@ public class DownloadManager { } } - // Public method to checks if a chapter is downloaded + // Public method to check if a chapter is downloaded public boolean isChapterDownloaded(Source source, Manga manga, Chapter chapter) { File directory = getAbsoluteChapterDirectory(source, manga, chapter); if (!directory.exists()) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java b/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java index 1ebf9bdb3a..00648b98db 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java @@ -3,10 +3,12 @@ package eu.kanade.mangafeed.data.download.model; import java.io.File; import java.util.List; +import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.database.models.Manga; -import eu.kanade.mangafeed.data.source.model.Page; import eu.kanade.mangafeed.data.source.base.Source; +import eu.kanade.mangafeed.data.source.model.Page; +import eu.kanade.mangafeed.event.DownloadStatusEvent; import rx.subjects.PublishSubject; public class Download { @@ -52,5 +54,6 @@ public class Download { private void notifyStatus() { if (statusSubject != null) statusSubject.onNext(this); + EventBus.getDefault().post(new DownloadStatusEvent(chapter.id, status)); } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java b/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java new file mode 100644 index 0000000000..7ab00a8142 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java @@ -0,0 +1,21 @@ +package eu.kanade.mangafeed.event; + +public class DownloadStatusEvent { + + private long chapterId; + private int status; + + public DownloadStatusEvent(long chapterId, int status) { + this.chapterId = chapterId; + this.status = status; + } + + public long getChapterId() { + return chapterId; + } + + public int getStatus() { + return status; + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java index 9daa434137..b67399a929 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java @@ -5,6 +5,8 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; +import de.greenrobot.event.EventBus; + public class BaseActivity extends AppCompatActivity { protected void setupToolbar(Toolbar toolbar) { @@ -47,4 +49,16 @@ public class BaseActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } + public void registerForStickyEvents() { + EventBus.getDefault().registerSticky(this); + } + + public void registerForEvents() { + EventBus.getDefault().register(this); + } + + public void unregisterForEvents() { + EventBus.getDefault().unregister(this); + } + } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java index 7ab3b0eb85..b4c7ae02ac 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java @@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.base.fragment; import android.support.v4.app.Fragment; +import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.ui.base.activity.BaseActivity; public class BaseFragment extends Fragment { @@ -18,4 +19,16 @@ public class BaseFragment extends Fragment { return (BaseActivity) getActivity(); } + public void registerForStickyEvents() { + EventBus.getDefault().registerSticky(this); + } + + public void registerForEvents() { + EventBus.getDefault().register(this); + } + + public void unregisterForEvents() { + EventBus.getDefault().unregister(this); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java index 4bed992d14..9b282f47f7 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java @@ -24,11 +24,13 @@ import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.download.DownloadService; +import eu.kanade.mangafeed.event.DownloadStatusEvent; import eu.kanade.mangafeed.ui.base.activity.BaseActivity; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration; import eu.kanade.mangafeed.ui.manga.MangaActivity; import eu.kanade.mangafeed.ui.reader.ReaderActivity; +import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.ToastUtil; import nucleus.factory.RequiresPresenter; import rx.Observable; @@ -94,6 +96,18 @@ public class ChaptersFragment extends BaseRxFragment implemen return view; } + @Override + public void onResume() { + super.onResume(); + registerForEvents(); + } + + @Override + public void onPause() { + unregisterForEvents(); + super.onPause(); + } + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.chapters, menu); @@ -134,6 +148,19 @@ public class ChaptersFragment extends BaseRxFragment implemen startActivity(intent); } + @EventBusHook + public void onEventMainThread(DownloadStatusEvent event) { + Chapter chapter; + for (int i = 0; i < adapter.getItemCount(); i++) { + chapter = adapter.getItem(i); + if (event.getChapterId() == chapter.id) { + chapter.status = event.getStatus(); + adapter.notifyItemChanged(i); + break; + } + } + } + @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.chapter_selection, menu); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersHolder.java index 6bd0938406..8801e0cc67 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersHolder.java @@ -62,10 +62,17 @@ public class ChaptersHolder extends RecyclerView.ViewHolder implements pages.setText(""); } - if (chapter.status == Download.DOWNLOADED) { - downloadText.setVisibility(View.VISIBLE); - } else if (chapter.status == Download.NOT_DOWNLOADED) { - downloadText.setVisibility(View.INVISIBLE); + switch (chapter.status) { + case Download.QUEUE: + downloadText.setText(R.string.chapter_queued); break; + case Download.DOWNLOADING: + downloadText.setText(R.string.chapter_downloading); break; + case Download.DOWNLOADED: + downloadText.setText(R.string.chapter_downloaded); break; + case Download.ERROR: + downloadText.setText(R.string.chapter_error); break; + default: + downloadText.setText(""); break; } date.setText(sdf.format(new Date(chapter.date_upload))); diff --git a/app/src/main/res/layout/item_chapter.xml b/app/src/main/res/layout/item_chapter.xml index 98a37a012e..6c873cbbb4 100644 --- a/app/src/main/res/layout/item_chapter.xml +++ b/app/src/main/res/layout/item_chapter.xml @@ -49,7 +49,7 @@ android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" - android:text="@string/chapter_downloaded" + android:textAllCaps="true" android:textColor="@color/accent_text" android:textSize="12sp"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0787c78d25..360b62c3aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -87,7 +87,10 @@ Chapters Selected: %1$d No title - DOWNLOADED + Downloaded + Queued + Downloading + Error Downloading…