Show download status in chapters fragment

This commit is contained in:
inorichi 2015-12-02 10:17:08 +01:00
parent d5a6fac931
commit aa6dfddffa
9 changed files with 96 additions and 8 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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"/>
</RelativeLayout>

View file

@ -87,7 +87,10 @@
<string name="manga_chapters_tab">Chapters</string>
<string name="selected_chapters_title">Selected: %1$d</string>
<string name="manga_chapter_no_title">No title</string>
<string name="chapter_downloaded">DOWNLOADED</string>
<string name="chapter_downloaded">Downloaded</string>
<string name="chapter_queued">Queued</string>
<string name="chapter_downloading">Downloading</string>
<string name="chapter_error">Error</string>
<!-- Reader activity -->
<string name="downloading">Downloading…</string>