Listen for downloaded pages, but it's not updating the UI yet
This commit is contained in:
parent
de8b7b27e1
commit
c73779ea3b
@ -4,6 +4,7 @@ import android.os.Bundle;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ import rx.Observable;
|
|||||||
import rx.Subscription;
|
import rx.Subscription;
|
||||||
import rx.android.schedulers.AndroidSchedulers;
|
import rx.android.schedulers.AndroidSchedulers;
|
||||||
import rx.schedulers.Schedulers;
|
import rx.schedulers.Schedulers;
|
||||||
|
import rx.subjects.PublishSubject;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment> {
|
public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment> {
|
||||||
@ -25,6 +27,7 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
|
|||||||
private DownloadQueue downloadQueue;
|
private DownloadQueue downloadQueue;
|
||||||
private Subscription statusSubscription;
|
private Subscription statusSubscription;
|
||||||
private HashMap<Download, Subscription> progressSubscriptions;
|
private HashMap<Download, Subscription> progressSubscriptions;
|
||||||
|
private HashMap<Download, Subscription> pageStatusSubscriptions;
|
||||||
|
|
||||||
public final static int GET_DOWNLOAD_QUEUE = 1;
|
public final static int GET_DOWNLOAD_QUEUE = 1;
|
||||||
|
|
||||||
@ -34,6 +37,7 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
|
|||||||
|
|
||||||
downloadQueue = downloadManager.getQueue();
|
downloadQueue = downloadManager.getQueue();
|
||||||
progressSubscriptions = new HashMap<>();
|
progressSubscriptions = new HashMap<>();
|
||||||
|
pageStatusSubscriptions = new HashMap<>();
|
||||||
|
|
||||||
restartableLatestCache(GET_DOWNLOAD_QUEUE,
|
restartableLatestCache(GET_DOWNLOAD_QUEUE,
|
||||||
() -> Observable.just(downloadQueue.get()),
|
() -> Observable.just(downloadQueue.get()),
|
||||||
@ -48,12 +52,12 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
|
|||||||
protected void onTakeView(DownloadQueueFragment view) {
|
protected void onTakeView(DownloadQueueFragment view) {
|
||||||
super.onTakeView(view);
|
super.onTakeView(view);
|
||||||
|
|
||||||
statusSubscription = downloadQueue.getStatusObservable()
|
add(statusSubscription = downloadQueue.getStatusObservable()
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(download -> {
|
.subscribe(download -> {
|
||||||
processStatus(download, view);
|
processStatus(download, view);
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,9 +70,11 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
|
|||||||
switch (download.getStatus()) {
|
switch (download.getStatus()) {
|
||||||
case Download.DOWNLOADING:
|
case Download.DOWNLOADING:
|
||||||
observeProgress(download, view);
|
observeProgress(download, view);
|
||||||
|
observePagesStatus(download, view);
|
||||||
break;
|
break;
|
||||||
case Download.DOWNLOADED:
|
case Download.DOWNLOADED:
|
||||||
unsubscribeProgress(download);
|
unsubscribeProgress(download);
|
||||||
|
unsubscribePagesStatus(download);
|
||||||
download.totalProgress = download.pages.size() * 100;
|
download.totalProgress = download.pages.size() * 100;
|
||||||
view.updateProgress(download);
|
view.updateProgress(download);
|
||||||
break;
|
break;
|
||||||
@ -89,13 +95,52 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
|
|||||||
progressSubscriptions.put(download, subscription);
|
progressSubscriptions.put(download, subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void observePagesStatus(Download download, DownloadQueueFragment view) {
|
||||||
|
PublishSubject<Integer> pageStatusSubject = PublishSubject.create();
|
||||||
|
for (Page page : download.pages)
|
||||||
|
page.setStatusSubject(pageStatusSubject);
|
||||||
|
|
||||||
|
final AtomicInteger downloadedPages = new AtomicInteger(0);
|
||||||
|
|
||||||
|
Subscription subscription = pageStatusSubject
|
||||||
|
.startWith(Observable.from(download.pages)
|
||||||
|
.filter(page -> page.getStatus() == Page.READY)
|
||||||
|
.map(page -> Page.READY))
|
||||||
|
.filter(status -> status == Page.READY)
|
||||||
|
.map(status -> downloadedPages.incrementAndGet())
|
||||||
|
.subscribe(count -> {
|
||||||
|
// TODO
|
||||||
|
});
|
||||||
|
|
||||||
|
pageStatusSubscriptions.put(download, subscription);
|
||||||
|
}
|
||||||
|
|
||||||
private void unsubscribeProgress(Download download) {
|
private void unsubscribeProgress(Download download) {
|
||||||
Subscription subscription = progressSubscriptions.remove(download);
|
Subscription subscription = progressSubscriptions.remove(download);
|
||||||
if (subscription != null)
|
if (subscription != null)
|
||||||
subscription.unsubscribe();
|
subscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void unsubscribePagesStatus(Download download) {
|
||||||
|
for (Page page : download.pages)
|
||||||
|
page.setStatusSubject(null);
|
||||||
|
|
||||||
|
Subscription subscription = pageStatusSubscriptions.remove(download);
|
||||||
|
if (subscription != null)
|
||||||
|
subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
|
||||||
private void destroySubscriptions() {
|
private void destroySubscriptions() {
|
||||||
|
for (Download download : pageStatusSubscriptions.keySet()) {
|
||||||
|
for (Page page : download.pages)
|
||||||
|
page.setStatusSubject(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Subscription subscription : pageStatusSubscriptions.values()) {
|
||||||
|
subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
pageStatusSubscriptions.clear();
|
||||||
|
|
||||||
for (Subscription subscription : progressSubscriptions.values()) {
|
for (Subscription subscription : progressSubscriptions.values()) {
|
||||||
subscription.unsubscribe();
|
subscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ public class DownloadHolder extends ItemViewHolder<Download> {
|
|||||||
|
|
||||||
@ViewId(R.id.download_title) TextView downloadTitle;
|
@ViewId(R.id.download_title) TextView downloadTitle;
|
||||||
@ViewId(R.id.download_progress) ProgressBar downloadProgress;
|
@ViewId(R.id.download_progress) ProgressBar downloadProgress;
|
||||||
|
@ViewId(R.id.download_progress_text) TextView downloadProgressText;
|
||||||
|
|
||||||
public DownloadHolder(View view) {
|
public DownloadHolder(View view) {
|
||||||
super(view);
|
super(view);
|
||||||
|
@ -5,28 +5,34 @@
|
|||||||
android:layout_marginLeft="15dp"
|
android:layout_marginLeft="15dp"
|
||||||
android:layout_marginRight="15dp">
|
android:layout_marginRight="15dp">
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="10dp"
|
android:layout_marginTop="10dp"
|
||||||
android:id="@+id/download_title"/>
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<FrameLayout
|
<TextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content">
|
|
||||||
|
|
||||||
<ProgressBar
|
|
||||||
style="?android:attr/progressBarStyleHorizontal"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:id="@+id/download_progress" />
|
android:id="@+id/download_title"/>
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:id="@+id/download_progress_text"
|
android:id="@+id/download_progress_text"
|
||||||
android:layout_gravity="center_horizontal" />
|
android:layout_gravity="left" />
|
||||||
|
|
||||||
</FrameLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
style="?android:attr/progressBarStyleHorizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:id="@+id/download_progress" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
Reference in New Issue
Block a user