Rename project
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package eu.kanade.tachiyomi.ui.download;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||
import eu.kanade.tachiyomi.R;
|
||||
import eu.kanade.tachiyomi.data.download.model.Download;
|
||||
|
||||
public class DownloadAdapter extends FlexibleAdapter<DownloadHolder, Download> {
|
||||
|
||||
private Context context;
|
||||
|
||||
public DownloadAdapter(Context context) {
|
||||
this.context = context;
|
||||
mItems = new ArrayList<>();
|
||||
setHasStableIds(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View v = LayoutInflater.from(context).inflate(R.layout.item_download, parent, false);
|
||||
return new DownloadHolder(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(DownloadHolder holder, int position) {
|
||||
final Download download = getItem(position);
|
||||
holder.onSetValues(download);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return getItem(position).chapter.id;
|
||||
}
|
||||
|
||||
public void setItems(List<Download> downloads) {
|
||||
mItems = downloads;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDataSet(String param) {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package eu.kanade.tachiyomi.ui.download;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import eu.kanade.tachiyomi.R;
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService;
|
||||
import eu.kanade.tachiyomi.data.download.model.Download;
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment;
|
||||
import nucleus.factory.RequiresPresenter;
|
||||
import rx.Subscription;
|
||||
|
||||
@RequiresPresenter(DownloadPresenter.class)
|
||||
public class DownloadFragment extends BaseRxFragment<DownloadPresenter> {
|
||||
|
||||
@Bind(R.id.download_list) RecyclerView recyclerView;
|
||||
private DownloadAdapter adapter;
|
||||
|
||||
private MenuItem startButton;
|
||||
private MenuItem stopButton;
|
||||
|
||||
private Subscription queueStatusSubscription;
|
||||
private boolean isRunning;
|
||||
|
||||
public static DownloadFragment newInstance() {
|
||||
return new DownloadFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.fragment_download_queue, container, false);
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
setToolbarTitle(R.string.label_download_queue);
|
||||
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
recyclerView.setHasFixedSize(true);
|
||||
createAdapter();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.download_queue, menu);
|
||||
startButton = menu.findItem(R.id.start_queue);
|
||||
stopButton = menu.findItem(R.id.stop_queue);
|
||||
|
||||
// Menu seems to be inflated after onResume in fragments, so we initialize them here
|
||||
startButton.setVisible(!isRunning && !getPresenter().downloadManager.getQueue().isEmpty());
|
||||
stopButton.setVisible(isRunning);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.start_queue:
|
||||
DownloadService.start(getActivity());
|
||||
break;
|
||||
case R.id.stop_queue:
|
||||
DownloadService.stop(getActivity());
|
||||
break;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
queueStatusSubscription = getPresenter().downloadManager.getRunningSubject()
|
||||
.subscribe(this::onRunningChange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
queueStatusSubscription.unsubscribe();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void onRunningChange(boolean running) {
|
||||
isRunning = running;
|
||||
if (startButton != null)
|
||||
startButton.setVisible(!running && !getPresenter().downloadManager.getQueue().isEmpty());
|
||||
if (stopButton != null)
|
||||
stopButton.setVisible(running);
|
||||
}
|
||||
|
||||
private void createAdapter() {
|
||||
adapter = new DownloadAdapter(getActivity());
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
public void onNextDownloads(List<Download> downloads) {
|
||||
adapter.setItems(downloads);
|
||||
}
|
||||
|
||||
public void updateProgress(Download download) {
|
||||
DownloadHolder holder = getHolder(download);
|
||||
if (holder != null) {
|
||||
holder.setDownloadProgress(download);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDownloadedPages(Download download) {
|
||||
DownloadHolder holder = getHolder(download);
|
||||
if (holder != null) {
|
||||
holder.setDownloadedPages(download);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private DownloadHolder getHolder(Download download) {
|
||||
return (DownloadHolder) recyclerView.findViewHolderForItemId(download.chapter.id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package eu.kanade.tachiyomi.ui.download;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import eu.kanade.tachiyomi.R;
|
||||
import eu.kanade.tachiyomi.data.download.model.Download;
|
||||
|
||||
public class DownloadHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
@Bind(R.id.download_title) TextView downloadTitle;
|
||||
@Bind(R.id.download_progress) ProgressBar downloadProgress;
|
||||
@Bind(R.id.download_progress_text) TextView downloadProgressText;
|
||||
|
||||
public DownloadHolder(View view) {
|
||||
super(view);
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
public void onSetValues(Download download) {
|
||||
downloadTitle.setText(download.chapter.name);
|
||||
|
||||
if (download.pages == null) {
|
||||
downloadProgress.setProgress(0);
|
||||
downloadProgress.setMax(1);
|
||||
downloadProgressText.setText("");
|
||||
} else {
|
||||
downloadProgress.setMax(download.pages.size() * 100);
|
||||
setDownloadProgress(download);
|
||||
setDownloadedPages(download);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDownloadedPages(Download download) {
|
||||
String progressText = download.downloadedImages + "/" + download.pages.size();
|
||||
downloadProgressText.setText(progressText);
|
||||
}
|
||||
|
||||
public void setDownloadProgress(Download download) {
|
||||
if (downloadProgress.getMax() == 1)
|
||||
downloadProgress.setMax(download.pages.size() * 100);
|
||||
downloadProgress.setProgress(download.totalProgress);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package eu.kanade.tachiyomi.ui.download;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager;
|
||||
import eu.kanade.tachiyomi.data.download.model.Download;
|
||||
import eu.kanade.tachiyomi.data.download.model.DownloadQueue;
|
||||
import eu.kanade.tachiyomi.data.source.model.Page;
|
||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter;
|
||||
import rx.Observable;
|
||||
import rx.Subscription;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class DownloadPresenter extends BasePresenter<DownloadFragment> {
|
||||
|
||||
@Inject DownloadManager downloadManager;
|
||||
|
||||
private DownloadQueue downloadQueue;
|
||||
private Subscription statusSubscription;
|
||||
private Subscription pageProgressSubscription;
|
||||
private HashMap<Download, Subscription> progressSubscriptions;
|
||||
|
||||
public final static int GET_DOWNLOAD_QUEUE = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
|
||||
downloadQueue = downloadManager.getQueue();
|
||||
progressSubscriptions = new HashMap<>();
|
||||
|
||||
restartableLatestCache(GET_DOWNLOAD_QUEUE,
|
||||
() -> Observable.just(downloadQueue),
|
||||
DownloadFragment::onNextDownloads,
|
||||
(view, error) -> Timber.e(error.getMessage()));
|
||||
|
||||
if (savedState == null)
|
||||
start(GET_DOWNLOAD_QUEUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTakeView(DownloadFragment view) {
|
||||
super.onTakeView(view);
|
||||
|
||||
add(statusSubscription = downloadQueue.getStatusObservable()
|
||||
.startWith(downloadQueue.getActiveDownloads())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(download -> {
|
||||
processStatus(download, view);
|
||||
}));
|
||||
|
||||
add(pageProgressSubscription = downloadQueue.getProgressObservable()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(view::updateDownloadedPages));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDropView() {
|
||||
destroySubscriptions();
|
||||
super.onDropView();
|
||||
}
|
||||
|
||||
private void processStatus(Download download, DownloadFragment view) {
|
||||
switch (download.getStatus()) {
|
||||
case Download.DOWNLOADING:
|
||||
observeProgress(download, view);
|
||||
// Initial update of the downloaded pages
|
||||
view.updateDownloadedPages(download);
|
||||
break;
|
||||
case Download.DOWNLOADED:
|
||||
unsubscribeProgress(download);
|
||||
view.updateProgress(download);
|
||||
view.updateDownloadedPages(download);
|
||||
break;
|
||||
case Download.ERROR:
|
||||
unsubscribeProgress(download);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void observeProgress(Download download, DownloadFragment view) {
|
||||
Subscription subscription = Observable.interval(50, TimeUnit.MILLISECONDS, Schedulers.newThread())
|
||||
.flatMap(tick -> Observable.from(download.pages)
|
||||
.map(Page::getProgress)
|
||||
.reduce((x, y) -> x + y))
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(progress -> {
|
||||
if (download.totalProgress != progress) {
|
||||
download.totalProgress = progress;
|
||||
view.updateProgress(download);
|
||||
}
|
||||
});
|
||||
|
||||
// Avoid leaking subscriptions
|
||||
Subscription oldSubscription = progressSubscriptions.remove(download);
|
||||
if (oldSubscription != null) oldSubscription.unsubscribe();
|
||||
|
||||
progressSubscriptions.put(download, subscription);
|
||||
}
|
||||
|
||||
private void unsubscribeProgress(Download download) {
|
||||
Subscription subscription = progressSubscriptions.remove(download);
|
||||
if (subscription != null)
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
|
||||
private void destroySubscriptions() {
|
||||
for (Subscription subscription : progressSubscriptions.values()) {
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
progressSubscriptions.clear();
|
||||
|
||||
remove(pageProgressSubscription);
|
||||
remove(statusSubscription);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user